Skip to content
WebAssembly

Tables

Function references for indirect calls.

By EZ4Code Team
tableindirect-call

Code

// A table holds function references for indirect calls
const table = new WebAssembly.Table({
  element: "anyfunc",
  initial: 2,
  maximum: 10,
});

// Pass the table in as a module import
const { instance } = await WebAssembly.instantiate(bytes, {
  env: { tbl: table },
});

// Invoke a function by index (indirect call)
const result = table.get(0)(5, 3);
console.log("indirect result:", result);

// Mutate entries (exports must populate them)
// table.set(1, instance.exports.someFn);
console.log("table length:", table.length);

Explanation

A WebAssembly Table stores function references that can be called by index, enabling function pointers and vtables. Tables are declared with element type anyfunc and a size range, then shared with the module through imports. The get and set methods read and replace entries, and grow expands the table.

More WebAssembly Snippets