From 8d8d48f669f55ed43ebba87dbd146cb59c8d0fc3 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 30 Nov 2018 15:57:34 +0300 Subject: [PATCH] show function signature and code example --- examples/show.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/show.rs diff --git a/examples/show.rs b/examples/show.rs new file mode 100644 index 0000000..d426081 --- /dev/null +++ b/examples/show.rs @@ -0,0 +1,36 @@ +extern crate parity_wasm; + +use std::env; + +fn main() { + let args = env::args().collect::>(); + if args.len() != 3 { + println!("Usage: {} ", args[0]); + return; + } + + let module = parity_wasm::deserialize_file(&args[1]).expect("Failed to load module"); + let function_index = args[2].parse::().expect("Failed to parse function index"); + + if module.code_section().is_none() { + println!("no code in module!"); + std::process::exit(1); + } + + let sig = match module.function_section().unwrap().entries().get(function_index) { + Some(s) => s, + None => { + println!("no such function in module!"); + std::process::exit(1) + } + }; + + let sig_type = &module.type_section().expect("No type section: module malformed").types()[sig.type_ref() as usize]; + let code = &module.code_section().expect("Already checked, impossible").bodies()[function_index]; + + println!("signature: {:?}", sig_type); + println!("code: "); + for instruction in code.code().elements() { + println!("{}", instruction); + } +} \ No newline at end of file