Skip to content

Commit 4b204db

Browse files
authored
fix: Unify ctor generation with and without base ctor (#1590)
1 parent a4f0c3b commit 4b204db

7 files changed

+177
-29
lines changed

src/compiler.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -9242,7 +9242,8 @@ export class Compiler extends DiagnosticEmitter {
92429242
// do not attempt to compile if inlined anyway
92439243
if (!instance.hasDecorator(DecoratorFlags.INLINE)) this.compileFunction(instance);
92449244
} else {
9245-
// clone base constructor if a derived class
9245+
// clone base constructor if a derived class. note that we cannot just
9246+
// call the base ctor since the derived class may have additional fields.
92469247
let baseClass = classInstance.base;
92479248
let contextualTypeArguments = uniqueMap(classInstance.contextualTypeArguments);
92489249
if (baseClass) {
@@ -9276,15 +9277,18 @@ export class Compiler extends DiagnosticEmitter {
92769277
new Signature(this.program, null, classInstance.type, classInstance.type),
92779278
contextualTypeArguments
92789279
);
9279-
let members = classInstance.members;
9280-
if (!members) classInstance.members = members = new Map();
9281-
members.set("constructor", instance.prototype);
92829280
}
92839281

9284-
instance.internalName = classInstance.internalName + INSTANCE_DELIMITER + "constructor";
92859282
instance.set(CommonFlags.COMPILED);
92869283
instance.prototype.setResolvedInstance("", instance);
9284+
if (classInstance.is(CommonFlags.MODULE_EXPORT)) {
9285+
instance.set(CommonFlags.MODULE_EXPORT);
9286+
}
92879287
classInstance.constructorInstance = instance;
9288+
let members = classInstance.members;
9289+
if (!members) classInstance.members = members = new Map();
9290+
members.set("constructor", instance.prototype);
9291+
92889292
let previousFlow = this.currentFlow;
92899293
let flow = instance.flow;
92909294
this.currentFlow = flow;

tests/compiler/class-implements.optimized.wat

+20-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
(export "A#constructor" (func $class-implements/A#constructor))
1313
(export "C" (global $class-implements/C))
1414
(export "C#foo" (func $class-implements/C#foo))
15+
(export "C#constructor" (func $class-implements/C#constructor))
1516
(start $~start)
1617
(func $~lib/rt/stub/__new (param $0 i32) (result i32)
1718
(local $1 i32)
@@ -101,6 +102,22 @@
101102
(func $class-implements/A#foo (param $0 i32) (result i32)
102103
i32.const 1
103104
)
105+
(func $class-implements/C#constructor (param $0 i32) (result i32)
106+
local.get $0
107+
i32.eqz
108+
if
109+
i32.const 5
110+
call $~lib/rt/stub/__new
111+
local.set $0
112+
end
113+
local.get $0
114+
if (result i32)
115+
local.get $0
116+
else
117+
i32.const 6
118+
call $~lib/rt/stub/__new
119+
end
120+
)
104121
(func $class-implements/C#foo (param $0 i32) (result i32)
105122
i32.const 2
106123
)
@@ -110,13 +127,8 @@
110127
i32.const 3
111128
call $~lib/rt/stub/__new
112129
drop
113-
i32.const 5
114-
call $~lib/rt/stub/__new
115-
i32.eqz
116-
if
117-
i32.const 6
118-
call $~lib/rt/stub/__new
119-
drop
120-
end
130+
i32.const 0
131+
call $class-implements/C#constructor
132+
drop
121133
)
122134
)

tests/compiler/class-implements.untouched.wat

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(export "A#constructor" (func $class-implements/A#constructor))
2222
(export "C" (global $class-implements/C))
2323
(export "C#foo" (func $class-implements/C#foo))
24+
(export "C#constructor" (func $class-implements/C#constructor))
2425
(start $~start)
2526
(func $~lib/rt/stub/computeSize (param $0 i32) (result i32)
2627
local.get $0

tests/compiler/extends-recursive.optimized.wat

+105-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,112 @@
11
(module
2-
(type $i32_i32_=>_none (func (param i32 i32)))
32
(type $i32_=>_i32 (func (param i32) (result i32)))
3+
(type $none_=>_none (func))
4+
(type $i32_i32_=>_none (func (param i32 i32)))
45
(memory $0 0)
6+
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
57
(global $extends-recursive/Child i32 (i32.const 3))
68
(export "memory" (memory $0))
79
(export "Child" (global $extends-recursive/Child))
810
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
911
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
12+
(export "Child#constructor" (func $extends-recursive/Child#constructor))
13+
(start $~start)
14+
(func $~lib/rt/stub/__new (param $0 i32) (result i32)
15+
(local $1 i32)
16+
(local $2 i32)
17+
(local $3 i32)
18+
(local $4 i32)
19+
(local $5 i32)
20+
global.get $~lib/rt/stub/offset
21+
global.get $~lib/rt/stub/offset
22+
i32.const 4
23+
i32.add
24+
local.tee $3
25+
i32.const 28
26+
i32.add
27+
local.tee $1
28+
memory.size
29+
local.tee $4
30+
i32.const 16
31+
i32.shl
32+
i32.const 15
33+
i32.add
34+
i32.const -16
35+
i32.and
36+
local.tee $2
37+
i32.gt_u
38+
if
39+
local.get $4
40+
local.get $1
41+
local.get $2
42+
i32.sub
43+
i32.const 65535
44+
i32.add
45+
i32.const -65536
46+
i32.and
47+
i32.const 16
48+
i32.shr_u
49+
local.tee $2
50+
local.get $2
51+
local.get $4
52+
i32.lt_s
53+
select
54+
memory.grow
55+
i32.const 0
56+
i32.lt_s
57+
if
58+
local.get $2
59+
memory.grow
60+
i32.const 0
61+
i32.lt_s
62+
if
63+
unreachable
64+
end
65+
end
66+
end
67+
local.get $1
68+
global.set $~lib/rt/stub/offset
69+
i32.const 28
70+
i32.store
71+
local.get $3
72+
i32.const 4
73+
i32.sub
74+
local.tee $1
75+
i32.const 0
76+
i32.store offset=4
77+
local.get $1
78+
i32.const 0
79+
i32.store offset=8
80+
local.get $1
81+
local.get $0
82+
i32.store offset=12
83+
local.get $1
84+
i32.const 4
85+
i32.store offset=16
86+
local.get $3
87+
i32.const 16
88+
i32.add
89+
)
90+
(func $extends-recursive/Child#constructor (param $0 i32) (result i32)
91+
local.get $0
92+
i32.eqz
93+
if
94+
i32.const 3
95+
call $~lib/rt/stub/__new
96+
local.set $0
97+
end
98+
local.get $0
99+
i32.eqz
100+
if
101+
i32.const 4
102+
call $~lib/rt/stub/__new
103+
local.set $0
104+
end
105+
local.get $0
106+
i32.const 0
107+
i32.store
108+
local.get $0
109+
)
10110
(func $extends-recursive/Parent#get:child (param $0 i32) (result i32)
11111
local.get $0
12112
i32.load
@@ -19,4 +119,8 @@
19119
local.get $1
20120
i32.store
21121
)
122+
(func $~start
123+
i32.const 1036
124+
global.set $~lib/rt/stub/offset
125+
)
22126
)

tests/compiler/extends-recursive.untouched.wat

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(export "Child" (global $extends-recursive/Child))
1515
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
1616
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
17+
(export "Child#constructor" (func $extends-recursive/Child#constructor))
1718
(start $~start)
1819
(func $~lib/rt/stub/computeSize (param $0 i32) (result i32)
1920
local.get $0

tests/compiler/std/array.optimized.wat

+32-6
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@
428428
(global $std/array/ArrayStr i32 (i32.const 42))
429429
(export "memory" (memory $0))
430430
(export "ArrayU32" (global $std/array/ArrayU32))
431-
(export "ArrayU32#constructor" (func $~lib/array/Array<u32>#constructor@varargs))
431+
(export "ArrayU32#constructor" (func $std/array/ArrayU32#constructor@varargs))
432432
(export "ArrayU32#get:length" (func $~lib/array/Array<u8>#get:length))
433433
(export "ArrayU32#set:length" (func $~lib/array/Array<u32>#set:length))
434434
(export "ArrayU32#every" (func $~lib/array/Array<i32>#every))
@@ -454,7 +454,7 @@
454454
(export "ArrayU32#flat" (func $~lib/array/Array<u32>#flat))
455455
(export "ArrayU32#toString" (func $~lib/array/Array<u32>#toString))
456456
(export "ArrayU8" (global $std/array/ArrayU8))
457-
(export "ArrayU8#constructor" (func $~lib/array/Array<u8>#constructor@varargs))
457+
(export "ArrayU8#constructor" (func $std/array/ArrayU8#constructor@varargs))
458458
(export "ArrayU8#get:length" (func $~lib/array/Array<u8>#get:length))
459459
(export "ArrayU8#set:length" (func $~lib/array/Array<u8>#set:length))
460460
(export "ArrayU8#every" (func $~lib/array/Array<u8>#every))
@@ -480,7 +480,7 @@
480480
(export "ArrayU8#flat" (func $~lib/array/Array<u32>#flat))
481481
(export "ArrayU8#toString" (func $~lib/array/Array<u8>#toString))
482482
(export "ArrayStr" (global $std/array/ArrayStr))
483-
(export "ArrayStr#constructor" (func $~lib/array/Array<~lib/string/String>#constructor@varargs))
483+
(export "ArrayStr#constructor" (func $std/array/ArrayStr#constructor@varargs))
484484
(export "ArrayStr#get:length" (func $~lib/array/Array<u8>#get:length))
485485
(export "ArrayStr#set:length" (func $~lib/array/Array<std/array/Ref>#set:length))
486486
(export "ArrayStr#every" (func $~lib/array/Array<i32>#every))
@@ -19815,7 +19815,7 @@
1981519815
i32.load
1981619816
call $~lib/rt/pure/__visit
1981719817
)
19818-
(func $~lib/array/Array<u32>#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
19818+
(func $std/array/ArrayU32#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
1981919819
(local $2 i32)
1982019820
(local $3 i32)
1982119821
(local $4 i32)
@@ -19833,6 +19833,15 @@
1983319833
end
1983419834
local.get $0
1983519835
i32.eqz
19836+
if
19837+
i32.const 16
19838+
i32.const 40
19839+
call $~lib/rt/pure/__new
19840+
call $~lib/rt/pure/__retain
19841+
local.set $0
19842+
end
19843+
local.get $0
19844+
i32.eqz
1983619845
if
1983719846
i32.const 16
1983819847
i32.const 7
@@ -20183,7 +20192,7 @@
2018320192
local.get $1
2018420193
call $~lib/array/Array<u32>#join
2018520194
)
20186-
(func $~lib/array/Array<u8>#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
20195+
(func $std/array/ArrayU8#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
2018720196
(local $2 i32)
2018820197
(local $3 i32)
2018920198
(local $4 i32)
@@ -20200,6 +20209,15 @@
2020020209
end
2020120210
local.get $0
2020220211
i32.eqz
20212+
if
20213+
i32.const 16
20214+
i32.const 41
20215+
call $~lib/rt/pure/__new
20216+
call $~lib/rt/pure/__retain
20217+
local.set $0
20218+
end
20219+
local.get $0
20220+
i32.eqz
2020320221
if
2020420222
i32.const 16
2020520223
i32.const 6
@@ -20640,7 +20658,7 @@
2064020658
local.get $1
2064120659
call $~lib/array/Array<u8>#join
2064220660
)
20643-
(func $~lib/array/Array<~lib/string/String>#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
20661+
(func $std/array/ArrayStr#constructor@varargs (param $0 i32) (param $1 i32) (result i32)
2064420662
block $1of1
2064520663
block $0of1
2064620664
block $outOfRange
@@ -20653,6 +20671,14 @@
2065320671
local.set $1
2065420672
end
2065520673
local.get $0
20674+
if (result i32)
20675+
local.get $0
20676+
else
20677+
i32.const 16
20678+
i32.const 42
20679+
call $~lib/rt/pure/__new
20680+
call $~lib/rt/pure/__retain
20681+
end
2065620682
local.get $1
2065720683
call $~lib/array/Array<~lib/string/String>#constructor
2065820684
)

0 commit comments

Comments
 (0)