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.
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.