diff --git a/__wasm/wasmtime/examples/example4.rs b/__wasm/wasmtime/examples/example4.rs new file mode 100644 index 0000000..8cfb613 --- /dev/null +++ b/__wasm/wasmtime/examples/example4.rs @@ -0,0 +1,29 @@ +use anyhow::Result; +use wasmtime::*; + +fn main() -> Result<()> { + // Modules can be compiled through either the text or binary format + let engine = Engine::default(); + let wasm = include_bytes!("/Users/hatterjiang/Code/hattergit/simple-rust-tests/__wasm/wasmtime/wasms/simple_rust_wasm/target/wasm32-unknown-unknown/debug/simple_rust_wasm.wasm"); + let module = Module::new(&engine, wasm)?; + + // 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(()) +} \ No newline at end of file diff --git a/__wasm/wasmtime/justfile b/__wasm/wasmtime/justfile index 5f00ed0..c37a50d 100644 --- a/__wasm/wasmtime/justfile +++ b/__wasm/wasmtime/justfile @@ -15,3 +15,8 @@ run_example2: run_example3: cargo run --example example3 +run_example4: + cd wasms/simple_rust_wasm; cargo build --target wasm32-unknown-unknown + cp wasms/simple_rust_wasm/target/wasm32-unknown-unknown/debug/simple_rust_wasm.wasm . + cargo run --example example4 + rm simple_rust_wasm.wasm \ No newline at end of file diff --git a/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.lock b/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.lock new file mode 100644 index 0000000..6145c42 --- /dev/null +++ b/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "simple_rust_wasm" +version = "0.1.0" diff --git a/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.toml b/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.toml new file mode 100644 index 0000000..dea9c32 --- /dev/null +++ b/__wasm/wasmtime/wasms/simple_rust_wasm/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "simple_rust_wasm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ['cdylib'] + +[dependencies] + diff --git a/__wasm/wasmtime/wasms/simple_rust_wasm/justfile b/__wasm/wasmtime/wasms/simple_rust_wasm/justfile new file mode 100644 index 0000000..15c7fbb --- /dev/null +++ b/__wasm/wasmtime/wasms/simple_rust_wasm/justfile @@ -0,0 +1,7 @@ +_: + @just --list + +build: + cargo build --target wasm32-unknown-unknown + + diff --git a/__wasm/wasmtime/wasms/simple_rust_wasm/src/lib.rs b/__wasm/wasmtime/wasms/simple_rust_wasm/src/lib.rs new file mode 100644 index 0000000..6762799 --- /dev/null +++ b/__wasm/wasmtime/wasms/simple_rust_wasm/src/lib.rs @@ -0,0 +1,12 @@ +#[link(wasm_import_module = "host")] +extern "C" { + + #[link_name = "hello"] + fn host_hello(a: i32); +} + +#[no_mangle] +pub extern "C" fn hello() { + unsafe { host_hello(3); } +} +