Events / Technology / Top Stories

Are You Ready for the WebAssembly Revolution?

7 Nov 2017 12:32pm, by

Imagine a day when you can write a web application in any (statically-typed) language of your choosing. No more fiddling with JavaScript, or even HTML or CSS for that matter. And the apps you build will run faster as well.

This is the dream of the developers behind WebAssembly (WASM), a new-ish project to package web applications as low-level bytecode to be run by web browsers. We learned about WASM through Jay Phelps, Netflix senior software engineer, who gave a talk about WASM at the All Things Open conference last month in Raleigh North Carolina.

Today, JavaScript is the dominant language being used for building web applications. All the browsers support it; it is used to learn. But there is no guarantee it will always be the default language.

“I think we will eventually be using a language that doesn’t even exist today,” Phelps said. Instead, what might evolve would be a language designed to form the ground-up specifically to target the web applications.

With WASM, developers can write their programs in C, C++ or Rust — and other statically typed languages will be supported in the future — which is then compiled into low-level bytecode, which the browser can then execute within a virtual machine.

By rendering the application logic into bytecode, it should be faster to load and execute, at least compared to JavaScript, which still takes browsers quite a bit of time to execute. “It was designed from the ground up to be fast,” Phelps said of WASM. Plus, because it runs in, in effect, within a sandbox, it is safer too in that malicious code won’t touch the host (though some have expressed concern in this approach about the source code not being accessible for viewing by end users, as has traditionally been the case with the Web).

There have been attempts to bring binary code to the Web before, at least since Java Applets of two decades ago, which were clunky and slow to load, largely because they operated entirely outside the browser environs. More recently, Google attempted work of this sort as Native Client, which was discontinued in favor of WASM.

Somewhat similar is ASM.js, which offers a way to compile an optimized subset of JavaScript. JavaScript is not a really suitable language for compilation though, Phelps warned, in that it is dynamically typed, offering too much variance in the code for easy digestion by the compiler.

Unlike earlier attempts, however, WASM appears to be actually a momentum of sorts. “This is the first time all the browser makers, even Edge, is on board and working on the specification,” Phelps said. Part of the appeal for the browser makers is that they didn’t have to generate a new virtual machine from scratch to run the bytecode; they could use their JavaScript virtual machines instead.

The best use for WebAssembly now would be for those web applications that demand much of the CPU. Smaller programs would lag from the time it takes to call them from the browser. “You want to cross that boundary only when you have some heavy computational thing,” Phelps said. Video encoding or games are prime potential uses. Game engine provider Unity assembled a demo game involving the driving of tanks around a virtual sandbox.

The Ethereum blockchain platform is even investigating the possibility of running WebAssembly on the servers (The work raises the interesting possibility of WebAssembly not only being used in browsers but even as a stand-alone cross-platform development environment, Phelps pointed out).

C++ Binary Text
int factorial(int n) {
  if (n == 0)
    return 1;
    return n * factorial(n-1);
20 00
42 00
04 7e
42 01
20 00
20 00
42 01
10 00
get_local 0
i64.const 0
if i64
    i64.const 1
    get_local 0
    get_local 0
    i64.const 1
    call 0
A C++ Function, Represented as Source Code, Binary, and Bytecode (Source: WebAssembly GitHub)

To get started, download the WebAssembly Explorer, which provides a way to type in C or C++ and get in return the code in WebAssembly. The most common way to write WebAssembly is through Emscripten, which generates JavaScript through C and C++ code compiled through the Low-Level Virtual Machine (LLVM). A stand-alone JavaScript file will mount WebAssembly, or, if a full page app, it can be mounted directly to HTML.

The WebPack JavaScript dependency bundler has added first class support for WebAssembly, thanks from a grant from the Mozilla Foundation.This will allow developers to have their C++ or Rust code automatically compiled into a JavaScript project.

There’s still some work to do on WebAssembly, which was launched in 2015 by Mozilla, Microsoft and Google. The minimum viable product (MVP) was released earlier this year. But there is still no direct access to any of the web APIs for functions such as system calls, short of interfacing through JavaScript. Eventually, the plan will be to have WebAssembly speak directly to the browser. Some form of garbage collection will be needed to reclaim no-longer-used memory. There is also work on multi-threading capability, though now this is supported in a way through Web Workers.

A digest of the week’s most important stories & analyses.

View / Add Comments