TExcelTAD
Home › WebAssembly › Imports & Exports

Imports & Exports

A WebAssembly module is a sealed box with two kinds of openings: imports (what it needs from the host) and exports (what it offers to the host). Four kinds of things can pass through them:

Kind Import syntax Export syntax
function (import "m" "f" (func $f (param i32))) (export "f" (func $f))
memory (import "m" "mem" (memory 1)) (export "memory" (memory 0))
table (import "m" "tbl" (table 4 funcref)) (export "tbl" (table 0))
global (import "m" "g" (global $g i32)) (export "g" (global $g))

Two-level import names

Every import has a module name and a field name, such as "console" "log". The host supplies an import object with the same shape:

js
const imports = {
  console: { log: (n) => console.log(n) },   // "console" "log"
  env: { memory: new WebAssembly.Memory({ initial: 1 }) },
};
const { instance } = await WebAssembly.instantiate(bytes, imports);

The module name is just a namespace. Toolchains have their own conventions: Emscripten uses "env", WASI uses "wasi_snapshot_preview1", and wasm-bindgen uses generated names.

Imports are typed

The import declares the type the module expects. For functions from JavaScript, the engine converts arguments and results at the boundary using that type. The same JavaScript function can therefore be imported with different signatures, as the playground's console.log shows:

wat · try it yourself
(module
  (import "console" "log" (func $log_int (param i32)))
  (import "console" "log" (func $log_pair (param i32 f64)))
  (import "console" "log" (func $log_big (param i64)))
  (func (export "main")
    (call $log_int (i32.const 1))
    (call $log_pair (i32.const 2) (f64.const 2.5))
    (call $log_big (i64.const 12345678901234567))))
1
2 2.5
12345678901234567

Missing imports fail at instantiation

Every import must be provided, even if it is never called. If the host's object lacks it, instantiation fails with a LinkError (or a TypeError if a whole module namespace is missing). The playground host's console object has no beep function:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (import "console" "beep" (func $beep))
  (func (export "main")
    (call $log (i32.const 1))))
LinkError: Import #1 "console" "beep": function import requires a callable

Exports

Exports give names to a module's functions, memories, tables and globals. You can export inline, or with a separate export field. The same item can be exported under several names:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (memory $mem 1)
  (global $version i32 (i32.const 3))

  (func $answer (result i32) (i32.const 42))

  (export "answer" (func $answer))
  (export "theAnswer" (func $answer))        ;; an alias
  (export "memory" (memory $mem))
  (export "version" (global $version))

  (func (export "main")
    (call $log (call $answer))
    (call $log (global.get $version))))
42
3

Export names must be unique within a module. Exports are what you see on instance.exports in JavaScript.

A complete round trip from JavaScript

Here is the whole thing from the JavaScript side. The module below was assembled from this WAT:

wat
(module
  (import "env" "report" (func $report (param i32)))
  (func (export "triple") (param i32) (result i32)
    (call $report (local.get 0))
    (i32.mul (local.get 0) (i32.const 3))))

The bytes are written inline, section by section, so you can see where the import and export live:

js · try it yourself
const bytes = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,           // "\0asm", version 1
  // type section (1): two signatures: (i32)->() and (i32)->(i32)
  0x01, 0x0a, 0x02, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x01, 0x7f, 0x01, 0x7f,
  // import section (2): "env" "report", a function of type 0
  0x02, 0x0e, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x00, 0x00,
  // function section (3): one function of type 1
  0x03, 0x02, 0x01, 0x01,
  // export section (7): "triple" = function 1 (index 0 is the import)
  0x07, 0x0a, 0x01, 0x06, 0x74, 0x72, 0x69, 0x70, 0x6c, 0x65, 0x00, 0x01,
  // code section (10): local.get 0, call 0, local.get 0, i32.const 3, i32.mul, end
  0x0a, 0x0d, 0x01, 0x0b, 0x00, 0x20, 0x00, 0x10, 0x00, 0x20, 0x00, 0x41, 0x03, 0x6c, 0x0b,
]);

const seen = [];
const imports = { env: { report: (n) => seen.push(n) } };
const { module, instance } = await WebAssembly.instantiate(bytes, imports);

console.log(WebAssembly.Module.imports(module));
console.log(WebAssembly.Module.exports(module));
console.log(instance.exports.triple(14));
console.log(instance.exports.triple(-5));
console.log('reported:', seen);

try {
  await WebAssembly.instantiate(bytes, {});
} catch (err) {
  console.log(err.constructor.name);
}
[ { module: 'env', name: 'report', kind: 'function' } ]
[ { name: 'triple', kind: 'function' } ]
42
-15
reported: [ 14, -5 ]
TypeError

WebAssembly.Module.imports() and .exports() let a host inspect what a module needs and offers before instantiating it, which bundlers and loaders use to wire things up automatically.

Design tips

  • Keep the import surface small and explicit: it is the module's entire capability set. A module that imports only console.log can't do anything else, no matter what its code says.
  • Export a memory named memory when the host needs to read or write data (strings, arrays). That's the convention every toolchain follows.
  • Prefer coarse-grained calls across the boundary (process a whole array per call) over many tiny calls.

Test yourself

What are the two parts of an import's name?

  1. Type and index
  2. Module name and field name
  3. Host and port
  4. File and line

What happens if an imported function isn't provided?

  1. It is replaced by a no-op
  2. Instantiation fails with a LinkError
  3. It traps only when called
  4. The module compiles to a smaller binary

Which kinds of items can be imported and exported?

  1. Only functions
  2. Functions and memories
  3. Functions, memories, tables and globals (and tags with exceptions)
  4. Anything, including locals