1 // This short example provides the utility to inspect
2 // wasm file data section.
3 
4 extern crate parity_wasm;
5 
6 use std::env;
7 
main()8 fn main() {
9 
10 	// Example executable takes one argument which must
11 	// refernce the existing file with a valid wasm module
12 	let args = env::args().collect::<Vec<_>>();
13 	if args.len() != 2 {
14 		println!("Usage: {} somefile.wasm", args[0]);
15 		return;
16 	}
17 
18 	// Here we load module using dedicated for this purpose
19 	// `deserialize_file` function (which works only with modules)
20 	let module = parity_wasm::deserialize_file(&args[1]).expect("Failed to load module");
21 
22 	// We query module for data section. Note that not every valid
23 	// wasm module must contain a data section. So in case the provided
24 	// module does not contain data section, we panic with an error
25 	let data_section = module.data_section().expect("no data section in module");
26 
27 	// Printing the total count of data segments
28 	println!("Data segments: {}", data_section.entries().len());
29 
30 	let mut index = 0;
31 	for entry in data_section.entries() {
32 		// Printing the details info of each data segment
33 		// see `elements::DataSegment` for more properties
34 		// you can query
35 		println!("  Entry #{}", index);
36 
37 		// This shows the initialization member of data segment
38 		// (expression which must resolve in the linear memory location).
39 		if let Some(offset) = entry.offset() {
40 			println!("	init: {}", offset.code()[0]);
41 		}
42 
43 		// This shows the total length of the data segment in bytes.
44 		println!("	size: {}", entry.value().len());
45 
46 		index += 1;
47 	}
48 }
49