You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling C code from JavaScript involving Structs #3083
Now my question:
I am in the process of transpiling a C library to JS which should then be used as a node.js module. While I managed to export functions accepting or returning standard types such as floats or doubles, the documentation is not clear on how to properly convert functions accepting or returning custom C structs (the Embind and WebIDL facilities seem to be tailored only at C++ code).
Ideally, I would like to end up with a constructor function than can create 3d vectors and a length function to calculate their length. This seems to be a quite common task when porting C code, but browsing the Internet I couldn't find a proper explanation of how to proceed? I would greatly appreciate if someone could tell me whether and if so how one can convert such C code to JavaScript. I found a discussion on the Google group (Link) suggesting that it is possible, but was not able to reproduce the necessary steps, so a worked example would be of much help.
That works just as expected. When trying WebIDL, I was not able to export a function which is not a method belonging to a class. It seems that you always have to define an interface, at least in the examples I came across. Is that correct?
That works just as expected. When trying WebIDL, I was not able to export
a function which is not a method belonging to a class. It seems that you
always have to define an interface (which will be converted to a JS
constructor function), at least in the examples I came across.
—
Reply to this email directly or view it on GitHub #3083 (comment).
Activity
kripken commentedon Dec 16, 2014
In WebIDL, you can use
[Value]to allow passing and returning structs by value,http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/WebIDL-Binder.html#pointers-references-value-types-ref-and-value
For efficiency, though, it is much better for your APIs to hand off pointers to objects.
Planeshifter commentedon Dec 17, 2014
Thank you for your quick response. By now, I managed to convert above example using Embind as follows:
That works just as expected. When trying WebIDL, I was not able to export a function which is not a method belonging to a class. It seems that you always have to define an interface, at least in the examples I came across. Is that correct?
kripken commentedon Dec 17, 2014
Yes, WebIDL currently exports only classes and structs, I don't think
anyone has added global functions to it.
On Tue, Dec 16, 2014 at 11:03 PM, Philipp Burckhardt <
notifications@github.com> wrote:
Planeshifter commentedon Dec 20, 2014
Thanks for the clarification!
Best,
Philipp