Skip to content

Commit ca6e319

Browse files
authored
Ensure constructors of exported classes (#1154)
1 parent fb521c9 commit ca6e319

File tree

5 files changed

+451
-25
lines changed

5 files changed

+451
-25
lines changed

src/compiler.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,10 @@ export class Compiler extends DiagnosticEmitter {
655655
break;
656656
}
657657
case ElementKind.PROPERTY_PROTOTYPE: {
658-
let getter = (<PropertyPrototype>element).getterPrototype;
658+
let prototype = <PropertyPrototype>element;
659+
let getter = prototype.getterPrototype;
659660
if (getter) this.ensureModuleExport(GETTER_PREFIX + name, getter, prefix);
660-
let setter = (<PropertyPrototype>element).setterPrototype;
661+
let setter = prototype.setterPrototype;
661662
if (setter) this.ensureModuleExport(SETTER_PREFIX + name, setter, prefix);
662663
break;
663664
}
@@ -699,28 +700,31 @@ export class Compiler extends DiagnosticEmitter {
699700
break;
700701
}
701702
case ElementKind.PROPERTY: {
702-
let getter = (<Property>element).getterInstance;
703+
let instance = <Property>element;
704+
let getter = instance.getterInstance;
703705
if (getter) this.ensureModuleExport(GETTER_PREFIX + name, getter, prefix);
704-
let setter = (<Property>element).setterInstance;
706+
let setter = instance.setterInstance;
705707
if (setter) this.ensureModuleExport(SETTER_PREFIX + name, setter, prefix);
706708
break;
707709
}
708710
case ElementKind.FIELD: {
711+
let instance = <Field>element;
709712
if (element.is(CommonFlags.COMPILED)) {
710713
let module = this.module;
711-
module.addFunctionExport((<Field>element).internalGetterName, prefix + GETTER_PREFIX + name);
714+
module.addFunctionExport(instance.internalGetterName, prefix + GETTER_PREFIX + name);
712715
if (!element.is(CommonFlags.READONLY)) {
713-
module.addFunctionExport((<Field>element).internalSetterName, prefix + SETTER_PREFIX + name);
716+
module.addFunctionExport(instance.internalSetterName, prefix + SETTER_PREFIX + name);
714717
}
715718
}
716719
break;
717720
}
718721
case ElementKind.CLASS: {
722+
let instance = <Class>element;
719723
// make the class name itself represent its runtime id
720-
if (!(<Class>element).type.isUnmanaged) {
724+
if (!instance.type.isUnmanaged) {
721725
let module = this.module;
722-
let internalName = (<Class>element).internalName;
723-
module.addGlobal(internalName, NativeType.I32, false, module.i32((<Class>element).id));
726+
let internalName = instance.internalName;
727+
module.addGlobal(internalName, NativeType.I32, false, module.i32(instance.id));
724728
module.addGlobalExport(internalName, prefix + name);
725729
}
726730
break;
@@ -1501,8 +1505,7 @@ export class Compiler extends DiagnosticEmitter {
15011505
}
15021506
}
15031507
}
1504-
var ctorInstance = instance.constructorInstance;
1505-
if (ctorInstance) this.compileFunction(ctorInstance);
1508+
this.ensureConstructor(instance, instance.identifierNode);
15061509
var instanceMembers = instance.members;
15071510
if (instanceMembers) {
15081511
// TODO: for (let element of instanceMembers.values()) {
@@ -8704,14 +8707,17 @@ export class Compiler extends DiagnosticEmitter {
87048707
CommonNames.constructor,
87058708
new FunctionPrototype(
87068709
CommonNames.constructor,
8707-
classInstance,
8710+
classInstance, // bound
87088711
this.program.makeNativeFunctionDeclaration(CommonNames.constructor,
87098712
CommonFlags.INSTANCE | CommonFlags.CONSTRUCTOR
87108713
)
87118714
),
87128715
new Signature(this.program, null, classInstance.type, classInstance.type),
87138716
contextualTypeArguments
87148717
);
8718+
let members = classInstance.members;
8719+
if (!members) classInstance.members = members = new Map();
8720+
members.set("constructor", instance.prototype);
87158721
}
87168722

87178723
instance.internalName = classInstance.internalName + INSTANCE_DELIMITER + "constructor";

tests/compiler/extends-recursive.optimized.wat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
(module
2+
(type $none_=>_none (func))
23
(type $i32_i32_=>_none (func (param i32 i32)))
34
(type $i32_=>_i32 (func (param i32) (result i32)))
45
(memory $0 0)
6+
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
7+
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
58
(global $extends-recursive/Child i32 (i32.const 3))
69
(export "memory" (memory $0))
710
(export "Child" (global $extends-recursive/Child))
811
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
912
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
13+
(start $~start)
1014
(func $extends-recursive/Parent#get:child (; 0 ;) (param $0 i32) (result i32)
1115
local.get $0
1216
i32.load
@@ -19,4 +23,10 @@
1923
local.get $1
2024
i32.store
2125
)
26+
(func $~start (; 2 ;)
27+
i32.const 16
28+
global.set $~lib/rt/stub/startOffset
29+
i32.const 16
30+
global.set $~lib/rt/stub/offset
31+
)
2232
)

tests/compiler/extends-recursive.untouched.wat

Lines changed: 160 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,170 @@
11
(module
22
(type $i32_=>_i32 (func (param i32) (result i32)))
33
(type $i32_=>_none (func (param i32)))
4+
(type $none_=>_none (func))
45
(type $i32_i32_=>_none (func (param i32 i32)))
6+
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
57
(memory $0 0)
68
(table $0 1 funcref)
9+
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
10+
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
11+
(global $~lib/heap/__heap_base i32 (i32.const 8))
712
(global $extends-recursive/Child i32 (i32.const 3))
813
(export "memory" (memory $0))
914
(export "Child" (global $extends-recursive/Child))
1015
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
1116
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
12-
(func $~lib/rt/stub/__retain (; 0 ;) (param $0 i32) (result i32)
17+
(start $~start)
18+
(func $~lib/rt/stub/maybeGrowMemory (; 0 ;) (param $0 i32)
19+
(local $1 i32)
20+
(local $2 i32)
21+
(local $3 i32)
22+
(local $4 i32)
23+
(local $5 i32)
24+
memory.size
25+
local.set $1
26+
local.get $1
27+
i32.const 16
28+
i32.shl
29+
local.set $2
30+
local.get $0
31+
local.get $2
32+
i32.gt_u
33+
if
34+
local.get $0
35+
local.get $2
36+
i32.sub
37+
i32.const 65535
38+
i32.add
39+
i32.const 65535
40+
i32.const -1
41+
i32.xor
42+
i32.and
43+
i32.const 16
44+
i32.shr_u
45+
local.set $3
46+
local.get $1
47+
local.tee $4
48+
local.get $3
49+
local.tee $5
50+
local.get $4
51+
local.get $5
52+
i32.gt_s
53+
select
54+
local.set $4
55+
local.get $4
56+
memory.grow
57+
i32.const 0
58+
i32.lt_s
59+
if
60+
local.get $3
61+
memory.grow
62+
i32.const 0
63+
i32.lt_s
64+
if
65+
unreachable
66+
end
67+
end
68+
end
69+
local.get $0
70+
global.set $~lib/rt/stub/offset
71+
)
72+
(func $~lib/rt/stub/__alloc (; 1 ;) (param $0 i32) (param $1 i32) (result i32)
73+
(local $2 i32)
74+
(local $3 i32)
75+
(local $4 i32)
76+
(local $5 i32)
77+
(local $6 i32)
78+
local.get $0
79+
i32.const 1073741808
80+
i32.gt_u
81+
if
82+
unreachable
83+
end
84+
global.get $~lib/rt/stub/offset
85+
i32.const 16
86+
i32.add
87+
local.set $2
88+
local.get $0
89+
i32.const 15
90+
i32.add
91+
i32.const 15
92+
i32.const -1
93+
i32.xor
94+
i32.and
95+
local.tee $3
96+
i32.const 16
97+
local.tee $4
98+
local.get $3
99+
local.get $4
100+
i32.gt_u
101+
select
102+
local.set $5
103+
local.get $2
104+
local.get $5
105+
i32.add
106+
call $~lib/rt/stub/maybeGrowMemory
107+
local.get $2
108+
i32.const 16
109+
i32.sub
110+
local.set $6
111+
local.get $6
112+
local.get $5
113+
i32.store
114+
local.get $6
115+
i32.const 1
116+
i32.store offset=4
117+
local.get $6
118+
local.get $1
119+
i32.store offset=8
120+
local.get $6
121+
local.get $0
122+
i32.store offset=12
123+
local.get $2
124+
)
125+
(func $~lib/rt/stub/__retain (; 2 ;) (param $0 i32) (result i32)
13126
local.get $0
14127
)
15-
(func $extends-recursive/Parent#get:child (; 1 ;) (param $0 i32) (result i32)
128+
(func $extends-recursive/Parent#constructor (; 3 ;) (param $0 i32) (result i32)
129+
local.get $0
130+
i32.eqz
131+
if
132+
i32.const 4
133+
i32.const 4
134+
call $~lib/rt/stub/__alloc
135+
call $~lib/rt/stub/__retain
136+
local.set $0
137+
end
138+
local.get $0
139+
i32.const 0
140+
call $~lib/rt/stub/__retain
141+
i32.store
142+
local.get $0
143+
)
144+
(func $extends-recursive/Child#constructor (; 4 ;) (param $0 i32) (result i32)
145+
local.get $0
146+
i32.eqz
147+
if
148+
i32.const 4
149+
i32.const 3
150+
call $~lib/rt/stub/__alloc
151+
call $~lib/rt/stub/__retain
152+
local.set $0
153+
end
154+
local.get $0
155+
call $extends-recursive/Parent#constructor
156+
local.set $0
157+
local.get $0
158+
)
159+
(func $extends-recursive/Parent#get:child (; 5 ;) (param $0 i32) (result i32)
16160
local.get $0
17161
i32.load
18162
call $~lib/rt/stub/__retain
19163
)
20-
(func $~lib/rt/stub/__release (; 2 ;) (param $0 i32)
164+
(func $~lib/rt/stub/__release (; 6 ;) (param $0 i32)
21165
nop
22166
)
23-
(func $extends-recursive/Parent#set:child (; 3 ;) (param $0 i32) (param $1 i32)
167+
(func $extends-recursive/Parent#set:child (; 7 ;) (param $0 i32) (param $1 i32)
24168
(local $2 i32)
25169
local.get $0
26170
local.get $1
@@ -38,4 +182,16 @@
38182
local.get $1
39183
i32.store
40184
)
185+
(func $~start (; 8 ;)
186+
global.get $~lib/heap/__heap_base
187+
i32.const 15
188+
i32.add
189+
i32.const 15
190+
i32.const -1
191+
i32.xor
192+
i32.and
193+
global.set $~lib/rt/stub/startOffset
194+
global.get $~lib/rt/stub/startOffset
195+
global.set $~lib/rt/stub/offset
196+
)
41197
)

0 commit comments

Comments
 (0)