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 ?
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?
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.
Oh, an expert did reply just as I was typing, ignore me, you have much better help now.
Did the "x" get dropped from "wit-bindgen" ?
Ah yeah witx-bindgen
was recently renamed to wit-bindgen
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?
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)
the Rust crate brings in the intrinsics needed
Terrific! thanks a lot, it worked with wit_bindgen_rust
Thanks for your help and your great work @Alex Crichton
Last updated: Nov 22 2024 at 17:03 UTC