On How WebAssembly Can Change Software (Outside of the Web)
Before I delved into WebAssembly, I had a vague understanding of it as some web-related technology meant to modernise JavaScript. And to say I was missing the point is a massive understatement.
I kept stumbling upon references to WebAssembly while researching efficient methods for preserving software so that it doesn't break with every system update.
And as it turns out, WebAssembly is perhaps the best shot we've ever had at decoupling compiled binaries from the underlying operating system and system calls.
Now, if you're about to go - wait, wasn't that supposed to be Java? - let me explain.
- WebAssembly is, at its core, a minimal specification of a language-agnostic virtual machine. That's it. It doesn't have any UI, system calls (not even
printf
), and it doesn't try to take over your entire host machine. It's simply a set of stack-based instructions to which you can compile programs. - There are many professional-grade compilers to WebAssembly for a variety of mainstream languages: Rust, Swift, C/C++, Go, etc. Clang itself can readily compile its intermediate format to Wasm (short for - yes, you guessed it).
- Outside of the web, there are many different open-source runtimes for Wasm's bytecode running on a plethora of host machines, from desktop OSes to bare-metal embedded devices.
- WebAssembly is supported by every major browser.
- Members of the WebAssembly Working Group include people from Google, Microsoft, Apple, Mozilla, and more. It is a truly cross-company effort.
- Wasm is not just a big promise of something that may ship someday. It's already here, ready for production.
Why does all of this matter? Because writing modern software is hard. There are countless platforms to target, each with its own APIs, constraints, programming languages, security implications, and so on.
Even if you only want to target the three mainstream desktop OSes, each of them is an area of expertise that could take an entire career to master. And they all keep changing (some faster than others).
Now, what if we could flip it the other way around? What if you could, say, pick your favourite programming language, compile a binary, and deploy it to all these different platforms at once? Eventually, a few experts in each host platform would maintain the runtime, and the rest could benefit from the freedom of a truly portable binary.
Disney+ and Prime Video both built exactly this sort of Wasm-based infrastructure to deploy software to set-top boxes, smart TVs, and a multitude of other devices. Perhaps more will follow.
But what about performance? Isn't it interpreted code after all? Yes, and as such it may not suit every project, but WebAssembly's specification stands apart in the interpreted world.
Having a standardised, clean, stable, and minimal specification of the instruction set means compiler developers can concentrate on a range of optimisations. For example, some Wasm runtimes support ahead-of-time compilation to native assembly on desktop host platforms.
Perhaps the biggest barrier to wider adoption is the lack of a standardised set of system calls to complement the core instruction set.
Going in this direction, WASI aims to provide a "standard library" for WebAssembly akin to what C and other languages have. It still has a way to go, but many things are already working (WASI Preview 2 includes primitives for I/O, networking, random number generation and time measurement).
Even for use cases not yet covered by WASI, maintaining a focused set of low-level calls across different platforms requires far less effort than maintaining and recompiling all the programs built on top. And as WASI matures, there's less and less we need to build ourselves.
All that is to say, if you're anything like me and dismissed WebAssembly as some web-specific thing, it may be time to reconsider.
To dive deeper, I recommend the book WebAssembly The Definitive Guide by Brian Sletten. It covers pretty much everything there is to know in small, easy-to-digest chapters.
While generative AI is the tech revolution everyone's talking about, WebAssembly is quietly securing itself a decent shot at the write once, run anywhere dream.