Skip to content

Commit dbacb88

Browse files
committed
wasm: fix build (?)
1 parent 7d09e15 commit dbacb88

6 files changed

Lines changed: 20 additions & 13 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
ava-ts.config.js
88
visualizer/assets/**
99
packages/cli/src/helpers/generateTypes.test.js
10+
packages/wasm/build/**
1011
packages/wasm/runtime/**

packages/wasm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ build/%.O3.wasm: build/%.wasm
2626
wasm-opt -O3 --enable-bulk-memory-opt -o $@ $<
2727

2828
dist/index.js: build/ohmRuntime.wasm_sections.ts $(SRC_JS_FILES) $(SRC_TS_FILES)
29-
pnpm esbuild src/index.js --bundle --loader:.ts=ts --outfile=dist/index.js --format=esm --platform=node --external:ohm-js
29+
pnpm esbuild index.ts --bundle --loader:.ts=ts --outfile=dist/index.js --format=esm --platform=node --external:ohm-js
3030

3131
dist/cli.js: dist/index.js src/cli.js
3232
pnpm esbuild src/cli.js --bundle --loader:.ts=ts --outfile=dist/cli.js --format=esm --platform=node --external:ohm-js --external:./index.js --banner:js="#!/usr/bin/env node"

packages/wasm/scripts/es5ToWasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {writeFileSync} from 'node:fs';
22
import process from 'node:process';
33

4-
import {Compiler} from '../src/index.js';
4+
import {Compiler} from '../src/Compiler.js';
55
import es5 from '../test/data/_es5.js';
66

77
const outFilename = process.argv[2];

packages/wasm/src/ir.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ export interface Any {
4040

4141
export const any = (): Any => ({type: 'Any'});
4242

43+
type ApplyLike = Extract<Expr, {type: 'Apply' | 'LiftedTerminal' | 'Param'}>;
44+
4345
export interface Apply {
4446
type: 'Apply';
4547
ruleName: string;
46-
children: (Apply | Param)[];
48+
children: ApplyLike[];
4749
}
4850

49-
export const apply = (ruleName: string, children: (Apply | Param)[] = []): Apply => ({
51+
export const apply = (ruleName: string, children: ApplyLike[] = []): Apply => ({
5052
type: 'Apply',
5153
ruleName,
5254
children
@@ -217,12 +219,16 @@ function checkNotNull<T>(x: T, msg = 'unexpected null value'): NonNullable<T> {
217219
return x;
218220
}
219221

220-
function checkExprType<T extends ExprType>(
222+
function checkApplyLike(exp: Expr): ApplyLike {
223+
return checkExprType(exp, 'Apply', 'LiftedTerminal', 'Param');
224+
}
225+
226+
function checkExprType<T extends Expr['type']>(
221227
exp: Expr,
222-
expectedType: T
228+
...types: T[]
223229
): Extract<Expr, {type: T}> {
224-
if (exp.type !== expectedType) {
225-
throw new Error(`Expected expression of type '${expectedType}', but got '${exp.type}'`);
230+
if (!types.includes(exp.type as T)) {
231+
throw new Error(`Expected one of [${types.join(', ')}], but got '${exp.type}'`);
226232
}
227233
return exp as Extract<Expr, {type: T}>;
228234
}
@@ -261,7 +267,7 @@ export function collectParams(exp: Expr, seen = new Set<number>()): Param[] {
261267
}
262268
}
263269

264-
// TODO: Maybe make the types tighter here, to avoid the use checkExprType.
270+
// TODO: Maybe make the types tighter here, to avoid the use checkApplyLike.
265271
export function substituteParams<T extends Expr>(
266272
exp: T,
267273
actuals: Exclude<Expr, Param>[]
@@ -273,9 +279,9 @@ export function substituteParams<T extends Expr>(
273279
if (exp.children.length === 0) return exp;
274280
return apply(
275281
exp.ruleName,
276-
exp.children.map((c): Apply => {
282+
exp.children.map((c): ApplyLike => {
277283
const ans = substituteParams(c, actuals);
278-
return checkExprType(ans, 'Apply');
284+
return checkApplyLike(ans);
279285
})
280286
);
281287
case 'Dispatch': {

packages/wasm/test/_helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import {WasmMatcher} from '@ohm-js/miniohm-js';
44

5-
import {Compiler} from '../src/index.js';
5+
import {Compiler} from '../src/Compiler.js';
66

77
const DEBUG = process.env.OHM_DEBUG === '1';
88

packages/wasm/test/test-wasm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fc from 'fast-check';
44
import * as ohm from 'ohm-js';
55
import {performance} from 'perf_hooks';
66

7-
import {Compiler, ConstantsForTesting as Constants} from '../src/index.js';
7+
import {Compiler, ConstantsForTesting as Constants} from '../src/Compiler.js';
88
import {matchWithInput, unparse, wasmMatcherForGrammar} from './_helpers.js';
99

1010
const SIZEOF_UINT32 = 4;

0 commit comments

Comments
 (0)