diff --git a/__wasm/wasmtime/add.c b/__wasm/wasmtime/add.c new file mode 100644 index 0000000..d71efc0 --- /dev/null +++ b/__wasm/wasmtime/add.c @@ -0,0 +1,5 @@ +// add.c +int add (int first, int second) +{ + return first + second; +} diff --git a/__wasm/wasmtime/add.wasm b/__wasm/wasmtime/add.wasm new file mode 100755 index 0000000..6bd522b Binary files /dev/null and b/__wasm/wasmtime/add.wasm differ diff --git a/__wasm/wasmtime/examples/example1.rs b/__wasm/wasmtime/examples/example1.rs new file mode 100644 index 0000000..10fb87a --- /dev/null +++ b/__wasm/wasmtime/examples/example1.rs @@ -0,0 +1,38 @@ +use anyhow::Result; +use wasmtime::*; + +fn main() -> Result<()> { + // Modules can be compiled through either the text or binary format + let engine = Engine::default(); + let wat = r#" + (module + (import "host" "hello" (func $host_hello (param i32))) + + (func (export "hello") + i32.const 3 + call $host_hello) + ) + "#; + let module = Module::new(&engine, wat)?; + + // 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(()) +} + diff --git a/__wasm/wasmtime/justfile b/__wasm/wasmtime/justfile new file mode 100644 index 0000000..84c7ae3 --- /dev/null +++ b/__wasm/wasmtime/justfile @@ -0,0 +1,12 @@ +_: + @just --list + +# https://git.hatter.ink/hatter/learn_programming/src/branch/master/webassembly/clang-llvm.md +compile_add_wasm: + export PATH=/usr/local/opt/llvm/bin:$PATH + clang --target=wasm32 --no-standard-libraries -Wl,--export-all -Wl,--no-entry -o add.wasm add.c + +run_example1: + cargo run --example example1 + + diff --git a/__wasm/wasmtime/src/main.rs b/__wasm/wasmtime/src/main.rs index 10fb87a..5db05b7 100644 --- a/__wasm/wasmtime/src/main.rs +++ b/__wasm/wasmtime/src/main.rs @@ -3,35 +3,27 @@ use wasmtime::*; fn main() -> Result<()> { // Modules can be compiled through either the text or binary format - let engine = Engine::default(); - let wat = r#" - (module - (import "host" "hello" (func $host_hello (param i32))) + // let engine = Engine::default(); + let mut config = Config::default(); + config.consume_fuel(true); + config.static_memory_maximum_size(1_000_000_u64); + config.dynamic_memory_guard_size(0_u64); + let engine = Engine::new(&config).unwrap(); - (func (export "hello") - i32.const 3 - call $host_hello) - ) - "#; - let module = Module::new(&engine, wat)?; + let add_wasm_bytes = std::fs::read("add.wasm").expect("Read wasm failed"); + let module = Module::new(&engine, add_wasm_bytes)?; - // 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 linker = Linker::new(&engine); let mut store = Store::new(&engine, 4); let instance = linker.instantiate(&mut store, &module)?; - let hello = instance.get_typed_func::<(), (), _>(&mut store, "hello")?; + let hello = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "add")?; - // And finally we can call the wasm! - hello.call(&mut store, ())?; + store.add_fuel(10_000_u64).ok(); + + println!("1 + 2 = {:?}", hello.call(&mut store, (1, 2))?); + println!("Fuel consumed: {:?}", store.fuel_consumed()); + println!("2 + 3 = {:?}", hello.call(&mut store, (2, 3))?); + println!("Fuel consumed: {:?}", store.fuel_consumed()); Ok(()) }