feat: wasmtime
This commit is contained in:
5
__wasm/wasmtime/add.c
Normal file
5
__wasm/wasmtime/add.c
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// add.c
|
||||||
|
int add (int first, int second)
|
||||||
|
{
|
||||||
|
return first + second;
|
||||||
|
}
|
||||||
BIN
__wasm/wasmtime/add.wasm
Executable file
BIN
__wasm/wasmtime/add.wasm
Executable file
Binary file not shown.
38
__wasm/wasmtime/examples/example1.rs
Normal file
38
__wasm/wasmtime/examples/example1.rs
Normal 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
12
__wasm/wasmtime/justfile
Normal 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
|
||||||
|
|
||||||
|
|
||||||
@@ -3,35 +3,27 @@ use wasmtime::*;
|
|||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Modules can be compiled through either the text or binary format
|
// Modules can be compiled through either the text or binary format
|
||||||
let engine = Engine::default();
|
// let engine = Engine::default();
|
||||||
let wat = r#"
|
let mut config = Config::default();
|
||||||
(module
|
config.consume_fuel(true);
|
||||||
(import "host" "hello" (func $host_hello (param i32)))
|
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")
|
let add_wasm_bytes = std::fs::read("add.wasm").expect("Read wasm failed");
|
||||||
i32.const 3
|
let module = Module::new(&engine, add_wasm_bytes)?;
|
||||||
call $host_hello)
|
|
||||||
)
|
|
||||||
"#;
|
|
||||||
let module = Module::new(&engine, wat)?;
|
|
||||||
|
|
||||||
// Create a `Linker` which will be later used to instantiate this module.
|
let linker = Linker::new(&engine);
|
||||||
// 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 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::<(i32, i32), i32, _>(&mut store, "add")?;
|
||||||
|
|
||||||
// And finally we can call the wasm!
|
store.add_fuel(10_000_u64).ok();
|
||||||
hello.call(&mut store, ())?;
|
|
||||||
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user