Commit bf3c8b8f authored by Ben Smith's avatar Ben Smith Committed by Commit Bot

[wasm] Cleanup wasm-module-builder.js

Use naming similar to the spec: "table" instead of "function table",
"element segment" instead of "function table init".

Change-Id: Ib1b6cdfa566f8bd00017ccedf9440084204f10ff
Reviewed-on: https://chromium-review.googlesource.com/c/1273612
Commit-Queue: Ben Smith <binji@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56545}
parent 0c968742
...@@ -16,7 +16,7 @@ builder0.addFunction('main', kSig_i_i) ...@@ -16,7 +16,7 @@ builder0.addFunction('main', kSig_i_i)
kExprCallIndirect, sig_index, kTableZero kExprCallIndirect, sig_index, kTableZero
]) // -- ]) // --
.exportAs('main'); .exportAs('main');
builder0.setFunctionTableBounds(3, 3); builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable); builder0.addExportOfKind('table', kExternalTable);
let module0 = new WebAssembly.Module(builder0.toBuffer()); let module0 = new WebAssembly.Module(builder0.toBuffer());
let instance0 = new WebAssembly.Instance(module0); let instance0 = new WebAssembly.Instance(module0);
...@@ -25,7 +25,7 @@ let builder1 = new WasmModuleBuilder(); ...@@ -25,7 +25,7 @@ let builder1 = new WasmModuleBuilder();
builder1.setName('module_1'); builder1.setName('module_1');
builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]); builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]);
builder1.addImportedTable('z', 'table'); builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true); builder1.addElementSegment(0, false, [0], true);
let module1 = new WebAssembly.Module(builder1.toBuffer()); let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 = let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}}); new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......
...@@ -12,7 +12,7 @@ let q_table = builder.addImportedTable("q", "table") ...@@ -12,7 +12,7 @@ let q_table = builder.addImportedTable("q", "table")
let q_base = builder.addImportedGlobal("q", "base", kWasmI32); let q_base = builder.addImportedGlobal("q", "base", kWasmI32);
let q_fun = builder.addImport("q", "fun", kSig_v_v); let q_fun = builder.addImport("q", "fun", kSig_v_v);
builder.addType(kSig_i_ii); builder.addType(kSig_i_ii);
builder.addFunctionTableInit(q_base, true, [ q_fun ]) builder.addElementSegment(q_base, true, [ q_fun ])
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
let table = new WebAssembly.Table({ let table = new WebAssembly.Table({
element: "anyfunc", element: "anyfunc",
......
...@@ -17,7 +17,7 @@ builder.addFunction('main', kSig_i_ii).addBody([ ...@@ -17,7 +17,7 @@ builder.addFunction('main', kSig_i_ii).addBody([
sig_index1, sig_index1,
kTableZero kTableZero
]).exportAs('main'); ]).exportAs('main');
builder.setFunctionTableBounds(kTableSize, kTableSize); builder.setTableBounds(kTableSize, kTableSize);
var m1_bytes = builder.toBuffer(); var m1_bytes = builder.toBuffer();
var m1 = new WebAssembly.Module(m1_bytes); var m1 = new WebAssembly.Module(m1_bytes);
......
...@@ -20,6 +20,6 @@ const builder2 = new WasmModuleBuilder(); ...@@ -20,6 +20,6 @@ const builder2 = new WasmModuleBuilder();
const mul_import = builder2.addImport('q', 'wasm_mul', kSig_i_ii); const mul_import = builder2.addImport('q', 'wasm_mul', kSig_i_ii);
builder2.addImportedTable('q', 'table'); builder2.addImportedTable('q', 'table');
const glob_import = builder2.addImportedGlobal('q', 'glob', kWasmI32); const glob_import = builder2.addImportedGlobal('q', 'glob', kWasmI32);
builder2.addFunctionTableInit(glob_import, true, [mul_import]); builder2.addElementSegment(glob_import, true, [mul_import]);
builder2.instantiate( builder2.instantiate(
{q: {glob: 0, js_div: i => i, wasm_mul: mul, table: table}}); {q: {glob: 0, js_div: i => i, wasm_mul: mul, table: table}});
...@@ -33,7 +33,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -33,7 +33,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, 0, kTableZero kExprCallIndirect, 0, kTableZero
]) ])
.exportFunc(); .exportFunc();
builder.addFunctionTableInit(0, false, [0, 1, 1, 0]); builder.addElementSegment(0, false, [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}}); return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})(); })();
......
...@@ -217,8 +217,8 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -217,8 +217,8 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
kExprCallIndirect, sig_index1, kTableZero]) // -- kExprCallIndirect, sig_index1, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.setFunctionTableBounds(kTableSize, kTableSize); builder.setTableBounds(kTableSize, kTableSize);
builder.addFunctionTableInit(0, false, [f1.index]); builder.addElementSegment(0, false, [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
return new WebAssembly.Module(builder.toBuffer()); return new WebAssembly.Module(builder.toBuffer());
...@@ -239,7 +239,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -239,7 +239,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
.exportAs("main"); .exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize); builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addFunctionTableInit(1, false, [f2.index], true); builder.addElementSegment(1, false, [f2.index], true);
var m2_bytes = builder.toBuffer(); var m2_bytes = builder.toBuffer();
var m2 = new WebAssembly.Module(m2_bytes); var m2 = new WebAssembly.Module(m2_bytes);
......
...@@ -42,8 +42,8 @@ let kTableSize = 50; ...@@ -42,8 +42,8 @@ let kTableSize = 50;
let f17 = addConstFunc(builder, 17); let f17 = addConstFunc(builder, 17);
builder.addExport("f15", f15); builder.addExport("f15", f15);
builder.addExport("f17", f17); builder.addExport("f17", f17);
builder.addFunctionTableInit(15, false, [f15], true); builder.addElementSegment(15, false, [f15], true);
builder.addFunctionTableInit(1, false, [call.index], true); builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule(); var mod1 = builder.toModule();
} }
...@@ -63,10 +63,10 @@ let kTableSize = 50; ...@@ -63,10 +63,10 @@ let kTableSize = 50;
]) ])
.exportAs("call"); .exportAs("call");
let f26 = addConstFunc(builder, 26); let f26 = addConstFunc(builder, 26);
builder.addFunctionTableInit(17, false, [f17], true); builder.addElementSegment(17, false, [f17], true);
builder.addFunctionTableInit(21, false, [f21], true); builder.addElementSegment(21, false, [f21], true);
builder.addFunctionTableInit(26, false, [f26], true); builder.addElementSegment(26, false, [f26], true);
builder.addFunctionTableInit(5, false, [call.index], true); builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule(); var mod2 = builder.toModule();
} }
...@@ -114,8 +114,8 @@ function addConstFuncUsingGlobal(builder, val) { ...@@ -114,8 +114,8 @@ function addConstFuncUsingGlobal(builder, val) {
let f18 = addConstFuncUsingGlobal(builder, 18); let f18 = addConstFuncUsingGlobal(builder, 18);
builder.addExport("f14", f14); builder.addExport("f14", f14);
builder.addExport("f18", f18); builder.addExport("f18", f18);
builder.addFunctionTableInit(14, false, [f14], true); builder.addElementSegment(14, false, [f14], true);
builder.addFunctionTableInit(1, false, [call.index], true); builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule(); var mod1 = builder.toModule();
} }
...@@ -135,10 +135,10 @@ function addConstFuncUsingGlobal(builder, val) { ...@@ -135,10 +135,10 @@ function addConstFuncUsingGlobal(builder, val) {
]) ])
.exportAs("call"); .exportAs("call");
let f28 = addConstFuncUsingGlobal(builder, 28); let f28 = addConstFuncUsingGlobal(builder, 28);
builder.addFunctionTableInit(18, false, [f18], true); builder.addElementSegment(18, false, [f18], true);
builder.addFunctionTableInit(22, false, [f22], true); builder.addElementSegment(22, false, [f22], true);
builder.addFunctionTableInit(28, false, [f28], true); builder.addElementSegment(28, false, [f28], true);
builder.addFunctionTableInit(5, false, [call.index], true); builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule(); var mod2 = builder.toModule();
} }
...@@ -194,8 +194,8 @@ function addConstFuncUsingMemory(builder, val) { ...@@ -194,8 +194,8 @@ function addConstFuncUsingMemory(builder, val) {
let f19 = addConstFuncUsingMemory(builder, 19); let f19 = addConstFuncUsingMemory(builder, 19);
builder.addExport("f13", f13); builder.addExport("f13", f13);
builder.addExport("f19", f19); builder.addExport("f19", f19);
builder.addFunctionTableInit(13, false, [f13], true); builder.addElementSegment(13, false, [f13], true);
builder.addFunctionTableInit(1, false, [call.index], true); builder.addElementSegment(1, false, [call.index], true);
var mod1 = builder.toModule(); var mod1 = builder.toModule();
} }
...@@ -217,10 +217,10 @@ function addConstFuncUsingMemory(builder, val) { ...@@ -217,10 +217,10 @@ function addConstFuncUsingMemory(builder, val) {
]) ])
.exportAs("call"); .exportAs("call");
let f29 = addConstFuncUsingMemory(builder, 29); let f29 = addConstFuncUsingMemory(builder, 29);
builder.addFunctionTableInit(19, false, [f19], true); builder.addElementSegment(19, false, [f19], true);
builder.addFunctionTableInit(23, false, [f23], true); builder.addElementSegment(23, false, [f23], true);
builder.addFunctionTableInit(29, false, [f29], true); builder.addElementSegment(29, false, [f29], true);
builder.addFunctionTableInit(5, false, [call.index], true); builder.addElementSegment(5, false, [call.index], true);
var mod2 = builder.toModule(); var mod2 = builder.toModule();
} }
......
...@@ -169,8 +169,8 @@ function AddFunctions(builder) { ...@@ -169,8 +169,8 @@ function AddFunctions(builder) {
kExprCallIndirect, 0, kTableZero]) // -- kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.setFunctionTableBounds(length, length); builder.setTableBounds(length, length);
builder.addFunctionTableInit(base, false, [f.add.index, f.sub.index, f.mul.index]); builder.addElementSegment(base, false, [f.add.index, f.sub.index, f.mul.index]);
return builder.instantiate(); return builder.instantiate();
} }
...@@ -206,9 +206,9 @@ function AddFunctions(builder) { ...@@ -206,9 +206,9 @@ function AddFunctions(builder) {
kExprCallIndirect, 0, kTableZero]) // -- kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.setFunctionTableBounds(10, 10); builder.setTableBounds(10, 10);
var g = builder.addImportedGlobal("fff", "base", kWasmI32); var g = builder.addImportedGlobal("fff", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index]); builder.addElementSegment(g, true, [f.mul.index, f.add.index, f.sub.index]);
var module = new WebAssembly.Module(builder.toBuffer()); var module = new WebAssembly.Module(builder.toBuffer());
......
...@@ -49,11 +49,10 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -49,11 +49,10 @@ function js_div(a, b) { return (a / b) | 0; }
f.add.exportAs("blarg"); f.add.exportAs("blarg");
builder.setFunctionTableBounds(10, 10); builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, builder.addElementSegment(
f.sub.index, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
d]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
...@@ -109,11 +108,10 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -109,11 +108,10 @@ function js_div(a, b) { return (a / b) | 0; }
let d = builder.addImport("q", "js_div", kSig_i_ii); let d = builder.addImport("q", "js_div", kSig_i_ii);
let f = AddFunctions(builder); let f = AddFunctions(builder);
builder.setFunctionTableBounds(kTableSize, kTableSize); builder.setTableBounds(kTableSize, kTableSize);
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, builder.addElementSegment(
f.sub.index, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
d]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
let m1 = new WebAssembly.Module(builder.toBuffer()); let m1 = new WebAssembly.Module(builder.toBuffer());
...@@ -182,9 +180,8 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -182,9 +180,8 @@ function js_div(a, b) { return (a / b) | 0; }
builder.addImportedTable("q", "table", kTableSize, kTableSize); builder.addImportedTable("q", "table", kTableSize, kTableSize);
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
let f = AddFunctions(builder); let f = AddFunctions(builder);
builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, builder.addElementSegment(
f.sub.index, g, true, [f.mul.index, f.add.index, f.sub.index, d]);
d]);
builder.addFunction("main", kSig_i_ii) builder.addFunction("main", kSig_i_ii)
.addBody([ .addBody([
kExprI32Const, 55, // -- kExprI32Const, 55, // --
...@@ -260,7 +257,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -260,7 +257,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprGetLocal, 0, kExprGetLocal, 0,
kExprCallIndirect, sig_index, kTableZero]) // -- kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.addFunctionTableInit(g, true, [f.index]); builder.addElementSegment(g, true, [f.index]);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
...@@ -300,8 +297,8 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -300,8 +297,8 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, sig_index1, kTableZero]) // -- kExprCallIndirect, sig_index1, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.setFunctionTableBounds(kTableSize, kTableSize); builder.setTableBounds(kTableSize, kTableSize);
builder.addFunctionTableInit(0, false, [f1.index]); builder.addElementSegment(0, false, [f1.index]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
var m1 = new WebAssembly.Module(builder.toBuffer()); var m1 = new WebAssembly.Module(builder.toBuffer());
...@@ -320,7 +317,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -320,7 +317,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main"); .exportAs("main");
builder.addImportedTable("z", "table", kTableSize, kTableSize); builder.addImportedTable("z", "table", kTableSize, kTableSize);
builder.addFunctionTableInit(1, false, [f2.index], true); builder.addElementSegment(1, false, [f2.index], true);
var m2 = new WebAssembly.Module(builder.toBuffer()); var m2 = new WebAssembly.Module(builder.toBuffer());
...@@ -349,7 +346,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -349,7 +346,7 @@ function js_div(a, b) { return (a / b) | 0; }
for (var impsize = 1; impsize < 4; impsize++) { for (var impsize = 1; impsize < 4; impsize++) {
print(" expsize = " + expsize + ", impsize = " + impsize); print(" expsize = " + expsize + ", impsize = " + impsize);
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.setFunctionTableBounds(expsize, expsize); builder.setTableBounds(expsize, expsize);
builder.addExportOfKind("expfoo", kExternalTable, 0); builder.addExportOfKind("expfoo", kExternalTable, 0);
let m1 = new WebAssembly.Module(builder.toBuffer()); let m1 = new WebAssembly.Module(builder.toBuffer());
...@@ -415,7 +412,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -415,7 +412,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprGetLocal, 0, kExprGetLocal, 0,
kExprCallIndirect, sig_index, kTableZero]) // -- kExprCallIndirect, sig_index, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.addFunctionTableInit(g, true, [g]); builder.addElementSegment(g, true, [g]);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
var instances = []; var instances = [];
...@@ -538,7 +535,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -538,7 +535,7 @@ function js_div(a, b) { return (a / b) | 0; }
for (let num_segments = 1; num_segments < 4; ++num_segments) { for (let num_segments = 1; num_segments < 4; ++num_segments) {
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.setFunctionTableBounds(kTableSize, kTableSize); builder.setTableBounds(kTableSize, kTableSize);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
let f = AddFunctions(builder); let f = AddFunctions(builder);
let indexes = [f.mul.index, f.add.index, f.sub.index]; let indexes = [f.mul.index, f.add.index, f.sub.index];
...@@ -546,7 +543,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -546,7 +543,7 @@ function js_div(a, b) { return (a / b) | 0; }
let offset = i + 1; let offset = i + 1;
let len = i + 2; let len = i + 2;
let index = indexes[i]; let index = indexes[i];
builder.addFunctionTableInit(offset, false, new Array(len).fill(index)); builder.addElementSegment(offset, false, new Array(len).fill(index));
} }
let instance = builder.instantiate(); let instance = builder.instantiate();
...@@ -580,7 +577,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -580,7 +577,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, sig_index, kTableZero kExprCallIndirect, sig_index, kTableZero
]) ])
.exportAs('main'); .exportAs('main');
builder0.setFunctionTableBounds(3, 3); builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable); builder0.addExportOfKind('table', kExternalTable);
let module0 = new WebAssembly.Module(builder0.toBuffer()); let module0 = new WebAssembly.Module(builder0.toBuffer());
let instance0 = new WebAssembly.Instance(module0); let instance0 = new WebAssembly.Instance(module0);
...@@ -590,7 +587,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -590,7 +587,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder1.setName('module_1'); builder1.setName('module_1');
builder1.addFunction('f', kSig_i_i).addBody([kExprGetLocal, 0]); builder1.addFunction('f', kSig_i_i).addBody([kExprGetLocal, 0]);
builder1.addImportedTable('z', 'table'); builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true); builder1.addElementSegment(0, false, [0], true);
let module1 = new WebAssembly.Module(builder1.toBuffer()); let module1 = new WebAssembly.Module(builder1.toBuffer());
let instance1 = let instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}}); new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
...@@ -619,7 +616,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -619,7 +616,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs('main'); .exportAs('main');
builder.addImportedMemory('', 'memory', 1); builder.addImportedMemory('', 'memory', 1);
builder.setFunctionTableBounds(1, 1); builder.setTableBounds(1, 1);
builder.addExportOfKind('table', kExternalTable); builder.addExportOfKind('table', kExternalTable);
let module1 = new WebAssembly.Module(builder.toBuffer()); let module1 = new WebAssembly.Module(builder.toBuffer());
...@@ -628,7 +625,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -628,7 +625,7 @@ function js_div(a, b) { return (a / b) | 0; }
builder = new WasmModuleBuilder(); builder = new WasmModuleBuilder();
builder.addFunction('main', kSig_i_v).addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0]); builder.addFunction('main', kSig_i_v).addBody([kExprI32Const, 0, kExprI32LoadMem, 0, 0]);
builder.addImportedTable('', 'table'); builder.addImportedTable('', 'table');
builder.addFunctionTableInit(0, false, [0], true); builder.addElementSegment(0, false, [0], true);
builder.addImportedMemory('', 'memory', 1); builder.addImportedMemory('', 'memory', 1);
...@@ -734,9 +731,9 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -734,9 +731,9 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, 0, kTableZero]) // -- kExprCallIndirect, 0, kTableZero]) // --
.exportAs("main"); .exportAs("main");
builder.setFunctionTableBounds(10, 10); builder.setTableBounds(10, 10);
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [j, w]); builder.addElementSegment(g, true, [j, w]);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
...@@ -785,7 +782,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -785,7 +782,7 @@ function js_div(a, b) { return (a / b) | 0; }
.exportAs("main"); .exportAs("main");
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, [j, w]); builder.addElementSegment(g, true, [j, w]);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
...@@ -824,7 +821,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -824,7 +821,7 @@ function js_div(a, b) { return (a / b) | 0; }
kExprCallIndirect, 0, kTableZero kExprCallIndirect, 0, kTableZero
]) ])
.exportFunc(); .exportFunc();
builder.addFunctionTableInit(0, false, [0, 1, 1, 0]); builder.addElementSegment(0, false, [0, 1, 1, 0]);
return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}}); return builder.instantiate({q: {f2: i1.exports.f2, f1: i1.exports.f1}});
})(); })();
...@@ -886,7 +883,7 @@ function js_div(a, b) { return (a / b) | 0; } ...@@ -886,7 +883,7 @@ function js_div(a, b) { return (a / b) | 0; }
]) ])
.exportFunc(); .exportFunc();
builder.exportMemoryAs("memory"); builder.exportMemoryAs("memory");
builder.addFunctionTableInit(0, false, [0, 1, 2, 3]); builder.addElementSegment(0, false, [0, 1, 2, 3]);
var instance = builder.instantiate({q: {f1: f100, f2: f200, f3: f300}}); var instance = builder.instantiate({q: {f1: f100, f2: f200, f3: f300}});
setMemI32(instance, 0, 5000000); setMemI32(instance, 0, 5000000);
setMemI32(instance, 4, 6000000); setMemI32(instance, 4, 6000000);
......
...@@ -530,7 +530,7 @@ function checkStack(stack, expected_lines) { ...@@ -530,7 +530,7 @@ function checkStack(stack, expected_lines) {
kExprCallIndirect, sig_index, kTableZero kExprCallIndirect, sig_index, kTableZero
]) // -- ]) // --
.exportAs('main'); .exportAs('main');
builder0.setFunctionTableBounds(3, 3); builder0.setTableBounds(3, 3);
builder0.addExportOfKind('table', kExternalTable); builder0.addExportOfKind('table', kExternalTable);
const module0 = new WebAssembly.Module(builder0.toBuffer()); const module0 = new WebAssembly.Module(builder0.toBuffer());
const instance0 = new WebAssembly.Instance(module0); const instance0 = new WebAssembly.Instance(module0);
...@@ -538,7 +538,7 @@ function checkStack(stack, expected_lines) { ...@@ -538,7 +538,7 @@ function checkStack(stack, expected_lines) {
const builder1 = new WasmModuleBuilder(); const builder1 = new WasmModuleBuilder();
builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]); builder1.addFunction('main', kSig_i_v).addBody([kExprUnreachable]);
builder1.addImportedTable('z', 'table'); builder1.addImportedTable('z', 'table');
builder1.addFunctionTableInit(0, false, [0], true); builder1.addElementSegment(0, false, [0], true);
const module1 = new WebAssembly.Module(builder1.toBuffer()); const module1 = new WebAssembly.Module(builder1.toBuffer());
const instance1 = const instance1 =
new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}}); new WebAssembly.Instance(module1, {z: {table: instance0.exports.table}});
......
...@@ -265,7 +265,7 @@ let exportingModuleBinary2 = (() => { ...@@ -265,7 +265,7 @@ let exportingModuleBinary2 = (() => {
builder.addFunction('foo', kSig_v_v).addBody([]).exportAs('a'); builder.addFunction('foo', kSig_v_v).addBody([]).exportAs('a');
builder.addMemory(1, 1, false); builder.addMemory(1, 1, false);
builder.exportMemoryAs('b'); builder.exportMemoryAs('b');
builder.setFunctionTableBounds(1, 1); builder.setTableBounds(1, 1);
builder.addExportOfKind('c', kExternalTable, 0); builder.addExportOfKind('c', kExternalTable, 0);
var o = builder.addGlobal(kWasmI32, false).exportAs('x'); var o = builder.addGlobal(kWasmI32, false).exportAs('x');
return new Int8Array(builder.toBuffer()); return new Int8Array(builder.toBuffer());
......
...@@ -133,7 +133,7 @@ let id = (() => { // identity exported function ...@@ -133,7 +133,7 @@ let id = (() => { // identity exported function
builder.addImportedTable("q", "table", 5, 32); builder.addImportedTable("q", "table", 5, 32);
let g = builder.addImportedGlobal("q", "base", kWasmI32); let g = builder.addImportedGlobal("q", "base", kWasmI32);
builder.addFunctionTableInit(g, true, builder.addElementSegment(g, true,
[funcs.mul.index, funcs.add.index, funcs.sub.index]); [funcs.mul.index, funcs.add.index, funcs.sub.index]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
...@@ -179,10 +179,10 @@ let id = (() => { // identity exported function ...@@ -179,10 +179,10 @@ let id = (() => { // identity exported function
let i = builder.addImport("q", "exp_inc", sig_i_i); let i = builder.addImport("q", "exp_inc", sig_i_i);
let t = builder.addImport("q", "exp_ten", sig_i_v); let t = builder.addImport("q", "exp_ten", sig_i_v);
builder.setFunctionTableBounds(7, 35); builder.setTableBounds(7, 35);
// builder.addFunctionTableInit(g1, true, // builder.addElementSegment(g1, true,
// [funcs.mul.index, funcs.add.index, funcs.sub.index]); // [funcs.mul.index, funcs.add.index, funcs.sub.index]);
builder.addFunctionTableInit(g1, true, [a, i, t]); builder.addElementSegment(g1, true, [a, i, t]);
builder.addExportOfKind("table", kExternalTable, 0); builder.addExportOfKind("table", kExternalTable, 0);
let module = new WebAssembly.Module(builder.toBuffer()); let module = new WebAssembly.Module(builder.toBuffer());
...@@ -215,7 +215,7 @@ let id = (() => { // identity exported function ...@@ -215,7 +215,7 @@ let id = (() => { // identity exported function
let funcs = addFunctions(builder1); let funcs = addFunctions(builder1);
builder1.addImportedTable("q", "table", 6, 36); builder1.addImportedTable("q", "table", 6, 36);
builder1.addFunctionTableInit(g, true, builder1.addElementSegment(g, true,
[funcs.mul.index, funcs.add.index, funcs.sub.index]); [funcs.mul.index, funcs.add.index, funcs.sub.index]);
let module1 = new WebAssembly.Module(builder1.toBuffer()); let module1 = new WebAssembly.Module(builder1.toBuffer());
...@@ -267,7 +267,7 @@ let id = (() => { // identity exported function ...@@ -267,7 +267,7 @@ let id = (() => { // identity exported function
kExprGetLocal, 0, kExprGetLocal, 0,
kExprCallIndirect, index_i_ii, kTableZero]) kExprCallIndirect, index_i_ii, kTableZero])
.exportAs("main"); .exportAs("main");
builder.addFunctionTableInit(0, false, [0], true); builder.addElementSegment(0, false, [0], true);
return new WebAssembly.Module(builder.toBuffer()); return new WebAssembly.Module(builder.toBuffer());
} }
......
...@@ -171,11 +171,10 @@ class WasmModuleBuilder { ...@@ -171,11 +171,10 @@ class WasmModuleBuilder {
this.globals = []; this.globals = [];
this.exceptions = []; this.exceptions = [];
this.functions = []; this.functions = [];
this.function_table = []; this.table_length_min = 0;
this.function_table_length_min = 0; this.table_length_max = undefined;
this.function_table_length_max = 0; this.element_segments = [];
this.function_table_inits = []; this.data_segments = [];
this.segments = [];
this.explicit = []; this.explicit = [];
this.num_imported_funcs = 0; this.num_imported_funcs = 0;
this.num_imported_globals = 0; this.num_imported_globals = 0;
...@@ -301,24 +300,24 @@ class WasmModuleBuilder { ...@@ -301,24 +300,24 @@ class WasmModuleBuilder {
} }
addDataSegment(addr, data, is_global = false) { addDataSegment(addr, data, is_global = false) {
this.segments.push({addr: addr, data: data, is_global: is_global}); this.data_segments.push({addr: addr, data: data, is_global: is_global});
return this.segments.length - 1; return this.data_segments.length - 1;
} }
exportMemoryAs(name) { exportMemoryAs(name) {
this.exports.push({name: name, kind: kExternalMemory, index: 0}); this.exports.push({name: name, kind: kExternalMemory, index: 0});
} }
addFunctionTableInit(base, is_global, array, is_import = false) { addElementSegment(base, is_global, array, is_import = false) {
this.function_table_inits.push({base: base, is_global: is_global, this.element_segments.push({base: base, is_global: is_global,
array: array}); array: array});
if (!is_global) { if (!is_global) {
var length = base + array.length; var length = base + array.length;
if (length > this.function_table_length_min && !is_import) { if (length > this.table_length_min && !is_import) {
this.function_table_length_min = length; this.table_length_min = length;
} }
if (length > this.function_table_length_max && !is_import) { if (length > this.table_length_max && !is_import) {
this.function_table_length_max = length; this.table_length_max = length;
} }
} }
return this; return this;
...@@ -329,12 +328,18 @@ class WasmModuleBuilder { ...@@ -329,12 +328,18 @@ class WasmModuleBuilder {
if (typeof n != 'number') if (typeof n != 'number')
throw new Error('invalid table (entries have to be numbers): ' + array); throw new Error('invalid table (entries have to be numbers): ' + array);
} }
return this.addFunctionTableInit(this.function_table.length, false, array); return this.addElementSegment(this.table_length_min, false, array);
} }
setFunctionTableBounds(min, max) { setTableBounds(min, max) {
this.function_table_length_min = min; this.table_length_min = min;
this.function_table_length_max = max; this.table_length_max = max;
return this;
}
setTableLength(length) {
this.table_length_min = length;
this.table_length_max = length;
return this; return this;
} }
...@@ -422,17 +427,17 @@ class WasmModuleBuilder { ...@@ -422,17 +427,17 @@ class WasmModuleBuilder {
}); });
} }
// Add function_table. // Add table section
if (wasm.function_table_length_min > 0) { if (wasm.table_length_min > 0) {
if (debug) print("emitting table @ " + binary.length); if (debug) print("emitting table @ " + binary.length);
binary.emit_section(kTableSectionCode, section => { binary.emit_section(kTableSectionCode, section => {
section.emit_u8(1); // one table entry section.emit_u8(1); // one table entry
section.emit_u8(kWasmAnyFunctionTypeForm); section.emit_u8(kWasmAnyFunctionTypeForm);
// TODO(gdeepti): Cleanup to use optional max flag, const max = wasm.table_length_max;
// fix up tests to set correct initial/maximum values const has_max = max !== undefined;
section.emit_u32v(1); section.emit_u8(has_max ? kHasMaximumFlag : 0);
section.emit_u32v(wasm.function_table_length_min); section.emit_u32v(wasm.table_length_min);
section.emit_u32v(wasm.function_table_length_max); if (has_max) section.emit_u32v(max);
}); });
} }
...@@ -546,11 +551,11 @@ class WasmModuleBuilder { ...@@ -546,11 +551,11 @@ class WasmModuleBuilder {
}); });
} }
// Add table elements. // Add element segments
if (wasm.function_table_inits.length > 0) { if (wasm.element_segments.length > 0) {
if (debug) print("emitting table @ " + binary.length); if (debug) print("emitting element segments @ " + binary.length);
binary.emit_section(kElementSectionCode, section => { binary.emit_section(kElementSectionCode, section => {
var inits = wasm.function_table_inits; var inits = wasm.element_segments;
section.emit_u32v(inits.length); section.emit_u32v(inits.length);
for (let init of inits) { for (let init of inits) {
...@@ -618,11 +623,11 @@ class WasmModuleBuilder { ...@@ -618,11 +623,11 @@ class WasmModuleBuilder {
} }
// Add data segments. // Add data segments.
if (wasm.segments.length > 0) { if (wasm.data_segments.length > 0) {
if (debug) print("emitting data segments @ " + binary.length); if (debug) print("emitting data segments @ " + binary.length);
binary.emit_section(kDataSectionCode, section => { binary.emit_section(kDataSectionCode, section => {
section.emit_u32v(wasm.segments.length); section.emit_u32v(wasm.data_segments.length);
for (let seg of wasm.segments) { for (let seg of wasm.data_segments) {
section.emit_u8(0); // linear memory index 0 section.emit_u8(0); // linear memory index 0
if (seg.is_global) { if (seg.is_global) {
// initializer is a global variable // initializer is a global variable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment