You can also browse this source code online and clone the wasmtime repository to run the example locally.
This example shows how to use the wasmtime-wasi
crate to define WASI
functions within a Linker
which can then be used to instantiate a
WebAssembly component.
For this WASI example, this Hello World program is compiled to a WebAssembly component using the WASIp2 API.
wasi.rs
{{#include ../examples/wasm/wasi.rs}}
Building instructions:
- Have Rust installed
- Add WASIp2 target if you haven't already:
rustup target add wasm32-wasip2
cargo build --target wasm32-wasip2
Building this program generates target/wasm32-wasip2/debug/wasi.wasm
, used below.
This example shows adding and configuring the WASI imports to invoke the above WASM component.
main.rs
{{#include ../examples/wasip2/main.rs}}
This async example code shows how to use the wasmtime-wasi crate to
execute the same WASIp2 component from the example above. This example requires the wasmtime
crate async
feature to be enabled.
This does not require any change to the WASIp2 component, it's just the WASIp2 API host functions which are implemented to be async. See wasmtime async support.
{{#include ../examples/wasip2-async/main.rs}}
You can also browse this source code online and clone the wasmtime repository to run the example locally.
Please see these references:
- The book for understanding the component model of WASIp2.
- Bindgen Examples for implementing WASIp2 hosts and guests.