feat: update examples

This commit is contained in:
2023-01-18 23:57:29 +08:00
parent d514f80f64
commit 3eefa3cba1
3 changed files with 54 additions and 28 deletions

View File

@@ -28,7 +28,7 @@ fn main() -> Result<()> {
// this case we're using `4` for. // this case we're using `4` for.
let mut store = Store::new(&engine, 4); let mut store = Store::new(&engine, 4);
let instance = linker.instantiate(&mut store, &module)?; let instance = linker.instantiate(&mut store, &module)?;
let hello = instance.get_typed_func::<(), (), _>(&mut store, "hello")?; let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
// And finally we can call the wasm! // And finally we can call the wasm!
hello.call(&mut store, ())?; hello.call(&mut store, ())?;

View File

@@ -38,11 +38,11 @@ fn main() -> Result<()> {
// println!("memory size: {}", memory.size(&store)); // println!("memory size: {}", memory.size(&store));
let msg = "hatter".as_bytes(); let msg = "hatter".as_bytes();
let malloc_fn = instance.get_typed_func::<i32, i32, _>(&mut store, "__wbindgen_malloc")?; let malloc_fn = instance.get_typed_func::<i32, i32>(&mut store, "__wbindgen_malloc")?;
let msg_ptr = malloc_fn.call(&mut store, msg.len() as i32)?; let msg_ptr = malloc_fn.call(&mut store, msg.len() as i32)?;
memory.write(&mut store, msg_ptr as usize, msg)?; memory.write(&mut store, msg_ptr as usize, msg)?;
let greet_fn = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "greet")?; let greet_fn = instance.get_typed_func::<(i32, i32), ()>(&mut store, "greet")?;
greet_fn.call(&mut store, (msg_ptr, msg.len() as i32))?; greet_fn.call(&mut store, (msg_ptr, msg.len() as i32))?;
Ok(()) Ok(())

View File

@@ -1,37 +1,63 @@
use anyhow::Result; use anyhow::Result;
use wasmtime::*; use wasmtime::*;
struct MyState {
name: String,
count: usize,
}
fn main() -> Result<()> { fn main() -> Result<()> {
// Modules can be compiled through either the text or binary format // First the wasm module needs to be compiled. This is done with a global
// "compilation environment" within an `Engine`. Note that engines can be
// further configured through `Config` if desired instead of using the
// default like this is here.
println!("Compiling module...");
let engine = Engine::default(); let engine = Engine::default();
let wat = r#" let module = Module::new(&engine, r#"
(module (module
(import "host" "hello" (func $host_hello (param i32))) (func $hello (import "" "hello"))
(func (export "run") (call $hello))
)"#).unwrap();
(func (export "hello") // After a module is compiled we create a `Store` which will contain
i32.const 3 // instantiated modules and other items like host functions. A Store
call $host_hello) // contains an arbitrary piece of host information, and we use `MyState`
) // here.
"#; println!("Initializing...");
let module = Module::new(&engine, wat)?; let mut store = Store::new(
&engine,
MyState {
name: "hello, world!".to_string(),
count: 0,
},
);
// Create a `Linker` which will be later used to instantiate this module. // Our wasm module we'll be instantiating requires one imported function.
// Host functionality is defined by name within the `Linker`. // the function takes no parameters and returns no results. We create a host
let mut linker = Linker::new(&engine); // implementation of that function here, and the `caller` parameter here is
linker.func_wrap("host", "hello", |caller: Caller<'_, u32>, param: i32| { // used to get access to our original `MyState` value.
println!("Got {} from WebAssembly", param); println!("Creating callback...");
println!("my host state is: {}", caller.data()); let hello_func = Func::wrap(&mut store, |mut caller: Caller<'_, MyState>| {
})?; println!("Calling back...");
println!("> {}", caller.data().name);
caller.data_mut().count += 1;
});
// All wasm objects operate within the context of a "store". Each // Once we've got that all set up we can then move to the instantiation
// `Store` has a type parameter to store host-specific data, which in // phase, pairing together a compiled module as well as a set of imports.
// this case we're using `4` for. // Note that this is where the wasm `start` function, if any, would run.
let mut store = Store::new(&engine, 4); println!("Instantiating module...");
let instance = linker.instantiate(&mut store, &module)?; let imports = [hello_func.into()];
let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?; let instance = Instance::new(&mut store, &module, &imports)?;
// And finally we can call the wasm! // Next we poke around a bit to extract the `run` function from the module.
hello.call(&mut store, ())?; println!("Extracting export...");
let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
// And last but not least we can call it!
println!("Calling export...");
run.call(&mut store, ())?;
println!("Done.");
Ok(()) Ok(())
} }