Skip to content
WebAssembly

Tables

Function references for indirect calls.

#table#indirect-call

Code

webassembly
// 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);