@@ -700,7 +700,7 @@ export class Compiler extends DiagnosticEmitter {
700
700
// expose the arguments length helper if there are varargs exports
701
701
if ( this . runtimeFeatures & RuntimeFeatures . setArgumentsLength ) {
702
702
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 ) )
704
704
) ;
705
705
module . addFunctionExport ( BuiltinNames . setArgumentsLength , ExportNames . setArgumentsLength ) ;
706
706
}
@@ -863,7 +863,6 @@ export class Compiler extends DiagnosticEmitter {
863
863
if ( signature . requiredParameters < signature . parameterTypes . length ) {
864
864
// utilize varargs stub to fill in omitted arguments
865
865
functionInstance = this . ensureVarargsStub ( functionInstance ) ;
866
- this . ensureArgumentsLength ( ) ;
867
866
this . runtimeFeatures |= RuntimeFeatures . setArgumentsLength ;
868
867
}
869
868
if ( functionInstance . is ( CommonFlags . COMPILED ) ) {
@@ -6653,11 +6652,13 @@ export class Compiler extends DiagnosticEmitter {
6653
6652
}
6654
6653
6655
6654
/** Makes sure that the arguments length helper global is present. */
6656
- ensureArgumentsLength ( ) : void {
6655
+ ensureArgumentsLength ( ) : string {
6656
+ var name = BuiltinNames . argumentsLength ;
6657
6657
if ( ! this . builtinArgumentsLength ) {
6658
6658
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 ) ) ;
6660
6660
}
6661
+ return name ;
6661
6662
}
6662
6663
6663
6664
/** Ensures compilation of the varargs stub for the specified function. */
@@ -6725,17 +6726,18 @@ export class Compiler extends DiagnosticEmitter {
6725
6726
let label = i . toString ( ) + ofN ;
6726
6727
names [ i ] = label ;
6727
6728
}
6729
+ var argumentsLength = this . ensureArgumentsLength ( ) ;
6728
6730
var table = module . block ( names [ 0 ] , [
6729
6731
module . block ( "outOfRange" , [
6730
6732
module . switch ( names , "outOfRange" ,
6731
6733
// condition is number of provided optional arguments, so subtract required arguments
6732
6734
minArguments
6733
6735
? module . binary (
6734
6736
BinaryOp . SubI32 ,
6735
- module . global_get ( BuiltinNames . argumentsLength , NativeType . I32 ) ,
6737
+ module . global_get ( argumentsLength , NativeType . I32 ) ,
6736
6738
module . i32 ( minArguments )
6737
6739
)
6738
- : module . global_get ( BuiltinNames . argumentsLength , NativeType . I32 )
6740
+ : module . global_get ( argumentsLength , NativeType . I32 )
6739
6741
)
6740
6742
] ) ,
6741
6743
module . unreachable ( )
@@ -6912,9 +6914,8 @@ export class Compiler extends DiagnosticEmitter {
6912
6914
let nativeReturnType = overloadSignature . returnType . toNativeType ( ) ;
6913
6915
let stmts = new Array < ExpressionRef > ( ) ;
6914
6916
if ( needsVarargsStub ) {
6915
- this . ensureArgumentsLength ( ) ;
6916
6917
// 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 ) ) ) ;
6918
6919
}
6919
6920
if ( returnType == Type . void ) {
6920
6921
stmts . push (
@@ -7123,7 +7124,7 @@ export class Compiler extends DiagnosticEmitter {
7123
7124
assert ( ! ( getSideEffects ( lastOperand ) & SideEffects . WritesGlobal ) ) ;
7124
7125
let lastOperandType = parameterTypes [ maxArguments - 1 ] ;
7125
7126
operands [ maxOperands - 1 ] = module . block ( null , [
7126
- module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numArguments ) ) ,
7127
+ module . global_set ( this . ensureArgumentsLength ( ) , module . i32 ( numArguments ) ) ,
7127
7128
lastOperand
7128
7129
] , lastOperandType . toNativeType ( ) ) ;
7129
7130
this . operandsTostack ( instance . signature , operands ) ;
@@ -7134,7 +7135,6 @@ export class Compiler extends DiagnosticEmitter {
7134
7135
} else {
7135
7136
this . currentType = returnType ;
7136
7137
}
7137
- this . ensureArgumentsLength ( ) ;
7138
7138
return expr ;
7139
7139
}
7140
7140
}
@@ -7226,20 +7226,20 @@ export class Compiler extends DiagnosticEmitter {
7226
7226
// We might be calling a varargs stub here, even if all operands have been
7227
7227
// provided, so we must set `argumentsLength` in any case. Inject setting it
7228
7228
// into the index argument, which becomes executed last after any operands.
7229
- this . ensureArgumentsLength ( ) ;
7229
+ var argumentsLength = this . ensureArgumentsLength ( ) ;
7230
7230
var nativeSizeType = this . options . nativeSizeType ;
7231
7231
if ( getSideEffects ( functionArg ) & SideEffects . WritesGlobal ) {
7232
7232
let flow = this . currentFlow ;
7233
7233
let temp = flow . getTempLocal ( this . options . usizeType , findUsedLocals ( functionArg ) ) ;
7234
7234
functionArg = module . block ( null , [
7235
7235
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 ) ) ,
7237
7237
module . local_get ( temp . index , nativeSizeType )
7238
7238
] , nativeSizeType ) ;
7239
7239
flow . freeTempLocal ( temp ) ;
7240
7240
} else { // simplify
7241
7241
functionArg = module . block ( null , [
7242
- module . global_set ( BuiltinNames . argumentsLength , module . i32 ( numArguments ) ) ,
7242
+ module . global_set ( argumentsLength , module . i32 ( numArguments ) ) ,
7243
7243
functionArg
7244
7244
] , nativeSizeType ) ;
7245
7245
}
0 commit comments