Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WebAssembly.Table() constructor

BaselineWidely available

TheWebAssembly.Table() constructor creates a newTable object of the given size and element type, filled with the provided value.

Syntax

js
new WebAssembly.Table(tableDescriptor)new WebAssembly.Table(tableDescriptor, value)

Parameters

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.

maximumOptional

The maximum number of elements the WebAssembly Table is allowed to grow to.

valueOptional

The element to fill the newly-allocated space with.

Exceptions

  • IftableDescriptor is not an object, aTypeError is thrown.
  • Ifmaximum is specified and is smaller thaninitial, aRangeError is thrown.
  • Ifelement is not one of thereference types, then aTypeError is thrown.
  • Ifvalue is not a value of the typeelement, aTypeError is thrown.

Examples

Creating a new WebAssembly Table instance

The following example creates aWebAssembly.Table instance with an initial size of 2 elements. TheWebAssembly.Table contents are populated using a WebAssembly module and are accessible from JavaScript. When viewing thelive example, open your developer console to display console log messages from the code snippets below.

This example uses the following reference files:

  1. table2.html: An HTML file containing JavaScript that creates aWebAssembly.Table (source code)
  2. table2.wasm: A WebAssembly module imported by the JavaScript code intable2.html (source code)

Intable2.html, we create aWebAssembly.Table:

js
const tbl = new WebAssembly.Table({  initial: 2,  element: "anyfunc",});

We can retrieve the index contents usingTable.prototype.get():

js
console.log(tbl.length); // a table with 2 elementsconsole.log(tbl.get(0)); // content for index 0 is nullconsole.log(tbl.get(1)); // content for index 1 is null

Next, we create an import object that contains theWebAssembly.Table:

js
const importObject = {  js: { tbl },};

Next, we load and instantiate a WebAssembly module. Thetable2.wasm module defines a table containing two functions. The first function returns 42, and the second returns 83:

wat
(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 instantiatetable2.wasm using theWebAssembly.instantiateStreaming() method:

js
const instantiating = WebAssembly.instantiateStreaming(  fetch("table2.wasm"),  importObject,);

After instantiatingtable2.wasm,tbl is updated with the following:

  • table length is still 2
  • content for index 0 is now a function which returns 42
  • content for index 1 is now a function which returns 83

The items at indexes 0 and 1 of the table are now callableExported WebAssembly Functions. To call them, note that we must add the function invocation operator() after theget() call:

js
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 theWebAssembly.Table from JavaScript, the sameTable is also visible and callable inside the WebAssembly instance.

Creating a new WebAssembly Table instance with a value

The following example creates a new WebAssembly Table instance with 4 elements, full of the same object:

js
const myObject = { hello: "world" };const table = new WebAssembly.Table(  {    element: "externref",    initial: 4,    maximum: 4,  },  myObject,);console.log(myObject === table.get(2)); // true

Specifications

Specification
WebAssembly JavaScript Interface
# dom-table-table

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp