WebAssembly module boundaries
A WebAssembly module communicates with its host through two flat lists: imports
it requires and exports it offers. Both reference one of four external kinds —
func, table, memory or global. This reference shows the WebAssembly text
(WAT) syntax for each and the matching JavaScript binding used by
WebAssembly.instantiate.
How it works
Every import carries a two-level name: a module string and a field string. At
instantiation the host supplies an import object — a nested record — and the
linker matches (import "mod" "name" ...) to importObject.mod.name:
(module
(import "env" "log" (func $log (param i32)))
(import "env" "mem" (memory 1))
(global $g (export "counter") (mut i32) (i32.const 0))
(func (export "run") ... ))
const importObject = {
env: {
log: (n) => console.log(n),
mem: new WebAssembly.Memory({ initial: 1 }),
},
};
const { instance } = await WebAssembly.instantiate(bytes, importObject);
instance.exports.run();
Exports appear on instance.exports after instantiation. The supplied import
value must match the declared kind and limits, or a LinkError is thrown.
Tips and notes
- Memory size is in 64 KiB pages; a
(memory 1)is exactly 65536 bytes. i64globals/params surface in JS asBigInt, notNumber.- Mutable globals need
WebAssembly.Global({ mutable: true }, ...). - A table’s element type is
funcref(orexternrefwith reference types).