echojoys

echojoys

Return string form rust in Wasm

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.

Thanks! :smiley:

Most Liked

tessi

tessi

Hey @echojoys ,

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:

{:ok, [pointer, length]} = Wasmex.call_function(instance, :to_string, [54321])
{:ok, memory} = Wasmex.memory(instance)
assert Wasmex.Memory.read_string(store, memory, pointer, length) == "54321"

(the code snippets are an extract from Wasmex tests, have a look at those wasmex_test.exs tests to see more examples)

I hope this helps! :slight_smile:

cheers,
tessi

echojoys

echojoys

Hey Tessi,

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.

Best regards

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement