Stream: general

Topic: ✔ wasm component model + wasi


view this post on Zulip Dima (Dec 20 2023 at 12:05):

Hi there!

I have a code:

    // Set up Wasmtime components
    let mut config = Config::new();
    config.wasm_component_model(true);
    let engine = Engine::new(&config)?;
    let component = Component::new(&engine, decoded_input)?;

    // Set up Wasmtime linker
    let mut linker = Linker::new(&engine);
    MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?;

    // Set up Wasmtime store
    let mut store = Store::new(&engine, MyState);
    let (bindings, _) = MyWorld::instantiate(&mut store, &component, &linker)?;

I've updated guest code so it needs wasi now:

package root:component;

world root {
  import levo:portal/my-imports;
  import wasi:io/error@0.2.0-rc-2023-11-10;
  import wasi:io/streams@0.2.0-rc-2023-11-10;
  import wasi:filesystem/types@0.2.0-rc-2023-11-10;
  import wasi:filesystem/preopens@0.2.0-rc-2023-11-10;
  import wasi:sockets/tcp@0.2.0-rc-2023-11-10;
  import wasi:cli/environment@0.2.0-rc-2023-11-10;
  import wasi:cli/exit@0.2.0-rc-2023-11-10;
  import wasi:cli/stdin@0.2.0-rc-2023-11-10;
  import wasi:cli/stdout@0.2.0-rc-2023-11-10;
  import wasi:cli/stderr@0.2.0-rc-2023-11-10;
  import wasi:cli/terminal-input@0.2.0-rc-2023-11-10;
  import wasi:cli/terminal-output@0.2.0-rc-2023-11-10;
  import wasi:cli/terminal-stdin@0.2.0-rc-2023-11-10;
  import wasi:cli/terminal-stdout@0.2.0-rc-2023-11-10;
  import wasi:cli/terminal-stderr@0.2.0-rc-2023-11-10;

  export update: func();
  export setup: func();
}

How can I provide wasi environment to my wasm component wasmtime execution?
I've tried wasmtime-wasi and I think the answer is to use Preview2 but is there any code example how to achieve it? I think spin app uses component model and wasi but I wasn't able to figure out how to adjust my code accordingly, to support wasi with my wasm component.

Thank you,
Dima :)

view this post on Zulip Pat Hickey (Dec 20 2023 at 20:52):

https://docs.rs/wasmtime-wasi/latest/wasmtime_wasi/preview2/command/fn.add_to_linker.html

view this post on Zulip Pat Hickey (Dec 20 2023 at 20:52):

or command::sync::add_to_linker if you are not using Config::async_support

view this post on Zulip Dima (Dec 22 2023 at 12:33):

Pat Hickey said:

or command::sync::add_to_linker if you are not using Config::async_support

Thanks! Is there a way to pass to the guest app host imports when using wasi with component model?

I have something like this:

struct MyCtx {
    table: Table,
    wasi: WasiCtx,
}

impl WasiView for MyCtx {
    fn table(&self) -> &Table {
        &self.table
    }
    fn table_mut(&mut self) -> &mut Table {
        &mut self.table
    }
    fn ctx(&self) -> &WasiCtx {
        &self.wasi
    }
    fn ctx_mut(&mut self) -> &mut WasiCtx {
        &mut self.wasi
    }
}

// #[async_trait::async_trait]
impl Host for MyCtx {
    fn gen_random_integer(&mut self) -> wasmtime::Result<u32> {
        Ok(rand::thread_rng().next_u32())
    }

    fn print(&mut self, from_wasm: String) -> wasmtime::Result<(), wasmtime::Error> {
        dbg!(from_wasm);
        Ok(())
    }
}
...
...
...
    // Set up Wasmtime components
    let mut config = Config::new();
    config.wasm_component_model(true).async_support(true);
    let engine = Engine::new(&config)?;
    let component = Component::new(&engine, decoded_input)?;

    // Set up Wasmtime linker
    let mut linker = Linker::new(&engine);
    add_to_linker(&mut linker)?;
    let table = Table::new();
    let wasi = WasiCtxBuilder::new()
        .build();

    // Set up Wasmtime store
    let mut store = Store::new(&engine, MyCtx { table, wasi });
    let (bindings, _) = MyWorld::instantiate_async(&mut store, &component, &linker).await?;

Unfortunately it doesn't work: it doesn't see that imports implementation for print and gen_random_integer were provided. I feel I am missing something obvious.

view this post on Zulip Lann Martin (Dec 22 2023 at 13:50):

You probably need an additional call to Host::add_to_linker; see HelloWorld::add_to_linker in the bindgen example.

view this post on Zulip Notification Bot (Dec 22 2023 at 14:45):

Dima has marked this topic as resolved.


Last updated: Nov 22 2024 at 16:03 UTC