W3cubDocs

/JavaScript

WebAssembly.table

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The Table() constructor of the WebAssembly global object creates a new Table object instance.

This is a JavaScript wrapper object — an array-like structure representing a WebAssembly Table, which stores function references. A table created by JavaScript or in WebAssembly code will be accessible and mutable from both JavaScript and WebAssembly.

Note: Tables can currently only store function references, but this will likely be expanded in the future.

Syntax

var myTable = new WebAssembly.Table(tableDescriptor);

Parameters

tableDescriptor
An object that can contain the following members:
element
A DOMString representing the type of reference to be stored in the table. At the moment this can only have a value of "anyfunc" (functions).
initial
The initial length of the WebAssembly Table, in single units representing the number of references the table will contain.
maximum Optional
The maximum size (number of references) the WebAssembly Table is allowed to grow to.

Exceptions

  • If tableDescriptor is not of type object, a TypeError is thrown.
  • If maximum is specified and is smaller than initial, a RangeError is thrown.

Table constructor methods

None.

Table instances

All Table instances inherit from the Table() constructor's prototype object — this can be modified to affect all Table instances.

Instance properties

Table.prototype.constructor
Returns the function that created this object's instance. By default this is the WebAssembly.Table() constructor.
Table.prototype.length
Returns the length of the table, i.e. the number of references stored in the table.

Instance methods

Table.prototype.get()
Accessor function — gets a reference stored at a given index.
Table.prototype.grow()
Increases the size of the Table instance by a specified number of references.
Table.prototype.set()
Mutates a reference stored at a given index to a different value.

Examples

The following example (see table2.html source code and live version) creates a new WebAssembly Table instance with an initial size of 2 references. We then print out the table length and contents of the two indexes (retrieved via Table.prototype.get()) to show that the length is two, and the indexes currently contain no function references (they currently return null).

var tbl = new WebAssembly.Table({initial:2, element:"anyfunc"});
console.log(tbl.length);
console.log(tbl.get(0));
console.log(tbl.get(1));

We then create an import object that contains a reference to the table:

var importObj = {
  js: {
    tbl:tbl
  }
};

Finally, we load and instantiate a wasm module (table2.wasm) using our fetchAndInstantiate() utility function, log the table length, and invoke the two referenced functions that are now stored in the table (the table2.wasm module (see text representation) adds two function references to the table, both of which print out a simple value):

fetch('table2.wasm').then(function(response) {
  response.arrayBuffer().then(function(bytes) {
    WebAssembly.instantiate(bytes, importObj).then(function(results) {
      console.log(tbl.length);
      console.log(tbl.get(0)());
      console.log(tbl.get(1)());
    });
  });
});

Note how you've got to include a second function invocation operator at the end of the accessor to actually invoke the referenced function and log the value stored inside it (e.g. get(0)() rather than get(0)) .

This example shows that we're creating and accessing the table from JavaScript, but the same table is visible and callable inside the wasm instance too.

Specifications

Specification Status Comment
Web Assembly JavaScript API
The definition of 'Table' in that specification.
Draft Initial draft definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support No support[1] No support No support[2] No support No support[1] No support
Feature Android Android Webview Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support No support[1] No support No support[2] No support No support No support No support[1]

[1] Experimental support can be enabled in Chrome 51+ and Opera 38+ by going to chrome://flags and enabling the Experimental WebAssembly flag.

[2] Experimental support can be enabled in Firefox 47+ by enabling the javascript.options.wasm flag in about:config.

See also

  • Table instance page
  • WebAssembly landing page.
  • WebAssembly concepts
  • Using the WebAssembly JavaScript API

© 2005–2017 Mozilla Developer Network and individual 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