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

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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

We're in Beta

About us Mission Statement