Skip to content

Commit 5b7b29c

Browse files
committed
renaming for clarity
1 parent 9a03920 commit 5b7b29c

File tree

5 files changed

+250
-21
lines changed

5 files changed

+250
-21
lines changed

src/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export enum CommonFlags {
7171
/** Is a virtual method. */
7272
VIRTUAL = 1 << 26,
7373
/** Is (part of) a closure. */
74-
CLOSURE = 1 << 27,
74+
IN_SCOPE_CLOSURE = 1 << 27,
7575

7676
// Other
7777

src/compiler.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -3565,7 +3565,7 @@ export class Compiler extends DiagnosticEmitter {
35653565
}
35663566
var toSignature = toType.signatureReference;
35673567
var fromSignature = fromType.signatureReference;
3568-
if (toSignature !== null && fromSignature !== null && fromSignature.externalEquals(toSignature) && fromType.is(TypeFlags.CLOSURE)) {
3568+
if (toSignature !== null && fromSignature !== null && fromSignature.externalEquals(toSignature) && fromType.is(TypeFlags.IN_SCOPE_CLOSURE)) {
35693569
// When we convert from the closure type into a function pointer, we first
35703570
// update the local copy of the scope with the newest values
35713571
var tempResult = this.currentFlow.getTempLocal(fromType);
@@ -6547,7 +6547,7 @@ export class Compiler extends DiagnosticEmitter {
65476547
let local = <Local>target;
65486548
let nativeSizeType = this.options.nativeSizeType;
65496549
signature = assert(local.type.signatureReference);
6550-
if (local.type.is(TypeFlags.CLOSURE)) {
6550+
if (local.type.is(TypeFlags.IN_SCOPE_CLOSURE)) {
65516551
// If we're calling a local we know to be a closure, then we must still be in the creator functions
65526552
// scope. Because of this, we should update the values of locals that are still available
65536553
return module.block(null, [
@@ -7374,7 +7374,7 @@ export class Compiler extends DiagnosticEmitter {
73747374

73757375
var retainInstance = this.program.retainInstance;
73767376
this.compileFunction(retainInstance);
7377-
if (type !== null && type.isFunction) {
7377+
if (type !== null && type.isFunctionIndex) {
73787378
var exprLocal = this.currentFlow.getTempLocal(type);
73797379
var exprLocalIndex = exprLocal.index;
73807380
var nativeType = type.toNativeType();
@@ -7412,7 +7412,7 @@ export class Compiler extends DiagnosticEmitter {
74127412
var releaseInstance = this.program.releaseInstance;
74137413
this.compileFunction(releaseInstance);
74147414

7415-
if (type !== null && type.isFunction) {
7415+
if (type !== null && type.isFunctionIndex) {
74167416
var exprLocal = this.currentFlow.getTempLocal(type);
74177417
var exprLocalIndex = exprLocal.index;
74187418
var nativeType = type.toNativeType()
@@ -8218,13 +8218,16 @@ export class Compiler extends DiagnosticEmitter {
82188218
// copied closed locals into type
82198219
this.currentType.locals = instance.closedLocals;
82208220

8221-
const closureSize = instance.nextGlobalClosureOffset
8221+
const closureSize = instance.nextGlobalClosureOffset;
8222+
8223+
var allocInstance = this.program.allocInstance;
8224+
this.compileFunction(allocInstance);
82228225

82238226
var closureExpr = this.module.flatten([
82248227
this.module.local_set( // Allocate memory for the closure
82258228
tempLocalIndex,
82268229
this.makeRetain(
8227-
this.module.call(this.program.allocInstance.internalName, [
8230+
this.module.call(allocInstance.internalName, [
82288231
wasm64 ? this.module.i64(closureSize) : this.module.i32(closureSize),
82298232
wasm64 ? this.module.i64(0) : this.module.i32(0)
82308233
], nativeUsize)

src/types.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const enum TypeFlags {
9898
/** Is a host type. */
9999
HOST = 1 << 11,
100100
/** Is a closure type. */
101-
CLOSURE = 1 << 12
101+
IN_SCOPE_CLOSURE = 1 << 12
102102
}
103103

104104
/** Represents a resolved type. */
@@ -169,7 +169,7 @@ export class Type {
169169

170170
/** Tests if this is a managed type that needs GC hooks. */
171171
get isManaged(): bool {
172-
if (this.isFunction) return true;
172+
if (this.isFunctionIndex) return true;
173173
if (this.is(TypeFlags.INTEGER | TypeFlags.REFERENCE)) {
174174
let classReference = this.classReference;
175175
if (classReference) return !classReference.hasDecorator(DecoratorFlags.UNMANAGED);
@@ -184,8 +184,8 @@ export class Type {
184184
return classReference !== null && classReference.hasDecorator(DecoratorFlags.UNMANAGED);
185185
}
186186

187-
get isFunction(): bool {
188-
return this.signatureReference !== null && !this.is(TypeFlags.CLOSURE);
187+
get isFunctionIndex(): bool {
188+
return this.signatureReference !== null && !this.is(TypeFlags.IN_SCOPE_CLOSURE);
189189
}
190190

191191
/** Computes the sign-extending shift in the target type. */
@@ -220,13 +220,6 @@ export class Type {
220220
return ret;
221221
}
222222

223-
asClosure(signature: Signature): Type {
224-
var functionType = this.asFunction(signature);
225-
functionType.flags |= TypeFlags.CLOSURE;
226-
functionType.classReference = this.classReference;
227-
return functionType;
228-
}
229-
230223
/** Composes the respective nullable type of this type. */
231224
asNullable(): Type {
232225
assert(this.is(TypeFlags.REFERENCE));
@@ -538,7 +531,7 @@ export class Type {
538531

539532
/** Type of an in-context local */
540533
static readonly closure32: Type = new Type(Type.i32.kind,
541-
TypeFlags.CLOSURE |
534+
TypeFlags.IN_SCOPE_CLOSURE |
542535
TypeFlags.REFERENCE,
543536
Type.i32.size
544537
);

tests/compiler/closure-limitations-runtime.optimized.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@
6868
i32.const 1
6969
i32.store offset=4
7070
local.get $1
71-
i32.const 3
71+
i32.const 0
7272
i32.store offset=8
7373
local.get $1
74-
i32.const 4
74+
i32.const 8
7575
i32.store offset=12
7676
local.get $0
7777
i32.const 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
(module
2+
(type $none_=>_none (func))
3+
(type $i32_=>_none (func (param i32)))
4+
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
5+
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
6+
(type $none_=>_i32 (func (result i32)))
7+
(type $i32_=>_i32 (func (param i32) (result i32)))
8+
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
9+
(memory $0 1)
10+
(data (i32.const 16) "<\00\00\00\01\00\00\00\01\00\00\00<\00\00\00c\00l\00o\00s\00u\00r\00e\00-\00l\00i\00m\00i\00t\00a\00t\00i\00o\00n\00s\00-\00r\00u\00n\00t\00i\00m\00e\00.\00t\00s\00")
11+
(table $0 2 funcref)
12+
(elem (i32.const 1) $closure-limitations-runtime/exportedClosureReturns~inner)
13+
(global $~lib/rt/stub/startOffset (mut i32) (i32.const 0))
14+
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
15+
(global $~lib/heap/__heap_base i32 (i32.const 92))
16+
(export "memory" (memory $0))
17+
(export "exportedClosureReturns" (func $closure-limitations-runtime/exportedClosureReturns))
18+
(start $~start)
19+
(func $closure-limitations-runtime/exportedClosureReturns~inner (param $0 i32) (param $1 i32) (result i32)
20+
local.get $0
21+
i32.load offset=4
22+
)
23+
(func $~lib/rt/stub/maybeGrowMemory (param $0 i32)
24+
(local $1 i32)
25+
(local $2 i32)
26+
(local $3 i32)
27+
(local $4 i32)
28+
(local $5 i32)
29+
memory.size
30+
local.set $1
31+
local.get $1
32+
i32.const 16
33+
i32.shl
34+
local.set $2
35+
local.get $0
36+
local.get $2
37+
i32.gt_u
38+
if
39+
local.get $0
40+
local.get $2
41+
i32.sub
42+
i32.const 65535
43+
i32.add
44+
i32.const 65535
45+
i32.const -1
46+
i32.xor
47+
i32.and
48+
i32.const 16
49+
i32.shr_u
50+
local.set $3
51+
local.get $1
52+
local.tee $4
53+
local.get $3
54+
local.tee $5
55+
local.get $4
56+
local.get $5
57+
i32.gt_s
58+
select
59+
local.set $4
60+
local.get $4
61+
memory.grow
62+
i32.const 0
63+
i32.lt_s
64+
if
65+
local.get $3
66+
memory.grow
67+
i32.const 0
68+
i32.lt_s
69+
if
70+
unreachable
71+
end
72+
end
73+
end
74+
local.get $0
75+
global.set $~lib/rt/stub/offset
76+
)
77+
(func $~lib/rt/stub/__alloc (param $0 i32) (param $1 i32) (result i32)
78+
(local $2 i32)
79+
(local $3 i32)
80+
(local $4 i32)
81+
(local $5 i32)
82+
(local $6 i32)
83+
local.get $0
84+
i32.const 1073741808
85+
i32.gt_u
86+
if
87+
unreachable
88+
end
89+
global.get $~lib/rt/stub/offset
90+
i32.const 16
91+
i32.add
92+
local.set $2
93+
local.get $0
94+
i32.const 15
95+
i32.add
96+
i32.const 15
97+
i32.const -1
98+
i32.xor
99+
i32.and
100+
local.tee $3
101+
i32.const 16
102+
local.tee $4
103+
local.get $3
104+
local.get $4
105+
i32.gt_u
106+
select
107+
local.set $5
108+
local.get $2
109+
local.get $5
110+
i32.add
111+
call $~lib/rt/stub/maybeGrowMemory
112+
local.get $2
113+
i32.const 16
114+
i32.sub
115+
local.set $6
116+
local.get $6
117+
local.get $5
118+
i32.store
119+
i32.const 1
120+
drop
121+
local.get $6
122+
i32.const 1
123+
i32.store offset=4
124+
local.get $6
125+
local.get $1
126+
i32.store offset=8
127+
local.get $6
128+
local.get $0
129+
i32.store offset=12
130+
local.get $2
131+
)
132+
(func $~lib/rt/stub/__retain (param $0 i32) (result i32)
133+
local.get $0
134+
)
135+
(func $~lib/rt/stub/__release (param $0 i32)
136+
nop
137+
)
138+
(func $closure-limitations-runtime/exportedClosureReturns (result i32)
139+
(local $0 i32)
140+
(local $1 i32)
141+
(local $2 i32)
142+
(local $3 i32)
143+
(local $4 i32)
144+
i32.const 0
145+
local.set $0
146+
i32.const 8
147+
i32.const 0
148+
call $~lib/rt/stub/__alloc
149+
call $~lib/rt/stub/__retain
150+
local.set $1
151+
local.get $1
152+
i32.const 1
153+
i32.store
154+
local.get $1
155+
local.set $2
156+
local.get $2
157+
local.get $0
158+
i32.store offset=4
159+
local.get $2
160+
i32.const 4
161+
i32.shr_s
162+
i32.const -2147483648
163+
i32.or
164+
local.set $3
165+
local.get $3
166+
i32.const -2147483648
167+
i32.and
168+
i32.const -2147483648
169+
i32.eq
170+
if (result i32)
171+
local.get $3
172+
i32.const 4
173+
i32.shl
174+
else
175+
i32.const 0
176+
end
177+
call $~lib/rt/stub/__retain
178+
drop
179+
local.get $3
180+
local.set $4
181+
local.get $1
182+
call $~lib/rt/stub/__release
183+
local.get $4
184+
local.set $1
185+
local.get $1
186+
i32.const -2147483648
187+
i32.and
188+
i32.const -2147483648
189+
i32.eq
190+
if
191+
i32.const 0
192+
i32.const 32
193+
i32.const 3
194+
i32.const 3
195+
call $~lib/builtins/abort
196+
unreachable
197+
else
198+
nop
199+
end
200+
local.get $1
201+
)
202+
(func $start:closure-limitations-runtime
203+
(local $0 i32)
204+
global.get $~lib/heap/__heap_base
205+
i32.const 15
206+
i32.add
207+
i32.const 15
208+
i32.const -1
209+
i32.xor
210+
i32.and
211+
global.set $~lib/rt/stub/startOffset
212+
global.get $~lib/rt/stub/startOffset
213+
global.set $~lib/rt/stub/offset
214+
call $closure-limitations-runtime/exportedClosureReturns
215+
local.set $0
216+
local.get $0
217+
i32.const -2147483648
218+
i32.and
219+
i32.const -2147483648
220+
i32.eq
221+
if (result i32)
222+
local.get $0
223+
i32.const 4
224+
i32.shl
225+
else
226+
i32.const 0
227+
end
228+
call $~lib/rt/stub/__release
229+
)
230+
(func $~start
231+
call $start:closure-limitations-runtime
232+
)
233+
)

0 commit comments

Comments
 (0)