tmtbe opened issue #7579:
This is WIT:
package local:demo; interface host{ log: func(msg: string); } world my-world { import host; export run: func(); }
This is component code:
use crate::local::demo::host::log; // Use a procedural macro to generate bindings for the world we specified in // `host.wit` wit_bindgen::generate!({ // the name of the world in the `*.wit` input file world: "my-world", // For all exported worlds, interfaces, and resources, this specifies what // type they're corresponding to in this module. In this case the `MyHost` // struct defined below is going to define the exports of the `world`, // namely the `run` function. exports: { world: MyWord, }, }); // Define a custom type and implement the generated `Guest` trait for it which // represents implementing all the necessary exported interfaces for this // component. struct MyWord; impl Guest for MyWord { fn run() { log("Hello, world!"); } }
wasm-tools component new ./target/wasm32-wasi/debug/wasm_test2.wasm -o my-world.wasm --adapt ./wasi_snapshot_preview1.reactor.wasm
This is the relevant call code:
use wasmtime::{Config, Engine, Store}; use wasmtime::component::bindgen; use wasmtime::component::Component; use wasmtime::component::Linker; use crate::local::demo::host::Host; bindgen!(); struct MyState { } impl Host for MyState{ fn log(&mut self, msg: String) -> wasmtime::Result<()> { Ok(println!("{msg}")) } } fn main() -> wasmtime::Result<()> { // Configure an `Engine` and compile the `Component` that is being run for // the application. let mut config = Config::new(); config.wasm_component_model(true); let engine = Engine::new(&config)?; let component = Component::from_file(&engine, "./my-world.wasm")?; let mut store = Store::new( &engine, MyState {}, ); let mut linker:Linker<MyState> = Linker::new(&engine); MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; let (bindings, _) = MyWorld::instantiate(&mut store, &component, &linker)?; Ok(bindings.call_run(&mut store)?) }
Error: import `wasi:io/error@0.2.0-rc-2023-11-10` has the wrong type Caused by: 0: instance export `error` has the wrong type 1: expected resource found nothing
tmtbe commented on issue #7579:
My wasmtime version is 15.0.0 ,the adpate wasm version is 15.0.0 too.
tmtbe commented on issue #7579:
When I switch to 14.0.4 I still get the error, I don't know what caused it:
Error: import `wasi:io/streams@0.2.0-rc-2023-10-18` has the wrong type Caused by: 0: instance export `output-stream` has the wrong type 1: expected resource found nothing
The version of the adapter is consistent with the wasmtime library.
tmtbe commented on issue #7579:
I forgot to add wasmtime-wasi = "15.0.0" dependency, now no problem
tmtbe closed issue #7579:
This is WIT:
package local:demo; interface host{ log: func(msg: string); } world my-world { import host; export run: func(); }
This is component code:
use crate::local::demo::host::log; // Use a procedural macro to generate bindings for the world we specified in // `host.wit` wit_bindgen::generate!({ // the name of the world in the `*.wit` input file world: "my-world", // For all exported worlds, interfaces, and resources, this specifies what // type they're corresponding to in this module. In this case the `MyHost` // struct defined below is going to define the exports of the `world`, // namely the `run` function. exports: { world: MyWord, }, }); // Define a custom type and implement the generated `Guest` trait for it which // represents implementing all the necessary exported interfaces for this // component. struct MyWord; impl Guest for MyWord { fn run() { log("Hello, world!"); } }
wasm-tools component new ./target/wasm32-wasi/debug/wasm_test2.wasm -o my-world.wasm --adapt ./wasi_snapshot_preview1.reactor.wasm
This is the relevant call code:
use wasmtime::{Config, Engine, Store}; use wasmtime::component::bindgen; use wasmtime::component::Component; use wasmtime::component::Linker; use crate::local::demo::host::Host; bindgen!(); struct MyState { } impl Host for MyState{ fn log(&mut self, msg: String) -> wasmtime::Result<()> { Ok(println!("{msg}")) } } fn main() -> wasmtime::Result<()> { // Configure an `Engine` and compile the `Component` that is being run for // the application. let mut config = Config::new(); config.wasm_component_model(true); let engine = Engine::new(&config)?; let component = Component::from_file(&engine, "./my-world.wasm")?; let mut store = Store::new( &engine, MyState {}, ); let mut linker:Linker<MyState> = Linker::new(&engine); MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; let (bindings, _) = MyWorld::instantiate(&mut store, &component, &linker)?; Ok(bindings.call_run(&mut store)?) }
Error: import `wasi:io/error@0.2.0-rc-2023-11-10` has the wrong type Caused by: 0: instance export `error` has the wrong type 1: expected resource found nothing
tmtbe deleted a comment on issue #7579:
I forgot to add wasmtime-wasi = "15.0.0" dependency, now no problem
tmtbe reopened issue #7579:
This is WIT:
package local:demo; interface host{ log: func(msg: string); } world my-world { import host; export run: func(); }
This is component code:
use crate::local::demo::host::log; // Use a procedural macro to generate bindings for the world we specified in // `host.wit` wit_bindgen::generate!({ // the name of the world in the `*.wit` input file world: "my-world", // For all exported worlds, interfaces, and resources, this specifies what // type they're corresponding to in this module. In this case the `MyHost` // struct defined below is going to define the exports of the `world`, // namely the `run` function. exports: { world: MyWord, }, }); // Define a custom type and implement the generated `Guest` trait for it which // represents implementing all the necessary exported interfaces for this // component. struct MyWord; impl Guest for MyWord { fn run() { log("Hello, world!"); } }
wasm-tools component new ./target/wasm32-wasi/debug/wasm_test2.wasm -o my-world.wasm --adapt ./wasi_snapshot_preview1.reactor.wasm
This is the relevant call code:
use wasmtime::{Config, Engine, Store}; use wasmtime::component::bindgen; use wasmtime::component::Component; use wasmtime::component::Linker; use crate::local::demo::host::Host; bindgen!(); struct MyState { } impl Host for MyState{ fn log(&mut self, msg: String) -> wasmtime::Result<()> { Ok(println!("{msg}")) } } fn main() -> wasmtime::Result<()> { // Configure an `Engine` and compile the `Component` that is being run for // the application. let mut config = Config::new(); config.wasm_component_model(true); let engine = Engine::new(&config)?; let component = Component::from_file(&engine, "./my-world.wasm")?; let mut store = Store::new( &engine, MyState {}, ); let mut linker:Linker<MyState> = Linker::new(&engine); MyWorld::add_to_linker(&mut linker, |state: &mut MyState| state)?; let (bindings, _) = MyWorld::instantiate(&mut store, &component, &linker)?; Ok(bindings.call_run(&mut store)?) }
Error: import `wasi:io/error@0.2.0-rc-2023-11-10` has the wrong type Caused by: 0: instance export `error` has the wrong type 1: expected resource found nothing
tmtbe commented on issue #7579:
It needs
wasmtime_wasi::preview2::command::sync::add_to_linker(&mut linker)?;
for it to work but I don't know where the documentation is.
alexcrichton commented on issue #7579:
Yes the "problem" here in a sense is the
--adapt ./wasi_snapshot_preview1.reactor.wasm
argument you're passing. Theworld my-world
annotation doesn't actually fully describe the world of this component because through the adapter the component is going to need to additionally import WASI interfaces. If you usewasm-tools component wit my-world.wasm
you'll see that the component imports WASI interfaces.To provide WASI interfaces in the linker you're going to want to use the
wasmtime_wasi::preview2::*
APIs.
alexcrichton commented on issue #7579:
Ah sorry, you've already found this. Currently WASI is not documented well, and that's something we should improve.
Last updated: Dec 23 2024 at 12:05 UTC