feat: wasmtime

This commit is contained in:
2022-07-10 14:48:03 +08:00
parent 6160185ace
commit 2a6b904c2a
5 changed files with 71 additions and 24 deletions

5
__wasm/wasmtime/add.c Normal file
View File

@@ -0,0 +1,5 @@
// add.c
int add (int first, int second)
{
return first + second;
}

BIN
__wasm/wasmtime/add.wasm Executable file

Binary file not shown.

View File

@@ -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(())
}

12
__wasm/wasmtime/justfile Normal file
View File

@@ -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

View File

@@ -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(())
}