Skip to content

Refactor to string interpolation where possible in compiler #2319

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 4 commits into from
Jun 13, 2022
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
14 changes: 10 additions & 4 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9956,7 +9956,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
var base = instance.base;
if (base) {
body.push(
module.call(base.internalName + "~visit", [
module.call(`${base.internalName}~visit`, [
module.local_get(0, sizeTypeRef), // this
module.local_get(1, TypeRef.I32) // cookie
], TypeRef.None)
Expand Down Expand Up @@ -10035,7 +10035,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
}

// Create the visitor function
instance.visitRef = module.addFunction(instance.internalName + "~visit",
instance.visitRef = module.addFunction(`${instance.internalName}~visit`,
createType([sizeTypeRef, TypeRef.I32]),
TypeRef.None,
needsTempValue ? [ sizeTypeRef ] : null,
Expand Down Expand Up @@ -10073,7 +10073,7 @@ export function compileVisitMembers(compiler: Compiler): void {
cases[i] = module.return();
} else {
cases[i] = module.block(null, [
module.call(instance.internalName + "~visit", [
module.call(`${instance.internalName}~visit`, [
module.local_get(0, sizeTypeRef), // this
module.local_get(1, TypeRef.I32) // cookie
], TypeRef.None),
Expand Down Expand Up @@ -10252,7 +10252,13 @@ export function compileClassInstanceOf(compiler: Compiler, prototype: ClassProto
)
);

module.addFunction(prototype.internalName + "~instanceof", sizeTypeRef, TypeRef.I32, null, module.flatten(stmts));
module.addFunction(
`${prototype.internalName}~instanceof`,
sizeTypeRef,
TypeRef.I32,
null,
module.flatten(stmts)
);
}

// Helpers
Expand Down
41 changes: 20 additions & 21 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2341,11 +2341,11 @@ export class Compiler extends DiagnosticEmitter {
var flowBefore = flow.fork();
this.currentFlow = flow;

var breakLabel = "do-break|" + label;
var breakLabel = `do-break|${label}`;
flow.breakLabel = breakLabel;
var continueLabel = "do-continue|" + label;
var continueLabel = `do-continue|${label}`;
flow.continueLabel = continueLabel;
var loopLabel = "do-loop|" + label;
var loopLabel = `do-loop|${label}`;

// Compile the body (always executes)
var bodyFlow = flow.fork();
Expand Down Expand Up @@ -2487,11 +2487,11 @@ export class Compiler extends DiagnosticEmitter {
var flow = outerFlow.fork(/* resetBreakContext */ true);
this.currentFlow = flow;

var breakLabel = "for-break" + label;
var breakLabel = `for-break${label}`;
flow.breakLabel = breakLabel;
var continueLabel = "for-continue|" + label;
var continueLabel = `for-continue|${label}`;
flow.continueLabel = continueLabel;
var loopLabel = "for-loop|" + label;
var loopLabel = `for-loop|${label}`;

// Compile initializer if present
var initializer = statement.initializer;
Expand Down Expand Up @@ -2842,7 +2842,7 @@ export class Compiler extends DiagnosticEmitter {
let case_ = cases[i];
let label = case_.label;
if (label) {
breaks[breakIndex++] = module.br("case" + i.toString() + "|" + context,
breaks[breakIndex++] = module.br(`case${i}|${context}`,
module.binary(BinaryOp.EqI32,
module.local_get(tempLocalIndex, TypeRef.I32),
this.compileExpression(label, Type.u32,
Expand All @@ -2858,13 +2858,13 @@ export class Compiler extends DiagnosticEmitter {
outerFlow.freeTempLocal(tempLocal);

// otherwise br to default respectively out of the switch if there is no default case
breaks[breakIndex] = module.br((defaultIndex >= 0
? "case" + defaultIndex.toString()
: "break"
) + "|" + context);
breaks[breakIndex] = module.br(defaultIndex >= 0
? `case${defaultIndex}|${context}`
: `break|${context}`
);

// nest blocks in order
var currentBlock = module.block("case0|" + context, breaks, TypeRef.None);
var currentBlock = module.block(`case0|${context}`, breaks, TypeRef.None);
var commonCategorical = FlowFlags.ANY_CATEGORICAL;
var commonConditional = 0;
for (let i = 0; i < numCases; ++i) {
Expand All @@ -2875,11 +2875,11 @@ export class Compiler extends DiagnosticEmitter {
// Each switch case initiates a new branch
let innerFlow = outerFlow.fork();
this.currentFlow = innerFlow;
let breakLabel = "break|" + context;
let breakLabel = `break|${context}`;
innerFlow.breakLabel = breakLabel;

let isLast = i == numCases - 1;
let nextLabel = isLast ? breakLabel : "case" + (i + 1).toString() + "|" + context;
let nextLabel = isLast ? breakLabel : `case${i + 1}|${context}`;
let stmts = new Array<ExpressionRef>(1 + numStatements);
stmts[0] = currentBlock;
let count = 1;
Expand Down Expand Up @@ -3194,9 +3194,9 @@ export class Compiler extends DiagnosticEmitter {
var flowBefore = flow.fork();
this.currentFlow = flow;

var breakLabel = "while-break|" + label;
var breakLabel = `while-break|${label}`;
flow.breakLabel = breakLabel;
var continueLabel = "while-continue|" + label;
var continueLabel = `while-continue|${label}`;
flow.continueLabel = continueLabel;

// Precompute the condition
Expand Down Expand Up @@ -6812,10 +6812,9 @@ export class Compiler extends DiagnosticEmitter {
// create a br_table switching over the number of optional parameters provided
var numNames = numOptional + 1; // incl. outer block
var names = new Array<string>(numNames);
var ofN = "of" + numOptional.toString();
var ofN = `of${numOptional}`;
for (let i = 0; i < numNames; ++i) {
let label = i.toString() + ofN;
names[i] = label;
names[i] = `${i}${ofN}`;
}
var argumentsLength = this.ensureArgumentsLength();
var table = module.block(names[0], [
Expand Down Expand Up @@ -7373,7 +7372,7 @@ export class Compiler extends DiagnosticEmitter {
var isSemanticallyAnonymous = !isNamed || contextualType != Type.void;
var prototype = new FunctionPrototype(
isSemanticallyAnonymous
? (isNamed ? declaration.name.text + "|" : "anonymous|") + (actualFunction.nextAnonymousId++).toString()
? `${isNamed ? declaration.name.text : "anonymous"}|${actualFunction.nextAnonymousId++}`
: declaration.name.text,
actualFunction,
declaration,
Expand Down Expand Up @@ -7974,7 +7973,7 @@ export class Compiler extends DiagnosticEmitter {
// dynamic check against all possible concrete ids
} else if (prototype.extends(classReference.prototype)) {
this.pendingClassInstanceOf.add(prototype);
return module.call(prototype.internalName + "~instanceof", [ expr ], TypeRef.I32);
return module.call(`${prototype.internalName}~instanceof`, [ expr ], TypeRef.I32);
}
}

Expand Down
32 changes: 9 additions & 23 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,19 @@ export class DiagnosticMessage {

/** Converts this message to a string. */
toString(): string {
var category = diagnosticCategoryToString(this.category);
var range = this.range;
var code = this.code;
var message = this.message;
if (range) {
let source = range.source;
return (
diagnosticCategoryToString(this.category) +
" " +
this.code.toString() +
": \"" +
this.message +
"\" in " +
source.normalizedPath +
"(" +
source.lineAt(range.start).toString() +
"," +
source.columnAt().toString() +
"+" +
(range.end - range.start).toString() +
")"
);
let path = source.normalizedPath;
let line = source.lineAt(range.start);
let column = source.columnAt();
let len = range.end - range.start;
return `${category} ${code}: "${message}" in ${path}(${line},${column}+${len})`;
}
return (
diagnosticCategoryToString(this.category) +
" " +
this.code.toString() +
": " +
this.message
);
return `${category} ${code}: ${message}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class Flow {
static createInline(parentFunction: Function, inlineFunction: Function): Flow {
var flow = new Flow(parentFunction);
flow.inlineFunction = inlineFunction;
flow.inlineReturnLabel = inlineFunction.internalName + "|inlined." + (inlineFunction.nextInlineId++).toString();
flow.inlineReturnLabel = `${inlineFunction.internalName}|inlined.${(inlineFunction.nextInlineId++)}`;
if (inlineFunction.is(CommonFlags.CONSTRUCTOR)) {
flow.initThisFieldFlags();
}
Expand Down Expand Up @@ -1506,7 +1506,7 @@ export class Flow {
if (this.is(FlowFlags.CONDITIONALLY_CONTINUES)) sb.push("CONDITIONALLY_CONTINUES");
if (this.is(FlowFlags.CONDITIONALLY_ACCESSES_THIS)) sb.push("CONDITIONALLY_ACCESSES_THIS");
if (this.is(FlowFlags.MAY_RETURN_NONTHIS)) sb.push("MAY_RETURN_NONTHIS");
return "Flow(" + this.actualFunction.toString() + ")[" + levels.toString() + "] " + sb.join(" ");
return `Flow(${this.actualFunction})[${levels}] ${sb.join(" ")}`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3028,7 +3028,7 @@ export class SwitchBuilder {
var entry = new Array<ExpressionRef>(1 + numValues + 1);
var labels = new Array<string>(numCases);
for (let i = 0; i < numCases; ++i) {
labels[i] = "case" + i.toString() + labelPostfix;
labels[i] = `case${i}${labelPostfix}`;
}
entry[0] = module.local_set(localIndex, this.condition, false); // u32
for (let i = 0; i < numValues; ++i) {
Expand All @@ -3041,7 +3041,7 @@ export class SwitchBuilder {
);
}
var defaultIndex = this.defaultIndex;
var defaultLabel = "default" + labelPostfix;
var defaultLabel = `default${labelPostfix}`;
entry[1 + numValues] = module.br(
~defaultIndex
? labels[defaultIndex]
Expand Down
47 changes: 30 additions & 17 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,8 @@ export class Program extends DiagnosticEmitter {
/** Requires that a global library element of the specified kind is present and returns it. */
private require(name: string, kind: ElementKind): Element {
var element = this.lookup(name);
if (!element) throw new Error("Missing standard library component: " + name);
if (element.kind != kind) throw Error("Invalid standard library component kind: " + name);
if (!element) throw new Error(`Missing standard library component: ${name}`);
if (element.kind != kind) throw Error(`Invalid standard library component kind: ${name}`);
return element;
}

Expand All @@ -1557,15 +1557,15 @@ export class Program extends DiagnosticEmitter {
requireClass(name: string): Class {
var prototype = this.require(name, ElementKind.CLASS_PROTOTYPE);
var resolved = this.resolver.resolveClass(<ClassPrototype>prototype, null);
if (!resolved) throw new Error("Invalid standard library class: " + name);
if (!resolved) throw new Error(`Invalid standard library class: ${name}`);
return resolved;
}

/** Requires that a global function is present and returns it. */
requireFunction(name: string, typeArguments: Type[] | null = null): Function {
var prototype = <FunctionPrototype>this.require(name, ElementKind.FUNCTION_PROTOTYPE);
var resolved = this.resolver.resolveFunction(prototype, typeArguments);
if (!resolved) throw new Error("Invalid standard library function: " + name);
if (!resolved) throw new Error(`Invalid standard library function: ${name}`);
return resolved;
}

Expand Down Expand Up @@ -2873,7 +2873,7 @@ export abstract class Element {

/** Returns a string representation of this element. */
toString(): string {
return this.internalName + ", kind=" + this.kind.toString();
return `${this.internalName}, kind=${this.kind}`;
}
}

Expand Down Expand Up @@ -3049,7 +3049,7 @@ export class File extends Element {
assert(!program.filesByName.has(this.internalName));
program.filesByName.set(this.internalName, this);
var startFunction = this.program.makeNativeFunction(
"start:" + this.internalName,
`start:${this.internalName}`,
new Signature(program, null, Type.void),
this
);
Expand Down Expand Up @@ -3693,9 +3693,7 @@ export class Function extends TypedElement {
// if it has a name, check previously as this method will throw otherwise
var localIndex = this.signature.parameterTypes.length + this.additionalLocals.length;
if (this.is(CommonFlags.INSTANCE)) ++localIndex;
var localName = name != null
? name
: "var$" + localIndex.toString();
var localName = name != null ? name : `var$${localIndex}`;
if (!declaration) declaration = this.program.makeNativeVariableDeclaration(localName);
var local = new Local(
localName,
Expand Down Expand Up @@ -3851,31 +3849,39 @@ export class Field extends VariableLikeElement {
/** Gets the internal name of the respective getter function. */
get internalGetterName(): string {
var cached = this._internalGetterName;
if (cached == null) this._internalGetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + GETTER_PREFIX + this.name;
if (cached == null) {
this._internalGetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${GETTER_PREFIX}${this.name}`;
}
return cached;
}
private _internalGetterName: string | null = null;

/** Gets the internal name of the respective setter function. */
get internalSetterName(): string {
var cached = this._internalSetterName;
if (cached == null) this._internalSetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + SETTER_PREFIX + this.name;
if (cached == null) {
this._internalSetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${SETTER_PREFIX}${this.name}`;
}
return cached;
}
private _internalSetterName: string | null = null;

/** Gets the signature of the respective getter function. */
get internalGetterSignature(): Signature {
var cached = this._internalGetterSignature;
if (!cached) this._internalGetterSignature = cached = new Signature(this.program, null, this.type, this.thisType);
if (!cached) {
this._internalGetterSignature = cached = new Signature(this.program, null, this.type, this.thisType);
}
return cached;
}
private _internalGetterSignature: Signature | null = null;

/** Gets the signature of the respective setter function. */
get internalSetterSignature(): Signature {
var cached = this._internalSetterSignature;
if (!cached) this._internalGetterSignature = cached = new Signature(this.program, [ this.type ], Type.void, this.thisType);
if (!cached) {
this._internalGetterSignature = cached = new Signature(this.program, [ this.type ], Type.void, this.thisType);
}
return cached;
}
private _internalSetterSignature: Signature | null = null;
Expand Down Expand Up @@ -4720,7 +4726,12 @@ function copyMembers(src: Element, dest: Element): void {
}

/** Mangles the internal name of an element with the specified name that is a child of the given parent. */
export function mangleInternalName(name: string, parent: Element, isInstance: bool, asGlobal: bool = false): string {
export function mangleInternalName(
name: string,
parent: Element,
isInstance: bool,
asGlobal: bool = false
): string {
switch (parent.kind) {
case ElementKind.FILE: {
if (asGlobal) return name;
Expand All @@ -4737,8 +4748,10 @@ export function mangleInternalName(name: string, parent: Element, isInstance: bo
// fall-through
}
default: {
return mangleInternalName(parent.name, parent.parent, parent.is(CommonFlags.INSTANCE), asGlobal)
+ (isInstance ? INSTANCE_DELIMITER : STATIC_DELIMITER) + name;
return (
mangleInternalName(parent.name, parent.parent, parent.is(CommonFlags.INSTANCE), asGlobal) +
(isInstance ? INSTANCE_DELIMITER : STATIC_DELIMITER) + name
);
}
}
}
Expand All @@ -4749,7 +4762,7 @@ var cachedDefaultParameterNames: string[] = [];
/** Gets the cached default parameter name for the specified index. */
export function getDefaultParameterName(index: i32): string {
for (let i = cachedDefaultParameterNames.length; i <= index; ++i) {
cachedDefaultParameterNames.push("$" + i.toString());
cachedDefaultParameterNames.push(`$${i}`);
}
return cachedDefaultParameterNames[index];
}
Loading