Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 38 ยท Modules, Memory and Tables

Tables and Indirect Calls

Learn how WebAssembly tables hold function references, how call_indirect implements function pointers and virtual methods, and how to manage tables from JavaScript.

A normal call instruction names its target function in the bytecode, so the target can never change. Real programs need more: callbacks, function pointers in C, virtual methods in C++, trait objects in Rust, switch dispatch in interpreters. WebAssembly supports these with tables and the call_indirect instruction.

A table is a resizable array of references. The most common table holds funcref values, which point at functions. Code never sees a raw address, only an index into the table. That is how Wasm keeps control flow safe: you can call only what is in the table, and the engine checks the function's type on every indirect call.

flowchart LR
    subgraph T["Table 0 (funcref)"]
      direction TB
      S0["0: add"]
      S1["1: sub"]
      S2["2: mul"]
      S3["3: null"]
    end
    C["call_indirect (type i32 i32 -> i32)\nindex = 1"] --> S1

Declaring and filling a table

  • The Table section (id 4) declares each table: an element type (0x70 is funcref) and limits (a minimum size and optional maximum), exactly like memory limits but counted in elements.
  • The Element section (id 9) fills it. An active element segment says: table 0, starting at offset n, these function indexes. Slots nobody fills hold null.

call_indirect

call_indirect typeidx tableidx (binary 0x11 typeidx tableidx) pops an index from the stack, then the arguments that the type requires, looks up the table entry and calls it. Three things can go wrong, and all of them trap with a RuntimeError:

  1. The index is outside the table.
  2. The entry is null.
  3. The function's type does not match the expected type (signature mismatch).

That third check is what makes function pointers safe: you cannot call an (i32) -> i32 function as if it were (i32, i32) -> i32.

The next example builds a four-slot table with add, sub and mul in slots 0 to 2, and an apply(op, a, b) function that calls whichever one you choose.

Node.js A dispatch table with call_indirect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Changing a table at run time

From JavaScript you can read and change a table through the WebAssembly.Table object: table.get(i), table.set(i, value), table.grow(n) and table.length. Only a Wasm function (an exported function, not an ordinary JavaScript function) can be stored in a funcref table. Replacing slot 3 with a function whose type differs gives a trap when called indirectly with the other type, which is exactly the safety rule.

Node.js Table.set, grow and signature checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

To store a JavaScript function in a table you wrap it first: either make a tiny Wasm module that imports the function and re-exports it, or use a toolchain feature that does the same. Libraries such as Emscripten's addFunction automate this.

Where tables come from in real programs

  • C and C++ function pointers. A pointer to a function is really a table index. Taking the address of a function adds it to the table.
  • Virtual methods and trait objects. The vtable holds table indexes.
  • Dynamic linking. Plug-in style modules share one table so they can call each other's functions.
  • externref tables. A table of type externref can hold any JavaScript value (objects, DOM nodes) without exposing it as a number, which is how safe handles to host objects are built.

Index 0 of a table is a valid function in WebAssembly, but C toolchains reserve it so that a null function pointer (the integer 0) traps instead of calling something real.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does call_indirect pop from the stack first?

    The table index is on top of the stack; the arguments are below it.
  2. 2. Which situation does NOT trap on call_indirect?

    Only a valid entry with a matching type is called. Out-of-range, null and mismatched entries all trap.
  3. 3. Which kind of value can table.set store in a funcref table?

    funcref tables hold references to Wasm functions. Plain JavaScript functions must be wrapped by a module first.
  4. 4. How does a C function pointer appear in Wasm?

    Compilers place address-taken functions in a table and use the index as the pointer value.

Summary

  • A table is a resizable array of references, usually funcref, declared in the Table section and filled by element segments.
  • call_indirect calls a table entry by index and traps on out-of-range, null or mismatched signature.
  • Tables are how function pointers, vtables and dynamic dispatch work in Wasm.
  • From JavaScript: get, set, grow and length; only Wasm functions can be stored in a funcref table.
  • Tables can be imported, exported and shared between modules.

Next: Globals