@ohm-js/wasm 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
- /* global process */
2
1
  import * as w from '@wasmgroundup/emit';
3
2
  import * as ohm from 'ohm-js';
4
3
  // import wabt from 'wabt';
5
4
  import * as ir from "./ir.js";
6
5
  import * as prebuilt from "../build/ohmRuntime.wasm_sections.js";
7
6
  const WASM_PAGE_SIZE = 64 * 1024;
8
- const DEBUG = process.env.OHM_DEBUG === '1';
7
+ // eslint-disable-next-line no-undef
8
+ const DEBUG = typeof process !== 'undefined' && process.env.OHM_DEBUG === '1';
9
9
  const FAST_SAVE_BINDINGS = true;
10
10
  const FAST_RESTORE_BINDINGS = true;
11
11
  const IMPLICIT_SPACE_SKIPPING = true;
@@ -108,10 +108,10 @@ export class AstBuilder {
108
108
  }
109
109
  toAst(nodeOrResult) {
110
110
  let node = nodeOrResult;
111
- if (typeof nodeOrResult.succeeded === 'function') {
111
+ if (typeof nodeOrResult._succeeded === 'boolean') {
112
112
  const matchResult = nodeOrResult;
113
- assert(matchResult.succeeded(), 'Cannot convert failed match result to AST');
114
- node = matchResult.grammar.getCstRoot();
113
+ assert(matchResult._succeeded, 'Cannot convert failed match result to AST');
114
+ node = matchResult.getCstRoot();
115
115
  }
116
116
  let ans;
117
117
  this._depth++;
@@ -30,7 +30,6 @@ export declare class WasmGrammar {
30
30
  private _input;
31
31
  _failureDescriptions: string[];
32
32
  private _resultStack;
33
- private _managedResultCount;
34
33
  /**
35
34
  * Create a new WasmGrammar object.
36
35
  * If `bytes` is specified, the WebAssembly module will be synchronously
@@ -39,15 +38,14 @@ export declare class WasmGrammar {
39
38
  */
40
39
  constructor(bytes?: BufferSource);
41
40
  private _init;
42
- _beginUse(result: MatchResult): void;
43
- _endUse(result: MatchResult): void;
41
+ _manage(result: MatchResult): void;
42
+ _dispose(result: MatchResult): void;
44
43
  static instantiate(source: BufferSource): Promise<WasmGrammar>;
45
44
  static instantiateStreaming(source: Response | Promise<Response>): Promise<WasmGrammar>;
46
45
  _instantiate(source: BufferSource, debugImports?: any): Promise<this>;
47
46
  _instantiateStreaming(source: Response | Promise<Response>, debugImports?: any): Promise<WasmGrammar>;
48
47
  private _getGrammarName;
49
48
  private _extractStrings;
50
- private _detachMatchResult;
51
49
  match<T>(input: string, ruleName?: string): MatchResult;
52
50
  recordFailures(): number[];
53
51
  getFailureDescription(id: number): string;
@@ -214,10 +212,11 @@ export declare class MatchResult {
214
212
  _ctx: MatchContext;
215
213
  _succeeded: boolean;
216
214
  _attached: boolean;
215
+ _managed: boolean;
217
216
  constructor(grammar: WasmGrammar, startExpr: string, ctx: MatchContext, succeeded: boolean);
218
217
  get input(): string;
219
- [Symbol.dispose](): void;
220
- detach(): void;
218
+ get [Symbol.dispose](): () => void;
219
+ dispose(): void;
221
220
  succeeded(): this is SucceededMatchResult;
222
221
  failed(): this is FailedMatchResult;
223
222
  toString(): string;
@@ -226,6 +225,7 @@ export declare class MatchResult {
226
225
  export declare class SucceededMatchResult extends MatchResult {
227
226
  _cst: CstNode;
228
227
  constructor(grammar: WasmGrammar, startExpr: string, ctx: MatchContext, succeeded: boolean);
228
+ getCstRoot(): CstNode;
229
229
  }
230
230
  export declare class FailedMatchResult extends MatchResult {
231
231
  _rightmostFailurePosition: number;
@@ -155,7 +155,6 @@ export class WasmGrammar {
155
155
  _input = '';
156
156
  _failureDescriptions = []; // Should be treated as package private
157
157
  _resultStack = [];
158
- _managedResultCount = 0;
159
158
  /**
160
159
  * Create a new WasmGrammar object.
161
160
  * If `bytes` is specified, the WebAssembly module will be synchronously
@@ -175,20 +174,16 @@ export class WasmGrammar {
175
174
  this.name = this._getGrammarName(module);
176
175
  return this;
177
176
  }
178
- _beginUse(result) {
179
- assert(this._resultStack.at(-1) === result, `You can only use() the most recent MatchResult`);
180
- result.detach = () => {
181
- throw new Error("MatchResult shouldn't be detached inside use()");
182
- };
183
- this._managedResultCount++;
177
+ _manage(result) {
178
+ result._managed = true;
184
179
  }
185
- _endUse(result) {
186
- const r = this._resultStack.pop();
187
- assert(r === result, 'Mismatched _endUse');
188
- this._managedResultCount--;
180
+ _dispose(result) {
181
+ assert(this._resultStack.at(-1) === result, `You can only dispose() the most recent MatchResult`);
182
+ this._resultStack.pop();
189
183
  if (this._resultStack.length === 0) {
190
184
  this._instance.exports.resetHeap();
191
185
  }
186
+ result._attached = false;
192
187
  }
193
188
  static async instantiate(source) {
194
189
  return new WasmGrammar()._instantiate(source);
@@ -232,16 +227,11 @@ export class WasmGrammar {
232
227
  this._failureDescriptions.push(str);
233
228
  }
234
229
  }
235
- _detachMatchResult(result) {
236
- assert(this._resultStack.at(-1) === result, `You can only detach() the most recent MatchResult`);
237
- this._beginUse(result);
238
- this._endUse(result);
239
- result._attached = false;
240
- }
241
230
  match(input, ruleName) {
242
- assert(this._resultStack.length === this._managedResultCount, 'Cannot match while there are unmanaged MatchResults');
231
+ assert(this._resultStack.every(r => r._managed), 'Cannot match while there are unmanaged MatchResults. ' +
232
+ 'Use `using` or `.use()` to manage the MatchResult lifecycle.');
243
233
  this._input = input;
244
- if (process.env.OHM_DEBUG === '1')
234
+ if (typeof process !== 'undefined' && process.env.OHM_DEBUG === '1')
245
235
  debugger; // eslint-disable-line no-debugger
246
236
  const ruleId = checkNotNull(this._ruleIds.get(ruleName || this._ruleNames[0]), `unknown rule: '${ruleName}'`);
247
237
  const succeeded = this._instance.exports.match(input, ruleId);
@@ -251,7 +241,7 @@ export class WasmGrammar {
251
241
  input: this._instance.exports.input.value,
252
242
  };
253
243
  const result = createMatchResult(this, ruleName || this._ruleNames[0], ctx, !!succeeded);
254
- result.detach = this._detachMatchResult.bind(this, result);
244
+ result.dispose = this._dispose.bind(this, result);
255
245
  this._resultStack.push(result);
256
246
  return result;
257
247
  }
@@ -598,6 +588,7 @@ export class MatchResult {
598
588
  _ctx;
599
589
  _succeeded;
600
590
  _attached = true;
591
+ _managed = false;
601
592
  constructor(grammar, startExpr, ctx, succeeded) {
602
593
  this.grammar = grammar;
603
594
  this.startExpr = startExpr;
@@ -607,10 +598,13 @@ export class MatchResult {
607
598
  get input() {
608
599
  return this.grammar._input;
609
600
  }
610
- [Symbol.dispose]() {
611
- this.detach();
601
+ // `using` accesses [Symbol.dispose] at declaration time to get the
602
+ // disposal method. We use this as the signal that the result is managed.
603
+ get [Symbol.dispose]() {
604
+ this.grammar._manage(this);
605
+ return () => this.dispose();
612
606
  }
613
- detach() {
607
+ dispose() {
614
608
  throw new Error('MatchResult is not attached to any grammar');
615
609
  }
616
610
  succeeded() {
@@ -625,12 +619,12 @@ export class MatchResult {
625
619
  : '[match succeeded]';
626
620
  }
627
621
  use(cb) {
622
+ this.grammar._manage(this);
628
623
  try {
629
- this.grammar._beginUse(this);
630
624
  return cb(this);
631
625
  }
632
626
  finally {
633
- this.grammar._endUse(this);
627
+ this.dispose();
634
628
  }
635
629
  }
636
630
  }
@@ -645,6 +639,9 @@ export class SucceededMatchResult extends MatchResult {
645
639
  super(grammar, startExpr, ctx, succeeded);
646
640
  this._cst = grammar.getCstRoot();
647
641
  }
642
+ getCstRoot() {
643
+ return this._cst;
644
+ }
648
645
  }
649
646
  export class FailedMatchResult extends MatchResult {
650
647
  _rightmostFailurePosition;
@@ -656,8 +653,8 @@ export class FailedMatchResult extends MatchResult {
656
653
  }
657
654
  _assertAttached(property) {
658
655
  if (!this._attached) {
659
- throw new Error(`Cannot access '${property}' after MatchResult has been detached. ` +
660
- `Access failure information before calling detach(), or use result.use().`);
656
+ throw new Error(`Cannot access '${property}' after MatchResult has been disposed. ` +
657
+ `Access failure information before calling dispose(), or use result.use().`);
661
658
  }
662
659
  }
663
660
  getRightmostFailurePosition() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohm-js/wasm",
3
- "version": "0.7.4",
3
+ "version": "0.7.5",
4
4
  "description": "Compile Ohm.js grammars to WebAsssembly",
5
5
  "main": "dist/index.js",
6
6
  "exports": {