@@ -536,6 +536,12 @@ export class Compiler extends DiagnosticEmitter {
536536 // initialize lookup maps, built-ins, imports, exports, etc.
537537 this . program . initialize ( ) ;
538538
539+
540+ // Binaryen treats all function references as being leaked to the outside world when
541+ // the module isn't marked as closed-world (see WebAssembly/binaryen#7135). Therefore,
542+ // we should mark the module as closed-world when we're definitely sure it is.
543+ module . setClosedWorld ( true ) ;
544+
539545 // obtain the main start function
540546 let startFunctionInstance = this . currentFlow . targetFunction ;
541547 assert ( startFunctionInstance . internalName == BuiltinNames . start ) ;
@@ -857,6 +863,7 @@ export class Compiler extends DiagnosticEmitter {
857863 ImportNames . DefaultNamespace ,
858864 ImportNames . Table
859865 ) ;
866+ module . setClosedWorld ( false ) ;
860867 if ( options . pedantic && options . willOptimize ) {
861868 this . pedantic (
862869 DiagnosticCode . Importing_the_table_disables_some_indirect_call_optimizations ,
@@ -866,6 +873,7 @@ export class Compiler extends DiagnosticEmitter {
866873 }
867874 if ( options . exportTable ) {
868875 module . addTableExport ( CommonNames . DefaultTable , ExportNames . Table ) ;
876+ module . setClosedWorld ( false ) ;
869877 if ( options . pedantic && options . willOptimize ) {
870878 this . pedantic (
871879 DiagnosticCode . Exporting_the_table_disables_some_indirect_call_optimizations ,
@@ -977,6 +985,7 @@ export class Compiler extends DiagnosticEmitter {
977985 }
978986 }
979987 }
988+ if ( functionInstance . signature . returnType . kind == TypeKind . Func ) this . module . setClosedWorld ( false ) ;
980989 }
981990 return ;
982991 }
@@ -1007,6 +1016,7 @@ export class Compiler extends DiagnosticEmitter {
10071016 this . desiresExportRuntime = true ;
10081017 }
10091018 }
1019+ if ( global . type . kind == TypeKind . Func ) this . module . setClosedWorld ( false ) ;
10101020 }
10111021 if ( global . type == Type . v128 ) {
10121022 this . warning (
@@ -4975,8 +4985,7 @@ export class Compiler extends DiagnosticEmitter {
49754985 return module . select (
49764986 module . i32 ( 1 ) ,
49774987 module . binary ( BinaryOp . EqI32 , rightExpr , module . i32 ( 0 ) ) ,
4978- leftExpr ,
4979- TypeRef . I32
4988+ leftExpr
49804989 ) ;
49814990 }
49824991 case TypeKind . I8 :
0 commit comments