Skip to content

Commit 1bf4140

Browse files
authored
Fix field export naming (#1074)
1 parent d11db68 commit 1bf4140

File tree

6 files changed

+120
-102
lines changed

6 files changed

+120
-102
lines changed

src/compiler.ts

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,12 @@ export class Compiler extends DiagnosticEmitter {
623623
break;
624624
}
625625
case ElementKind.FIELD: {
626-
this.makeExportedFieldGetter(prefix + GETTER_PREFIX + name, <Field>element);
627-
if (!element.is(CommonFlags.READONLY)) {
628-
this.makeExportedFieldSetter(prefix + SETTER_PREFIX + name, <Field>element);
626+
if (element.is(CommonFlags.COMPILED)) {
627+
let module = this.module;
628+
module.addFunctionExport((<Field>element).internalGetterName, prefix + GETTER_PREFIX + name);
629+
if (!element.is(CommonFlags.READONLY)) {
630+
module.addFunctionExport((<Field>element).internalSetterName, prefix + SETTER_PREFIX + name);
631+
}
629632
}
630633
break;
631634
}
@@ -674,48 +677,6 @@ export class Compiler extends DiagnosticEmitter {
674677
}
675678
}
676679

677-
/** Makes an exported function to get the value of an instance field. */
678-
private makeExportedFieldGetter(name: string, field: Field): void {
679-
var type = field.type;
680-
var nativeThisType = this.options.nativeSizeType;
681-
var nativeValueType = type.toNativeType();
682-
var module = this.module;
683-
var returnExpr = module.load(type.byteSize, type.is(TypeFlags.SIGNED),
684-
module.local_get(0, nativeThisType),
685-
nativeValueType, field.memoryOffset
686-
);
687-
// functions retain the return value for the caller
688-
if (type.isManaged) returnExpr = this.makeRetain(returnExpr);
689-
module.addFunction(name, nativeThisType, nativeValueType, null, returnExpr);
690-
module.addFunctionExport(name, name);
691-
}
692-
693-
/** Makes an exported function to set the value of an instance field. */
694-
private makeExportedFieldSetter(name: string, field: Field): void {
695-
var type = field.type;
696-
var nativeThisType = this.options.nativeSizeType;
697-
var nativeValueType = type.toNativeType();
698-
var module = this.module;
699-
var valueExpr = module.local_get(1, nativeValueType);
700-
if (type.isManaged) {
701-
valueExpr = this.makeReplace(
702-
module.load(type.byteSize, false,
703-
module.local_get(0, nativeThisType),
704-
nativeValueType, field.memoryOffset
705-
),
706-
valueExpr
707-
);
708-
}
709-
module.addFunction(name, createType([ nativeThisType, nativeValueType ]), NativeType.None, null,
710-
module.store(type.byteSize,
711-
module.local_get(0, nativeThisType),
712-
valueExpr,
713-
nativeValueType, field.memoryOffset
714-
)
715-
);
716-
module.addFunctionExport(name, name);
717-
}
718-
719680
// === Elements =================================================================================
720681

721682
/** Compiles any element. */
@@ -1423,15 +1384,12 @@ export class Compiler extends DiagnosticEmitter {
14231384
}
14241385
break;
14251386
}
1426-
case ElementKind.FIELD_PROTOTYPE: {
1427-
element.set(CommonFlags.COMPILED);
1387+
case ElementKind.FIELD: {
1388+
this.compileField(<Field>element);
14281389
break;
14291390
}
14301391
case ElementKind.PROPERTY: {
1431-
let getterInstance = (<Property>element).getterInstance;
1432-
if (getterInstance) this.compileFunction(getterInstance);
1433-
let setterInstance = (<Property>element).setterInstance;
1434-
if (setterInstance) this.compileFunction(setterInstance);
1392+
this.compileProperty(<Property>element);
14351393
break;
14361394
}
14371395
}
@@ -1440,6 +1398,56 @@ export class Compiler extends DiagnosticEmitter {
14401398
return true;
14411399
}
14421400

