Struct wasmparser::Parser
source · pub struct Parser<'a> { /* private fields */ }
Expand description
The Parser
type. A simple event-driven parser of WebAssembly binary
format. The read(&mut self)
is used to iterate through WebAssembly records.
Implementations
sourceimpl<'a> Parser<'a>
impl<'a> Parser<'a>
sourcepub fn new(data: &[u8]) -> Parser<'_>
pub fn new(data: &[u8]) -> Parser<'_>
Constructs Parser
type.
Examples
let data: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x4, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
0x0a, 0x05, 0x01, 0x03, 0x00, 0x01, 0x0b];
let mut parser = wasmparser::Parser::new(data);
pub fn eof(&self) -> bool
pub fn current_position(&self) -> usize
Trait Implementations
sourceimpl<'a> WasmDecoder<'a> for Parser<'a>
impl<'a> WasmDecoder<'a> for Parser<'a>
sourcefn read(&mut self) -> &ParserState<'a>
fn read(&mut self) -> &ParserState<'a>
Reads next record from the WebAssembly binary data. The methods returns
reference to current state of the parser. See ParserState
num.
Examples
use wasmparser::WasmDecoder;
let mut parser = wasmparser::Parser::new(data);
{
let state = parser.read();
println!("First state {:?}", state);
}
{
let state = parser.read();
println!("Second state {:?}", state);
}
sourcefn create_binary_reader<'b>(&mut self) -> BinaryReader<'b>where
'a: 'b,
fn create_binary_reader<'b>(&mut self) -> BinaryReader<'b>where
'a: 'b,
Creates a BinaryReader when current state is ParserState::BeginSection or ParserState::BeginFunctionBody.
Examples
use wasmparser::{WasmDecoder, Parser, ParserState};
let mut parser = Parser::new(data);
let mut function_readers = Vec::new();
loop {
match *parser.read() {
ParserState::Error(_) |
ParserState::EndWasm => break,
ParserState::BeginFunctionBody {..} => {
let reader = parser.create_binary_reader();
function_readers.push(reader);
}
_ => continue
}
}
for (i, reader) in function_readers.iter_mut().enumerate() {
println!("Function {}", i);
while let Ok(ref op) = reader.read_operator() {
println!(" {:?}", op);
}
}
sourcefn read_with_input(&mut self, input: ParserInput) -> &ParserState<'a>
fn read_with_input(&mut self, input: ParserInput) -> &ParserState<'a>
Reads next record from the WebAssembly binary data. It also allows to
control how parser will treat the next record(s). The method accepts the
ParserInput
parameter that allows e.g. to skip section or function
operators. The methods returns reference to current state of the parser.
Examples
use wasmparser::WasmDecoder;
let mut parser = wasmparser::Parser::new(data);
let mut next_input = wasmparser::ParserInput::Default;
loop {
let state = parser.read_with_input(next_input);
match *state {
wasmparser::ParserState::EndWasm => break,
wasmparser::ParserState::BeginWasm { .. } |
wasmparser::ParserState::EndSection =>
next_input = wasmparser::ParserInput::Default,
wasmparser::ParserState::BeginSection { ref code, .. } => {
println!("Found section: {:?}", code);
next_input = wasmparser::ParserInput::SkipSection;
},
_ => unreachable!()
}
}
fn push_input(&mut self, input: ParserInput)
fn last_state(&self) -> &ParserState<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Parser<'a>
impl<'a> Send for Parser<'a>
impl<'a> Sync for Parser<'a>
impl<'a> Unpin for Parser<'a>
impl<'a> UnwindSafe for Parser<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more