Unsupported non-pure initializer in .\index.js on line 1
Opened this issue · 1 comments
I'm trying to use a .JS file in unity and it contains websockets implementation. and the way to get websockets is:
var WebSocket = require('ws');
But this line causes the Liberary build to fail and provides following errors:
Unsupported non-pure initializer in .\index.js on line 1
Initialize variables in a function and invoke it from inside main()
I'm new to JS and I dont know how to invoke it inside main(). Can you please help me.
To invoke something from main()
, you'd put the call to it in the body of the function called main
:
extern "C" {
void runSomeJS();
}
int main(int argc, char *argv[]) {
runSomeJS();
return 0;
}
Then you can Initialize variables in a function:
var WebSocket;
function runSomeJS() {
WebSocket = require('ws');
}
The code above is basically the same thing as what's already demonstrated in the README, where the JavaScript function Foo_new
can be called from C++ and manipulates the JavaScript global called handles
.
Note that I wrote this library quite a while ago and haven't used emscripten in a long time. It's possible that everything still works, but it's also possible that emscripten has changed something and this library no longer works. Just something to keep in mind. The code I posted above should work with how emscripten worked when I wrote it but I haven't tested it with the latest version of emscripten.