1401+
/** Compiles an instance field to a getter and a setter. */
1402+
compileField(instance: Field): bool {
1403+
if (instance.is(CommonFlags.COMPILED)) return true;
1404+
instance.set(CommonFlags.COMPILED);
1405+
var type = instance.type;
1406+
var nativeThisType = this.options.nativeSizeType;
1407+
var nativeValueType = type.toNativeType();
1408+
var module = this.module;
1409+
1410+
// Make a getter
1411+
var returnExpr = module.load(type.byteSize, type.is(TypeFlags.SIGNED),
1412+
module.local_get(0, nativeThisType),
1413+
nativeValueType, instance.memoryOffset
1414+
);
1415+
if (type.isManaged) returnExpr = this.makeRetain(returnExpr);
1416+
module.addFunction(instance.internalGetterName, nativeThisType, nativeValueType, null, returnExpr);
1417+
1418+
// Make a setter
1419+
var valueExpr = module.local_get(1, nativeValueType);
1420+
if (type.isManaged) {
1421+
valueExpr = this.makeReplace(
1422+
module.load(type.byteSize, false,
1423+
module.local_get(0, nativeThisType),
1424+
nativeValueType, instance.memoryOffset
1425+
),
1426+
valueExpr
1427+
);
1428+
}
1429+
module.addFunction(instance.internalSetterName, createType([ nativeThisType, nativeValueType ]), NativeType.None, null,
1430+
module.store(type.byteSize,
1431+
module.local_get(0, nativeThisType),
1432+
valueExpr,
1433+
nativeValueType, instance.memoryOffset
1434+
)
1435+
);
1436+
1437+
return true;
1438+
}
1439+
1440+
/** Compiles a property to a getter and potentially a setter. */
1441+
compileProperty(instance: Property): bool {
1442+
if (instance.is(CommonFlags.COMPILED)) return true;
1443+
instance.set(CommonFlags.COMPILED);
1444+
var getterInstance = instance.getterInstance;
1445+
if (getterInstance) this.compileFunction(getterInstance);
1446+
var setterInstance = instance.setterInstance;
1447+
if (setterInstance) this.compileFunction(setterInstance);
1448+
return true;
1449+
}
1450+
14431451
// === Memory ===================================================================================
14441452

14451453
/** Adds a static memory segment with the specified data. */

src/program.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,16 @@ export class Field extends VariableLikeElement {
30713071
this.setType(type);
30723072
registerConcreteElement(this.program, this);
30733073
}
3074+
3075+
/** Gets the internal name of the respective getter function. */
3076+
get internalGetterName(): string {
3077+
return this.parent.internalName + INSTANCE_DELIMITER + GETTER_PREFIX + this.name;
3078+
}
3079+
3080+
/** Gets the internal name of the respective setter function. */
3081+
get internalSetterName(): string {
3082+
return this.parent.internalName + INSTANCE_DELIMITER + SETTER_PREFIX + this.name;
3083+
}
30743084
}
30753085

30763086
/** A property comprised of a getter and a setter function. */

