The WebAssembly.Table()
constructor creates a new Table
object of the given size and element type.
The WebAssembly.Table()
constructor creates a new Table
object of the given size and element type.
new WebAssembly.Table(tableDescriptor)
tableDescriptor
An object that can contain the following members:
element
A string representing the type of value to be stored in the table. This can have a value of "anyfunc"
(functions) or "externref"
(host references).
initial
The initial number of elements of the WebAssembly Table.
maximum
Optional
The maximum number of elements the WebAssembly Table is allowed to grow to.
tableDescriptor
is not of type object, a TypeError
is thrown. maximum
is specified and is smaller than initial
, a RangeError
is thrown. tableDescriptor.element
is not one of the reference types, then a TypeError
is thrown. The following example creates a WebAssembly.Table
instance with an initial size of 2 elements. The WebAssembly.Table
contents are populated using a WebAssembly module and are accessible from JavaScript. When viewing the live example, open your developer console to display console log messages from the code snippets below.
This example uses the following reference files:
table2.html
: An HTML file containing JavaScript that creates a WebAssembly.Table
(source code)table2.wasm
: A WebAssembly module imported by the JavaScript code in table2.html
(source code)In table2.html
, we create a WebAssembly.Table
:
const tbl = new WebAssembly.Table({ initial: 2, element: "anyfunc", });
We can retrieve the index contents using Table.prototype.get()
:
console.log(tbl.length); // a table with 2 elements console.log(tbl.get(0)); // content for index 0 is null console.log(tbl.get(1)); // content for index 1 is null
Next, we create an import object that contains the WebAssembly.Table
:
const importObject = { js: { tbl }, };
Next, we load and instantiate a WebAssembly module. The table2.wasm
module defines a table containing two functions. The first function returns 42, and the second returns 83:
(module (import "js" "tbl" (table 2 anyfunc)) (func $f42 (result i32) i32.const 42) (func $f83 (result i32) i32.const 83) (elem (i32.const 0) $f42 $f83) )
We instantiate table2.wasm
using the WebAssembly.instantiateStreaming()
method:
const instantiating = WebAssembly.instantiateStreaming(fetch('table2.wasm'), importObject);
After instantiating table2.wasm
, tbl
is updated with the following:
The items at indexes 0 and 1 of the table are now callable Exported WebAssembly Functions. To call them, note that we must add the function invocation operator ()
after the get()
call:
instantiating.then((obj) => { console.log(tbl.length); // 2 console.log(tbl.get(0)()); // 42 console.log(tbl.get(1)()); // 83 });
While we are creating and accessing the WebAssembly.Table
from JavaScript, the same Table
is also visible and callable inside the WebAssembly instance.
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
Table |
57 |
16 |
52 |
No |
44 |
11 |
57 |
57 |
52 |
43 |
11 |
7.0 |
1.0 |
8.0.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Table/Table