Skip to content

Commit 0cc98ee

Browse files
author
jonas.ong
committed
make WasmRaw subtype of WasmInstruction subtypes
1 parent ac3869b commit 0cc98ee

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/wasm-compiler/builderGenerator.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,15 @@ type Binding = { name: string; tag: "local" | "nonlocal" };
104104
// all expressions compile to a call to a function like makeX, get/setLexAddress, arithOp, etc.
105105
// so expressions return WasmCalls. (every expression results in i32 i64)
106106
// WasmRaw is for function calls
107+
108+
interface BuilderVisitor<S, E> extends StmtNS.Visitor<S>, ExprNS.Visitor<E> {
109+
visit(stmt: StmtNS.Stmt): S;
110+
visit(stmt: ExprNS.Expr): E;
111+
visit(stmt: StmtNS.Stmt | ExprNS.Expr): S | E;
112+
}
113+
107114
export class BuilderGenerator
108-
implements
109-
StmtNS.Visitor<WasmInstruction>,
110-
ExprNS.Visitor<WasmCall | WasmRaw>
115+
implements BuilderVisitor<WasmInstruction, WasmCall | WasmRaw>
111116
{
112117
private strings: [string, number][] = [];
113118
private heapPointer = 0;
@@ -181,8 +186,8 @@ export class BuilderGenerator
181186
}
182187

183188
visit(stmt: StmtNS.Stmt): WasmInstruction;
184-
visit(stmt: ExprNS.Expr): WasmCall;
185-
visit(stmt: StmtNS.Stmt | ExprNS.Expr): WasmInstruction | WasmCall {
189+
visit(stmt: ExprNS.Expr): WasmCall | WasmRaw;
190+
visit(stmt: StmtNS.Stmt | ExprNS.Expr): WasmInstruction | WasmCall | WasmRaw {
186191
return stmt.accept(this);
187192
}
188193

@@ -281,7 +286,7 @@ export class BuilderGenerator
281286
return wasm.drop(wasm.drop(expr));
282287
}
283288

284-
visitGroupingExpr(expr: ExprNS.Grouping): WasmCall {
289+
visitGroupingExpr(expr: ExprNS.Grouping): WasmCall | WasmRaw {
285290
return this.visit(expr.expression);
286291
}
287292

src/wasm-compiler/wasm-util/types.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ export type WasmNumeric =
216216
| WasmNumericFor<"i32">
217217
| WasmNumericFor<"i64">
218218
| WasmNumericFor<"f32">
219-
| WasmNumericFor<"f64">;
219+
| WasmNumericFor<"f64">
220+
| WasmRaw;
220221

221222
// ------------------------ WASM Variable Instructions ----------------------------
222223

@@ -243,7 +244,8 @@ type WasmVariable =
243244
| WasmLocalGet
244245
| WasmLocalTee
245246
| WasmGlobalSet
246-
| WasmGlobalGet;
247+
| WasmGlobalGet
248+
| WasmRaw;
247249

248250
// ------------------------ WASM Memory Instructions ----------------------------
249251
// Technically WasmStoreOp and WasmLoadOp are memory instructions, but they are defined
@@ -263,7 +265,7 @@ export type WasmMemoryFill = {
263265
numOfBytes: WasmNumericFor<"i32">;
264266
};
265267

266-
type WasmMemory = WasmMemoryCopy | WasmMemoryFill;
268+
type WasmMemory = WasmMemoryCopy | WasmMemoryFill | WasmRaw;
267269

268270
// ------------------------ WASM Control Instructions ----------------------------
269271

@@ -322,7 +324,8 @@ type WasmControl =
322324
| WasmBrTable
323325
| WasmCall
324326
| WasmReturn
325-
| WasmSelect;
327+
| WasmSelect
328+
| WasmRaw;
326329

327330
// ------------------------ WASM Module Instructions ----------------------------
328331

@@ -394,7 +397,8 @@ export type WasmModuleInstruction =
394397
| WasmFunction
395398
| WasmExport
396399
| WasmStart
397-
| WasmModule;
400+
| WasmModule
401+
| WasmRaw;
398402

399403
// meant to be used with wasm.raw (tagged template)
400404
export type WasmRaw = {
@@ -408,5 +412,4 @@ export type WasmInstruction =
408412
| WasmMemory
409413
| WasmControl
410414
| WasmVariable
411-
| WasmModuleInstruction
412-
| WasmRaw;
415+
| WasmModuleInstruction;

0 commit comments

Comments
 (0)