Skip to content

Handle instantiation of unmanaged class via object literal #1189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6628,13 +6628,20 @@ export class Compiler extends DiagnosticEmitter {
}

/** Checks that an unsafe expression is allowed. */
private checkUnsafe(reportNode: Node): void {
private checkUnsafe(reportNode: Node, relatedReportNode: Node | null = null): void {
// Library files may always use unsafe features
if (this.options.noUnsafe && !reportNode.range.source.isLibrary) {
this.error(
DiagnosticCode.Operation_is_unsafe,
reportNode.range
);
if (relatedReportNode) {
this.errorRelated(
DiagnosticCode.Operation_is_unsafe,
reportNode.range, relatedReportNode.range
);
} else {
this.error(
DiagnosticCode.Operation_is_unsafe,
reportNode.range
);
}
}
}

Expand Down Expand Up @@ -8640,6 +8647,11 @@ export class Compiler extends DiagnosticEmitter {
if (ctor.hasDecorator(DecoratorFlags.UNSAFE)) this.checkUnsafe(expression);
}

var isManaged = classReference.type.isManaged;
if (!isManaged) {
this.checkUnsafe(expression, findDecorator(DecoratorKind.UNMANAGED, classReference.decoratorNodes));
}

// check and compile field values
var names = expression.names;
var numNames = names.length;
Expand All @@ -8648,7 +8660,9 @@ export class Compiler extends DiagnosticEmitter {
var hasErrors = false;
var exprs = new Array<ExpressionRef>(numNames + 2);
var flow = this.currentFlow;
var tempLocal = flow.getAutoreleaseLocal(classReference.type);
var tempLocal = isManaged
? flow.getAutoreleaseLocal(classReference.type)
: flow.getTempLocal(classReference.type);
assert(numNames == values.length);
for (let i = 0, k = numNames; i < k; ++i) {
let member = members ? members.get(names[i].text) : null;
Expand Down Expand Up @@ -8676,14 +8690,15 @@ export class Compiler extends DiagnosticEmitter {
// allocate a new instance first and assign 'this' to the temp. local
exprs[0] = module.local_set(
tempLocal.index,
this.makeRetain(
this.makeAllocation(classReference)
)
isManaged
? this.makeRetain(this.makeAllocation(classReference))
: this.makeAllocation(classReference)
);

// once all field values have been set, return 'this'
exprs[exprs.length - 1] = module.local_get(tempLocal.index, this.options.nativeSizeType);

if (!isManaged) flow.freeTempLocal(tempLocal);
this.currentType = classReference.type;
return module.flatten(exprs, this.options.nativeSizeType);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/std/object-literal-unmanaged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"asc_flags": [
"--runtime half",
"--use ASC_RTRACE=1"
]
}
Loading