Skip to content

Commit 1cdee67

Browse files
committed
simplify init table
1 parent a506ecb commit 1cdee67

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/builtins.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3812,7 +3812,6 @@ function builtin_call_indirect(ctx: BuiltinFunctionContext): ExpressionRef {
38123812
paramTypeRefs[i] = compiler.currentType.toRef();
38133813
}
38143814
compiler.currentType = returnType;
3815-
compiler.ensureFunctionTable();
38163815
return module.call_indirect(null /* TODO */, indexArg, operandExprs, createType(paramTypeRefs), returnType.toRef());
38173816
}
38183817
builtinFunctions.set(BuiltinNames.call_indirect, builtin_call_indirect);

src/compiler.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,6 @@ export class Compiler extends DiagnosticEmitter {
458458
staticGcObjectOffsets: Map<i32, Set<i32>> = new Map();
459459
/** Function table being compiled. First elem is blank. */
460460
functionTable: Function[] = [];
461-
/** Whether the default function table has been declared in the module yet. */
462-
functionTableDeclared: bool = false;
463461
/** Arguments length helper global. */
464462
builtinArgumentsLength: GlobalRef = 0;
465463
/** Requires runtime features. */
@@ -879,12 +877,21 @@ export class Compiler extends DiagnosticEmitter {
879877
}
880878
}
881879

882-
/** Ensures the default function table is declared so indirect calls can be built and their effects analyzed. */
883-
ensureFunctionTable(): void {
884-
if (this.functionTableDeclared) return;
885-
this.functionTableDeclared = true;
880+
/** Declares the default function table early so an indirect call's effects can be analyzed before it is populated. */
881+
private ensureFunctionTable(): void {
882+
// imported tables are declared by initDefaultTable. only the local default table
883+
// needs to exist early, since effect analysis of an indirect call looks it up
884+
if (!this.options.importTable) {
885+
this.module.ensureFunctionTable(CommonNames.DefaultTable);
886+
}
887+
}
888+
889+
private initDefaultTable(): void {
886890
let options = this.options;
887891
let module = this.module;
892+
893+
// import and/or export table if requested (default table is named '0' by Binaryen).
894+
// non-imported tables are declared lazily on first indirect call, see ensureFunctionTable
888895
if (options.importTable) {
889896
module.addTableImport(
890897
CommonNames.DefaultTable,
@@ -898,19 +905,6 @@ export class Compiler extends DiagnosticEmitter {
898905
null
899906
);
900907
}
901-
} else {
902-
module.ensureFunctionTable(CommonNames.DefaultTable);
903-
}
904-
}
905-
906-
private initDefaultTable(): void {
907-
let options = this.options;
908-
let module = this.module;
909-
910-
// declare the default table now if imported (default table is named '0' by Binaryen).
911-
// non-imported tables are declared lazily on first indirect call, see ensureFunctionTable
912-
if (options.importTable) {
913-
this.ensureFunctionTable();
914908
}
915909
if (options.exportTable) {
916910
module.addTableExport(CommonNames.DefaultTable, ExportNames.Table);

0 commit comments

Comments
 (0)