wasmtime_wasi

Function add_to_linker_sync

source
pub fn add_to_linker_sync<T: WasiView>(linker: &mut Linker<T>) -> Result<()>
Expand description

Add all WASI interfaces from this crate into the linker provided.

This function will add the synchronous variant of all interfaces into the [Linker] provided. By synchronous this means that this function is only compatible with Config::async_support(false). For embeddings with async support enabled see add_to_linker_async instead.

This function will add all interfaces implemented by this crate to the [Linker], which corresponds to the wasi:cli/imports world supported by this crate.

ยงExample

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

fn main() -> Result<()> {
    let engine = Engine::default();

    let mut linker = Linker::<MyState>::new(&engine);
    wasmtime_wasi::add_to_linker_sync(&mut linker)?;
    // ... add any further functionality to `linker` if desired ...

    let mut builder = WasiCtxBuilder::new();

    // ... configure `builder` more to add env vars, args, etc ...

    let mut store = Store::new(
        &engine,
        MyState {
            ctx: builder.build(),
            table: ResourceTable::new(),
        },
    );

    // ... use `linker` to instantiate within `store` ...

    Ok(())
}

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

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