Wasm is not a programming language but a compiled code format: programs in C, C++, Rust, Go and other languages are built into a binary module that is run by a browser or a standalone runtime. The stack-based virtual machine makes the module portable: the same binary runs on different processors and platforms, with no tie to an architecture.
How a module is structured
A module begins with the magic number \0asm and a format version number, followed by sections: types, functions, memory, tables, globals. Besides the binary format, the standard defines a text format with the .wat extension, which is handy for reading and debugging; function names can be kept in custom sections of the binary, but this makes the module larger.
(module (func $add (param i32 i32) (result i32) local.get 0 local.get 1 i32.add) (export "add" (func $add)))Once compiled, this module takes up 41 bytes and exports the function add: JavaScript passes it two numbers and gets back the sum.
Security and environments
- A module is validated on load: argument and result types, the correctness of branches, and access only to its own memory.
- Memory is a linear array that grows in pages of 64 KiB; going out of bounds is an error, not a read of someone else’s data.
- In the browser, the same-origin policy applies, as it does for JavaScript.
- Outside the browser, access to files, the network and environment variables is possible only through capabilities that the host grants explicitly; the WASI interface is being developed for this.
| Environment | How it runs |
|---|---|
| Browser | through the JavaScript API; for streaming compilation with instantiateStreaming, the server must serve the application/wasm MIME type |
| Node.js | as an ES module or through the global WebAssembly object |
| Server | in a standalone runtime: Wasmtime, Wasmer, WasmEdge |
Where the term comes from and where it is used
The format is developed by the W3C community; on 5 December 2019, the WebAssembly Core specification became a W3C Recommendation, making it the fourth language for the browser after HTML, CSS and JavaScript. The name points to a kinship with assembly language: it is a low-level representation of a program, but a portable one, not tied to a particular processor.
- Games and graphics editors in the browser: engines, CAD systems, maps.
- Ready-made libraries built to Wasm: SQLite, PHP and Python interpreters.
- Server-side plugins and extensions: code in any language in an isolated runtime.
- Lightweight containers in Kubernetes: Wasm modules are packaged as OCI images and run without OS virtualisation.
Sources
the term is written from these pagesVerified 9 Nov