diff options
Diffstat (limited to 'third_party/rust/wasmparser-0.48.2/examples')
-rw-r--r-- | third_party/rust/wasmparser-0.48.2/examples/dump.rs | 57 | ||||
-rw-r--r-- | third_party/rust/wasmparser-0.48.2/examples/simple.rs | 47 |
2 files changed, 104 insertions, 0 deletions
diff --git a/third_party/rust/wasmparser-0.48.2/examples/dump.rs b/third_party/rust/wasmparser-0.48.2/examples/dump.rs new file mode 100644 index 0000000000..f0defb4000 --- /dev/null +++ b/third_party/rust/wasmparser-0.48.2/examples/dump.rs @@ -0,0 +1,57 @@ +extern crate wasmparser; + +use std::env; +use std::fs::File; +use std::io; +use std::io::prelude::*; +use std::str; +use wasmparser::Parser; +use wasmparser::ParserState; +use wasmparser::WasmDecoder; + +fn main() { + let args = env::args().collect::<Vec<_>>(); + if args.len() != 2 { + println!("Usage: {} in.wasm", args[0]); + return; + } + + let buf: Vec<u8> = read_wasm(&args[1]).unwrap(); + let mut parser = Parser::new(&buf); + loop { + print!("0x{:08x}\t", parser.current_position()); + let state = parser.read(); + match *state { + ParserState::ExportSectionEntry { + field, + ref kind, + index, + } => { + println!( + "ExportSectionEntry {{ field: \"{}\", kind: {:?}, index: {} }}", + field, kind, index + ); + } + ParserState::ImportSectionEntry { + module, + field, + ref ty, + } => { + println!( + "ImportSectionEntry {{ module: \"{}\", field: \"{}\", ty: {:?} }}", + module, field, ty + ); + } + ParserState::EndWasm => break, + ParserState::Error(err) => panic!("Error: {:?}", err), + _ => println!("{:?}", state), + } + } +} + +fn read_wasm(file: &str) -> io::Result<Vec<u8>> { + let mut data = Vec::new(); + let mut f = File::open(file)?; + f.read_to_end(&mut data)?; + Ok(data) +} diff --git a/third_party/rust/wasmparser-0.48.2/examples/simple.rs b/third_party/rust/wasmparser-0.48.2/examples/simple.rs new file mode 100644 index 0000000000..132aa41f9f --- /dev/null +++ b/third_party/rust/wasmparser-0.48.2/examples/simple.rs @@ -0,0 +1,47 @@ +extern crate wasmparser; + +use std::env; +use std::fs::File; +use std::io; +use std::io::prelude::*; +use std::str; +use wasmparser::Parser; +use wasmparser::ParserState; +use wasmparser::WasmDecoder; + +fn main() { + let args = env::args().collect::<Vec<_>>(); + if args.len() != 2 { + println!("Usage: {} in.wasm", args[0]); + return; + } + + let buf: Vec<u8> = read_wasm(&args[1]).unwrap(); + let mut parser = Parser::new(&buf); + loop { + let state = parser.read(); + match *state { + ParserState::BeginWasm { .. } => { + println!("====== Module"); + } + ParserState::ExportSectionEntry { + field, ref kind, .. + } => { + println!(" Export {} {:?}", field, kind); + } + ParserState::ImportSectionEntry { module, field, .. } => { + println!(" Import {}::{}", module, field) + } + ParserState::EndWasm => break, + ParserState::Error(err) => panic!("Error: {:?}", err), + _ => ( /* println!(" Other {:?}", state); */ ), + } + } +} + +fn read_wasm(file: &str) -> io::Result<Vec<u8>> { + let mut data = Vec::new(); + let mut f = File::open(file)?; + f.read_to_end(&mut data)?; + Ok(data) +} |