Stream: wit-bindgen

Topic: rust-wasm import/export code gen abnormality


view this post on Zulip baluchicken (Jun 24 2022 at 14:59):

Hi, can somebody help me with this one I am clueless about this behaviour. I am using wit-bindgen rev="2b86ad0747a4b32eec98e17979dd2de773716a2d". I have a simple foo.wit file which contains the following:

enum error {
    runtime-error,
    not-found-error,
}
foo: func(a: string) ->expected<unit, error>

I have created two rust projects one imports this wit using wit_bindgen_rust::import!("foo.wit") the other one exports it. I have a third rust project which tries to link these wasm modules and instantiate it using the wastime crate= 0.38. Here comes the fun part, it fails with

function types incompatible: expected func of type (i32, i32, i32) -> () found func of type (i32, i32) -> i32

I debugged this further by generating the rust code for import and for export and it turns out that the function signature for the same function is indeed different. What am I missing here?
Another fun part is: If I try to link these wasm modules using the wasmlink cli tool then run it with cli wasmtime it magically starts to work.

view this post on Zulip Alex Crichton (Jun 24 2022 at 15:12):

The raw wasm modules are not intended to be hooked up directly since the wit interface represents a "shared nothing" boundary where memory has to be copied between the modules. An adapter of some form (which wasmlink generates) must be used to bridge the gap here rather than hooking up the modules directly.

view this post on Zulip baluchicken (Jun 24 2022 at 19:48):

Thanks for the quick reply I understand that, but I thought that bridge code was generated by the wasmtime lib when instantiating a module with the Linker. So the copy to one modules memory to the other memory should be handled. The only difference between this example https://docs.wasmtime.dev/examples-rust-linking.html and what I want to do is I used a wit file as a common interface and generated one wasm file which imports those function and an another one which exports them. But in my case the wit-bindgen generates different function syntax for import and for export, so the linking fails. I think the only reason why the wasmlink can figure it out is the fact that it also requires the wit file as a parameter.

view this post on Zulip Alex Crichton (Jun 24 2022 at 20:00):

Yes the difference in export/import are expected, they can't be hooked up directly. Some adapter needs to be in the middle that does validation and copying, and that is what wasmlink does and what the component model will signify in the future

view this post on Zulip baluchicken (Jul 04 2022 at 08:02):

Thanks for the reply (sorry I was on vacation for the last week.)


Last updated: Oct 23 2024 at 20:03 UTC