Skip to content

Reference Types

Reference types let Wasm refer to functions and host objects.

  • funcref — reference to a function inside the module or table.
  • externref — opaque reference to a host value (e.g. a JS object).
(module
(func $double (param $x i32) (result i32)
(i32.mul (local.get $x) (i32.const 2)))
(table 2 funcref)
(elem (i32.const 0) $double)
(func (result i32)
(ref.is_null (ref.null funcref))) ;; -> 1
(func (result i32)
(ref.is_null (ref.func $double))) ;; -> 0
)
(module
(type $t0 (func (param i32) (result i32)))
(func $double (type $t0) (param $x i32) (result i32)
(i32.mul (local.get $x) (i32.const 2)))
(table 1 funcref)
(elem (i32.const 0) $double)
(func (param $n i32) (result i32)
(call_indirect (type $t0) (local.get $n) (i32.const 0)))
)

The last argument to call_indirect is the table element index. The preceding arguments are passed to the function.

From JS, you can pass host references as externref via imports or function parameters.