From 251dbe1caa63236c8d8394af81ae75fa90a161e8 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 12 Jun 2022 10:27:49 +0300 Subject: [PATCH 1/4] refactor --- src/builtins.ts | 14 ++++++++++---- src/compiler.ts | 35 +++++++++++++++++----------------- src/diagnostics.ts | 32 +++++++++---------------------- src/flow.ts | 4 ++-- src/module.ts | 4 ++-- src/program.ts | 47 +++++++++++++++++++++++++++++----------------- src/resolver.ts | 6 +++--- src/types.ts | 10 ++++------ 8 files changed, 77 insertions(+), 75 deletions(-) diff --git a/src/builtins.ts b/src/builtins.ts index 8859d4e237..6e8cd28d4e 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -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) @@ -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, @@ -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), @@ -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 diff --git a/src/compiler.ts b/src/compiler.ts index 06a1871b90..11b3450edf 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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(); @@ -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; @@ -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, @@ -2859,12 +2859,12 @@ export class Compiler extends DiagnosticEmitter { // otherwise br to default respectively out of the switch if there is no default case breaks[breakIndex] = module.br((defaultIndex >= 0 - ? "case" + defaultIndex.toString() + ? `case${defaultIndex}` : "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) { @@ -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(1 + numStatements); stmts[0] = currentBlock; let count = 1; @@ -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 @@ -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(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], [ @@ -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, @@ -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); } } diff --git a/src/diagnostics.ts b/src/diagnostics.ts index ae9221b4c5..93123291d3 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -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}`; } } diff --git a/src/flow.ts b/src/flow.ts index 21680eaf8d..8e37112692 100644 --- a/src/flow.ts +++ b/src/flow.ts @@ -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(); } @@ -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(" ")}`; } } diff --git a/src/module.ts b/src/module.ts index cb385aad55..2f1c01c4c7 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3028,7 +3028,7 @@ export class SwitchBuilder { var entry = new Array(1 + numValues + 1); var labels = new Array(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) { @@ -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] diff --git a/src/program.ts b/src/program.ts index 4e28ba2478..dafa1abaa5 100644 --- a/src/program.ts +++ b/src/program.ts @@ -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; } @@ -1557,7 +1557,7 @@ export class Program extends DiagnosticEmitter { requireClass(name: string): Class { var prototype = this.require(name, ElementKind.CLASS_PROTOTYPE); var resolved = this.resolver.resolveClass(prototype, null); - if (!resolved) throw new Error("Invalid standard library class: " + name); + if (!resolved) throw new Error(`Invalid standard library class: ${name}`); return resolved; } @@ -1565,7 +1565,7 @@ export class Program extends DiagnosticEmitter { requireFunction(name: string, typeArguments: Type[] | null = null): Function { var prototype = 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; } @@ -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}`; } } @@ -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 ); @@ -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, @@ -3851,7 +3849,9 @@ 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; @@ -3859,7 +3859,9 @@ export class Field extends VariableLikeElement { /** 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; @@ -3867,7 +3869,9 @@ export class Field extends VariableLikeElement { /** 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; @@ -3875,7 +3879,9 @@ export class Field extends VariableLikeElement { /** 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; @@ -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; @@ -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 + ); } } } @@ -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]; } diff --git a/src/resolver.ts b/src/resolver.ts index db485a0973..61a7621886 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -239,7 +239,7 @@ export class Resolver extends DiagnosticEmitter { if (reportMode == ReportMode.REPORT) { this.error( DiagnosticCode.Type_0_cannot_be_nullable, - node.range, element.name + "/i32" + node.range, `${element.name}/i32` ); } } @@ -2722,7 +2722,7 @@ export class Resolver extends DiagnosticEmitter { signature.requiredParameters = requiredParameters; var nameInclTypeParameters = prototype.name; - if (instanceKey.length) nameInclTypeParameters += "<" + instanceKey + ">"; + if (instanceKey.length) nameInclTypeParameters += `<${instanceKey}>`; var instance = new Function( nameInclTypeParameters, prototype, @@ -2897,7 +2897,7 @@ export class Resolver extends DiagnosticEmitter { // Otherwise create var nameInclTypeParamters = prototype.name; - if (instanceKey.length) nameInclTypeParamters += "<" + instanceKey + ">"; + if (instanceKey.length) nameInclTypeParamters += `<${instanceKey}>`; if (prototype.kind == ElementKind.INTERFACE_PROTOTYPE) { instance = new Interface(nameInclTypeParamters, prototype, typeArguments); } else { diff --git a/src/types.ts b/src/types.ts index ac2ee9c1f4..d0d48fdb2e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -222,7 +222,7 @@ export class Type { get isFloatValue(): bool { return this.is(TypeFlags.FLOAT | TypeFlags.VALUE); } - + /** Tests if this type represents a numeric (integer or floating point) value. */ get isNumericValue(): bool { return this.isIntegerValue || this.isFloatValue; @@ -237,7 +237,7 @@ export class Type { get isVectorValue(): bool { return this.is(TypeFlags.VECTOR | TypeFlags.VALUE); } - + /** Tests if this type represents an internal or external reference. */ get isReference(): bool { return this.is(TypeFlags.REFERENCE); @@ -466,9 +466,7 @@ export class Type { /** Converts this type to a string. */ toString(validWat: bool = false): string { - const nullablePostfix = validWat - ? "|null" - : " | null"; + const nullablePostfix = validWat ? "|null" : " | null"; if (this.isReference) { let classReference = this.getClass(); if (classReference) { @@ -479,7 +477,7 @@ export class Type { let signatureReference = this.getSignature(); if (signatureReference) { return this.isNullableReference - ? "(" + signatureReference.toString(validWat) + ")" + nullablePostfix + ? `(${signatureReference.toString(validWat)})${nullablePostfix}` : signatureReference.toString(validWat); } } From cedb565af6e3f90c94e3f8d228e9d9af1b167263 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 12 Jun 2022 10:36:06 +0300 Subject: [PATCH 2/4] fix --- src/module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/module.ts b/src/module.ts index 2f1c01c4c7..d4e3ebe998 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3028,7 +3028,7 @@ export class SwitchBuilder { var entry = new Array(1 + numValues + 1); var labels = new Array(numCases); for (let i = 0; i < numCases; ++i) { - labels[i] = `case${i}labelPostfix`; + labels[i] = `case${i}${labelPostfix}`; } entry[0] = module.local_set(localIndex, this.condition, false); // u32 for (let i = 0; i < numValues; ++i) { From df69a3b28a5329c5ab7dc9210b16a13d24a915ee Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 12 Jun 2022 13:06:18 +0300 Subject: [PATCH 3/4] more --- src/compiler.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 11b3450edf..f14ec3e787 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -2858,10 +2858,10 @@ 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}` - : "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); From b4c7c430022234f7ab42972b654594277e0cb90e Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 12 Jun 2022 13:44:34 +0300 Subject: [PATCH 4/4] more --- src/program.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/program.ts b/src/program.ts index dafa1abaa5..2958241478 100644 --- a/src/program.ts +++ b/src/program.ts @@ -3850,7 +3850,7 @@ export class Field extends VariableLikeElement { get internalGetterName(): string { var cached = this._internalGetterName; if (cached == null) { - this._internalGetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + GETTER_PREFIX + this.name; + this._internalGetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${GETTER_PREFIX}${this.name}`; } return cached; } @@ -3860,7 +3860,7 @@ export class Field extends VariableLikeElement { get internalSetterName(): string { var cached = this._internalSetterName; if (cached == null) { - this._internalSetterName = cached = this.parent.internalName + INSTANCE_DELIMITER + SETTER_PREFIX + this.name; + this._internalSetterName = cached = `${this.parent.internalName}${INSTANCE_DELIMITER}${SETTER_PREFIX}${this.name}`; } return cached; }