Skip to content

Commit c8ecb53

Browse files
authoredJun 13, 2022
Refactor to string interpolation where possible in compiler (#2319)
1 parent 67457f3 commit c8ecb53

File tree

8 files changed

+80
-78
lines changed

8 files changed

+80
-78
lines changed
 

‎src/builtins.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -9956,7 +9956,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
99569956
var base = instance.base;
99579957
if (base) {
99589958
body.push(
9959-
module.call(base.internalName + "~visit", [
9959+
module.call(`${base.internalName}~visit`, [
99609960
module.local_get(0, sizeTypeRef), // this
99619961
module.local_get(1, TypeRef.I32) // cookie
99629962
], TypeRef.None)
@@ -10035,7 +10035,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
1003510035
}
1003610036

1003710037
// Create the visitor function
10038-
instance.visitRef = module.addFunction(instance.internalName + "~visit",
10038+
instance.visitRef = module.addFunction(`${instance.internalName}~visit`,
1003910039
createType([sizeTypeRef, TypeRef.I32]),
1004010040
TypeRef.None,
1004110041
needsTempValue ? [ sizeTypeRef ] : null,
@@ -10073,7 +10073,7 @@ export function compileVisitMembers(compiler: Compiler): void {
1007310073
cases[i] = module.return();
1007410074
} else {
1007510075
cases[i] = module.block(null, [
10076-
module.call(instance.internalName + "~visit", [
10076+
module.call(`${instance.internalName}~visit`, [
1007710077
module.local_get(0, sizeTypeRef), // this
1007810078
module.local_get(1, TypeRef.I32) // cookie
1007910079
], TypeRef.None),
@@ -10252,7 +10252,13 @@ export function compileClassInstanceOf(compiler: Compiler, prototype: ClassProto
1025210252
)
1025310253
);
1025410254

10255-
module.addFunction(prototype.internalName + "~instanceof", sizeTypeRef, TypeRef.I32, null, module.flatten(stmts));
10255+
module.addFunction(
10256+
`${prototype.internalName}~instanceof`,
10257+
sizeTypeRef,
10258+
TypeRef.I32,
10259+
null,
10260+
module.flatten(stmts)
10261+
);
1025610262
}
1025710263

1025810264
// Helpers

‎src/compiler.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -2341,11 +2341,11 @@ export class Compiler extends DiagnosticEmitter {
23412341
var flowBefore = flow.fork();
23422342
this.currentFlow = flow;
23432343

2344-
var breakLabel = "do-break|" + label;
2344+
var breakLabel = `do-break|${label}`;
23452345
flow.breakLabel = breakLabel;
2346-
var continueLabel = "do-continue|" + label;
2346+
var continueLabel = `do-continue|${label}`;
23472347
flow.continueLabel = continueLabel;
2348-
var loopLabel = "do-loop|" + label;
2348+
var loopLabel = `do-loop|${label}`;
23492349

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

2490-
var breakLabel = "for-break" + label;
2490+
var breakLabel = `for-break${label}`;
24912491
flow.breakLabel = breakLabel;
2492-
var continueLabel = "for-continue|" + label;
2492+
var continueLabel = `for-continue|${label}`;
24932493
flow.continueLabel = continueLabel;
2494-
var loopLabel = "for-loop|" + label;
2494+
var loopLabel = `for-loop|${label}`;
24952495

24962496
// Compile initializer if present
24972497
var initializer = statement.initializer;
@@ -2842,7 +2842,7 @@ export class Compiler extends DiagnosticEmitter {
28422842
let case_ = cases[i];
28432843
let label = case_.label;
28442844
if (label) {
2845-
breaks[breakIndex++] = module.br("case" + i.toString() + "|" + context,
2845+
breaks[breakIndex++] = module.br(`case${i}|${context}`,
28462846
module.binary(BinaryOp.EqI32,
28472847
module.local_get(tempLocalIndex, TypeRef.I32),
28482848
this.compileExpression(label, Type.u32,
@@ -2858,13 +2858,13 @@ export class Compiler extends DiagnosticEmitter {
28582858
outerFlow.freeTempLocal(tempLocal);
28592859

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

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

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

3197-
var breakLabel = "while-break|" + label;
3197+
var breakLabel = `while-break|${label}`;
31983198
flow.breakLabel = breakLabel;
3199-
var continueLabel = "while-continue|" + label;
3199+
var continueLabel = `while-continue|${label}`;
32003200
flow.continueLabel = continueLabel;
32013201

32023202
// Precompute the condition
@@ -6812,10 +6812,9 @@ export class Compiler extends DiagnosticEmitter {
68126812
// create a br_table switching over the number of optional parameters provided
68136813
var numNames = numOptional + 1; // incl. outer block
68146814
var names = new Array<string>(numNames);
6815-
var ofN = "of" + numOptional.toString();
6815+
var ofN = `of${numOptional}`;
68166816
for (let i = 0; i < numNames; ++i) {
6817-
let label = i.toString() + ofN;
6818-
names[i] = label;
6817+
names[i] = `${i}${ofN}`;
68196818
}
68206819
var argumentsLength = this.ensureArgumentsLength();
68216820
var table = module.block(names[0], [
@@ -7373,7 +7372,7 @@ export class Compiler extends DiagnosticEmitter {
73737372
var isSemanticallyAnonymous = !isNamed || contextualType != Type.void;
73747373
var prototype = new FunctionPrototype(
73757374
isSemanticallyAnonymous
7376-
? (isNamed ? declaration.name.text + "|" : "anonymous|") + (actualFunction.nextAnonymousId++).toString()
7375+
? `${isNamed ? declaration.name.text : "anonymous"}|${actualFunction.nextAnonymousId++}`
73777376
: declaration.name.text,
73787377
actualFunction,
73797378
declaration,
@@ -7974,7 +7973,7 @@ export class Compiler extends DiagnosticEmitter {
79747973
// dynamic check against all possible concrete ids
79757974
} else if (prototype.extends(classReference.prototype)) {
79767975
this.pendingClassInstanceOf.add(prototype);
7977-
return module.call(prototype.internalName + "~instanceof", [ expr ], TypeRef.I32);
7976+
return module.call(`${prototype.internalName}~instanceof`, [ expr ], TypeRef.I32);
79787977
}
79797978
}
79807979

‎src/diagnostics.ts

+9-23
Original file line numberDiff line numberDiff line change
@@ -142,33 +142,19 @@ export class DiagnosticMessage {
142142

143143
/** Converts this message to a string. */
144144
toString(): string {
145+
var category = diagnosticCategoryToString(this.category);
145146
var range = this.range;
147+
var code = this.code;
148+
var message = this.message;
146149
if (range) {
147150
let source = range.source;
148-
return (
149-
diagnosticCategoryToString(this.category) +
150-
" " +
151-
this.code.toString() +
152-
": \"" +
153-
this.message +
154-
"\" in " +
155-
source.normalizedPath +
156-
"(" +
157-
source.lineAt(range.start).toString() +
158-
"," +
159-
source.columnAt().toString() +
160-
"+" +
161-
(range.end - range.start).toString() +
162-
")"
163-
);
151+
let path = source.normalizedPath;
152+
let line = source.lineAt(range.start);
153+
let column = source.columnAt();
154+
let len = range.end - range.start;
155+
return `${category} ${code}: "${message}" in ${path}(${line},${column}+${len})`;
164156
}
165-
return (
166-
diagnosticCategoryToString(this.category) +
167-
" " +
168-
this.code.toString() +
169-
": " +
170-
this.message
171-
);
157+
return `${category} ${code}: ${message}`;
172158
}
173159
}
174160

‎src/flow.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class Flow {
207207
static createInline(parentFunction: Function, inlineFunction: Function): Flow {
208208
var flow = new Flow(parentFunction);
209209
flow.inlineFunction = inlineFunction;
210-
flow.inlineReturnLabel = inlineFunction.internalName + "|inlined." + (inlineFunction.nextInlineId++).toString();
210+
flow.inlineReturnLabel = `${inlineFunction.internalName}|inlined.${(inlineFunction.nextInlineId++)}`;
211211
if (inlineFunction.is(CommonFlags.CONSTRUCTOR)) {
212212
flow.initThisFieldFlags();
213213
}
@@ -1506,7 +1506,7 @@ export class Flow {
15061506
if (this.is(FlowFlags.CONDITIONALLY_CONTINUES)) sb.push("CONDITIONALLY_CONTINUES");
15071507
if (this.is(FlowFlags.CONDITIONALLY_ACCESSES_THIS)) sb.push("CONDITIONALLY_ACCESSES_THIS");
15081508
if (this.is(FlowFlags.MAY_RETURN_NONTHIS)) sb.push("MAY_RETURN_NONTHIS");
1509-
return "Flow(" + this.actualFunction.toString() + ")[" + levels.toString() + "] " + sb.join(" ");
1509+
return `Flow(${this.actualFunction})[${levels}] ${sb.join(" ")}`;
15101510
}
15111511
}
15121512

‎src/module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3028,7 +3028,7 @@ export class SwitchBuilder {
30283028
var entry = new Array<ExpressionRef>(1 + numValues + 1);
30293029
var labels = new Array<string>(numCases);
30303030
for (let i = 0; i < numCases; ++i) {
3031-
labels[i] = "case" + i.toString() + labelPostfix;
3031+
labels[i] = `case${i}${labelPostfix}`;
30323032
}
30333033
entry[0] = module.local_set(localIndex, this.condition, false); // u32
30343034
for (let i = 0; i < numValues; ++i) {
@@ -3041,7 +3041,7 @@ export class SwitchBuilder {
30413041
);
30423042
}
30433043
var defaultIndex = this.defaultIndex;
3044-
var defaultLabel = "default" + labelPostfix;
3044+
var defaultLabel = `default${labelPostfix}`;
30453045
entry[1 + numValues] = module.br(
30463046
~defaultIndex
30473047
? labels[defaultIndex]

‎src/program.ts

+30-17
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,8 @@ export class Program extends DiagnosticEmitter {
15431543
/** Requires that a global library element of the specified kind is present and returns it. */
15441544
private require(name: string, kind: ElementKind): Element {
15451545
var element = this.lookup(name);
1546-
if (!element) throw new Error("Missing standard library component: " + name);
1547-
if (element.kind != kind) throw Error("Invalid standard library component kind: " + name);
1546+
if (!element) throw new Error(`Missing standard library component: ${name}`);
1547+
if (element.kind != kind) throw Error(`Invalid standard library component kind: ${name}`);
15481548
return element;
15491549
}
15501550

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

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

@@ -2873,7 +2873,7 @@ export abstract class Element {
28732873

28742874
/** Returns a string representation of this element. */
28752875
toString(): string {
2876-
return this.internalName + ", kind=" + this.kind.toString();
2876+
return `${this.internalName}, kind=${this.kind}`;
28772877
}
28782878
}
28792879

@@ -3049,7 +3049,7 @@ export class File extends Element {
30493049
assert(!program.filesByName.has(this.internalName));
30503050
program.filesByName.set(this.internalName, this);
30513051
var startFunction = this.program.makeNativeFunction(
3052-
"start:" + this.internalName,
3052+
`start:${this.internalName}`,
30533053
new Signature(program, null, Type.void),
30543054
this
30553055
);
@@ -3693,9 +3693,7 @@ export class Function extends TypedElement {
36933693
// if it has a name, check previously as this method will throw otherwise
36943694
var localIndex = this.signature.parameterTypes.length + this.additionalLocals.length;
36953695
if (this.is(CommonFlags.INSTANCE)) ++localIndex;
3696-
var localName = name != null
3697-
? name
3698-
: "var$" + localIndex.toString();
3696+
var localName = name != null ? name : `var$${localIndex}`;
36993697
if (!declaration) declaration = this.program.makeNativeVariableDeclaration(localName);
37003698
var local = new Local(
37013699
localName,
@@ -3851,31 +3849,39 @@ export class Field extends VariableLikeElement {
38513849
/** Gets the internal name of the respective getter function. */
38523850
get internalGetterName(): string {
38533851
var cached = this._internalGetterName;
3854-
if (cached == null) this._internalGetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + GETTER_PREFIX + this.name;
3852+
if (cached == null) {
3853+
this._internalGetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${GETTER_PREFIX}${this.name}`;
3854+
}
38553855
return cached;
38563856
}
38573857
private _internalGetterName: string | null = null;
38583858

38593859
/** Gets the internal name of the respective setter function. */
38603860
get internalSetterName(): string {
38613861
var cached = this._internalSetterName;
3862-
if (cached == null) this._internalSetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + SETTER_PREFIX + this.name;
3862+
if (cached == null) {
3863+
this._internalSetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${SETTER_PREFIX}${this.name}`;
3864+
}
38633865
return cached;
38643866
}
38653867
private _internalSetterName: string | null = null;
38663868

38673869
/** Gets the signature of the respective getter function. */
38683870
get internalGetterSignature(): Signature {
38693871
var cached = this._internalGetterSignature;
3870-
if (!cached) this._internalGetterSignature = cached = new Signature(this.program, null, this.type, this.thisType);
3872+
if (!cached) {
3873+
this._internalGetterSignature = cached = new Signature(this.program, null, this.type, this.thisType);
3874+
}
38713875
return cached;
38723876
}
38733877
private _internalGetterSignature: Signature | null = null;
38743878

38753879
/** Gets the signature of the respective setter function. */
38763880
get internalSetterSignature(): Signature {
38773881
var cached = this._internalSetterSignature;
3878-
if (!cached) this._internalGetterSignature = cached = new Signature(this.program, [ this.type ], Type.void, this.thisType);
3882+
if (!cached) {
3883+
this._internalGetterSignature = cached = new Signature(this.program, [ this.type ], Type.void, this.thisType);
3884+
}
38793885
return cached;
38803886
}
38813887
private _internalSetterSignature: Signature | null = null;
@@ -4720,7 +4726,12 @@ function copyMembers(src: Element, dest: Element): void {
47204726
}
47214727

47224728
/** Mangles the internal name of an element with the specified name that is a child of the given parent. */
4723-
export function mangleInternalName(name: string, parent: Element, isInstance: bool, asGlobal: bool = false): string {
4729+
export function mangleInternalName(
4730+
name: string,
4731+
parent: Element,
4732+
isInstance: bool,
4733+
asGlobal: bool = false
4734+
): string {
47244735
switch (parent.kind) {
47254736
case ElementKind.FILE: {
47264737
if (asGlobal) return name;
@@ -4737,8 +4748,10 @@ export function mangleInternalName(name: string, parent: Element, isInstance: bo
47374748
// fall-through
47384749
}
47394750
default: {
4740-
return mangleInternalName(parent.name, parent.parent, parent.is(CommonFlags.INSTANCE), asGlobal)
4741-
+ (isInstance ? INSTANCE_DELIMITER : STATIC_DELIMITER) + name;
4751+
return (
4752+
mangleInternalName(parent.name, parent.parent, parent.is(CommonFlags.INSTANCE), asGlobal) +
4753+
(isInstance ? INSTANCE_DELIMITER : STATIC_DELIMITER) + name
4754+
);
47424755
}
47434756
}
47444757
}
@@ -4749,7 +4762,7 @@ var cachedDefaultParameterNames: string[] = [];
47494762
/** Gets the cached default parameter name for the specified index. */
47504763
export function getDefaultParameterName(index: i32): string {
47514764
for (let i = cachedDefaultParameterNames.length; i <= index; ++i) {
4752-
cachedDefaultParameterNames.push("$" + i.toString());
4765+
cachedDefaultParameterNames.push(`$${i}`);
47534766
}
47544767
return cachedDefaultParameterNames[index];
47554768
}

‎src/resolver.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class Resolver extends DiagnosticEmitter {
239239
if (reportMode == ReportMode.REPORT) {
240240
this.error(
241241
DiagnosticCode.Type_0_cannot_be_nullable,
242-
node.range, element.name + "/i32"
242+
node.range, `${element.name}/i32`
243243
);
244244
}
245245
}
@@ -2722,7 +2722,7 @@ export class Resolver extends DiagnosticEmitter {
27222722
signature.requiredParameters = requiredParameters;
27232723

27242724
var nameInclTypeParameters = prototype.name;
2725-
if (instanceKey.length) nameInclTypeParameters += "<" + instanceKey + ">";
2725+
if (instanceKey.length) nameInclTypeParameters += `<${instanceKey}>`;
27262726
var instance = new Function(
27272727
nameInclTypeParameters,
27282728
prototype,
@@ -2897,7 +2897,7 @@ export class Resolver extends DiagnosticEmitter {
28972897

28982898
// Otherwise create
28992899
var nameInclTypeParamters = prototype.name;
2900-
if (instanceKey.length) nameInclTypeParamters += "<" + instanceKey + ">";
2900+
if (instanceKey.length) nameInclTypeParamters += `<${instanceKey}>`;
29012901
if (prototype.kind == ElementKind.INTERFACE_PROTOTYPE) {
29022902
instance = new Interface(nameInclTypeParamters, <InterfacePrototype>prototype, typeArguments);
29032903
} else {

‎src/types.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class Type {
222222
get isFloatValue(): bool {
223223
return this.is(TypeFlags.FLOAT | TypeFlags.VALUE);
224224
}
225-
225+
226226
/** Tests if this type represents a numeric (integer or floating point) value. */
227227
get isNumericValue(): bool {
228228
return this.isIntegerValue || this.isFloatValue;
@@ -237,7 +237,7 @@ export class Type {
237237
get isVectorValue(): bool {
238238
return this.is(TypeFlags.VECTOR | TypeFlags.VALUE);
239239
}
240-
240+
241241
/** Tests if this type represents an internal or external reference. */
242242
get isReference(): bool {
243243
return this.is(TypeFlags.REFERENCE);
@@ -466,9 +466,7 @@ export class Type {
466466

467467
/** Converts this type to a string. */
468468
toString(validWat: bool = false): string {
469-
const nullablePostfix = validWat
470-
? "|null"
471-
: " | null";
469+
const nullablePostfix = validWat ? "|null" : " | null";
472470
if (this.isReference) {
473471
let classReference = this.getClass();
474472
if (classReference) {
@@ -479,7 +477,7 @@ export class Type {
479477
let signatureReference = this.getSignature();
480478
if (signatureReference) {
481479
return this.isNullableReference
482-
? "(" + signatureReference.toString(validWat) + ")" + nullablePostfix
480+
? `(${signatureReference.toString(validWat)})${nullablePostfix}`
483481
: signatureReference.toString(validWat);
484482
}
485483
}

0 commit comments

Comments
 (0)
Please sign in to comment.