Stream: general

Topic: How to manually implement wasi:cli/command


view this post on Zulip Akhil Reddimalla (Dec 11 2024 at 01:21):

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.

view this post on Zulip Victor Adossi (Dec 11 2024 at 01:53):

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:

The WebAssembly Binary Toolkit. Contribute to WebAssembly/wabt development by creating an account on GitHub.
CLI and Rust libraries for low-level manipulation of WebAssembly modules - GitHub - bytecodealliance/wasm-tools: CLI and Rust libraries for low-level manipulation of WebAssembly modules

view this post on Zulip Victor Adossi (Dec 11 2024 at 01:54):

I'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!

view this post on Zulip Akhil Reddimalla (Dec 11 2024 at 02:12):

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.

Command-Line Interface (CLI) World for WASI. Contribute to WebAssembly/wasi-cli development by creating an account on GitHub.

view this post on Zulip Victor Adossi (Dec 11 2024 at 02:22):

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.

Contribute to bytecodealliance/wasm-pkg-tools development by creating an account on GitHub.
Documentation around creating and using WebAssembly Components - bytecodealliance/component-docs

view this post on Zulip Dan Gohman (Dec 11 2024 at 13:57):

Victor Adossi said:

As a brief aside, wasm-tools can do this too; it's wasm-tools parse.

view this post on Zulip Victor Adossi (Dec 11 2024 at 17:35):

Oh yes thank you for the correction!

While I'm here, this talk by Alex from the recent Wasmcon was great:

YouTube - - YouTube


Last updated: Dec 23 2024 at 13:07 UTC