Skip to content

Commit abf9693

Browse files
authored
[JS API] Implement Table Bindings (#8186)
Fixes #8122
1 parent e0dfb22 commit abf9693

File tree

4 files changed

+182
-7
lines changed

4 files changed

+182
-7
lines changed

src/js/binaryen.js-post.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5264,6 +5264,130 @@ Module['Function'] = (() => {
52645264
return Function;
52655265
})();
52665266

5267+
// Table wrapper
5268+
Module['Table'] = (() => {
5269+
/** @constructor */
5270+
function Table(table) {
5271+
if (!(this instanceof Table)) {
5272+
if (!table) return null;
5273+
return new Table(table);
5274+
}
5275+
if (!table) throw Error("table reference must not be null");
5276+
this[thisPtr] = table;
5277+
}
5278+
/**
5279+
* Gets the name of the specified `Table`.
5280+
*
5281+
* @param {Table} table - The `Table` to get the name from.
5282+
*
5283+
* @return {string} The name of the `Table`.
5284+
*/
5285+
Table['getName'] = function(table) {
5286+
return UTF8ToString(Module['_BinaryenTableGetName'](table));
5287+
};
5288+
5289+
/**
5290+
* Sets the name of the specified `Table`.
5291+
*
5292+
* @param {Table} table - The `Table` to set the name for.
5293+
* @param {string} name - The new name for the `Table`.
5294+
*
5295+
* @return {void}
5296+
*/
5297+
Table['setName'] = function(table, name) {
5298+
preserveStack(() => {
5299+
Module['_BinaryenTableSetName'](table, strToStack(name));
5300+
});
5301+
};
5302+
5303+
/**
5304+
* Gets the initial number of pages of the specified `Table`.
5305+
*
5306+
* @param {Table} table - The `Table` to get the initial number of pages from.
5307+
*
5308+
* @return {number} The initial number of pages of the `Table`.
5309+
*/
5310+
Table['getInitial'] = function(table) {
5311+
return Module['_BinaryenTableGetInitial'](table);
5312+
};
5313+
5314+
/**
5315+
* Sets the initial number of pages of the specified `Table`.
5316+
*
5317+
* @param {Table} table - The `Table` to set the initial number of pages for.
5318+
* @param {number} initial - The new initial number of pages for the `Table`.
5319+
*
5320+
* @return {void}
5321+
*/
5322+
Table['setInitial'] = function(table, initial) {
5323+
Module['_BinaryenTableSetInitial'](table, initial);
5324+
};
5325+
5326+
/**
5327+
* Tests whether the specified `Table` has a maximum number of pages.
5328+
*
5329+
* @param {Table} table - The `Table` to test.
5330+
*
5331+
* @return {boolean} `true` if the `Table` has a maximum number of pages, `false` otherwise.
5332+
*/
5333+
Table['hasMax'] = function(table) {
5334+
return Boolean(Module['_BinaryenTableHasMax'](table));
5335+
};
5336+
5337+
/**
5338+
* Gets the maximum number of pages of the specified `Table`.
5339+
*
5340+
* @param {Table} table - The `Table` to get the maximum number of pages from.
5341+
*
5342+
* @return {number} The maximum number of pages of the `Table`.
5343+
*/
5344+
Table['getMax'] = function(table) {
5345+
return Module['_BinaryenTableGetMax'](table);
5346+
};
5347+
5348+
/**
5349+
* Sets the maximum number of pages of the specified `Table`.
5350+
*
5351+
* @param {Table} table - The `Table` to set the maximum number of pages for.
5352+
* @param {number} max - The new maximum number of pages for the `Table`.
5353+
*
5354+
* @return {void}
5355+
*/
5356+
Table['setMax'] = function(table, max) {
5357+
return Module['_BinaryenTableSetMax'](table, max);
5358+
};
5359+
5360+
/**
5361+
* Gets the table type of the specified `Table`.
5362+
*
5363+
* @param {Table} table - The `Table` to get the type from.
5364+
*
5365+
* @return {number} The type of the `Table`.
5366+
*/
5367+
Table['getType'] = function(table) {
5368+
return Module['_BinaryenTableGetType'](table);
5369+
};
5370+
5371+
/**
5372+
* Sets the table type of the specified `Table`.
5373+
*
5374+
* @param {Table} table - The `Table` to set the type for.
5375+
* @param {Type} tableType - The new type for the `Table`.
5376+
*
5377+
* @return {void}
5378+
*/
5379+
Table['setType'] = function(table, tableType) {
5380+
return Module['_BinaryenTableSetType'](table, tableType);
5381+
};
5382+
5383+
deriveWrapperInstanceMembers(Table.prototype, Table);
5384+
Table.prototype['valueOf'] = function() {
5385+
return this[thisPtr];
5386+
};
5387+
return Table;
5388+
})();
5389+
5390+
52675391
// Additional customizations
52685392

52695393
Module['exit'] = function(status) {

test/binaryen.js/kitchen-sink.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,6 @@ function test_core() {
744744
assert(tablePtr !== 0);
745745
assert(tablePtr === module.getTableByIndex(0));
746746

747-
var table = binaryen.getTableInfo(tablePtr);
748-
assert(table.name === "t1");
749-
assert(table.module === "");
750-
assert(table.base === "");
751-
assert(table.initial === 0);
752-
assert(table.max === 2);
753-
754747
module.removeTable("t1");
755748
assert(module.getNumTables() === 0);
756749

test/binaryen.js/tables.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const module = new binaryen.Module();
2+
3+
// Test table
4+
const tableName = "a-table";
5+
const tableMin = 5;
6+
const tableMax = 15;
7+
const tablePtr = module.addTable(tableName, tableMin, tableMax);
8+
9+
console.log("GetTable is equal: " + (tablePtr === module.getTable(tableName)));
10+
11+
const tableInfo = binaryen.getTableInfo(tablePtr);
12+
console.log("getTableInfo=" + JSON.stringify(tableInfo));
13+
14+
// Test Wrapper
15+
// get/set name
16+
assert(binaryen.Table.getName(tablePtr) === tableName);
17+
binaryen.Table.setName(tablePtr, "new-table-name");
18+
assert(binaryen.Table.getName(tablePtr) === "new-table-name");
19+
binaryen.Table.setName(tablePtr, tableName);
20+
21+
// get/set initial
22+
assert(binaryen.Table.getInitial(tablePtr) === tableMin);
23+
binaryen.Table.setInitial(tablePtr, 10);
24+
assert(binaryen.Table.getInitial(tablePtr) === 10);
25+
binaryen.Table.setInitial(tablePtr, tableMin);
26+
27+
// hasMax
28+
assert(binaryen.Table.hasMax(tablePtr) === true);
29+
30+
// get/set max
31+
assert(binaryen.Table.getMax(tablePtr) === tableMax);
32+
binaryen.Table.setMax(tablePtr, 20);
33+
assert(binaryen.Table.getMax(tablePtr) === 20);
34+
binaryen.Table.setMax(tablePtr, tableMax);
35+
36+
// get/set type
37+
assert(binaryen.Table.getType(tablePtr) === binaryen.funcref);
38+
binaryen.Table.setType(tablePtr, binaryen.anyref);
39+
assert(binaryen.Table.getType(tablePtr) === binaryen.anyref);
40+
binaryen.Table.setType(tablePtr, binaryen.funcref);
41+
42+
const tableRef = binaryen.Table(tablePtr);
43+
assert(tableRef.name === tableName);
44+
assert(tableRef.initial === tableMin);
45+
assert(tableRef.max === tableMax);
46+
assert(tableRef.type === binaryen.funcref);
47+
48+
// Cleanup
49+
assert(module.validate());
50+
console.log(module.emitText());
51+
52+
module.dispose();

test/binaryen.js/tables.js.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
GetTable is equal: true
2+
getTableInfo={"name":"a-table","module":"","base":"","initial":5,"max":15}
3+
(module
4+
(table $a-table 5 15 funcref)
5+
)
6+

0 commit comments

Comments
 (0)