问题
Given the following code:
typedef struct {
int foo;
} Bar;
Bar test() { Bar result = { .foo = 2 }; return result; }
and assuming that test()
can be accessed from JavaScript, how would I convert the struct to a JavaScript object or otherwise be able to use the return value such that (in JavaScript) myJavascriptObject.foo == 2
will return true?
回答1:
One workaround is doing things as in java:
Bar createBar(int foo) {
Bar result = { .foo = foo };
return result;
}
int getFoo(Bar in) { return in.foo; }
Ugly, adds boilerplate, and requires more stuff be exported, but it works.
来源:https://stackoverflow.com/questions/25346367/accessing-struct-fields-from-emscripten