The following is a partnership between Packt Publishing and me. The idea is for me to be a channel of valuable information made by independent publishers.
This disclaimer informs readers that the views, thoughts, and opinions expressed in the text belong solely to the author.
The Road to WebAssembly
Web development has had an interesting history, to say the least. Several (failed) attempts have been made to expand the platform to support different languages. Clunky solutions such as plugins failed to stand the test of time, and limiting a user to a single browser is a recipe for disaster.
Figure 1: Package count growth on npm since 2012, taken from Modulecounts
But the language does have some shortcomings:
As the web evolved into a valid platform for building and distributing applications, more and more complex and resource-intensive applications were created. In order to meet the demands of these applications, browser vendors began working on new technologies to integrate into their software without disrupting the normal course of web development. Google and Mozilla, creators of Chrome and Firefox, respectively, took two different paths to achieve this goal, culminating in the creation of WebAssembly.
Google and Native Client
Google developed Native Client ( NaCl ) with the intent to safely run native code within a web browser. The executable code would run in a sandbox and offered the performance advantages of native code execution. In the context of software development, a sandbox is an environment that prevents executable code from interacting with other parts of your system. It is intended to prevent the spread of malicious code and place restrictions on what the software can do.
NaCl was tied to a specific architecture, while Portable Native Client ( PNaCl ) was an architecture-independent version of NaCl developed to run on any platform. The technology consisted of two elements:
- Toolchains which could transform C/C++ code to NaCl modules
- Runtime components which were components embedded in the browser that allowed execution of NaCl modules:
Figure 2: The Native Client toolchains and their outputs
NaCl’s architecture-specific executable (nexe) was limited to applications and extensions that were installed from Google’s Chrome Web Store, but PNaCl executables (pexe) can be freely distributed on the web and embedded in web applications.
Figure 3: Pepper’s role in a Native Client application
NaCl offered promising opportunities to overcome the performance limitations of the web, but it had some drawbacks. Although Chrome had built-in support for PNaCl executables and Pepper, other major browser did not. Detractors of the technology took issue with the black-box nature of the applications as well as the potential security risks and complexity.
Mozilla and asm.js
Figure 4: The asm.js AOT compilation workflow
asm.js isn’t just a stepping stone. It forms the basis for WebAssembly’s Minimum Viable Product ( MVP ). The official WebAssembly site explicitly mentions asm.js in the section entitled WebAssembly High-Level Goals.
So why create WebAssembly when you could use asm.js? Aside from the potential performance loss, an asm.js module is a text file that must be transferred over the network before any compilation can take place. A WebAssembly module is in a binary format, which makes it much more efficient to transfer due to its smaller size.
WebAssembly is born
The initial implementation of WebAssembly support in browsers was based on the feature set of asm.js. WebAssembly’s binary format and corresponding .wasm file combined facets of asm.js output with PNaCl’s concept of a distributed executable.
So how will WebAssembly succeed where NaCl failed? According to Dr. Axel Rauschmayer, there are three reasons detailed at http://2ality.com/2015/06/web-assembly.html#what-is-different-this-time:
- Dr. Axel Rauschmayer
If this article piqued your interest about WebAssembly, you can check out Learn WebAssembly by Mike Rourke. Learn WebAssembly is both explanatory and practical, providing the essential theory and concepts for WebAssembly, and is a must-read for C/C++ programmers keen to leverage WebAssembly to build high-performance web applications.