feat: add wasmtime
This commit is contained in:
1229
__wasm/wasmtime/Cargo.lock
generated
Normal file
1229
__wasm/wasmtime/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
__wasm/wasmtime/Cargo.toml
Normal file
11
__wasm/wasmtime/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "wasmtime"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.58"
|
||||
wasmtime = "0.38.1"
|
||||
|
||||
12
__wasm/wasmtime/README.md
Normal file
12
__wasm/wasmtime/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
wasmtime demo.wat
|
||||
|
||||
wat2wasm demo.wat
|
||||
|
||||
wasm2wat demo.wasm
|
||||
|
||||
|
||||
|
||||
https://github.com/bytecodealliance/wasmtime/blob/main/docs/WASI-tutorial.md
|
||||
|
||||
BIN
__wasm/wasmtime/hello_world.wasm
Normal file
BIN
__wasm/wasmtime/hello_world.wasm
Normal file
Binary file not shown.
27
__wasm/wasmtime/hello_world.wat
Normal file
27
__wasm/wasmtime/hello_world.wat
Normal file
@@ -0,0 +1,27 @@
|
||||
(module
|
||||
;; Import the required fd_write WASI function which will write the given io vectors to stdout
|
||||
;; The function signature for fd_write is:
|
||||
;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
|
||||
(import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
|
||||
|
||||
(memory 1)
|
||||
(export "memory" (memory 0))
|
||||
|
||||
;; Write 'hello world\n' to memory at an offset of 8 bytes
|
||||
;; Note the trailing newline which is required for the text to appear
|
||||
(data (i32.const 8) "hello world\n")
|
||||
|
||||
(func $main (export "_start")
|
||||
;; Creating a new io vector within linear memory
|
||||
(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
|
||||
(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
|
||||
|
||||
(call $fd_write
|
||||
(i32.const 1) ;; file_descriptor - 1 for stdout
|
||||
(i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
|
||||
(i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
|
||||
(i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
|
||||
)
|
||||
drop ;; Discard the number of bytes written from the top of the stack
|
||||
)
|
||||
)
|
||||
38
__wasm/wasmtime/src/main.rs
Normal file
38
__wasm/wasmtime/src/main.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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user