Stream: wasmtime

Topic: Example of host for component model?


view this post on Zulip Ben Little (Apr 06 2024 at 18:08):

I'm trying to build a simple host for components built using cargo component new. I've basically copy/pasted the code here: https://github.com/bytecodealliance/wasmtime/blob/main/examples/component/main.rs

I deleted the bindgen call and add_to_linker from the example because my component does not use any explicit imports.

However my component (hello world from cargo component new) can't be instantiated. I get "Error: import wasi:cli/environment@0.2.0 has the wrong type"

I believe this is because bin components _implicitly_ import the wasi:cli world, but I could be mistaken. I also get the same error with a lib component with no imports.

Is there a good working example of a host that can run these components? I know that the wasmtime command can do it, but this is far from a minimal example and I'm getting lost in the code.

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Ben Little (Apr 06 2024 at 18:13):

Code for context: https://github.com/littlebenlittle/wasi-repl/blob/error/host/src/main.rs

Contribute to littlebenlittle/wasi-repl development by creating an account on GitHub.

view this post on Zulip Lann Martin (Apr 06 2024 at 18:24):

The wasm32-wasi rust target implements stdlib features with wasi imports.

view this post on Zulip Lann Martin (Apr 06 2024 at 18:25):

See this recent thread for similar discussion: https://bytecodealliance.zulipchat.com/#narrow/stream/407292-cargo-component/topic/.E2.9C.94.20Test.20host.20and.20guest.20not.20working

view this post on Zulip Ben Little (Apr 06 2024 at 18:29):

Thanks for the quick reply, Lann! I think this gets me pointed in the right direction. I'll update if (when) I get confused.

view this post on Zulip Ben Little (Apr 06 2024 at 19:02):

Sekhar's advice was exactly what I needed. I haven't been so delighted to see "hello world" in my terminal for quite some time.

Some general takeaways:

  1. Use wasm-tools component wit to view the actual interface used by the component
  2. Use the wasmtime-wasi crate and implement WasiView on your store's Host type

view this post on Zulip Karel Hrkal (kajacx) (Apr 13 2024 at 10:55):

I have a working example in this repo, but the code is a mess. You can look at the build-and-run-sys.sh script and go from there.

Various projects related to WASM, each in it's own branch, so that I don't have 50 repositories. - GitHub - kajacx/wasm-playground at wit-bindgen-wasi
Various projects related to WASM, each in it's own branch, so that I don't have 50 repositories. - kajacx/wasm-playground

Last updated: Dec 23 2024 at 13:07 UTC