wasmtime-java
wasmtime-java copied to clipboard
Load/run WASM created with wit-bindgen
Hello!
I am interested by loading in Java a WebAssembly created using the crate wit_bindgen
. Actually I was looking for the Java bindings for this but I could not find any related project. As you probably already know, having this would allow to have a common WASM loaded by different languages (Python, Java, C, etc.).
I run your project wasmtime-java
(and thanks for this) with a wit-bindgen generated WASM but I encounter 2 problems:
- this first problem deals with the
de/allocation
functions: forwasmtime-java
, the WASM file needs to have the 2 exported functions__alloc_bytes
and__dealloc_bytes
). Even this is not ideal, I temporarly added these functions (of course it would be better to use the existing wit-bindgen allocation functions) - the second problem occurs when function-signature (in WASM) looks like
fn my_function(input: Vec<u8>) -> Vec<u8>
. I cannot find a way to load the return value. I confirm that I can "see" the expected result in the WASM memory but I dont understand how to find the correct offset of this result in memory (and the related result size in bytes). The offset returned by the function call (which is correct) does not match the good offset of the result.
Could I ask your opinion about this? Thanks!!
Hi, thanks for the note. I'm excited that you're looking at the project. So far this is definitely not production ready. In terms of support for wit_bindgen
, I think that would be great. I've been a little stuck on trying to figure out generic language support.
I'm definitely aware of some of the limitations of the current structure of this project. I think the __alloc_bytes
and __dealloc_bytes
are artifacts of how data is passed back and forth with wasm from the Java environment.
As to your issue with Vec
parameters and returns, currently the project only supports limited C style arrays to be passed around. This example shows what is currently supported: https://github.com/bluejekyll/wasmtime-java/blob/main/tests/slices/src/lib.rs#L39-L43
Since I haven't played with wit_bindgen
yet, I think that would be a great first step in trying to normalize some of these conventions? Is this something you might want to try and help contribute to?
Hi, thanks for your quick answer!
Well, I want to help so I created a branch feature/add_ref_return_value
here where there is a way to build a wit-bindgen
wasm module (nightly rustc only). Feel free to take it or not!
For the time being, I do not know how to load the return value of my function (which is a byte vector whose value can be found in WASM-memory). Maybe we can contact us through the bytecodealliance zulip chat.
I’m not super active on the bytcodealliance Zulip, but I am on there. That would be a fine place for us to discuss things.