Files
simple-rust-tests/__wasm/wasmtime/examples/example4.rs
2023-01-18 23:46:14 +08:00

29 lines
1.2 KiB
Rust

use anyhow::Result;
use wasmtime::*;
fn main() -> Result<()> {
// Modules can be compiled through either the text or binary format
let engine = Engine::default();
let wasm = include_bytes!("/Users/hatterjiang/Code/hattergit/simple-rust-tests/__wasm/wasmtime/wasms/simple_rust_wasm/target/wasm32-unknown-unknown/debug/simple_rust_wasm.wasm");
let module = Module::new(&engine, wasm)?;
// Create a `Linker` which will be later used to instantiate this module.
// Host functionality is defined by name within the `Linker`.
let mut linker = Linker::new(&engine);
linker.func_wrap("host", "hello", |caller: Caller<'_, u32>, param: i32| {
println!("Got {} from WebAssembly", param);
println!("my host state is: {}", caller.data());
})?;
// 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, 4);
let instance = linker.instantiate(&mut store, &module)?;
let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
// And finally we can call the wasm!
hello.call(&mut store, ())?;
Ok(())
}