I have a compiler that produces a .wat file as output, and I am assembling that into a .wasm binary file. I want it to be able to run as a command component, but I'm lost on how to do so. I've read that wasm-tools can take a .wit file and .wat file and turn it into a core module and then into a component. The only issue is that I don't know how to supply the .wit files I need to make a command component. I would appreciate any help.
Hey Akhil! So if you're working from a WAT, and I assume that WAT is producing a WebAssembly core module (distinct from WebAssembly component), then you'd probably want to do the following as next steps:
wat2wasm
(from WABT) to convert the WAT to a WebAssembly binarywasm-tools component embed
(from wasm-tools
) to embed a component custom section into your Wasm core module (still a core module at this point)wasm-tools component new
to create a new WebAssembly component from your moduleI'm not sure if you need to adapt (does your WAT make use of preview1 APIs?), but trying those things should at least get you started on the right path!
What I am stuck on is the wasm-tools component embed
step. Maybe if I express this as a series of commands I can express my issue better.
The following is what I want to do:
wasm-tools component embed <???> source.wasm -o app.wasm
wasm-tools component new app.wasm -o app.component.wasm
jco run app.component.wasm
Now, the only issue is I don't know what to put in <???>. From what understand, it needs to be https://github.com/WebAssembly/wasi-cli/blob/main/wit/command.wit , but I have no idea how to supply that to embed.
Ah OK, so if your current problem that you don't have the WIT files available for running wasm-tools component embed
, then the first argument to embed is the path to WIT files to load, that should be embedded into the component!
To fetch standard WIT files that are managed by the BCA, you can use wkg
(from wasm-pkg-tools
):
wkg get wasi:cli@0.2.2
(You can also just.. download and copy the files over! WIT is perfectly usable as regular text files)
This will pull down the CLI world. You may need to pull down other worlds (ex. wasi:random
or others) to completely resolve the dependencies of the wasi:cli
interfaces.
As a first step, I might recommend making a component that fulfills a simpler world first. Rather than trying to target wasi:cli
from the start, it might make sense to target a simpler interface just to get the component building. Maybe one that looks very similar to WASI CLI but is only an interface with a run() -> string
function or something.
For example, consider the calculator interface from the component model book, there is an example host which shows how you might call any custom interface with a custom Rust host.
Also, rather than jco run
I might suggest using wasmtime run
instead.
Victor Adossi said:
wat2wasm
(from WABT) to convert the WAT to a WebAssembly binary
As a brief aside, wasm-tools can do this too; it's wasm-tools parse
.
Oh yes thank you for the correction!
While I'm here, this talk by Alex from the recent Wasmcon was great:
Last updated: Dec 23 2024 at 13:07 UTC