diff --git a/__wasm/wasmtime/examples/example2.rs b/__wasm/wasmtime/examples/example2.rs new file mode 100644 index 0000000..33594a8 --- /dev/null +++ b/__wasm/wasmtime/examples/example2.rs @@ -0,0 +1,57 @@ +extern crate core; + +use anyhow::Result; +use wasmtime::*; + +// #[wasm_bindgen] +// extern { +// pub fn alert(s: &str); +// } +// +// #[wasm_bindgen] +// pub fn greet(name: &str) { +// alert(&format!("Hello, {}!", name)); +// } +fn main() -> Result<()> { + // Modules can be compiled through either the text or binary format + let engine = Engine::default(); + let wasm = std::fs::read("./examples/hello_wasm_bg.wasm").unwrap(); + let module = Module::new(&engine, wasm)?; + + let mut linker = Linker::new(&engine); + linker.func_wrap("./hello_wasm.js", "__wbg_alert_a5a2f68cc09adc6e", |caller: Caller<'_, u32>, param: i32, param2: i32| { + let mut caller = caller; + if let Extern::Memory(memory) = caller.get_export("memory").unwrap() { + let mut buffer = vec![0; param2 as usize]; + memory.read(caller.as_context(), param as usize, &mut buffer).expect("read mem error"); + let s = String::from_utf8_lossy(&buffer).to_string(); + println!("[alert]: {}", s); + } else { + panic!("Should happen, got non 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 instance = linker.instantiate(&mut store, &module)?; + + let memory = instance.get_memory(&mut store, "memory") + .ok_or_else(|| anyhow::format_err!("failed to find `memory` export"))?; + + let msg = "hatter".as_bytes(); + + let malloc = instance.get_typed_func::(&mut store, "__wbindgen_malloc")?; + let p = malloc.call(&mut store, msg.len() as i32)?; + + memory.write(&mut store, p as usize, msg)?; + + 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(()) +} + diff --git a/__wasm/wasmtime/examples/hello_wasm_bg.wasm b/__wasm/wasmtime/examples/hello_wasm_bg.wasm new file mode 100644 index 0000000..1a65048 Binary files /dev/null and b/__wasm/wasmtime/examples/hello_wasm_bg.wasm differ diff --git a/__wasm/wasmtime/justfile b/__wasm/wasmtime/justfile index f645fde..0e45702 100644 --- a/__wasm/wasmtime/justfile +++ b/__wasm/wasmtime/justfile @@ -9,4 +9,7 @@ compile_add_wasm: run_example1: cargo run --example example1 +run_example2: + cargo run --example example2 + diff --git a/__wasm/wasmtime/src/main.rs b/__wasm/wasmtime/src/main.rs index 5db05b7..9a7deb7 100644 --- a/__wasm/wasmtime/src/main.rs +++ b/__wasm/wasmtime/src/main.rs @@ -6,6 +6,7 @@ fn main() -> Result<()> { // let engine = Engine::default(); let mut config = Config::default(); config.consume_fuel(true); + config.epoch_interruption(true); config.static_memory_maximum_size(1_000_000_u64); config.dynamic_memory_guard_size(0_u64); let engine = Engine::new(&config).unwrap(); @@ -15,8 +16,12 @@ fn main() -> Result<()> { let linker = Linker::new(&engine); let mut store = Store::new(&engine, 4); + store.set_epoch_deadline(10); + let instance = linker.instantiate(&mut store, &module)?; + let hello = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "add")?; + // let hello2 = instance.get_typed_func::<(&str, i32), i32, _>(&mut store, "add")?; store.add_fuel(10_000_u64).ok();