@@ -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