Web Assembly, struct to JavaScript Object and back

Sebastiaan Koppe mail at skoppe.eu
Wed Jun 24 10:53:19 UTC 2020


On Tuesday, 23 June 2020 at 18:15:25 UTC, tirithen wrote:
> Anyone that has something similar working with struct objects?

Passing anything besides int/double/bool between JS and wasm is 
hard work.

Currently the ABI generated by LDC is so that arguments besides 
int/double/bool (and array/delegates are special too) are passed 
by pointer.

So if you want to call a wasm function that expects a struct, you 
instead pass a pointer to wasm memory where you have encoded that 
struct.

Similarly if you want to call a js function with rich objects you 
have to do some handling on the JS side.

Return values other than int/double/bool from wasm functions are 
special and it is expected that the function is called with a 
pointer as first argument.

There are a few gotcha but this basically describes it.

In https://github.com/skoppe/spasm I am using this approach as 
well. Except I don't actually move D struct to and from JS, 
instead only references to JS objects, strings, delegates, unions 
and arrays. Same principle applies though.


More information about the Digitalmars-d-learn mailing list