WebAssembly is a compact binary instruction format for a portable, sandboxed virtual machine. Languages such as C, C++ and Rust compile to it, and browsers run it next to JavaScript at near-native speed. Outside the browser, it is used as a safe, portable way to run plugins and services. It is a compilation target more than a language you write by hand.
Key concepts
- Stack machine and modules: a module has typed functions with simple instructions over a value stack.
- Linear memory: one resizable array of bytes that the module addresses; the host decides what else it can reach.
- Sandboxing: a module can only do what its imports allow, with no direct access to the system.
- Imports and exports: functions passed in from the host and exposed back to it.
- Host interface: in a browser, JavaScript glues modules to the page; outside, a system interface standardises file and clock access.
- Text and binary formats: a readable text form for study and a compact binary for delivery.
Vocabulary
- Module: the compiled unit of WebAssembly, validated before it runs.
- Runtime: the engine that executes modules, in a browser or standalone.
- WASI: the system interface for running WebAssembly outside browsers.
- Toolchain: a compiler and linker that target WebAssembly from another language.
What to study, in order
- Read a small module in its text format and run it with a standalone runtime.
- Compile a C or Rust function to WebAssembly and call it from JavaScript.
- Pass strings and structures through linear memory, and see why it is manual.
- Run a module outside the browser and limit what it may access.
- Compare performance with plain JavaScript on a computation-heavy task.
- Read the specification's validation and execution sections.