From dfc94311dd9a0e5b48971caf39d5cbb481b5dd77 Mon Sep 17 00:00:00 2001 From: silur Date: Tue, 30 Nov 2021 00:01:25 +0100 Subject: [PATCH] get function/export names from wasm --- Cargo.toml | 1 + src/lib.rs | 38 +++++++++++++------------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c22fc4..43b0854 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" wasmi = "0.9.1" wabt = "0.10.0" clap = "3.0.0-beta.2" +parity-wasm = "0.42.2" diff --git a/src/lib.rs b/src/lib.rs index b5d74d8..d6e97f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,13 +8,16 @@ fn mutate() -> Vec { unimplemented!(); } +fn parse_expr(expr: Vec) -> Vec { + unimplemented!(); +} + fn asd() { - // Parse WAT (WebAssembly Text format) into wasm bytecode. let wasm_binary: Vec = wabt::wat2wasm( r#" (module - (func (export "test") (result i32) + (func (export "test") (param i32) (result i32) i32.const 1337 ) ) @@ -22,30 +25,15 @@ fn asd() { ) .expect("failed to parse wat"); - // Load wasm binary and prepare it for instantiation. - let module = wasmi::Module::from_buffer(&wasm_binary) + let module = parity_wasm::elements::Module::from_bytes(&wasm_binary) .expect("failed to load wasm"); - - // Instantiate a module with empty imports and - // assert that there is no `start` function. - let instance = - ModuleInstance::new( - &module, - &ImportsBuilder::default() - ) - .expect("failed to instantiate wasm module") - .assert_no_start(); - - // Finally, invoke the exported function "test" with no parameters - // and empty external function executor. - assert_eq!( - instance.invoke_export( - "test", - &[], - &mut NopExternals, - ).expect("failed to execute export"), - Some(RuntimeValue::I32(1337)), - ); + + for (export, function) in module.export_section().unwrap().entries().iter() + .zip(module.function_section().unwrap().entries().iter()) { + let func_body: parity_wasm::elements::FuncBody = + function.try_into().unwrap(); + println!("{} - {:?}", export.field(), function); + } } #[cfg(test)]