Skip to content

Wasm: Disposing MatchResult objects #529

@pdubroy

Description

@pdubroy

In the Wasm version, we allocate CST nodes via bump pointer allocation into linear memory. Currently, we reset the heap before each call to match. This is fine for typical use cases, but there are some languages that require two stages of parsing. For example, in Shopify's liquid-html-grammar, they call match in the process of building the initial intermediate representation (which they also call a "CST").

To support uses like this, we'll need to have a way of explicitly freeing linear memory. My current thought is to make the MatchResult object the main interface for resource management:

const result = grammar.match(sourceCode);
createAst(result);
result.dispose();

If we go this route, we'll probably also want a way of ensuring that users remember to call dispose. A few ideas:

  1. Use a FinalizationRegistry, and either (a) automatically free things, and/or (b) print a warning about undisposed MatchResults.
  2. Automatically call dispose before match, and/or warn if the previous result was not yet disposed. And then we could have another API to handle the special case.

For example:

const result = grammar.match(sourceCode);
result.beforeDisposing(() => {
  grammar.match(somethingElse);
});

or

const result = grammar.match(sourceCode);
result.retain(); // <- We could register with FinalizationRegistry here too.
grammar.match(somethingElse);
result.dispose();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions