Skip to content

Commit 25b9276

Browse files
authored
Retain parameter field initializers in ctors (AssemblyScript#757)
1 parent 9da3907 commit 25b9276

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/compiler.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8935,40 +8935,36 @@ export class Compiler extends DiagnosticEmitter {
89358935
let field = <Field>member; assert(!field.isAny(CommonFlags.CONST));
89368936
let fieldType = field.type;
89378937
let nativeFieldType = fieldType.toNativeType();
8938-
let initializerNode = field.prototype.initializerNode;
8938+
let fieldPrototype = field.prototype;
8939+
let initializerNode = fieldPrototype.initializerNode;
8940+
let parameterIndex = fieldPrototype.parameterIndex;
8941+
let initExpr: ExpressionRef;
89398942
if (initializerNode) { // use initializer
8940-
let initExpr = this.compileExpression(initializerNode, fieldType, // reports
8943+
initExpr = this.compileExpression(initializerNode, fieldType, // reports
89418944
Constraints.CONV_IMPLICIT | Constraints.WILL_RETAIN
89428945
);
89438946
if (fieldType.isManaged && !this.skippedAutoreleases.has(initExpr)) {
89448947
initExpr = this.makeRetain(initExpr);
89458948
}
8946-
stmts.push(
8947-
module.store(fieldType.byteSize,
8948-
module.local_get(thisLocalIndex, nativeSizeType),
8949-
initExpr,
8950-
nativeFieldType,
8951-
field.memoryOffset
8952-
)
8953-
);
8954-
} else {
8955-
let parameterIndex = field.prototype.parameterIndex;
8956-
stmts.push(
8957-
module.store(fieldType.byteSize,
8958-
module.local_get(thisLocalIndex, nativeSizeType),
8959-
parameterIndex >= 0 // initialized via parameter (here: a local)
8960-
? module.local_get(
8961-
isInline
8962-
? assert(flow.lookupLocal(field.name)).index
8963-
: 1 + parameterIndex, // this is local 0
8964-
nativeFieldType
8965-
)
8966-
: fieldType.toNativeZero(module),
8967-
nativeFieldType,
8968-
field.memoryOffset
8969-
)
8949+
} else if (parameterIndex >= 0) { // initialized via parameter (here: a local)
8950+
initExpr = module.local_get(
8951+
isInline
8952+
? assert(flow.lookupLocal(field.name)).index
8953+
: 1 + parameterIndex, // this is local 0
8954+
nativeFieldType
89708955
);
8956+
if (fieldType.isManaged) initExpr = this.makeRetain(initExpr);
8957+
} else { // initialize with zero
8958+
initExpr = fieldType.toNativeZero(module);
89718959
}
8960+
stmts.push(
8961+
module.store(fieldType.byteSize,
8962+
module.local_get(thisLocalIndex, nativeSizeType),
8963+
initExpr,
8964+
nativeFieldType,
8965+
field.memoryOffset
8966+
)
8967+
);
89728968
}
89738969
return stmts;
89748970
}

0 commit comments

Comments
 (0)