DEV Community

Adam Crockett πŸŒ€
Adam Crockett πŸŒ€

Posted on β€’ Edited on

2

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more