Advertisement
Guest User

Untitled

a guest
Oct 13th, 2021
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. use anyhow::Result;
  2. use wasmtime::*;
  3.  
  4.  
  5. fn main() -> Result<()> {
  6.  
  7. let mut config = wasmtime::Config::new();
  8. config.debug_info(true);
  9.  
  10. let engine = wasmtime::Engine::new(&config)?;
  11.  
  12. let module = wasmtime::Module::from_file(&engine, "g:\\dev\\test\\test.wasm")?;
  13.  
  14. // All wasm objects operate within the context of a "store". Each
  15. // `Store` has a type parameter to store host-specific data, which in
  16. // this case we're using `4` for.
  17. let mut store = Store::new(&engine, 4);
  18. let host_fn = Func::wrap(&mut store, |caller: Caller<'_, u32>, param: i32| {
  19. println!("Got {} from WebAssembly", param);
  20. println!("my host state is: {}", caller.data());
  21. });
  22.  
  23. // Instantiation of a module requires specifying its imports and then
  24. // afterwards we can fetch exports by name, as well as asserting the
  25. // type signature of the function with `get_typed_func`.
  26. let instance = Instance::new(&mut store, &module, &[host_fn.into()])?;
  27. let func = instance.get_typed_func::<(), (), _>(&mut store, "mytest")?;
  28.  
  29. let result = instance.get_global(&mut store, "my_global");
  30.  
  31. match result {
  32. Some(result) => println!("Found !"),
  33. None => println!("Not found."),
  34. }
  35.  
  36. // Call wasm function which should change value of the global.
  37. func.call(&mut store, ())?;
  38.  
  39. Ok(())
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement