Skip to content

Commit 104d52a

Browse files
authored
wasm2js: Emit table in a way that is friendly to emscripten minification (#2102)
Set it to a local in the asmFunc scope, so that minifiers can easily see it's a simple local value (instead of using it as an upvar from the parameters higher up, which was how the emscripten glue was emitting it).
1 parent b4eb90c commit 104d52a

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/wasm2js.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,14 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
354354
ValueBuilder::makeDot(ValueBuilder::makeName(ENV),
355355
ValueBuilder::makeName("memory")));
356356
}
357+
// for emscripten, add a table import - otherwise we would have
358+
// FUNCTION_TABLE be an upvar, and not as easy to be minified.
359+
if (flags.emscripten && wasm->table.exists && wasm->table.imported()) {
360+
Ref theVar = ValueBuilder::makeVar();
361+
asmFunc[3]->push_back(theVar);
362+
ValueBuilder::appendToVar(
363+
theVar, FUNCTION_TABLE, ValueBuilder::makeName("wasmTable"));
364+
}
357365
// create heaps, etc
358366
addBasics(asmFunc[3]);
359367
ModuleUtils::iterImportedFunctions(
@@ -1781,8 +1789,7 @@ void Wasm2JSGlue::emitPre() {
17811789
}
17821790

17831791
void Wasm2JSGlue::emitPreEmscripten() {
1784-
out
1785-
<< "function instantiate(asmLibraryArg, wasmMemory, FUNCTION_TABLE) {\n\n";
1792+
out << "function instantiate(asmLibraryArg, wasmMemory, wasmTable) {\n\n";
17861793
}
17871794

17881795
void Wasm2JSGlue::emitPreES6() {

test/wasm2js/emscripten.2asm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
function instantiate(asmLibraryArg, wasmMemory, FUNCTION_TABLE) {
1+
function instantiate(asmLibraryArg, wasmMemory, wasmTable) {
22

33
function asmFunc(global, env, buffer) {
44
"almost asm";
55
var memory = env.memory;
6+
var FUNCTION_TABLE = wasmTable;
67
var HEAP8 = new global.Int8Array(buffer);
78
var HEAP16 = new global.Int16Array(buffer);
89
var HEAP32 = new global.Int32Array(buffer);

test/wasm2js/emscripten.2asm.js.opt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
function instantiate(asmLibraryArg, wasmMemory, FUNCTION_TABLE) {
1+
function instantiate(asmLibraryArg, wasmMemory, wasmTable) {
22

33
function asmFunc(global, env, buffer) {
44
"almost asm";
55
var memory = env.memory;
6+
var FUNCTION_TABLE = wasmTable;
67
var HEAP8 = new global.Int8Array(buffer);
78
var HEAP16 = new global.Int16Array(buffer);
89
var HEAP32 = new global.Int32Array(buffer);

0 commit comments

Comments
 (0)