WebAssembly Intro
WebAssembly (often shortened to Wasm) is a compact binary instruction format for a virtual machine. You rarely write it by hand: compilers for C, C++, Rust, Go, Zig, AssemblyScript and many other languages produce it, and a WebAssembly engine (built into every modern browser, Node.js, Deno and standalone runtimes such as Wasmtime) turns it into fast native machine code and runs it inside a strict sandbox.
In this course you will learn WebAssembly from the inside: you will write modules in its human-readable text format (WAT), see exactly what each instruction does, and then learn how JavaScript and real toolchains use it. Here is a complete WebAssembly module that adds two numbers:
(module
(func $add (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.add)
(func (export "main") (result i32)
(call $add (i32.const 40) (i32.const 2))))42
Press ▶ Run: the text is compiled to a binary module, the browser's engine validates and compiles it, and the
exported main function is called. Its return value is printed.
Why WebAssembly exists
JavaScript engines are very fast, but JavaScript is dynamically typed: the engine has to guess types, optimise speculatively and sometimes throw away its optimised code. WebAssembly was designed (by engineers from all major browser vendors, first shipped in 2017) to complement JavaScript with:
| Goal | How Wasm achieves it |
|---|---|
| Fast | Static types and a simple instruction set, so it compiles straight to efficient machine code |
| Compact | A dense binary encoding that is quick to download and decode, even while streaming |
| Safe | Runs in a sandbox: it can only touch its own memory and the functions the host gives it |
| Portable | The same .wasm file runs on any CPU and OS that has an engine |
| Language-neutral | Any language with a compiler backend can target it |
| Deterministic (mostly) | Behaviour is precisely specified, so results match across engines |
Note: WebAssembly does not replace JavaScript. It has no direct access to the DOM, the network or files. Everything it does in the outside world goes through functions the host (usually JavaScript) imports into it.
Where WebAssembly runs
- Browsers: Chrome, Firefox, Safari and Edge all ship it. Figma, Google Earth, Photoshop on the web, AutoCAD Web and many games use it for their heavy computation.
- Server-side JavaScript: Node.js, Deno and Bun expose the same
WebAssemblyJavaScript API. - Standalone runtimes: Wasmtime, Wasmer and WasmEdge run modules outside a browser, using WASI for files, clocks and other system access.
- Edge and plugins: CDNs (Cloudflare Workers, Fastly Compute), databases, proxies (Envoy) and editors use Wasm to run untrusted extensions safely.
What a module contains
A WebAssembly program is a module. It can declare:
- functions with typed parameters and results,
- a linear memory: one big, resizable array of bytes,
- tables of function references (for function pointers),
- globals: single typed values,
- imports (things the host must provide) and exports (things the host may use).
That is the whole model. There are no objects, no strings and no garbage collector in the core language (newer proposals add GC types, but the classic model is "numbers and bytes"). Strings, arrays and structs are built in linear memory by convention, exactly like in C.
Talking to the outside world
Our playground's host provides a function called console.log. A module imports it and calls it like any
other function:
(module
(import "console" "log" (func $log (param i32)))
(func (export "main")
(call $log (i32.const 1))
(call $log (i32.const 2))
(call $log (i32.const 3))))1
2
3
The import has a two-level name, "console" "log", and a type: one i32 parameter, no result. When the module is
instantiated the host supplies a JavaScript function for that name. In the browser it's exactly the same idea: you
pass an object like { console: { log: (n) => … } } to WebAssembly.instantiate.
How this course works
| Fence | What runs |
|---|---|
wat run |
Your WebAssembly text is compiled with wabt (the WebAssembly Binary Toolkit) and run by your browser's own engine. The exported main is called automatically and its result printed |
js run |
JavaScript that uses the WebAssembly API directly, with the module bytes written inline |
| plain fences | Toolchain commands and source code for Rust, C, AssemblyScript and WASI that need tools a browser can't run |
If you make a mistake, the compiler tells you exactly where:
(module
(func (export "main") (result i32)
i32.const 1
i64.const 2
i32.add))main.wat:5:5: error: type mismatch in i32.add, expected [i32, i32] but got [i32, i64]
i32.add))
^^^^^^^
The validator noticed that i32.add needs two i32 values but found an i64 on top of the stack. Type errors
like this are caught before any code runs. That is one of WebAssembly's core safety properties.
Test yourself
What is WebAssembly primarily?
- A new JavaScript framework
- A binary instruction format for a sandboxed virtual machine
- A browser plugin like Flash
- A replacement for HTML
How does a WebAssembly module print text or access the DOM?
- With built-in
printanddocumentinstructions - It can't do anything outside its memory
- By calling functions that the host imports into it
- Through direct system calls
When is a type error such as adding an i64 with i32.add detected?
- Never, the value is converted
- At validation time, before the code runs
- Only when that line executes
- When the garbage collector runs