-
-
Notifications
You must be signed in to change notification settings - Fork 670
Passing arrays? #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Indirectly, yes. WASM has only four basic types (i32, i64, f32, f64), and what you are passing to/from JS is the offset (i32) of the array structure in memory. |
So it has performance overhead like passing strings, right? Would it be faster if I use a loop in JavaScript or AssemblyScript to pass the numbers in the array one by one? |
Depends, calling in and out of WASM has some overhead as well. If performance is a concern, I'd suggest to create (and then reuse) a typed array view of the memory region in question on the JS side, and use that to read respectively write the values. |
I've managed to use the loader to share my Float64Array between JS and WASM. Another question is, I couldn't get the correct offset through bitwise operators as here mentioned. So I had to manually put a value in the first of the array from WASM to get the offset from JS. For example, "34" for Int32Array and "17" for Float64Array. But these offsets didn't seem to be regular, so I had to do the test again when creating a new array. Moreover, when I re-declared the same array name with a different length, the offset shifted again. So while writing on the WASM side, I couldn't get the corresponding array from JS anymore. For the normal arrays it got more complex. Is there any simple and safe ways to do this? |
When returning a |
I've found out a safer way now. Here are the steps:
And all is done! Remember to free/reset memory after that. Use 3 typed arrays in my project without any problem :) But I still couldn't figure out how to get the typed array offset from the WASM side. The document link provided looks like the HEX code from the WASM file? I still have no luck to find "17" and "27" these 2 offsets as mentioned above... |
More precisely, what you get back is a pointer to the
(updated the wiki page accordingly) |
So all the pointers must be retrieved from the I32/U32 array view (or create an Int32Array/Uint32Array view on your own), and the code will look like this: WASM side:
JS side:
Thanks for all the answers and patience, now it all works :) |
@togekk even I have similar issue where I need to pass and return a u8[]. When converted I got, even though I have my AS function signature, How can I retrieve the argument from passed array pointer? Similarly how can I retrieve full u8[] (UInt8Array) from returned array pointer. I tried the snippet you shared I am getting module.I32 as 'undefined'. Should I do something to get I32 array view? |
It's not possible to get the pointer of Buffer pointer directly in AssemblyScript? I would like to do something like:
In that case the host can receive the pointer and manipulate the memory directly?
|
let x = new Uint8Array(1000);
export function Pointer() : usize {
return x.dataStart;
} or export function Pointer() : usize {
return changetype<usize>(x);
} It depends on what you actually need. In first case it will be pointer to raw data. In second case it will be pointer to Uint8Array's object |
Hi,
I noticed that arrays can only been manipulated within wasm modules when I'm using the playground. Is it possible to pass arrays (either typed or dynamic ones) to and from javascript at the moment? Thanks!
The text was updated successfully, but these errors were encountered: