pub struct BinaryReader<'a> { /* private fields */ }
Expand description

A binary reader of the WebAssembly structures and types.

Implementations§

Constructs BinaryReader type.

Examples
let fn_body = &vec![0x41, 0x00, 0x10, 0x00, 0x0B];
let mut reader = wasmparser::BinaryReader::new(fn_body);
while !reader.eof() {
    let op = reader.read_operator();
    println!("{:?}", op)
}

Constructs a BinaryReader with an explicit starting offset.

Gets the original position of the binary reader.

Whether or not to allow 64-bit memory arguments in functions.

This is intended to be true when support for the memory64 WebAssembly proposal is also enabled.

Returns a range from the starting offset to the end of the buffer.

Reads a value of type T from this binary reader, advancing the internal position in this reader forward as data is read.

Reads a variable-length 32-bit size from the byte stream while checking against a limit.

Reads a variable-length 32-bit size from the byte stream while checking against a limit.

Then reads that many values of type T and returns them as an iterator.

Note that regardless of how many items are read from the returned iterator the items will still be parsed from this reader.

Returns whether the BinaryReader has reached the end of the file.

Returns the BinaryReader’s current position.

Returns the number of bytes remaining in the BinaryReader.

Advances the BinaryReader size bytes, and returns a slice from the current position of size length.

Errors

If size exceeds the remaining length in BinaryReader.

Reads a length-prefixed list of bytes from this reader and returns a new BinaryReader to read that list of bytes.

Advances the position of this reader by the number of bytes read.

Advances the BinaryReader four bytes and returns a u32.

Errors

If BinaryReader has less than four bytes remaining.

Advances the BinaryReader eight bytes and returns a u64.

Errors

If BinaryReader has less than eight bytes remaining.

Advances the BinaryReader a single byte.

Errors

If BinaryReader has no bytes remaining.

Advances the BinaryReader up to four bytes to parse a variable length integer as a u32.

Errors

If BinaryReader has less than one or up to four bytes remaining, or the integer is larger than 32 bits.

Advances the BinaryReader up to four bytes to parse a variable length integer as a u64.

Errors

If BinaryReader has less than one or up to eight bytes remaining, or the integer is larger than 64 bits.

Executes f to skip some data in this binary reader and then returns a reader which will read the skipped data.

Advances the BinaryReader past a WebAssembly string. This method does not perform any utf-8 validation.

Errors

If BinaryReader has less than four bytes, the string’s length exceeds the remaining bytes, or the string length exceeds limits::MAX_WASM_STRING_SIZE.

Advances the BinaryReader up to four bytes to parse a variable length integer as a i32.

Errors

If BinaryReader has less than one or up to four bytes remaining, or the integer is larger than 32 bits.

Advances the BinaryReader up to four bytes to parse a variable length integer as a signed 33 bit integer, returned as a i64.

Errors

If BinaryReader has less than one or up to five bytes remaining, or the integer is larger than 33 bits.

Advances the BinaryReader up to eight bytes to parse a variable length integer as a 64 bit integer, returned as a i64.

Errors

If BinaryReader has less than one or up to eight bytes remaining, or the integer is larger than 64 bits.

Advances the BinaryReader up to four bytes to parse a variable length integer as a 32 bit floating point integer, returned as Ieee32.

Errors

If BinaryReader has less than one or up to four bytes remaining, or the integer is larger than 32 bits.

Advances the BinaryReader up to four bytes to parse a variable length integer as a 32 bit floating point integer, returned as Ieee32.

Errors

If BinaryReader has less than one or up to four bytes remaining, or the integer is larger than 32 bits.

Reads a WebAssembly string from the module.

Errors

If BinaryReader has less than up to four bytes remaining, the string’s length exceeds the remaining bytes, the string’s length exceeds limits::MAX_WASM_STRING_SIZE, or the string contains invalid utf-8.

Visit the next available operator with the specified VisitOperator instance.

Note that this does not implicitly propagate any additional information such as instruction offsets. In order to do so, consider storing such data within the visitor before visiting.

Errors

If BinaryReader has less bytes remaining than required to parse the Operator.

Examples

Store an offset for use in diagnostics or any other purposes:


pub fn dump(mut reader: BinaryReader) -> Result<()> {
    let mut visitor = Dumper { offset: 0 };
    while !reader.eof() {
        visitor.offset = reader.original_position();
        reader.visit_operator(&mut visitor)?;
    }
    Ok(())
}

struct Dumper {
    offset: usize
}

macro_rules! define_visit_operator {
    ($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident)*) => {
        $(
            fn $visit(&mut self $($(,$arg: $argty)*)?) -> Self::Output {
                println!("{}: {}", self.offset, stringify!($visit));
            }
        )*
    }
}

impl<'a> VisitOperator<'a> for Dumper {
    type Output = ();
    for_each_operator!(define_visit_operator);
}

Reads the next available Operator.

Errors

If BinaryReader has less bytes remaining than required to parse the Operator.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.