WebAssembly Import/Export Reference

Wasm import and export descriptor types — func, table, memory, global — with syntax.

Reference for WebAssembly module import and export descriptor kinds — func, table, memory, global — with WAT syntax and the JavaScript instantiate API binding.

What are the four WebAssembly external kinds?

A module can import and export functions (func), tables (table), linear memories (memory) and globals (global). Each import names a two-level module/name pair, and each export names a single field. These are the only externally-visible entity kinds.

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.
  • i64 globals/params surface in JS as BigInt, not Number.
  • Mutable globals need WebAssembly.Global({ mutable: true }, ...).
  • A table’s element type is funcref (or externref with reference types).