wasmtime_wasi::bindings

Module sync

source
Expand description

Synchronous-generated bindings for WASI interfaces.

This is the same as the top-level bindings module of this crate except that it’s for synchronous calls.

§Examples

If you have a WIT world which refers to WASI interfaces you probably want to use this crate’s bindings rather than generate fresh bindings. That can be done using the with option to bindgen!:

use wasmtime_wasi::{WasiCtx, ResourceTable, WasiView};
use wasmtime::{Result, Engine};
use wasmtime::component::Linker;

wasmtime::component::bindgen!({
    inline: "
        package example:wasi;

        // An example of extending the `wasi:cli/command` world with a
        // custom host interface.
        world my-world {
            include wasi:cli/command@0.2.2;

            import custom-host;
        }

        interface custom-host {
            my-custom-function: func();
        }
    ",
    path: "wit",
    with: {
        "wasi": wasmtime_wasi::bindings::sync,
    },
    // This is required for bindings using `wasmtime-wasi` and it otherwise
    // isn't the default for non-async bindings.
    require_store_data_send: true,
});

struct MyState {
    table: ResourceTable,
    ctx: WasiCtx,
}

impl example::wasi::custom_host::Host for MyState {
    fn my_custom_function(&mut self) {
        // ..
    }
}

impl WasiView for MyState {
    fn table(&mut self) -> &mut ResourceTable { &mut self.table }
    fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
}

fn main() -> Result<()> {
    let engine = Engine::default();
    let mut linker: Linker<MyState> = Linker::new(&engine);
    wasmtime_wasi::add_to_linker_sync(&mut linker)?;
    example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?;

    // .. use `Linker` to instantiate component ...

    Ok(())
}

Modules§

Structs§