Stream: general

Topic: Using an interface with wasm-tools component embed


view this post on Zulip Mark Fisher (Aug 15 2025 at 23:36):

I have the following in calculator.wat:

(module
  (func (export "add") (param f32 f32) (result f32)
    local.get 0
    local.get 1
    f32.add
  )
)

and the following in calculator.wit:

package example:component;

interface calculator {
    add: func(a: f32, b: f32) -> f32;
}

world calculator-world {
    export calculator;
}

and I get this error when trying to create a wrapping component via wasm-tools commands:

$ wat2wasm calculator.wat --output=- | \
wasm-tools component embed calculator.wit - | \
wasm-tools component new - -o calculator.wasm
error: failed to encode a component from module

Caused by:
    0: failed to decode world from module
    1: module was not valid
    2: failed to find export of interface `example:component/calculator` function `add`

It works fine if either the world exports the function directly (no interface) OR if I export "example:component/calculator#add" in the wat file. Simply adding the interface (export calculator#add) in the wat file does not work either, but I don't want the core module to have awareness of the WIT that will be wrapping it as a component anyways. I was hoping functions would map even through an interface as long as there were no ambiguities in function name. So, I thought I would check here in case this has been discussed. I don't see anything directly related to this in what I've searched so far.

view this post on Zulip Alex Crichton (Aug 15 2025 at 23:38):

I don't want the core module to have awareness of the WIT

More-or-less, this isn't supported. What you're describing is a viable use case and you can build it out manually, but when using standard tools you'll need to match standard naming conventions

view this post on Zulip Alex Crichton (Aug 15 2025 at 23:38):

put another way, the component model supports what you want to do, but the tooling does not

view this post on Zulip Alex Crichton (Aug 15 2025 at 23:41):

in theory this wouldn't be too hard to support though, you'd want a mapping specified to wasm-tools component new saying "hey when you look for example:component/calculator#add use add instead

view this post on Zulip Mark Fisher (Aug 15 2025 at 23:44):

Thanks for the quick reply! Sounds like I should create a pre-processing tool that also consults the WIT and simply updates the exports in the core module and call that before embed. Ah, or (reading your follow-up as I type), it would be nicer on the inbound side as mapping rules for expanding the simple function export.

view this post on Zulip Alex Crichton (Aug 15 2025 at 23:52):

I think this should be relatively straightforward to implement in wasm-tools component new (the wit-component crate in the wasm-tools repo), but doing a preprocessing step would also work

view this post on Zulip Mark Fisher (Aug 15 2025 at 23:56):

I will create a preprocessing tool, but will also explore what's involved to implement it in wasm-tools, possibly leading to a PR if you do think it would be a welcome addition.


Last updated: Dec 06 2025 at 05:03 UTC