Stream: wasi

Topic: Create WIT from wasm binary


view this post on Zulip Ari Seyhun (May 16 2023 at 06:42):

Is it possible to create a .wit file from a wasm component file rather than the typical approach of using .wit file to generate the code with wit-bindgen?

For example:

#[derive(Command)] // This derive macro would export to a `record add { amount: i64 }`
pub struct Add {
    amount: i64,
}

#[command]
impl Handler<Add> for Counter {
    type Result = Added;
    fn handle(&self, cmd: Add) -> Self::Result { ... }
}

// This function is generated from the `#[command]` attribute macro above,
// and exports to a `handle_add: func(state: Counter, cmd: Add) -> Added`
pub extern "c" fn handle_add(state: Counter, cmd: Add) -> (Added) {
    <Counter as Handler<Add>>::handle(&state, cmd)
}

And from this code, I can compile it to a wasm component, and extract these exported functions from a host loading the component?

view this post on Zulip Lann Martin (May 16 2023 at 12:34):

Take a look at the wasm-tools component wit subcommand, e.g. wit-tools component wit your-component.wasm

Low level tooling for WebAssembly in Rust. Contribute to bytecodealliance/wasm-tools development by creating an account on GitHub.

view this post on Zulip Lann Martin (May 16 2023 at 12:45):

typo: wit-tools -> wasm-tools

view this post on Zulip Alex Crichton (May 16 2023 at 14:12):

Currently there's no implementation of going from Rust source code annotations directly to a component without WIT. As Lann mentioned you can extract WIT from a component, but given your snippet that doesn't sound like the direction you'd like to go in (instead a more #[wasm_bindgen]-like-approach where you annotate Rust and a component gets generated)


Last updated: Dec 23 2024 at 13:07 UTC