feat: update wasmtime

This commit is contained in:
2022-07-16 16:03:51 +08:00
parent 4d45c2dbcf
commit 51685c6284

View File

@@ -13,7 +13,6 @@ use wasmtime::*;
// alert(&format!("Hello, {}!", name)); // alert(&format!("Hello, {}!", name));
// } // }
fn main() -> Result<()> { fn main() -> Result<()> {
// Modules can be compiled through either the text or binary format
let engine = Engine::default(); let engine = Engine::default();
let wasm = std::fs::read("./examples/hello_wasm_bg.wasm").unwrap(); let wasm = std::fs::read("./examples/hello_wasm_bg.wasm").unwrap();
let module = Module::new(&engine, wasm)?; let module = Module::new(&engine, wasm)?;
@@ -27,13 +26,10 @@ fn main() -> Result<()> {
let s = String::from_utf8_lossy(&buffer).to_string(); let s = String::from_utf8_lossy(&buffer).to_string();
println!("[alert]: {}", s); println!("[alert]: {}", s);
} else { } 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 mut store = Store::new(&engine, 0);
let instance = linker.instantiate(&mut store, &module)?; 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"))?; .ok_or_else(|| anyhow::format_err!("failed to find `memory` export"))?;
let msg = "hatter".as_bytes(); let msg = "hatter".as_bytes();
let malloc = instance.get_typed_func::<i32, i32, _>(&mut store, "__wbindgen_malloc")?; let malloc = instance.get_typed_func::<i32, i32, _>(&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 greet_fn = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "greet")?;
greet_fn.call(&mut store, (p, msg.len() as i32))?;
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))?;
Ok(()) Ok(())
} }