Skip to content

Commit 2ed3c22

Browse files
committed
Cleanup: Make use of type inference and use 'var' in functions to match actual WebAssembly semantics
1 parent 7795d48 commit 2ed3c22

File tree

16 files changed

+1066
-983
lines changed

16 files changed

+1066
-983
lines changed

src/ast.ts

Lines changed: 282 additions & 203 deletions
Large diffs are not rendered by default.

src/builtins.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function initialize(program: Program): void {
8383
addFunction(program, "assert");
8484

8585
// conversions and limits
86-
let i32Func: FunctionPrototype,
86+
var i32Func: FunctionPrototype,
8787
u32Func: FunctionPrototype,
8888
i64Func: FunctionPrototype,
8989
u64Func: FunctionPrototype;
@@ -144,7 +144,7 @@ export function initialize(program: Program): void {
144144

145145
/** Adds a built-in constant to the specified program. */
146146
function addConstant(program: Program, name: string, type: Type): Global {
147-
const global: Global = new Global(program, name, name, null);
147+
var global = new Global(program, name, name, null);
148148
global.isBuiltIn = true;
149149
global.isConstant = true;
150150
global.type = type;
@@ -154,9 +154,10 @@ function addConstant(program: Program, name: string, type: Type): Global {
154154

155155
/** Adds a built-in function to the specified program. */
156156
function addFunction(program: Program, name: string, isGeneric: bool = false): FunctionPrototype {
157-
let prototype: FunctionPrototype = new FunctionPrototype(program, name, name, null, null);
157+
var prototype = new FunctionPrototype(program, name, name, null, null);
158158
prototype.isBuiltIn = true;
159-
if (isGeneric) prototype.isGeneric = true;
159+
if (isGeneric)
160+
prototype.isGeneric = true;
160161
program.elements.set(name, prototype);
161162
return prototype;
162163
}
@@ -187,18 +188,18 @@ export function compileGetConstant(compiler: Compiler, global: Global): Expressi
187188

188189
/** Compiles a call to a built-in function. */
189190
export function compileCall(compiler: Compiler, prototype: FunctionPrototype, typeArguments: Type[], operands: Expression[], reportNode: Node): ExpressionRef {
190-
const module: Module = compiler.module;
191-
const usizeType: Type = select<Type>(Type.usize64, Type.usize32, compiler.options.target == Target.WASM64);
192-
const nativeUsizeType: NativeType = select<NativeType>(NativeType.I64, NativeType.I32, compiler.options.target == Target.WASM64);
191+
var module = compiler.module;
192+
var usizeType = select<Type>(Type.usize64, Type.usize32, compiler.options.target == Target.WASM64);
193+
var nativeUsizeType = select<NativeType>(NativeType.I64, NativeType.I32, compiler.options.target == Target.WASM64);
193194

194-
let arg0: ExpressionRef,
195+
var arg0: ExpressionRef,
195196
arg1: ExpressionRef,
196197
arg2: ExpressionRef;
197198

198-
let tempLocal0: Local;
199-
let tempLocal1: Local;
199+
var tempLocal0: Local,
200+
tempLocal1: Local;
200201

201-
let ftype: FunctionTypeRef;
202+
var ftype: FunctionTypeRef;
202203

203204
switch (prototype.internalName) {
204205

src/compiler.ts

Lines changed: 178 additions & 180 deletions
Large diffs are not rendered by default.

src/decompiler.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
export class Decompiler {
2121

2222
static decompile(module: Module): string {
23-
const decompiler: Decompiler = new Decompiler();
23+
var decompiler = new Decompiler();
2424
decompiler.decompile(module);
2525
return decompiler.finish();
2626
}
@@ -39,13 +39,12 @@ export class Decompiler {
3939
}
4040

4141
decompileFunction(func: FunctionRef): void {
42-
const name: string = readString(_BinaryenFunctionGetName(func)) || "$" + this.functionId.toString(10);
43-
const body: ExpressionRef = _BinaryenFunctionGetBody(func);
42+
var name = readString(_BinaryenFunctionGetName(func)) || "$" + this.functionId.toString(10);
43+
var body = _BinaryenFunctionGetBody(func);
4444
this.push("function ");
4545
this.push(name);
4646
this.push("(");
47-
let k: Index = _BinaryenFunctionGetNumParams(func);
48-
for (let i: Index = 0; i < k; ++i) {
47+
for (var i: Index = 0, k: Index = _BinaryenFunctionGetNumParams(func); i < k; ++i) {
4948
if (i > 0)
5049
this.push(", ");
5150
this.push("$");
@@ -67,12 +66,12 @@ export class Decompiler {
6766
}
6867

6968
decompileExpression(expr: ExpressionRef): void {
70-
const id: ExpressionId = _BinaryenExpressionGetId(expr);
71-
const type: NativeType = _BinaryenExpressionGetType(expr);
69+
var id = _BinaryenExpressionGetId(expr);
70+
var type = _BinaryenExpressionGetType(expr);
7271

73-
let nested: ExpressionRef;
74-
let string: string | null;
75-
let i: Index, k: Index;
72+
var nested: ExpressionRef;
73+
var string: string | null;
74+
var i: Index, k: Index;
7675

7776
switch (id) {
7877

@@ -829,7 +828,7 @@ export class Decompiler {
829828
}
830829

831830
finish(): string {
832-
const ret: string = this.text.join("");
831+
var ret = this.text.join("");
833832
this.text = [];
834833
return ret;
835834
}

src/diagnostics.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class DiagnosticMessage {
6464
}
6565

6666
static create(code: DiagnosticCode, category: DiagnosticCategory, arg0: string | null = null, arg1: string | null = null): DiagnosticMessage {
67-
let message: string = diagnosticCodeToString(code);
67+
var message = diagnosticCodeToString(code);
6868
if (arg0 != null)
6969
message = message.replace("{0}", arg0);
7070
if (arg1 != null)
@@ -98,7 +98,7 @@ export class DiagnosticMessage {
9898

9999
export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: bool = false, showContext: bool = false): string {
100100
// format context first (uses same string builder)
101-
let context: string = "";
101+
var context = "";
102102
if (message.range && showContext)
103103
context = formatDiagnosticContext(message.range, useColors);
104104

@@ -114,16 +114,16 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b
114114

115115
// range information if available
116116
if (message.range) {
117-
const range: Range = message.range;
118-
const text: string = range.source.text;
117+
var range = message.range;
118+
var text = range.source.text;
119119
if (showContext) {
120120
sb.push("\n");
121121
sb.push(context);
122122
}
123123
sb.push("\n");
124-
let pos: i32 = range.start;
125-
let line: i32 = 1;
126-
let column: i32 = 1;
124+
var pos = range.start;
125+
var line = 1;
126+
var column = 1;
127127
while (pos-- > 0)
128128
if (text.charCodeAt(pos) == CharCode.LINEFEED)
129129
line++;
@@ -141,10 +141,10 @@ export function formatDiagnosticMessage(message: DiagnosticMessage, useColors: b
141141
}
142142

143143
export function formatDiagnosticContext(range: Range, useColors: bool = false): string {
144-
const text: string = range.source.text;
145-
const len: i32 = text.length;
146-
let start: i32 = range.start;
147-
let end: i32 = range.end;
144+
var text = range.source.text;
145+
var len = text.length;
146+
var start = range.start;
147+
var end = range.end;
148148
while (start > 0 && !isLineBreak(text.charCodeAt(start - 1)))
149149
start--;
150150
while (end < len && !isLineBreak(text.charCodeAt(end)))
@@ -176,7 +176,7 @@ export abstract class DiagnosticEmitter {
176176
}
177177

178178
emitDiagnostic(code: DiagnosticCode, category: DiagnosticCategory, range: Range, arg0: string | null = null, arg1: string | null = null) {
179-
const message: DiagnosticMessage = DiagnosticMessage.create(code, category, arg0, arg1).withRange(range);
179+
var message = DiagnosticMessage.create(code, category, arg0, arg1).withRange(range);
180180
this.diagnostics.push(message);
181181
if (!this.silentDiagnostics) {
182182
console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export function nextFile(parser: Parser): string | null {
6767

6868
/** Obtains the next diagnostic message. Returns `null` once there are no more messages. */
6969
export function nextDiagnostic(parser: Parser): DiagnosticMessage | null {
70-
const program: Program = parser.program;
70+
var program = parser.program;
7171
if (program.diagnosticsOffset < program.diagnostics.length)
7272
return program.diagnostics[program.diagnosticsOffset++];
7373
return null;
@@ -120,14 +120,14 @@ export function setNoMemory(options: Options, noMemory: bool): void {
120120

121121
/** Compiles the sources computed by the parser to a module. */
122122
export function compile(parser: Parser, options: Options | null = null): Module {
123-
const program: Program = parser.finish();
124-
const compiler: Compiler = new Compiler(program, options);
123+
var program = parser.finish();
124+
var compiler = new Compiler(program, options);
125125
return compiler.compile();
126126
}
127127

128128
/** Decompiles a module to its (low level) source. */
129129
export function decompile(module: Module): string {
130-
const decompiler: Decompiler = new Decompiler();
130+
var decompiler = new Decompiler();
131131
decompiler.decompile(module);
132132
return decompiler.finish();
133133
}

0 commit comments

Comments
 (0)