46 lines
1.6 KiB
Rust
Raw Normal View History

2017-08-17 17:30:13 +03:00
// This short example provides the utility to inspect
// wasm file data section.
2017-07-27 16:29:16 +03:00
extern crate parity_wasm;
use std::env;
fn main() {
2017-08-17 17:30:13 +03:00
// Example executable takes one argument which must
// refernce the existing file with a valid wasm module
2017-07-27 16:29:16 +03:00
let args = env::args().collect::<Vec<_>>();
if args.len() != 2 {
println!("Usage: {} somefile.wasm", args[0]);
return;
}
2017-08-17 17:30:13 +03:00
// Here we load module using dedicated for this purpose
// `deserialize_file` function (which works only with modules)
2017-07-27 16:29:16 +03:00
let module = parity_wasm::deserialize_file(&args[1]).expect("Failed to load module");
2017-08-17 17:30:13 +03:00
// We query module for data section. Note that not every valid
// wasm module must contain a data section. So in case the provided
// module does not contain data section, we panic with an error
2017-07-27 16:29:16 +03:00
let data_section = module.data_section().expect("no data section in module");
2017-08-17 17:30:13 +03:00
// Printing the total count of data segments
2017-07-27 16:29:16 +03:00
println!("Data segments: {}", data_section.entries().len());
let mut index = 0;
for entry in data_section.entries() {
2017-08-17 17:30:13 +03:00
// Printing the details info of each data segment
// see `elements::DataSegment` for more properties
// you can query
2017-07-27 16:29:16 +03:00
println!(" Entry #{}", index);
2017-08-17 17:30:13 +03:00
// This shows the initialization member of data segment
// (expression which must resolve in the linear memory location).
2017-07-27 16:29:16 +03:00
println!(" init: {}", entry.offset().code()[0]);
2017-08-17 17:30:13 +03:00
// This shows the total length of the data segment in bytes.
2017-07-27 16:29:16 +03:00
println!(" size: {}", entry.value().len());
2017-08-17 17:30:13 +03:00
2017-07-27 16:29:16 +03:00
index += 1;
}
}