Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use anyhow::Result;
- use wasmtime::*;
- fn main() -> Result<()> {
- let mut config = wasmtime::Config::new();
- config.debug_info(true);
- let engine = wasmtime::Engine::new(&config)?;
- let module = wasmtime::Module::from_file(&engine, "g:\\dev\\test\\test.wasm")?;
- // 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 host_fn = Func::wrap(&mut store, |caller: Caller<'_, u32>, param: i32| {
- println!("Got {} from WebAssembly", param);
- println!("my host state is: {}", caller.data());
- });
- // Instantiation of a module requires specifying its imports and then
- // afterwards we can fetch exports by name, as well as asserting the
- // type signature of the function with `get_typed_func`.
- let instance = Instance::new(&mut store, &module, &[host_fn.into()])?;
- let func = instance.get_typed_func::<(), (), _>(&mut store, "mytest")?;
- let result = instance.get_global(&mut store, "my_global");
- match result {
- Some(result) => println!("Found !"),
- None => println!("Not found."),
- }
- // Call wasm function which should change value of the global.
- func.call(&mut store, ())?;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement