Fundamentals of WebAssembly: Modules, Binary and Text Formats
This article provides a comprehensive introduction to WebAssembly, covering its core concepts such as modules, types, variables, functions, instructions, traps, tables, linear memory, the binary module structure, the S‑expression based text format, and step‑by‑step guidance for writing a simple WebAssembly module with code examples.
The article begins with an overview of WebAssembly, its key features, history, and typical use cases, then moves to the basic concepts of modules, types, variables, functions, instructions, traps, tables, and linear memory.
It explains that a module is the fundamental unit containing functions, memory, globals, and tables, and describes how a module is instantiated into an instance via WebAssembly.instantiate or new WebAssembly.Instance .
Fourteen‑byte variable types ( i32 , i64 , f32 , f64 ) and extended types such as v128 , funcref , and externref are introduced, along with their signed/unsigned variants.
The binary module structure is detailed, listing the twelve optional sections (custom, type, function, table, memory, global, import, export, start, element, code, data, data‑count) and showing how they must appear in a fixed order after the four‑byte magic number and version.
For the text format, the article describes the S‑expression syntax, lexical tokens (integers, floats, strings, names, identifiers), and the various type definitions used in the textual representation.
Several concrete code snippets illustrate the syntax, for example a minimal module and a function definition:
(module (func $add (param $lhs i32) (param $rhs i32) (result i32) get_local $lhs get_local $rhs i32.add))
It also shows how to declare imports, exports, memories, data segments, globals, tables, element segments, and a start function, and provides a complete example that builds a simple module, compiles it with wat2wasm , and invokes it from JavaScript.
The concluding section summarizes that the WebAssembly binary and text formats are closely related, encourages readers to experiment with hand‑written modules, and points to further resources.
ByteDance Web Infra
ByteDance Web Infra team, focused on delivering excellent technical solutions, building an open tech ecosystem, and advancing front-end technology within the company and the industry | The best way to predict the future is to create it
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.