I’m currently learning the interaction between wasm and elixir. I am writing wasm in rust language, but it can only return the starting position of the address of the string, which is *const u8. I can’t get the length. What is the solution? Thanks again.
Hello, I don’t know what should be returned, sorry to ask you again, wasm seems to only return i32/i64 and f32/f64. I still don’t know how to combine the address and length of the memory together
Thanks, I’ve found a way to store the address and length using the high and low bits in binary. Then use bit operation to extract it, also based on your blog, thank you very much
We know wasm is unable to return the string directly, of course we can use some libraries, but with elixir wasmex is unable to get directly, we usually pass in the address and length of the string through the memory function to get the string. Currently some languages already support multi-value return, but rust is currently unable to do multi-value return, so I used binary bit manipulation to solve this problem this is my github repository, I hope to get everyone’s advice
wasm only seems to support i32/i64 and f32/f64 at the moment, which is why I can’t return the strings directly. I probably didn’t answer very well, thanks for your comments. If it’s not the answer you want, I hope you can ask again, thank you very much
I want to use wasm written by rust to handle some data, the data is not necessarily integer and floating point but there is no string type supported in the documentation of wasm, if it is a boolean value I can use 0/1 instead, so I can’t return the string properly. wasmex is the hex library used by elixir to call wasm.
We can’t know the length like “hello world”, so we need to return both the start address and the length of the memory. That’s why I mentioned above that rust can’t return multiple values, so I had to figure out how to get his length and address when calling elixir. My idea was to combine memory and length into one integer return, and then split it up into length and memory address when elixir is called。
Thank you very much, I don’t know if this answer is the answer you want. Because my English is very poor, I may not be able to understand you correctly. I’m sorry, if you have questions you can keep asking, I want to learn more.
Hey @echojoys, thanks for looking into wasmex and sorry for being notoriously late in replying
I wanted to implement multi-value returns in wasmex exactly for the use case you have (save string returns). Back then, I couldn’t because the APIs in underlying libraries/tooling just weren’t ready. But this changed in the meantime. Have a look at: wasmtime/multi.rs at main · bytecodealliance/wasmtime · GitHub Here, a tuple with two (and more) values is returned from wasm. The first part in that tuple could be the string length, the second could be the memory pointer.
I’d be happy to eventually implement support for this in wasmex - feel free to open an issue or give implementing it a try yourself if you feel like you want to contribute!
I want to follow up on the topic of “safe” string returns through multi-value returns from WASM. I implemented this feature here and expect support for multi-value returns to land very soon in wasmex.
After the MR landed, you can have Rust code that returns a string pointer and length from the same function call:
#[no_mangle]
pub fn to_string(a: i32) -> (i32, i32) {
let str = a.to_string();
(str.as_ptr() as i32, str.len() as i32)
}
from Elixir you can call that function like any old function:
I cannot express how appreciative I am for your detailed and insightful response. Your solution is ingenious, and it’s exactly what I was looking for.
The code snippets you provided have clarified my understanding significantly. It’s enlightening to see how Rust can return a string pointer and length from the same function call, and how Elixir can subsequently call that function. Moreover, I must commend your patience and effort in helping me resolve this issue.
Once again, thank you so much for your time and assistance. I look forward to learning more from your expertise in the future.