seogrady opened issue #9205:
Hello :wave: I'm new to wasmtime and have some questions after going through the examples. It looks like wasi-common is being deprecated in favor of wasmtime-wasi. There's a few different options such as wasmtime_wasi, wasmtime_wasi::preview0 and wasmtime_wasi::preview1. You can build a wasi_ctx with WasiCtxBuilder::new().build() or WasiCtxBuilder::new().build_p1(). Is there an example component using the latest preview?
I've managed to convert the provided component example from wasi-common to use wasmtime_wasi with async enabled.
bindgen!({ world: "convert", path: "../wasi-component/wit", // tracing: true, async: { only_imports: [], }, trappable_imports: true, require_store_data_send: true, });
What do I need to do in order to get basic APIs working like stdio? I've tried adding inherit_stdio
WasiCtxBuilder::new().inherit_stdio().build()
to WasiCtxBuilder. Do I need to specify in the WIT file as well? :folded_hands:
alexcrichton commented on issue #9205:
There are some examples in the documentation of the
wasmtime-wasi
crate throughout various functions such asadd_to_linker_async
. Does that satisfy what you need? Or is there something else you're curious about?
seogrady commented on issue #9205:
Hey @alexcrichton! Thanks for your response. I've managed to implement the example from
crates/wasi/src/bindings.rs
and I'm callingmy_custom_function
from the host side that prints "Hello from host!".This is my host file:
use wasmtime::{ component::{bindgen, Component, Linker}, Config, Engine, Result, Store, }; use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView}; // Generate bindings of the guest and host components. bindgen!({ world: "my-world", path: "../wasi-component/wit", with: { "wasi": wasmtime_wasi::bindings, }, async: true, }); struct MyState { table: ResourceTable, ctx: WasiCtx, } #[async_trait::async_trait] impl example::wasi::custom_host::Host for MyState { async fn my_custom_function(&mut self) { println!("Hello from host!"); } } impl WasiView for MyState { fn table(&mut self) -> &mut ResourceTable { &mut self.table } fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx } } #[tokio::main] async fn main() -> Result<()> { let mut config = Config::default(); config.async_support(true); let engine = Engine::new(&config)?; let mut linker: Linker<MyState> = Linker::new(&engine); wasmtime_wasi::add_to_linker_async(&mut linker)?; example::wasi::custom_host::add_to_linker(&mut linker, |state| state)?; let component = Component::from_file(&engine, "crates/wasi-component/wasm/component.wasm")?; let mut store = Store::new( &engine, MyState { table: ResourceTable::new(), ctx: WasiCtxBuilder::new().inherit_stdio().build(), }, ); let command = MyWorld::instantiate_async(&mut store, &component, &linker).await?; let program_result = command.wasi_cli_run().call_run(&mut store).await?; match program_result { Ok(()) => Ok(()), Err(()) => std::process::exit(1), } }
And this is the component:
wit_bindgen::generate!({ world: "my-world", generate_all }); use example::wasi::custom_host::my_custom_function; use crate::exports::wasi::cli::run::Guest; struct GuestComponent; export!(GuestComponent); impl Guest for GuestComponent { fn run() -> Result<(), ()> { my_custom_function(); println!("Hello world"); Ok(()) } }
When I run the program I get "Hello from host!". How can I use println! from the component?
seogrady closed issue #9205:
Hello :wave: I'm new to wasmtime and have some questions after going through the examples. It looks like wasi-common is being deprecated in favor of wasmtime-wasi. There's a few different options such as wasmtime_wasi, wasmtime_wasi::preview0 and wasmtime_wasi::preview1. You can build a wasi_ctx with WasiCtxBuilder::new().build() or WasiCtxBuilder::new().build_p1(). Is there an example component using the latest preview?
I've managed to convert the provided component example from wasi-common to use wasmtime_wasi with async enabled.
bindgen!({ world: "convert", path: "../wasi-component/wit", // tracing: true, async: { only_imports: [], }, trappable_imports: true, require_store_data_send: true, });
What do I need to do in order to get basic APIs working like stdio? I've tried adding inherit_stdio
WasiCtxBuilder::new().inherit_stdio().build()
to WasiCtxBuilder. Do I need to specify in the WIT file as well? :folded_hands:
seogrady commented on issue #9205:
:relieved: Got there in the end. I needed to compile with target wasm32-wasip1, rather than wasm32-unknown-unknown. Missed that step when converting the component example to use wasmtime_wasi from wasi_common :+1:
Last updated: Dec 23 2024 at 12:05 UTC