Binary format for the web

Queues, Modules, Layers

The project page describes WebAssembly as a "new portable, size- and load-time-efficient format suitable for compilation to the web." The features arise from the following design decisions.

WebAssembly generates code as a stack machine that uses a LIFO queue to process individual instructions. The instructions in WebAssembly, which regard the program flow as a sequence of blocks, use this value stack. The blocks comprise, for example, if conditions and for loops. This formal definition lets you deal efficiently with the resulting code as a binary representation, whereas asm.js uses a JavaScript subset as the output format.

The binary format lets you store and process the compiled code faster compared with interpreted source code. Compared with pure JavaScript code, the code presents more opportunities for optimization.

The JavaScript engine manages the storage area that is available to a WebAssembly instance (module). Each module runs in its own sandbox and thus has no way to manipulate inaccessible address ranges. Although this is the only option MVP provides, in the future, the developers hope to offer more memory management and usage options. Multiple threads will thus share a storage area.

The binary format defined by WebAssembly allows for encoding in three layers. Layer 0 encodes the bytecode instructions and associated data structures in a simple binary form. This coding is suitable for simple interactions and thus for just-in-time (JIT) scenarios, instrumentation tools, and debugging.

Layer 1 builds on Layer 0 and uses specific knowledge about the nature of the type of syntax tree and its nodes to establish structural compression. The latter allows for more efficient encoding of values, rearranging values in the module, and reducing structurally similar tree nodes. Finally, Layer 2 uses known compression algorithms, such as Gzip and Brotli, which are already available in browsers. In short: Layers 1 and 2 offer smaller file sizes, quicker load times, and the efficient use of existing technologies.

In addition to the binary format, WebAssembly defines a text format that is suitable for testing and debugging and allows developers to program directly on request. This makes it possible, among other things, to show the source code of a WebAssembly module running in the browser. This feature is already omnipresent in browsers and can be used, for example, in the JavaScript console.

The WebAssembly text format (WAT) [26] uses s-expressions syntax, which mainly consist of lists and brackets. This format is used by programming languages such as Lisp, among others.

WebAssembly Explorer

Mozilla is currently developing WebAssembly Explorer [27], which allows you to write C and C++ code in the browser and convert it directly to WebAssembly (Figure 5). This allows developers to see and analyze their own code in text format, while downloading the resulting WebAssembly code in a text or binary format.

Figure 5: The WebAssembly Explorer lets users convert pure C and C++ code directly to the WASM format.

Highly Efficient

As previously mentioned, WebAssembly is intended not only to run inside, but also outside, browser environments and is currently accomplished using Node.js. However, in the future, the idea is for it to work as a standalone module independent of the JavaScript engine. As the browser module, WebAssembly is part of the Open Web Platform [28]; therefore, all features that are already part of the JavaScript Web API are available. WebAssembly is by no means a replacement for JavaScript in browser environments but will complement it and offers the opportunity to port other features and applications to the browser.

According to the design, the binary format ensures the most efficient execution possible, regardless of operating system and architecture. It relies on various features of the underlying hardware and operating system, but even if the JavaScript engine does not offer these features, it can still run the WebAssembly code. In this case, it must emulate the desired behavior, which can affect execution speed.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News