Table of Contents
- Functions in WebAssembly
- Module Structure Overview
- Imports and Exports
- Security Considerations
- Portability and Universality
- Modularity and Reusability
- Links and References
Functions in WebAssembly
Functions are the fundamental execution units in a Wasm module. They can be called internally or from JavaScript, accept typed parameters, and return typed results. WebAssembly uses a stack-based evaluation model—operands are pushed to the stack, operations consume them, and results remain on the stack.Example: An add Function in WAT
$add: Function identifier (used in exports or tables).param/result: Typed inputs and outputs.- Stack Ops:
local.get $a/local.get $bpush parameters.i32.addpops twoi32values, adds them, pushes the result.
- Implicit Return: The final stack value is returned when a
resultis declared.
WebAssembly Text Format (WAT) offers human-readable syntax. For production, modules compile to a compact binary format (
.wasm).Module Structure Overview
A WebAssembly module combines several sections into one sealed binary:| Section | Purpose | WAT Example |
|---|---|---|
| Functions | Named code blocks; core execution units | See the add function above |
| Globals | Module-wide mutable or immutable variables | (global $count (mut i32) (i32.const 0)) |
| Tables | Arrays of funcref for indirect/dynamic calls | (table 2 funcref) |
| Memory | Linear byte buffers for data storage | (memory 1) |
| Imports | Declarations of host-provided functions/globals | (import "js" "log" (func $log (param i32))) |
| Exports | Public API: functions, memories, tables, globals | (export "memory" (memory 0)) |

- Define filter functions (
blur,contrast). - Map filter names to table indices.
- Allocate memory for raw pixel buffers.
Imports and Exports
WebAssembly modules interact with their host (e.g., JavaScript) through imports and exports.Importing Host Functions
- Module: Declares a dependency on a host function
env.getTimestamp. - Host Binding: JavaScript or a runtime must provide this symbol.
Exporting Module Components
- Export: Makes the
fibfunction accessible to the host. - Namespacing: Exports live in an export object (e.g.,
instance.exports.fibonacci).
Security Considerations
WebAssembly’s design enforces strong sandboxing. Modules cannot access host memory or resources unless explicitly imported.

Never run untrusted Wasm modules without proper validation. Always verify imports and sandbox boundaries to prevent privilege escalation.
Portability and Universality
Wasm modules run consistently across diverse platforms:- Web Browsers
- Native Runtimes (e.g., Wasmtime, Wasmer)
- Embedded & IoT Devices
Modularity and Reusability
Breaking a large application into focused Wasm modules simplifies maintenance and encourages code sharing.- 3D Editor Example
- Geometry Calculations
- Texture Mapping
- UI Logic
