Skip to content

Commit fd0f3aa

Browse files
authored
fix(as): init unmanaged class (#26)
AssemblyScript/assemblyscript#2948
1 parent 537f2ff commit fd0f3aa

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

assemblyscript/src/compiler.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10021,9 +10021,8 @@ export class Compiler extends DiagnosticEmitter {
1002110021
/** Check if possible to optimize the active initialization away if it's zero */
1002210022
canOptimizeZeroInitialization(valueExpr: ExpressionRef): bool {
1002310023
const runtime = this.options.runtime;
10024-
return (runtime == Runtime.Incremental || runtime == Runtime.Stub)
10025-
? isConstZero(valueExpr)
10026-
: false;
10024+
// Memory will be filled with 0 on itcms.__new
10025+
return runtime == Runtime.Incremental ? isConstZero(valueExpr) : false;
1002710026
}
1002810027

1002910028
/** Makes a constant zero of the specified type. */
@@ -10402,6 +10401,7 @@ export class Compiler extends DiagnosticEmitter {
1040210401

1040310402
// Initialize deferred non-parameter fields
1040410403
if (nonParameterFields) {
10404+
const unmanagedClass = classInstance.type.isUnmanaged;
1040510405
for (let i = 0, k = nonParameterFields.length; i < k; ++i) {
1040610406
let field = unchecked(nonParameterFields[i]);
1040710407
let fieldType = field.type;
@@ -10410,14 +10410,9 @@ export class Compiler extends DiagnosticEmitter {
1041010410
assert(fieldPrototype.parameterIndex < 0);
1041110411
let setterInstance = assert(field.setterInstance);
1041210412

10413-
if (initializerNode) {
10414-
// Explicit initializer
10415-
// Check if we need to initialize this field
10413+
if (initializerNode){
1041610414
const valueExpr: ExpressionRef = this.compileExpression(initializerNode, fieldType, Constraints.ConvImplicit);
10417-
// Memory will be filled with 0 on itcms.__new
10418-
// Memory grow will default to initialized with 0 as wasm spec
10419-
// So, optimize the active initialization away if it's zero
10420-
if (!this.canOptimizeZeroInitialization(valueExpr)) {
10415+
if(unmanagedClass || !this.canOptimizeZeroInitialization(valueExpr)) {
1042110416
let expr = this.makeCallDirect(setterInstance, [
1042210417
module.local_get(thisLocalIndex, sizeTypeRef),
1042310418
valueExpr
@@ -10427,6 +10422,18 @@ export class Compiler extends DiagnosticEmitter {
1042710422
}
1042810423
stmts.push(expr);
1042910424
}
10425+
} else {
10426+
if(unmanagedClass || (this.options.runtime != Runtime.Incremental)) {
10427+
let expr = this.makeCallDirect(setterInstance, [
10428+
module.local_get(thisLocalIndex, sizeTypeRef),
10429+
// Create only when necessary since makeZero will allocte persistent memory by Binaryen.
10430+
this.makeZero(fieldType)
10431+
], field.identifierNode, true);
10432+
if (this.currentType != Type.void) { // in case
10433+
expr = module.drop(expr);
10434+
}
10435+
stmts.push(expr);
10436+
}
1043010437
}
1043110438
}
1043210439
}

tests/frontend/compiler/object-literal.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,6 +3939,14 @@
39393939
)
39403940
)
39413941
)
3942+
(call $object-literal/Unmanaged#set:bar
3943+
(local.get $this)
3944+
(i32.const 0)
3945+
)
3946+
(call $object-literal/Unmanaged#set:baz
3947+
(local.get $this)
3948+
(i32.const 0)
3949+
)
39423950
(local.get $this)
39433951
)
39443952
(func $object-literal/Unmanaged#get:bar (param $this i32) (result i32)

0 commit comments

Comments
 (0)