Turns expression source into a reusable compiled accessor.
One parse per unique expression, cached, then every evaluation walks the AST.
That is the same cost profile the previous implementation had — it compiled a
Function once per unique source and cached that — minus the eval.
The fallback, and why it is visible
The parser covers the expression language templates use, not all of
ECMAScript: statements, async/await, destructuring and regular-expression
literals are out of scope, and action bodies are arbitrary JavaScript.
Anything the parser cannot read is handed to the legacy evaluator, which
still uses new Function.
That fallback is recorded rather than silent. getFallbackReport lists
every expression that took it, so "does this application still need
unsafe-eval?" has an answer that comes from the code rather than from a
claim — and avenx check can print it. A framework that quietly degraded
here would be making exactly the kind of promise this work exists to stop
making.
- Source:
Members
(inner, constant) UNPARSEABLE :object
A sentinel stored for sources known to be unparseable, so a failing expression is not re-parsed on every evaluation.
Type:
- object
- Source:
(inner) astCache :LruCache
Compiled ASTs by source text.
Type:
- Source:
(inner, constant) fallbacks :Map:.<string:, string:>
Sources the parser could not read, and why.
Type:
- Source:
Methods
(static) clearExpressionCache()
Empties the compiled-expression cache and the fallback report.
- Source:
(static) compileExpression(source) → {object|null}
Parses an expression, returning null when it is outside the language.
Parameters:
| Name | Type | Description |
|---|---|---|
source |
string | The expression source. |
- Source:
Returns:
The AST, or null when it cannot be parsed.
- Type
- object | null
(static) compileStatements(source) → {object|null}
Parses a statement body as a run of expressions, or returns null.
Parameters:
| Name | Type | Description |
|---|---|---|
source |
string | The statement source. |
- Source:
Returns:
A Program AST, or null when it is not expression-only.
- Type
- object | null
(static) describeParseFailure(source) → {string}
Why an expression could not be parsed.
Parameters:
| Name | Type | Description |
|---|---|---|
source |
string | The expression source. |
- Source:
Returns:
The parser's reason, or a generic message.
- Type
- string
(static) getExpressionCacheStats() → {Object}
Current cache occupancy, for diagnostics and tests.
- Source:
Returns:
Cache statistics.
- Type
- Object
(static) getFallbackReport() → {Array:.<{source:: string:, reason:: string:}>}
Every expression that could not be parsed, with the reason.
An application whose report is empty evaluates entirely through the AST
evaluator and needs no 'unsafe-eval' for its expressions.
- Source:
Returns:
The fallback list.
(static) isCompilable(source) → {boolean}
Whether an expression can be evaluated without new Function.
Parameters:
| Name | Type | Description |
|---|---|---|
source |
string | The expression source. |
- Source:
Returns:
True when the AST evaluator can handle it.
- Type
- boolean
(static) runExpression(source, scope) → {Object}
Evaluates an expression against a scope.
Parameters:
| Name | Type | Description |
|---|---|---|
source |
string | The expression source. |
scope |
object | The evaluation scope. |
- Source:
Returns:
Whether the AST evaluator ran, and
the value it produced. handled: false means the caller should fall back.
- Type
- Object
(static) setExpressionCacheCapacity(capacity)
Sets the compiled-expression cache capacity.
Parameters:
| Name | Type | Description |
|---|---|---|
capacity |
number | Positive integer limit. |
- Source: