3.5 KiB
WASI
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 wasi-common
crate to define WASI
functions within a Linker
which can then be used to instantiate a
WebAssembly module.
WebAssembly module source code
For this WASI example, this Hello World program is compiled to a WebAssembly module using the WASI Preview 1 API.
wasi.rs
{{#include ../examples/wasi/wasm/wasi.rs}}
Building this program generates target/wasm32-wasi/debug/wasi.wasm
, used below.
Invoke the WASM module
This example shows adding and configuring the WASI imports to invoke the above WASM module.
main.rs
{{#include ../examples/wasi/main.rs}}
WASI state with other custom host state
The add_to_linker
takes a second argument which is a closure to access &mut WasiCtx
from within the T
stored in the Store<T>
itself. In the above
example this is trivial because the T
in Store<T>
is WasiCtx
itself, but
you can also store other state in Store
like so:
# extern crate wasmtime;
# extern crate wasi_common;
# extern crate anyhow;
use anyhow::Result;
use std::borrow::{Borrow, BorrowMut};
use wasmtime::*;
use wasi_common::{WasiCtx, sync::WasiCtxBuilder};
struct MyState {
message: String,
wasi: WasiCtx,
}
fn main() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
wasi_common::sync::add_to_linker(&mut linker, |state: &mut MyState| &mut state.wasi)?;
let wasi = WasiCtxBuilder::new()
.inherit_stdio()
.inherit_args()?
.build();
let mut store = Store::new(&engine, MyState {
message: format!("hello!"),
wasi,
});
// ...
# let _linker: Linker<MyState> = linker;
Ok(())
}
WASI Preview 2
An experimental implementation of the WASI Preview 2 API is also available, along with an adapter layer for WASI Preview 1 WebAssembly modules. In future this preview2
API will become the default. There are some features which are currently only accessible through the preview2
API such as async support and overriding the clock and random implementations.
Async example
This async example code shows how to use the wasmtime-wasi::preview2 module to
execute the same WASI Preview 1 WebAssembly module from the example above. This example requires the wasmtime
crate async
feature to be enabled.
This does not require any change to the WebAssembly module, it's just the WASI API host functions which are implemented to be async. See wasmtime async support.
{{#include ../examples/wasi-async/main.rs}}
You can also browse this source code online and clone the wasmtime repository to run the example locally.