@@ -294,7 +294,9 @@ export const enum RuntimeFeatures {
294294 /** Requires the built-in globals visitor. */
295295 visitGlobals = 1 << 2 ,
296296 /** Requires the built-in members visitor. */
297- visitMembers = 1 << 3
297+ visitMembers = 1 << 3 ,
298+ /** Requires the setArgumentsLength export. */
299+ setArgumentsLength = 1 << 4
298300}
299301
300302/** Exported names of compiler-generated elements. */
@@ -602,6 +604,14 @@ export class Compiler extends DiagnosticEmitter {
602604 let file = unchecked ( _values [ i ] ) ;
603605 if ( file . source . sourceKind == SourceKind . USER_ENTRY ) this . ensureModuleExports ( file ) ;
604606 }
607+
608+ // expose the arguments length helper if there are varargs exports
609+ if ( this . runtimeFeatures & RuntimeFeatures . setArgumentsLength ) {
610+ module . addFunction ( BuiltinNames . setArgumentsLength , NativeType . I32 , NativeType . None , null ,
611+ module . global_set ( BuiltinNames . argumentsLength , module . local_get ( 0 , NativeType . I32 ) )
612+ ) ;
613+ module . addFunctionExport ( BuiltinNames . setArgumentsLength , ExportNames . setArgumentsLength ) ;
614+ }
605615 return module ;
606616 }
607617
@@ -703,6 +713,7 @@ export class Compiler extends DiagnosticEmitter {
703713 // utilize varargs stub to fill in omitted arguments
704714 functionInstance = this . ensureVarargsStub ( functionInstance ) ;
705715 this . ensureArgumentsLength ( ) ;
716+ this . runtimeFeatures |= RuntimeFeatures . setArgumentsLength ;
706717 }
707718 if ( functionInstance . is ( CommonFlags . COMPILED ) ) this . module . addFunctionExport ( functionInstance . internalName , prefix + name ) ;
708719 }
@@ -6849,15 +6860,6 @@ export class Compiler extends DiagnosticEmitter {
68496860 if ( ! this . builtinArgumentsLength ) {
68506861 let module = this . module ;
68516862 this . builtinArgumentsLength = module . addGlobal ( BuiltinNames . argumentsLength , NativeType . I32 , true , module . i32 ( 0 ) ) ;
6852- // TODO: Enable this once mutable globals are the default nearly everywhere.
6853- // if (this.options.hasFeature(Feature.MUTABLE_GLOBALS)) {
6854- // module.addGlobalExport(BuiltinNames.argumentsLength, ExportNames.argumentsLength);
6855- // } else {
6856- module . addFunction ( BuiltinNames . setArgumentsLength , NativeType . I32 , NativeType . None , null ,
6857- module . global_set ( BuiltinNames . argumentsLength , module . local_get ( 0 , NativeType . I32 ) )
6858- ) ;
6859- module . addFunctionExport ( BuiltinNames . setArgumentsLength , ExportNames . setArgumentsLength ) ;
6860- // }
68616863 }
68626864 }
68636865
0 commit comments