From 51685c6284420b66a6d5be867beadfff6a4dcbe6 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 16 Jul 2022 16:03:51 +0800 Subject: [PATCH] feat: update wasmtime --- __wasm/wasmtime/examples/example2.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/__wasm/wasmtime/examples/example2.rs b/__wasm/wasmtime/examples/example2.rs index 33594a8..064c04b 100644 --- a/__wasm/wasmtime/examples/example2.rs +++ b/__wasm/wasmtime/examples/example2.rs @@ -13,7 +13,6 @@ use wasmtime::*; // alert(&format!("Hello, {}!", name)); // } fn main() -> Result<()> { - // Modules can be compiled through either the text or binary format let engine = Engine::default(); let wasm = std::fs::read("./examples/hello_wasm_bg.wasm").unwrap(); let module = Module::new(&engine, wasm)?; @@ -27,13 +26,10 @@ fn main() -> Result<()> { let s = String::from_utf8_lossy(&buffer).to_string(); println!("[alert]: {}", s); } else { - panic!("Should happen, got non memory"); + panic!("Should happen, got not memory"); } })?; - // All wasm objects operate within the context of a "store". Each - // `Store` has a type parameter to store host-specific data, which in - // this case we're using `4` for. let mut store = Store::new(&engine, 0); let instance = linker.instantiate(&mut store, &module)?; @@ -41,16 +37,12 @@ fn main() -> Result<()> { .ok_or_else(|| anyhow::format_err!("failed to find `memory` export"))?; let msg = "hatter".as_bytes(); - let malloc = instance.get_typed_func::(&mut store, "__wbindgen_malloc")?; - let p = malloc.call(&mut store, msg.len() as i32)?; + let msg_ptr = malloc.call(&mut store, msg.len() as i32)?; + memory.write(&mut store, msg_ptr as usize, msg)?; - memory.write(&mut store, p as usize, msg)?; - - let hello = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "greet")?; - - // And finally we can call the wasm! - hello.call(&mut store, (p, msg.len() as i32))?; + let greet_fn = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "greet")?; + greet_fn.call(&mut store, (p, msg.len() as i32))?; Ok(()) }