tests/compiler/exports.optimized.wat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828
(export "animals.Animal.CAT" (global $exports/animals.Animal.CAT))
2929
(export "animals.Animal.DOG" (global $exports/animals.Animal.DOG))
3030
(export "Car" (global $exports/Car))
31-
(export "Car#get:doors" (func $exports/Car#get:numDoors))
32-
(export "Car#set:doors" (func $exports/Car#set:numDoors))
31+
(export "Car#get:doors" (func $exports/Car#get:doors))
32+
(export "Car#set:doors" (func $exports/Car#set:doors))
3333
(export "Car#constructor" (func $exports/Car#constructor|trampoline))
34-
(export "Car#get:numDoors" (func $exports/Car#get:numDoors))
35-
(export "Car#set:numDoors" (func $exports/Car#set:numDoors))
34+
(export "Car#get:numDoors" (func $exports/Car#get:doors))
35+
(export "Car#set:numDoors" (func $exports/Car#set:doors))
3636
(export "Car#openDoors" (func $exports/Car#openDoors))
3737
(export "Car.TIRES" (global $exports/Car.TIRES))
3838
(export "Car.getNumTires" (func $exports/Car.getNumTires))
3939
(export "vehicles.Car" (global $exports/vehicles.Car))
40-
(export "vehicles.Car#get:doors" (func $exports/Car#get:numDoors))
41-
(export "vehicles.Car#set:doors" (func $exports/Car#set:numDoors))
40+
(export "vehicles.Car#get:doors" (func $exports/Car#get:doors))
41+
(export "vehicles.Car#set:doors" (func $exports/Car#set:doors))
4242
(export "vehicles.Car#constructor" (func $exports/vehicles.Car#constructor|trampoline))
43-
(export "vehicles.Car#get:numDoors" (func $exports/Car#get:numDoors))
44-
(export "vehicles.Car#set:numDoors" (func $exports/Car#set:numDoors))
43+
(export "vehicles.Car#get:numDoors" (func $exports/Car#get:doors))
44+
(export "vehicles.Car#set:numDoors" (func $exports/Car#set:doors))
4545
(export "vehicles.Car#openDoors" (func $exports/Car#openDoors))
4646
(export "vehicles.Car.TIRES" (global $exports/vehicles.Car.TIRES))
4747
(export "vehicles.Car.getNumTires" (func $exports/Car.getNumTires))
@@ -129,11 +129,11 @@
129129
i32.store offset=12
130130
local.get $2
131131
)
132-
(func $exports/Car#get:numDoors (; 5 ;) (param $0 i32) (result i32)
132+
(func $exports/Car#get:doors (; 5 ;) (param $0 i32) (result i32)
133133
local.get $0
134134
i32.load
135135
)
136-
(func $exports/Car#set:numDoors (; 6 ;) (param $0 i32) (param $1 i32)
136+
(func $exports/Car#set:doors (; 6 ;) (param $0 i32) (param $1 i32)
137137
local.get $0
138138
local.get $1
139139
i32.store

tests/compiler/exports.untouched.wat

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
(export "animals.Animal.CAT" (global $exports/animals.Animal.CAT))
3131
(export "animals.Animal.DOG" (global $exports/animals.Animal.DOG))
3232
(export "Car" (global $exports/Car))
33-
(export "Car#get:doors" (func $Car#get:doors))
34-
(export "Car#set:doors" (func $Car#set:doors))
33+
(export "Car#get:doors" (func $exports/Car#get:doors))
34+
(export "Car#set:doors" (func $exports/Car#set:doors))
3535
(export "Car#constructor" (func $exports/Car#constructor|trampoline))
3636
(export "Car#get:numDoors" (func $exports/Car#get:numDoors))
3737
(export "Car#set:numDoors" (func $exports/Car#set:numDoors))
3838
(export "Car#openDoors" (func $exports/Car#openDoors))
3939
(export "Car.TIRES" (global $exports/Car.TIRES))
4040
(export "Car.getNumTires" (func $exports/Car.getNumTires))
4141
(export "vehicles.Car" (global $exports/vehicles.Car))
42-
(export "vehicles.Car#get:doors" (func $vehicles.Car#get:doors))
43-
(export "vehicles.Car#set:doors" (func $vehicles.Car#set:doors))
42+
(export "vehicles.Car#get:doors" (func $exports/vehicles.Car#get:doors))
43+
(export "vehicles.Car#set:doors" (func $exports/vehicles.Car#set:doors))
4444
(export "vehicles.Car#constructor" (func $exports/vehicles.Car#constructor|trampoline))
4545
(export "vehicles.Car#get:numDoors" (func $exports/vehicles.Car#get:numDoors))
4646
(export "vehicles.Car#set:numDoors" (func $exports/vehicles.Car#set:numDoors))
@@ -195,22 +195,31 @@
195195
i32.store
196196
local.get $0
197197
)
198-
(func $exports/Car#get:numDoors (; 8 ;) (param $0 i32) (result i32)
198+
(func $exports/Car#get:doors (; 8 ;) (param $0 i32) (result i32)
199199
local.get $0
200200
i32.load
201201
)
202-
(func $exports/Car#set:numDoors (; 9 ;) (param $0 i32) (param $1 i32)
202+
(func $exports/Car#set:doors (; 9 ;) (param $0 i32) (param $1 i32)
203203
local.get $0
204204
local.get $1
205205
i32.store
206206
)
207-
(func $exports/Car#openDoors (; 10 ;) (param $0 i32)
207+
(func $exports/Car#get:numDoors (; 10 ;) (param $0 i32) (result i32)
208+
local.get $0
209+
i32.load
210+
)
211+
(func $exports/Car#set:numDoors (; 11 ;) (param $0 i32) (param $1 i32)
212+
local.get $0
213+
local.get $1
214+
i32.store
215+
)
216+
(func $exports/Car#openDoors (; 12 ;) (param $0 i32)
208217
nop
209218
)
210-
(func $exports/vehicles.Car.getNumTires (; 11 ;) (result i32)
219+
(func $exports/vehicles.Car.getNumTires (; 13 ;) (result i32)
211220
global.get $exports/vehicles.Car.TIRES
212221
)
213-
(func $exports/vehicles.Car#constructor (; 12 ;) (param $0 i32) (param $1 i32) (result i32)
222+
(func $exports/vehicles.Car#constructor (; 14 ;) (param $0 i32) (param $1 i32) (result i32)
214223
local.get $0
215224
i32.eqz
216225
if
@@ -228,19 +237,28 @@
228237
i32.store
229238
local.get $0
230239
)
231-
(func $exports/vehicles.Car#get:numDoors (; 13 ;) (param $0 i32) (result i32)
240+
(func $exports/vehicles.Car#get:doors (; 15 ;) (param $0 i32) (result i32)
241+
local.get $0
242+
i32.load
243+
)
244+
(func $exports/vehicles.Car#set:doors (; 16 ;) (param $0 i32) (param $1 i32)
245+
local.get $0
246+
local.get $1
247+
i32.store
248+
)
249+
(func $exports/vehicles.Car#get:numDoors (; 17 ;) (param $0 i32) (result i32)
232250
local.get $0
233251
i32.load
234252
)
235-
(func $exports/vehicles.Car#set:numDoors (; 14 ;) (param $0 i32) (param $1 i32)
253+
(func $exports/vehicles.Car#set:numDoors (; 18 ;) (param $0 i32) (param $1 i32)
236254
local.get $0
237255
local.get $1
238256
i32.store
239257
)
240-
(func $exports/vehicles.Car#openDoors (; 15 ;) (param $0 i32)
258+
(func $exports/vehicles.Car#openDoors (; 19 ;) (param $0 i32)
241259
nop
242260
)
243-
(func $~start (; 16 ;)
261+
(func $~start (; 20 ;)
244262
global.get $~lib/heap/__heap_base
245263
i32.const 15
246264
i32.add
@@ -252,7 +270,7 @@
252270
global.get $~lib/rt/stub/startOffset
253271
global.set $~lib/rt/stub/offset
254272
)
255-
(func $exports/subOpt|trampoline (; 17 ;) (param $0 i32) (param $1 i32) (result i32)
273+
(func $exports/subOpt|trampoline (; 21 ;) (param $0 i32) (param $1 i32) (result i32)
256274
block $1of1
257275
block $0of1
258276
block $outOfRange
@@ -270,16 +288,7 @@
270288
local.get $1
271289
call $exports/subOpt
272290
)
273-
(func $Car#get:doors (; 18 ;) (param $0 i32) (result i32)
274-
local.get $0
275-
i32.load
276-
)
277-
(func $Car#set:doors (; 19 ;) (param $0 i32) (param $1 i32)
278-
local.get $0
279-
local.get $1
280-
i32.store
281-
)
282-
(func $exports/Car#constructor|trampoline (; 20 ;) (param $0 i32) (param $1 i32) (result i32)
291+
(func $exports/Car#constructor|trampoline (; 22 ;) (param $0 i32) (param $1 i32) (result i32)
283292
block $1of1
284293
block $0of1
285294
block $outOfRange
@@ -295,15 +304,6 @@
295304
local.get $1
296305
call $exports/Car#constructor
297306
)
298-
(func $vehicles.Car#get:doors (; 21 ;) (param $0 i32) (result i32)
299-
local.get $0
300-
i32.load
301-
)
302-
(func $vehicles.Car#set:doors (; 22 ;) (param $0 i32) (param $1 i32)
303-
local.get $0
304-
local.get $1
305-
i32.store
306-
)
307307
(func $exports/vehicles.Car#constructor|trampoline (; 23 ;) (param $0 i32) (param $1 i32) (result i32)
308308
block $1of1
309309
block $0of1

tests/compiler/extends-recursive.optimized.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
(global $extends-recursive/Child i32 (i32.const 3))
66
(export "memory" (memory $0))
77
(export "Child" (global $extends-recursive/Child))
8-
(export "Child#get:child" (func $Child#get:child))
9-
(export "Child#set:child" (func $Child#set:child))
10-
(func $Child#get:child (; 0 ;) (param $0 i32) (result i32)
8+
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
9+
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
10+
(func $extends-recursive/Parent#get:child (; 0 ;) (param $0 i32) (result i32)
1111
local.get $0
1212
i32.load
1313
)
14-
(func $Child#set:child (; 1 ;) (param $0 i32) (param $1 i32)
14+
(func $extends-recursive/Parent#set:child (; 1 ;) (param $0 i32) (param $1 i32)
1515
(local $2 i32)
1616
local.get $0
1717
local.get $1

tests/compiler/extends-recursive.untouched.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
(global $extends-recursive/Child i32 (i32.const 3))
88
(export "memory" (memory $0))
99
(export "Child" (global $extends-recursive/Child))
10-
(export "Child#get:child" (func $Child#get:child))
11-
(export "Child#set:child" (func $Child#set:child))
10+
(export "Child#get:child" (func $extends-recursive/Parent#get:child))
11+
(export "Child#set:child" (func $extends-recursive/Parent#set:child))
1212
(func $~lib/rt/stub/__retain (; 0 ;) (param $0 i32) (result i32)
1313
local.get $0
1414
)
15-
(func $Child#get:child (; 1 ;) (param $0 i32) (result i32)
15+
(func $extends-recursive/Parent#get:child (; 1 ;) (param $0 i32) (result i32)
1616
local.get $0
1717
i32.load
1818
call $~lib/rt/stub/__retain
1919
)
2020
(func $~lib/rt/stub/__release (; 2 ;) (param $0 i32)
2121
nop
2222
)
23-
(func $Child#set:child (; 3 ;) (param $0 i32) (param $1 i32)
23+
(func $extends-recursive/Parent#set:child (; 3 ;) (param $0 i32) (param $1 i32)
2424
local.get $0
2525
local.get $1
2626
local.tee $0

0 commit comments

Comments
 (0)