-
-
Notifications
You must be signed in to change notification settings - Fork 671
Mutating the same TypedArray on either side of the JS or WASM fence #850
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
returns a typed array view on the Wasm memory that can be mutated on both sides as long as the respective managed object is alive and has not been garbage collected (i.e. simply make sure it remains referenced somewhere on the Wasm side), while
does copy from Wasm memory to a new JS array. The reason for having both versions is that a normal Using |
Got it, thank you. Just to post my findings for anyone else that stumbles on this. It's really simple: AS:
JS: |
For completeness, instead of storing a reference, one can also trick the GC: export function createFloat32Array(length: i32): Float32Array {
let array = new Float32Array(length);
__retain(changetype<usize>(array));
return array;
} so the object has always one excess reference recorded. On the JS side one could then |
Similarly, arrays can also be allocated on the JS side from an existing array of values: // wasm
export const Float32Array_ID = idof<Float32Array>(); // js
let array = module.__retain(module.__allocArray(module.Float32Array_ID, [1, 2, 3, 4]));
// do something with array
...
// once not needed anymore:
module.__release(array); |
I may have spoken too soon. Here's where I'm at:
JS Side:
Is what I'm trying to do possible? |
This is calling |
I tried that as well, but same result
If I move |
Strange, this should work. Maybe a bug, need to investigate. |
Fix in my PR above :) |
Fantastic, that worked. Thanks!! |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Thanks for all the work on AssemblyScript -- the tooling is fantastic.
I'm aware there are other issues that ask a similar question, but unfortunately I couldn't find a specific answer, so I'm hoping for some clarification.
My search started in this thread but what I'm finding hard to follow is that OP seems to be trying to take two steps at once, both sharing memory and packing different data sets into it.
I did notice this comment which appears to be exactly what I want to do. An array that can be mutated and read from on either side WASM/JS.
I followed some links to this repo which sort of worked after I used what appeared to be the new API in
assemblyscript/lib/loader
with__allocArray
and__get(X)Array
instead. The issue here though is that__get
appears to be creating a new typed array, or atleast view of it. I also have to call__get
in JS every time the data has been mutated in AS, which isn't possible in my use case.The most promising avenue I found was this pull request but I wanted to ask this before trying to piece that together, because it seems like the API might have changed in the last year since those questions were asked.
My use case is I have a 3D engine and I want to do the expensive work in WASM. For example, A
Vector3
class has aFloat32Array
with 3 floats. I would like to mutate it in JS, but then allow AS to have a reference to and be able to use those 3 floats in computation.Could someone guide me to the best solution?
The text was updated successfully, but these errors were encountered: