Skip to content

Commit 50797ee

Browse files
authored
Expose setArgumentsLength only when required (AssemblyScript#1267)
1 parent 2641ecc commit 50797ee

39 files changed

+31
-206
lines changed

src/compiler.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/compiler/assert-nonnull.optimized.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(module
22
(type $i32_=>_i32 (func (param i32) (result i32)))
33
(type $none_=>_i32 (func (result i32)))
4-
(type $i32_=>_none (func (param i32)))
54
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
65
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
76
(memory $0 1)
@@ -10,7 +9,6 @@
109
(data (i32.const 1152) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
1110
(data (i32.const 1200) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
1211
(table $0 1 funcref)
13-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1412
(export "memory" (memory $0))
1513
(export "testVar" (func $assert-nonnull/testVar))
1614
(export "testObj" (func $assert-nonnull/testObj))
@@ -188,9 +186,6 @@
188186
call $~lib/builtins/abort
189187
unreachable
190188
)
191-
(func $~setArgumentsLength (param $0 i32)
192-
nop
193-
)
194189
(func $assert-nonnull/testFn (param $0 i32) (result i32)
195190
local.get $0
196191
call_indirect (type $none_=>_i32)

tests/compiler/assert-nonnull.untouched.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
(data (i32.const 192) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00")
1313
(table $0 1 funcref)
1414
(global $~argumentsLength (mut i32) (i32.const 0))
15-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1615
(export "memory" (memory $0))
1716
(export "testVar" (func $assert-nonnull/testVar))
1817
(export "testObj" (func $assert-nonnull/testObj))
@@ -336,10 +335,6 @@
336335
call $~lib/rt/stub/__release
337336
local.get $2
338337
)
339-
(func $~setArgumentsLength (param $0 i32)
340-
local.get $0
341-
global.set $~argumentsLength
342-
)
343338
(func $assert-nonnull/testFn (param $0 i32) (result i32)
344339
(local $1 i32)
345340
i32.const 0
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
(module
2-
(type $i32_=>_none (func (param i32)))
32
(memory $0 1)
43
(data (i32.const 1024) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00c\00a\00l\00l\00-\00i\00n\00f\00e\00r\00r\00e\00d\00.\00t\00s")
5-
(export "__setArgumentsLength" (func $~setArgumentsLength))
64
(export "memory" (memory $0))
7-
(func $~setArgumentsLength (param $0 i32)
8-
nop
9-
)
105
)

tests/compiler/call-inferred.untouched.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(module
22
(type $f32_=>_f32 (func (param f32) (result f32)))
33
(type $none_=>_none (func))
4-
(type $i32_=>_none (func (param i32)))
54
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
65
(type $i32_=>_i32 (func (param i32) (result i32)))
76
(type $f64_=>_f64 (func (param f64) (result f64)))
@@ -10,7 +9,6 @@
109
(data (i32.const 16) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00c\00a\00l\00l\00-\00i\00n\00f\00e\00r\00r\00e\00d\00.\00t\00s\00")
1110
(table $0 1 funcref)
1211
(global $~argumentsLength (mut i32) (i32.const 0))
13-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1412
(export "memory" (memory $0))
1513
(start $~start)
1614
(func $call-inferred/foo<i32> (param $0 i32) (result i32)
@@ -40,10 +38,6 @@
4038
local.get $0
4139
call $call-inferred/bar<f32>
4240
)
43-
(func $~setArgumentsLength (param $0 i32)
44-
local.get $0
45-
global.set $~argumentsLength
46-
)
4741
(func $start:call-inferred
4842
i32.const 42
4943
call $call-inferred/foo<i32>

tests/compiler/call-optional.optimized.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
(module
22
(type $none_=>_none (func))
33
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
4-
(type $i32_=>_none (func (param i32)))
54
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
65
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
76
(memory $0 1)
87
(data (i32.const 1024) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00c\00a\00l\00l\00-\00o\00p\00t\00i\00o\00n\00a\00l\00.\00t\00s")
98
(global $~argumentsLength (mut i32) (i32.const 0))
10-
(export "__setArgumentsLength" (func $~setArgumentsLength))
119
(export "memory" (memory $0))
1210
(start $~start)
1311
(func $call-optional/opt (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
@@ -40,10 +38,6 @@
4038
local.get $2
4139
call $call-optional/opt
4240
)
43-
(func $~setArgumentsLength (param $0 i32)
44-
local.get $0
45-
global.set $~argumentsLength
46-
)
4741
(func $start:call-optional
4842
i32.const 1
4943
global.set $~argumentsLength

tests/compiler/call-optional.untouched.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(module
22
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
33
(type $none_=>_none (func))
4-
(type $i32_=>_none (func (param i32)))
54
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
65
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
76
(memory $0 1)
@@ -10,7 +9,6 @@
109
(elem (i32.const 1) $call-optional/opt@varargs)
1110
(global $~argumentsLength (mut i32) (i32.const 0))
1211
(global $call-optional/optIndirect (mut i32) (i32.const 1))
13-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1412
(export "memory" (memory $0))
1513
(start $~start)
1614
(func $call-optional/opt (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
@@ -43,10 +41,6 @@
4341
local.get $2
4442
call $call-optional/opt
4543
)
46-
(func $~setArgumentsLength (param $0 i32)
47-
local.get $0
48-
global.set $~argumentsLength
49-
)
5044
(func $start:call-optional
5145
i32.const 3
5246
i32.const 0
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
(module
2-
(type $i32_=>_none (func (param i32)))
32
(memory $0 1)
43
(data (i32.const 1024) "0\00\00\00\01\00\00\00\01\00\00\000\00\00\00c\00l\00a\00s\00s\00-\00s\00t\00a\00t\00i\00c\00-\00f\00u\00n\00c\00t\00i\00o\00n\00.\00t\00s")
5-
(export "__setArgumentsLength" (func $~setArgumentsLength))
64
(export "memory" (memory $0))
7-
(func $~setArgumentsLength (param $0 i32)
8-
nop
9-
)
105
)

tests/compiler/class-static-function.untouched.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(module
22
(type $none_=>_none (func))
33
(type $none_=>_i32 (func (result i32)))
4-
(type $i32_=>_none (func (param i32)))
54
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
65
(type $i32_=>_i32 (func (param i32) (result i32)))
76
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
@@ -10,16 +9,11 @@
109
(table $0 2 funcref)
1110
(elem (i32.const 1) $class-static-function/Example.staticFunc)
1211
(global $~argumentsLength (mut i32) (i32.const 0))
13-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1412
(export "memory" (memory $0))
1513
(start $~start)
1614
(func $class-static-function/Example.staticFunc (result i32)
1715
i32.const 42
1816
)
19-
(func $~setArgumentsLength (param $0 i32)
20-
local.get $0
21-
global.set $~argumentsLength
22-
)
2317
(func $class-static-function/call (param $0 i32) (result i32)
2418
i32.const 0
2519
global.set $~argumentsLength

tests/compiler/exports.optimized.wat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
(global $exports/vehicles.Car i32 (i32.const 4))
2020
(export "memory" (memory $0))
2121
(export "add" (func $exports/add))
22-
(export "__setArgumentsLength" (func $~setArgumentsLength))
2322
(export "subOpt" (func $exports/subOpt@varargs))
2423
(export "math.sub" (func $exports/subOpt))
2524
(export "Animal.CAT" (global $exports/Animal.CAT))
@@ -45,6 +44,7 @@
4544
(export "vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES))
4645
(export "vehicles.Car.getNumTires" (func $exports/Car.getNumTires))
4746
(export "outer.inner.a" (global $exports/outer.inner.a))
47+
(export "__setArgumentsLength" (func $~setArgumentsLength))
4848
(start $~start)
4949
(func $exports/add (param $0 i32) (param $1 i32) (result i32)
5050
local.get $0
@@ -159,10 +159,6 @@
159159
local.get $1
160160
i32.sub
161161
)
162-
(func $~setArgumentsLength (param $0 i32)
163-
local.get $0
164-
global.set $~argumentsLength
165-
)
166162
(func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
167163
block $1of1
168164
block $0of1
@@ -217,4 +213,8 @@
217213
i32.store
218214
local.get $0
219215
)
216+
(func $~setArgumentsLength (param $0 i32)
217+
local.get $0
218+
global.set $~argumentsLength
219+
)
220220
)

tests/compiler/exports.untouched.wat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
(global $exports/vehicles.Car i32 (i32.const 4))
2323
(export "memory" (memory $0))
2424
(export "add" (func $exports/add))
25-
(export "__setArgumentsLength" (func $~setArgumentsLength))
2625
(export "subOpt" (func $exports/subOpt@varargs))
2726
(export "math.sub" (func $exports/math.sub))
2827
(export "Animal.CAT" (global $exports/Animal.CAT))
@@ -48,6 +47,7 @@
4847
(export "vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES))
4948
(export "vehicles.Car.getNumTires" (func $exports/vehicles.Car.getNumTires))
5049
(export "outer.inner.a" (global $exports/outer.inner.a))
50+
(export "__setArgumentsLength" (func $~setArgumentsLength))
5151
(start $~start)
5252
(func $exports/add (param $0 i32) (param $1 i32) (result i32)
5353
local.get $0
@@ -290,10 +290,6 @@
290290
local.get $1
291291
call $exports/subOpt
292292
)
293-
(func $~setArgumentsLength (param $0 i32)
294-
local.get $0
295-
global.set $~argumentsLength
296-
)
297293
(func $exports/Car#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
298294
block $1of1
299295
block $0of1
@@ -326,4 +322,8 @@
326322
local.get $1
327323
call $exports/vehicles.Car#constructor
328324
)
325+
(func $~setArgumentsLength (param $0 i32)
326+
local.get $0
327+
global.set $~argumentsLength
328+
)
329329
)
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
(module
2-
(type $i32_=>_none (func (param i32)))
32
(memory $0 1)
43
(data (i32.const 1024) ",\00\00\00\01\00\00\00\01\00\00\00,\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00e\00x\00p\00r\00e\00s\00s\00i\00o\00n\00.\00t\00s")
5-
(export "__setArgumentsLength" (func $~setArgumentsLength))
64
(export "memory" (memory $0))
7-
(func $~setArgumentsLength (param $0 i32)
8-
nop
9-
)
105
)

tests/compiler/function-expression.untouched.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
(type $none_=>_i32 (func (result i32)))
44
(type $i32_=>_i32 (func (param i32) (result i32)))
55
(type $none_=>_none (func))
6-
(type $i32_=>_none (func (param i32)))
76
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
87
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
98
(memory $0 1)
@@ -15,16 +14,11 @@
1514
(global $function-expression/f2 (mut i32) (i32.const 2))
1615
(global $function-expression/f3 (mut i32) (i32.const 3))
1716
(global $function-expression/f4 (mut i32) (i32.const 4))
18-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1917
(export "memory" (memory $0))
2018
(start $~start)
2119
(func $start:function-expression~anonymous|0 (param $0 i32) (result i32)
2220
local.get $0
2321
)
24-
(func $~setArgumentsLength (param $0 i32)
25-
local.get $0
26-
global.set $~argumentsLength
27-
)
2822
(func $start:function-expression~anonymous|1 (param $0 i32) (result i32)
2923
local.get $0
3024
)
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
(module
2-
(type $i32_=>_none (func (param i32)))
32
(memory $0 1)
43
(data (i32.const 1024) "\"\00\00\00\01\00\00\00\01\00\00\00\"\00\00\00f\00u\00n\00c\00t\00i\00o\00n\00-\00t\00y\00p\00e\00s\00.\00t\00s")
5-
(export "__setArgumentsLength" (func $~setArgumentsLength))
64
(export "memory" (memory $0))
7-
(func $~setArgumentsLength (param $0 i32)
8-
nop
9-
)
105
)

tests/compiler/function-types.untouched.wat

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
(type $none_=>_none (func))
66
(type $i64_i64_=>_i64 (func (param i64 i64) (result i64)))
77
(type $f64_f64_=>_f64 (func (param f64 f64) (result f64)))
8-
(type $i32_=>_none (func (param i32)))
98
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
109
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
1110
(memory $0 1)
@@ -15,7 +14,6 @@
1514
(global $function-types/i32Adder (mut i32) (i32.const 0))
1615
(global $~argumentsLength (mut i32) (i32.const 0))
1716
(global $function-types/i64Adder (mut i32) (i32.const 0))
18-
(export "__setArgumentsLength" (func $~setArgumentsLength))
1917
(export "memory" (memory $0))
2018
(start $~start)
2119
(func $function-types/makeAdder<i32>~anonymous|0 (param $0 i32) (param $1 i32) (result i32)
@@ -26,10 +24,6 @@
2624
(func $function-types/makeAdder<i32> (result i32)
2725
i32.const 1
2826
)
29-
(func $~setArgumentsLength (param $0 i32)
30-
local.get $0
31-
global.set $~argumentsLength
32-
)
3327
(func $function-types/makeAdder<i64>~anonymous|0 (param $0 i64) (param $1 i64) (result i64)
3428
local.get $0
3529
local.get $1

tests/compiler/getter-call.optimized.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
(module
22
(type $none_=>_none (func))
3-
(type $i32_=>_none (func (param i32)))
43
(type $none_=>_i32 (func (result i32)))
54
(memory $0 0)
65
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
7-
(export "__setArgumentsLength" (func $~setArgumentsLength))
86
(export "memory" (memory $0))
97
(export "test" (func $getter-call/test))
108
(start $~start)
11-
(func $~setArgumentsLength (param $0 i32)
12-
nop
13-
)
149
(func $getter-call/test (result i32)
1510
(local $0 i32)
1611
(local $1 i32)

0 commit comments

Comments
 (0)