DEV Community

Emscripten's compiled Web Assembly, used minimally

Sam Thorogood on July 03, 2018

(This is a technical counterpart to my post on using Wuffs to decode GIFs in the browser. If you'd just like to see fast GIF decoding, go here! 🖼️➡...
Collapse
 
nektro profile image
Meghan (she/her) • Edited

Wonderful article, though I have a quick question.

I don't believe that we'll ever really be able to directly import Web Assembly via ES2015 modules

Why do you think this? Personally I think should have been worked on immediately after the MVP and think this is a really important piece of functionality missing from WASM.

Collapse
 
samthor profile image
Sam Thorogood • Edited

I don't think it's unsolvable but as I mentioned, not without changes on the JS side. I suppose the key thing I wanted to say is "directly"—the challenge is that the current import statement just isn't expressive enough, because in 99% of cases we need to provide an environment to the .wasm code itself.

And it's challenging because some extension like this makes no sense:

const env = { ... };

import * as fastgif from './fastgif.wasm' with env;

I've invented with to provide the language support here, but regardless: with ES2015 modules, import is evaluated before all code, so env is just undefined on the import line.

Very basic WASM could be supported, because it doesn't need imports—but as soon as you need to provide memory, or any of the calls or variables I described above, all bets are off.

There might also be some solution I've not thought of yet :)

Collapse
 
alexbender profile image
Alex Bender • Edited

Hi Sam!
Really great article, and really long.
Thanks for that!
Now I see that wasm isn't silver bullet.
But anyway looks promising.
Are there already other runtimes?

Collapse
 
samthor profile image
Sam Thorogood

I'm not sure of any other runtimes for compiling C/C++ to WASM, no, sorry. The complaints I have are repeated on the bug tracker (possibly in more places too).

So I suspect there might some community projects because of the issues I'm raising. Let me know if you find any!

(Of course, there's also runtimes/compilers for Go, Rust, etc—I've not researched them enormously. I believe Go is much nicer to work with because its JS boilerplate is just a common bit of code you include, it's not built per-binary, which means it's much, much easier to understand and work with.)