From 2a6b904c2ac8e3fd4d16450d73901221bdb16f90 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 10 Jul 2022 14:48:03 +0800 Subject: [PATCH] feat: wasmtime --- __wasm/wasmtime/add.c | 5 ++++ __wasm/wasmtime/add.wasm | Bin 0 -> 373 bytes __wasm/wasmtime/examples/example1.rs | 38 +++++++++++++++++++++++++ __wasm/wasmtime/justfile | 12 ++++++++ __wasm/wasmtime/src/main.rs | 40 +++++++++++---------------- 5 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 __wasm/wasmtime/add.c create mode 100755 __wasm/wasmtime/add.wasm create mode 100644 __wasm/wasmtime/examples/example1.rs create mode 100644 __wasm/wasmtime/justfile 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 0000000000000000000000000000000000000000..6bd522be623b253a42eecb590ac52a3499a5e8ec GIT binary patch literal 373 zcmZvXF;BxV5QXpT#A%a2fsM@(3lgG$RApl*6aV7s#8wM+;wTQPI-qpqXX7d(+;H!{ zd(x90DBmCeV9GWC8#azOa?Su_j=;o2GUEF56J;Z;PqROJGT{8jEcu>`#x$-!GPou> zgk-9fwNwwT_W)dJ4VV*Y?`Wr5Z4JjXpc)jJR&#biw6(5MmMY~9=NZw?sBWStxKsSZ zaY!0eWzB>lePax=R}|yjDGU^BeN}MDB;Oa2j3ksS4}v9|$>^TtMSPZ7A!IDYXOYNc zk-;izRb%k>zXo_NklY|Fi2R`HJ$3G*9ZU~T^R9RLt2VvIJe;-h-ss2bh?noKF;#C4 PvbL(-ikH>$d0GAfhoxO4 literal 0 HcmV?d00001 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(()) }