@@ -700,7 +700,7 @@ export class Compiler extends DiagnosticEmitter {
700700 // expose the arguments length helper if there are varargs exports
701701 if ( this . runtimeFeatures & RuntimeFeatures . setArgumentsLength ) {
702702 module . addFunction ( BuiltinNames . setArgumentsLength , NativeType . I32 , NativeType . None , null ,
703- module . global_set ( BuiltinNames . argumentsLength , module . local_get ( 0 , NativeType . I32 ) )
703+ module . global_set ( this . ensureArgumentsLength ( ) , module . local_get ( 0 , NativeType . I32 ) )
704704 ) ;
705705 module . addFunctionExport ( BuiltinNames . setArgumentsLength , ExportNames . setArgumentsLength ) ;
706706 }
@@ -863,7 +863,6 @@ export class Compiler extends DiagnosticEmitter {
863863 if ( signature . requiredParameters < signature . parameterTypes . length ) {
864864 // utilize varargs stub to fill in omitted arguments
865865 functionInstance = this . ensureVarargsStub ( functionInstance ) ;
866- this . ensureArgumentsLength ( ) ;
867866 this . runtimeFeatures |= RuntimeFeatures . setArgumentsLength ;
868867 }
869868 if ( functionInstance . is ( CommonFlags . COMPILED ) ) {
@@ -6653,11 +6652,13 @@ export class Compiler extends DiagnosticEmitter {
66536652 }
66546653
66556654 /** Makes sure that the arguments length helper global is present. */
6656- ensureArgumentsLength ( ) : void {
6655+ ensureArgumentsLength ( ) : string {
6656+ var name = BuiltinNames . argumentsLength ;
66576657 if ( ! this . builtinArgumentsLength ) {
66586658 let module = this . module ;
6659- this . builtinArgumentsLength = module . addGlobal ( BuiltinNames . argumentsLength , NativeType . I32 , true , module . i32 ( 0 ) ) ;
6659+ this . builtinArgumentsLength = module . addGlobal ( name , NativeType . I32 , true , module . i32 ( 0 ) ) ;
66606660 }
6661+ return name ;
66616662 }
66626663
66636664 /** Ensures compilation of the varargs stub for the specified function. */
@@ -6725,17 +6726,18 @@ export class Compiler extends DiagnosticEmitter {
67256726 let label = i . toString ( ) + ofN ;
67266727 names [ i ] = label ;
67276728 }
6729+ var argumentsLength = this . ensureArgumentsLength ( ) ;
67286730 var table = module . block ( names [ 0 ] , [
67296731 module . block ( "outOfRange" , [
67306732 module . switch ( names , "outOfRange" ,
67316733 // condition is number of provided optional arguments, so subtract required arguments
67326734 minArguments
67336735 ? module . binary (
67346736 BinaryOp . SubI32 ,
6735- module . global_get ( BuiltinNames . argumentsLength , NativeType . I32 ) ,
6737+ module . global_get ( argumentsLength , NativeType . I32 ) ,
67366738 module . i32 ( minArguments )
67376739 )
6738- : module . global_get ( BuiltinNames . argumentsLength , NativeType . I32 )
6740+ : module . global_get ( argumentsLength , NativeType . I32 )
67396741 )
67406742 ] ) ,
67416743 module . unreachable ( )
@@ -6912,9 +6914,8 @@ export class Compiler extends DiagnosticEmitter {
69126914 let nativeReturnType = overloadSignature . returnType . toNativeType ( ) ;
69136915 let stmts = new Array < ExpressionRef > ( ) ;
69146916 if ( needsVarargsStub ) {
6915- this . ensureArgumentsLength ( ) ;
69166917 // Safe to prepend since paramExprs are local.get's
6917- stmts . push ( module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numParameters ) ) ) ;
6918+ stmts . push ( module . global_set ( this . ensureArgumentsLength ( ) , module . i32 ( numParameters ) ) ) ;
69186919 }
69196920 if ( returnType == Type . void ) {
69206921 stmts . push (
@@ -7123,7 +7124,7 @@ export class Compiler extends DiagnosticEmitter {
71237124 assert ( ! ( getSideEffects ( lastOperand ) & SideEffects . WritesGlobal ) ) ;
71247125 let lastOperandType = parameterTypes [ maxArguments - 1 ] ;
71257126 operands [ maxOperands - 1 ] = module . block ( null , [
7126- module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numArguments ) ) ,
7127+ module . global_set ( this . ensureArgumentsLength ( ) , module . i32 ( numArguments ) ) ,
71277128 lastOperand
71287129 ] , lastOperandType . toNativeType ( ) ) ;
71297130 this . operandsTostack ( instance . signature , operands ) ;
@@ -7134,7 +7135,6 @@ export class Compiler extends DiagnosticEmitter {
71347135 } else {
71357136 this . currentType = returnType ;
71367137 }
7137- this . ensureArgumentsLength ( ) ;
71387138 return expr ;
71397139 }
71407140 }
@@ -7226,20 +7226,20 @@ export class Compiler extends DiagnosticEmitter {
72267226 // We might be calling a varargs stub here, even if all operands have been
72277227 // provided, so we must set `argumentsLength` in any case. Inject setting it
72287228 // into the index argument, which becomes executed last after any operands.
7229- this . ensureArgumentsLength ( ) ;
7229+ var argumentsLength = this . ensureArgumentsLength ( ) ;
72307230 var nativeSizeType = this . options . nativeSizeType ;
72317231 if ( getSideEffects ( functionArg ) & SideEffects . WritesGlobal ) {
72327232 let flow = this . currentFlow ;
72337233 let temp = flow . getTempLocal ( this . options . usizeType , findUsedLocals ( functionArg ) ) ;
72347234 functionArg = module . block ( null , [
72357235 module . local_set ( temp . index , functionArg , true ) , // Function
7236- module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numArguments ) ) ,
7236+ module . global_set ( argumentsLength , module . i32 ( numArguments ) ) ,
72377237 module . local_get ( temp . index , nativeSizeType )
72387238 ] , nativeSizeType ) ;
72397239 flow . freeTempLocal ( temp ) ;
72407240 } else { // simplify
72417241 functionArg = module . block ( null , [
7242- module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numArguments ) ) ,
7242+ module . global_set ( argumentsLength , module . i32 ( numArguments ) ) ,
72437243 functionArg
72447244 ] , nativeSizeType ) ;
72457245 }
0 commit comments