Skip to content

Fix value confusion in recursive template literals #2070

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 1 commit into from
Sep 13, 2021
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
91 changes: 58 additions & 33 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8136,7 +8136,8 @@ export class Compiler extends DiagnosticEmitter {
var parts = expression.parts;
var numParts = parts.length;
var expressions = expression.expressions;
assert(numParts - 1 == expressions.length);
var numExpressions = expressions.length;
assert(numExpressions == numParts - 1);

var module = this.module;
var stringInstance = this.program.stringInstance;
Expand Down Expand Up @@ -8202,8 +8203,8 @@ export class Compiler extends DiagnosticEmitter {
return this.makeCallDirect(concatMethod, [ lhs, rhs ], expression);
}

// Compile to a `StaticArray<string>#join("") for general case
let length = 2 * numParts - 1;
// Compile to a `StaticArray<string>#join("") in the general case
let length = numParts + numExpressions;
let values = new Array<usize>(length);
values[0] = this.ensureStaticString(parts[0]);
for (let i = 1; i < numParts; ++i) {
Expand All @@ -8215,19 +8216,33 @@ export class Compiler extends DiagnosticEmitter {
let offset = i64_add(segment.offset, i64_new(this.program.totalOverhead));
let joinInstance = assert(arrayInstance.getMethod("join"));
let indexedSetInstance = assert(arrayInstance.lookupOverload(OperatorKind.INDEXED_SET, true));
let stmts = new Array<ExpressionRef>(numParts);
for (let i = 0, k = numParts - 1; i < k; ++i) {
let stmts = new Array<ExpressionRef>(2 * numExpressions + 1);
// Use one local per toString'ed subexpression, since otherwise recursion on the same
// static array would overwrite already prepared parts. Avoids a temporary array.
let temps = new Array<Local>(numExpressions);
let flow = this.currentFlow;
for (let i = 0; i < numExpressions; ++i) {
let expression = expressions[i];
stmts[i] = this.makeCallDirect(indexedSetInstance, [
module.usize(offset),
module.i32(2 * i + 1),
let temp = flow.getTempLocal(stringType);
temps[i] = temp;
stmts[i] = module.local_set(temp.index,
this.makeToString(
this.compileExpression(expression, stringType),
this.currentType, expression
)
),
true
);
}
// Populate the static array with the toString'ed subexpressions and call .join("")
for (let i = 0; i < numExpressions; ++i) {
stmts[numExpressions + i] = this.makeCallDirect(indexedSetInstance, [
module.usize(offset),
module.i32(2 * i + 1),
module.local_get(temps[i].index, stringType.toRef())
], expression);
flow.freeTempLocal(temps[i]);
}
stmts[numParts - 1] = this.makeCallDirect(joinInstance, [
stmts[2 * numExpressions] = this.makeCallDirect(joinInstance, [
module.usize(offset),
this.ensureStaticString("")
], expression);
Expand Down Expand Up @@ -8385,19 +8400,7 @@ export class Compiler extends DiagnosticEmitter {

// otherwise allocate a new array header and make it wrap a copy of the static buffer
} else {
// __newArray(length, alignLog2, classId, staticBuffer)
let expr = this.makeCallDirect(program.newArrayInstance, [
module.i32(length),
program.options.isWasm64
? module.i64(elementType.alignLog2)
: module.i32(elementType.alignLog2),
module.i32(arrayInstance.id),
program.options.isWasm64
? module.i64(i64_low(bufferAddress), i64_high(bufferAddress))
: module.i32(i64_low(bufferAddress))
], expression);
this.currentType = arrayType;
return expr;
return this.makeNewArray(arrayInstance, length, bufferAddress, expression);
}
}

Expand All @@ -8419,16 +8422,7 @@ export class Compiler extends DiagnosticEmitter {
// tempThis = __newArray(length, alignLog2, classId, source = 0)
stmts.push(
module.local_set(tempThis.index,
this.makeCallDirect(program.newArrayInstance, [
module.i32(length),
program.options.isWasm64
? module.i64(elementType.alignLog2)
: module.i32(elementType.alignLog2),
module.i32(arrayInstance.id),
program.options.isWasm64
? module.i64(0)
: module.i32(0)
], expression),
this.makeNewArray(arrayInstance, length, i64_new(0), expression),
arrayType.isManaged
)
);
Expand Down Expand Up @@ -8466,6 +8460,37 @@ export class Compiler extends DiagnosticEmitter {
return module.flatten(stmts, arrayTypeRef);
}

/** Makes a new array instance from a static buffer segment. */
private makeNewArray(
/** Concrete array class. */
arrayInstance: Class,
/** Length of the array. */
length: i32,
/** Source address to copy from. Array is zeroed if `0`. */
source: i64,
/** Report node. */
reportNode: Node
): ExpressionRef {
var program = this.program;
var module = this.module;
assert(!arrayInstance.extends(program.staticArrayPrototype));
var elementType = arrayInstance.getArrayValueType(); // asserts

// __newArray(length, alignLog2, classId, staticBuffer)
var expr = this.makeCallDirect(program.newArrayInstance, [
module.i32(length),
program.options.isWasm64
? module.i64(elementType.alignLog2)
: module.i32(elementType.alignLog2),
module.i32(arrayInstance.id),
program.options.isWasm64
? module.i64(i64_low(source), i64_high(source))
: module.i32(i64_low(source))
], reportNode);
this.currentType = arrayInstance.type;
return expr;
}

/** Compiles a special `fixed` array literal. */
private compileStaticArrayLiteral(
expression: ArrayLiteralExpression,
Expand Down
Loading