WebAssembly

What is WebAssembly?

WebAssembly is a binary file format specifically designed to be loaded and executed in a web browser. Yes, it is possible to run binary files in all four major web browsers. Moreover, it is highly likely that you can compile programs written in your favorite programming language into this binary format, allowing them to be loaded and executed in a standard browser.

In general, WebAssembly is not a programming language. Instead, it is designed as a compilation target for traditional programming languages such as C++, Java, and Python. This enables "ordinary" programs to run in regular web browsers and allows developers to create web software using familiar tools. WebAssembly is also optimized to execute programs at near-native speed, making it suitable for computationally intensive tasks such as rendering 3D graphics or processing audio and video files directly in a browser.

Like other internet standards, WebAssembly is open. Therefore, it is not a method for hiding proprietary source code, as WebAssembly binaries can be easily decompiled into high-level source code, such as C.

Currently, WebAssembly modules are loaded into browsers using JavaScript. However, in the future, browsers are expected to support direct loading of WebAssembly modules, similar to how JavaScript modules are loaded today.

WebAssembly is now supported by all major browsers: Chrome, Firefox, Safari, and Edge.

WebAssembly is developed by a consortium of organizations, including the WebAssembly Forum, the W3C WebAssembly Working Group, the Bytecode Alliance, and major companies such as Google, Intel, Microsoft, Amazon, and Mozilla.

How does it work?

The compilation of high-level source code results in a set of low-level instructions for a virtual stack machine running in the browser. WebAssembly is a bytecode format, meaning that each byte (except data bytes) corresponds to a specific instruction. For example, 0x7F (binary 0111 1111) represents the declaration of a 32-bit integer data type, 0x6A (binary 0110 1010) denotes an addition operation, while 0x0B (binary 0000 1011) represents a conditional statement (if).

WebAssembly's binary format (.wasm) has a human-readable counterpart known as the WebAssembly Text Format (.wat). This format allows developers to manually write WebAssembly code. Additionally, since binary files can be easily converted to text files and vice versa, they can be used for debugging compiled code from high-level languages. In fact, Chrome Developer Tools already support debugging WebAssembly code.

The smallest structural unit in WebAssembly that can be loaded into a web browser and executed is a module. Each WebAssembly file represents a single module. Conceptually, a WebAssembly module is a tree structure with interconnected nodes, where one node acts as the parent, and the remaining nodes are its children. These child nodes can belong to various predefined types.

In the WebAssembly Text Format, modules are represented using S-expressions, which provide a simple way to describe tree structures, including nodes, their dependencies, and values. In this format, each node is enclosed in parentheses. Inside the parentheses, the node's type is specified using a text label, followed by a space-separated list of properties, which may themselves contain nested nodes.

..