4 ms·
The interface between WASM and JS is limited to native types that WASM supports which is just integers [0] (and maybe doubles?). With that limited API you canno
by Game_Ender 9y ago
The interface between WASM and JS is limited to native types that WASM supports which is just integers [0] (and maybe doubles?). With that limited API you cannot pass full structures around, the best you can do is pass what are essentially pointers to memory around [1].
[0] - https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format#Signatures_and_parameters https://developer.mozilla.org/en-US/docs/WebAssembly/Underst...
[1] - https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97 https://becominghuman.ai/passing-and-returning-webassembly-a...
- wofo 9y agoDoubles are supported as well through the `f64` type I think. Interestingly, I am passing booleans in my code as well... and it seems to work.
- landonxjames 9y agoI wonder if its converting them to 0's and 1's and sending that to the javascript?
- wofo 9y agoJust checked what is going on and that is indeed the case. Rust booleans compile now to the i32 type (though there is no guarantee that will stay like that in the future).
- onion2k 9y agoYou can do something very similar in plain JS using a sharedArrayBuffer and a webworker. A sharedArrayBuffer is a typed array in an area of shared memory that the worker and the main script can both access. It works really well if it fits your use case. I wrote a 'javascript snow' thing that uses it - https://github.com/onion2k/snowfall https://github.com/onion2k/snowfall