DEV Community

Adam Crockett ๐ŸŒ€
Adam Crockett ๐ŸŒ€

Posted on • Edited on

Emscripten Node.js tips

Intended to collect a bunch of helpful things for the leser mentioned WASM in node.

Story time: I once spoke to a developer who was really into WASM but when I asked about node he laughed, "it's for the browser", a universal compile target doesn't mean the target is platform specific. Its just JavaScript ... And precomputed byte code now apparently.

1. Prune browser stuff.

If you only intend to compile to a node target. add -s ENVIRONMENT='node' to your compiler args.

This will reduce the main.js output, pruning most - not all browser related bits. - unfortunately emscripten_runscript("console.log('doh!')"); will still look for a window global, doh! thisGlobal, hurry up and be a thing.

For a list of flags see https://github.com/emscripten-core/emscripten/blob/incoming/src/settings.js

1.a Make a JavaScript object

Okay you got me, this isn't node specific but it's worth mentioning because the documentation is lacking on the subject. Emscripten provides a header emscripten/val.h , val is a bit special, not only is it a type val X = "some js value", it's also a handy way to create JavaScript object literals and access the ... ๐Ÿคจ global scope. Despite being advertised as a way to get values out of JavaScript, it can also create values and put them into JavaScript, val X = val::Object(); creates a real JavaScript object!
X.set("key","value"); then you could return this to JavaScript. Assuming you are using embind which I highly recommend.

Top comments (0)