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.
var myTable = new WebAssembly.Table(tableDescriptor);
DOMString representing the type of reference to be stored in the table. At the moment this can only have a value of "anyfunc" (functions).tableDescriptor is not of type object, a TypeError is thrown.maximum is specified and is smaller than initial, a RangeError is thrown.Table constructor methodsNone.
Table instancesAll Table instances inherit from the Table() constructor's prototype object — this can be modified to affect all Table instances.
Table.prototype.constructorWebAssembly.Table() constructor.Table.prototype.lengthTable.prototype.get()Table.prototype.grow()Table.prototype.set()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.
| Specification | Status | Comment |
|---|---|---|
| Web Assembly JavaScript API The definition of 'Table' in that specification. | Draft | Initial draft definition. |
| 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.
© 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