TExcelTAD
Home › WebAssembly › Reference Types

Reference Types

Numbers are not the only values WebAssembly can hold. Reference types (standard since WebAssembly 2.0) are opaque handles to things that live outside linear memory:

Type Refers to Null value
funcref a WebAssembly function ref.null func
externref any host value: a JavaScript object, string, DOM node… ref.null extern

"Opaque" is the key word: Wasm code can store, pass and compare references with null, but it can't look inside them or turn them into numbers. A reference can't be written to linear memory, only kept in locals, globals, tables, parameters and results.

Reference instructions

Instruction Type Meaning
ref.null func / ref.null extern [] → [ref] a null reference
ref.is_null [ref] → [i32] 1 if null
ref.func $f [] → [funcref] a reference to function $f
table.get / table.set read/write references in a table
wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (func $hello (result i32) (i32.const 1))
  (elem declare func $hello)
  (global $handler (mut funcref) (ref.null func))
  (func (export "main")
    (call $log (ref.is_null (global.get $handler)))   ;; still null
    (global.set $handler (ref.func $hello))
    (call $log (ref.is_null (global.get $handler)))
    (call $log (ref.is_null (ref.null extern)))))
1
0
1

Holding host objects with externref

Before reference types, a Wasm module that needed to "hold" a JavaScript object had to rely on glue code: JS kept the objects in an array and the module passed integer indices around. With externref the module can hold the object itself, and the garbage collector keeps it alive as long as Wasm references it.

This module keeps up to four host values in a table of externref:

wat
(module
  (table $things 4 externref)
  (func (export "put") (param i32 externref)
    (table.set $things (local.get 0) (local.get 1)))
  (func (export "get") (param i32) (result externref)
    (table.get $things (local.get 0)))
  (func (export "isNull") (param externref) (result i32)
    (ref.is_null (local.get 0))))

The JavaScript side passes any values in and gets the very same objects back:

js · try it yourself
const bytes = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
  // type section: (i32, externref)->(), (i32)->(externref), (externref)->(i32)   [0x6f = externref]
  0x01, 0x10, 0x03, 0x60, 0x02, 0x7f, 0x6f, 0x00, 0x60, 0x01, 0x7f, 0x01, 0x6f, 0x60, 0x01, 0x6f, 0x01, 0x7f,
  // function section: three functions with types 0, 1, 2
  0x03, 0x04, 0x03, 0x00, 0x01, 0x02,
  // table section: one table of externref, minimum 4 slots
  0x04, 0x04, 0x01, 0x6f, 0x00, 0x04,
  // export section: "put", "get", "isNull"
  0x07, 0x16, 0x03, 0x03, 0x70, 0x75, 0x74, 0x00, 0x00, 0x03, 0x67, 0x65, 0x74, 0x00, 0x01,
  0x06, 0x69, 0x73, 0x4e, 0x75, 0x6c, 0x6c, 0x00, 0x02,
  // code section: table.set (0x26), table.get (0x25), ref.is_null (0xd1)
  0x0a, 0x17, 0x03,
  0x08, 0x00, 0x20, 0x00, 0x20, 0x01, 0x26, 0x00, 0x0b,
  0x06, 0x00, 0x20, 0x00, 0x25, 0x00, 0x0b,
  0x05, 0x00, 0x20, 0x00, 0xd1, 0x0b,
]);

const { instance } = await WebAssembly.instantiate(bytes);
const { put, get, isNull } = instance.exports;

const user = { name: 'Ada', roles: ['admin'] };
put(0, user);
put(1, 'a plain string');
put(2, (x) => x * 2);

console.log(get(0) === user);          // the same object, not a copy
console.log(get(1));
console.log(get(2)(21));
console.log(get(3));                   // never set: null
console.log(isNull(null), isNull(user));
true
a plain string
42
null
1 0

Why it matters

  • Fewer glue layers: wasm-bindgen and other toolchains use externref to hand DOM objects, promises and callbacks directly to Wasm.
  • Safety: an opaque reference can't be forged from an integer, so a module can't invent access to objects it was never given.
  • Foundation for more: the typed function references and garbage collection (GC) features, part of WebAssembly 3.0, build on reference types. GC adds struct and array types managed by the engine's garbage collector, which languages such as Kotlin, Dart, Java and OCaml use to compile to Wasm without shipping their own collector.

Note: The GC and typed-function-reference instructions (struct.new, call_ref, ref.cast …) aren't enabled in this course's assembler. They are mostly emitted by compilers rather than written by hand.

Funcref tables revisited

The tables from the previous lessons are tables of funcref. A funcref value can only be called through a table with call_indirect, which keeps the runtime type check in place:

wat · try it yourself
(module
  (import "console" "log" (func $log (param i32)))
  (type $thunk (func (result i32)))
  (func $one (type $thunk) (i32.const 1))
  (func $two (type $thunk) (i32.const 2))
  (elem declare func $one $two)
  (table $slot 1 funcref)

  (func $call_ref_via_table (param $f funcref) (result i32)
    (table.set $slot (i32.const 0) (local.get $f))
    (call_indirect $slot (type $thunk) (i32.const 0)))

  (func (export "main")
    (call $log (call $call_ref_via_table (ref.func $two)))
    (call $log (call $call_ref_via_table (ref.func $one)))))
2
1

Test yourself

What can Wasm code do with an externref?

  1. Read its properties
  2. Store it in linear memory
  3. Pass it around, keep it in tables/globals/locals and test it for null
  4. Convert it to an i32 address

Which instruction produces a reference to a function in the module?

  1. call $f
  2. ref.func $f
  3. table.get $f
  4. global.get $f

When JavaScript passes an object as an externref and later gets it back, what does it receive?

  1. A copy of the object
  2. The same object (=== is true)
  3. A number
  4. A string description