Stream: general

Topic: wit-bindgen with python


view this post on Zulip Coenen Benjamin (Dec 02 2021 at 13:44):

Hello everyone. I'm currently trying to use wit-bindgen for a project. Let me explain what I did: First I wrote this .wit file

record crypto {
  yo: list <u8>,
}
hello: function(data: crypto) -> list <u8>

then uses the export! macro to generate the right rust code to compile to wasm. Then I want to launch this wasm module into a python file. I generated a python file thanks to the wit-bindgen-cli to run with wasmtime-python. But it's not working it's asking for canonical_abi_realloc function from exports but it doesn't exist in my .wasm file if I convert it to .wat. Do you have an idea of where I'm wrong please ?

view this post on Zulip Alex Crichton (Dec 02 2021 at 15:47):

Are you able to share some code associated with what you're doing? If functions like canonical_abi_realloc aren't exported then it may mean that the wasm file wasn't produced with wit_bindgen_rust perhaps?

view this post on Zulip Scott Waye (Dec 02 2021 at 15:47):

I'm not an expert, but I think witx-bindgen should have generated the canonical_abi_realloc. Maybe it doesn't realise it has to for that signature. What if in addition you add a string->string function, I wonder if that would force it to create it.

view this post on Zulip Scott Waye (Dec 02 2021 at 15:48):

Oh, an expert did reply just as I was typing, ignore me, you have much better help now.

view this post on Zulip Scott Waye (Dec 02 2021 at 15:50):

Did the "x" get dropped from "wit-bindgen" ?

view this post on Zulip Alex Crichton (Dec 02 2021 at 15:52):

Ah yeah witx-bindgen was recently renamed to wit-bindgen

view this post on Zulip Manuthor (Dec 02 2021 at 17:17):

Well, (I work with Benjamin), we generated the rust-wasm bindings using:

wit-bindgen rust-wasm -e ../input.wit

Then we implemented the trait Input of those bindings and build the wasm32-unknown-unknown target (there are only 2 exported functions in our test code).

Next, we used wasmtime-py to load the WASM. That is where we noticed the missing exportscanonical_abi_realloc and canonical_abe_free when using Python.

That's why we tried the wasmlink-cli (I am not totally sure if I use the correct input args) :

cargo run -p wasmlink-cli -- -i input=input.wit -m hello_world=hello_world.wasm -p wasmtime -o hello_world_linked.wasm hello_world.wasm

We tried again with the linked WASM version (with same result). Here is our Python loader code:

config = Config()
config.wasm_module_linking = True
engine = Engine(config)
store = Store(engine)
linker = Linker(store.engine)
store.engine = engine
my_module = Module.from_file(store.engine, "hello_world_linked.wasm")
input = bindings.Input(store, linker, my_module)

What did we miss?

view this post on Zulip Alex Crichton (Dec 02 2021 at 17:18):

oh for the rust code you'll need to link against the witx_bindgen_rust crate in addition to using the generated code (and I'd recommend using the generated code via the proc-macro rather than generating the source yourself)

view this post on Zulip Alex Crichton (Dec 02 2021 at 17:18):

the Rust crate brings in the intrinsics needed

view this post on Zulip Manuthor (Dec 02 2021 at 20:27):

Terrific! thanks a lot, it worked with wit_bindgen_rust

view this post on Zulip Coenen Benjamin (Dec 02 2021 at 20:30):

Thanks for your help and your great work @Alex Crichton


Last updated: Nov 22 2024 at 17:03 UTC