Hanks10100 / wasm-examples

WebAssembly Examples
227 stars 58 forks source link

Can I use the same example with Data structure? #4

Open signgitgit opened 5 years ago

signgitgit commented 5 years ago

Can i use the same example with the structure and pass the variable structure to javascript with one function example:

C Code:

`struct number { int x; int y; } num;

int numberfun () { num.x=100; num.x=400; return num; } `

Hanks10100 commented 5 years ago

Maybe not.

A more practical way is to encode your c++ data structure into binary and decode it in the javascript.

signgitgit commented 5 years ago

Thank for your response.

You know my first problem was how to pass multiple variables in the same function but after some research i saw that is impossible and i wanted to try data structures.

I want to ask you if the encode and decode data structures is the best solution for my first problem or exist another solutions

Hanks10100 commented 5 years ago

In my opinion, encode and decode the data in WebAssembly.Memory is the most efficient way (for now).

However, the Emscripten also provides the Embind and WebIDL Binder technology to bind C++ data structures to js, but it relies on extra IDL or js "glue" file to achieve that. It's convenient but seems expensive. Under the hood, I think it still based on encoding and decoding the WebAssembly.Memory (not confirmed).

signgitgit commented 5 years ago

Thank you for your quick and helpful response and also for the links.