wasmtime_wasi

Module preview1

Source
Available on crate feature preview1 only.
Expand description

Bindings for WASIp1 aka Preview 1 aka wasi_snapshot_preview1.

This module contains runtime support for configuring and executing WASIp1-using core WebAssembly modules. Support for WASIp1 is built on top of support for WASIp2 available at the crate root, but that’s just an internal implementation detail.

Unlike the crate root, support for WASIp1 centers around two APIs:

First a WasiCtxBuilder will be used and finalized with the build_p1 method to create a WasiCtx. Next a wasmtime::Linker is configured with WASI imports by using the add_to_linker_* desired (sync or async depending on Config::async_support).

Note that WASIp1 is not as extensible or configurable as WASIp2 so the support in this module is enough to run wasm modules but any customization beyond that WasiCtxBuilder already supports is not possible yet.

§Components vs Modules

Note that WASIp1 does not work for components at this time, only core wasm modules. That means this module is only for users of wasmtime::Module and wasmtime::Linker, for example. If you’re using wasmtime::component::Component or wasmtime::component::Linker you’ll want the WASIp2 support this crate has instead.

§Examples

use wasmtime::{Result, Engine, Linker, Module, Store};
use wasmtime_wasi::preview1::{self, WasiP1Ctx};
use wasmtime_wasi::WasiCtxBuilder;

// An example of executing a WASIp1 "command"
fn main() -> Result<()> {
    let args = std::env::args().skip(1).collect::<Vec<_>>();
    let engine = Engine::default();
    let module = Module::from_file(&engine, &args[0])?;

    let mut linker: Linker<WasiP1Ctx> = Linker::new(&engine);
    preview1::add_to_linker_async(&mut linker, |t| t)?;
    let pre = linker.instantiate_pre(&module)?;

    let wasi_ctx = WasiCtxBuilder::new()
        .inherit_stdio()
        .inherit_env()
        .args(&args)
        .build_p1();

    let mut store = Store::new(&engine, wasi_ctx);
    let instance = pre.instantiate(&mut store)?;
    let func = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
    func.call(&mut store, ())?;

    Ok(())
}

Modules§

types
wasi_snapshot_preview1

Structs§

WasiP1Ctx
Structure containing state for WASIp1.

Functions§

add_to_linker_async
Adds asynchronous versions of all WASIp1 functions to the wasmtime::Linker provided.
add_to_linker_sync
Adds synchronous versions of all WASIp1 functions to the wasmtime::Linker provided.