Repository navigation
JEXL-477: Parse interpolation strings with the features of their script #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bf440a3
0fe4d79
080dbc4
e0f00f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| <body> | ||
| <release version="3.7.2" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required."> | ||
| <!-- FIX --> | ||
| <action dev="henrib" due-to="Claude" type="fix" issue="JEXL-477">Interpolation strings are parsed with the engine features instead of those of their script.</action> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept as is, same as the earlier thread: in this file, fix entries state the problem as the JIRA title does. |
||
| <action dev="henrib" due-to="Claude" type="fix" issue="JEXL-475">A jexl.options pragma modifies the engine options when evaluated with a plain context.</action> | ||
| <!-- ADD --> | ||
| <action dev="henrib" due-to="Claude" type="add" issue="JEXL-476">Add the safeUndefined option (JexlOptions.setSafeUndefined(boolean), JexlBuilder.safeUndefined(boolean)) to restrict safe navigation to null, undefined variables, properties and methods remaining errors; safe access operators treat properties, methods and array accesses alike.</action> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -506,15 +506,19 @@ protected JexlOptions options(final JexlContext context) { | |
| */ | ||
| final class NestedExpression extends JexlBasedExpression { | ||
| private final Scope scope; | ||
| private final JexlFeatures features; | ||
| /** | ||
| * Creates a nested unified expression. | ||
| * | ||
| * @param expr The unified expression as a string | ||
| * @param node The unified expression as an AST | ||
| * @param sc The scope | ||
| * @param ftrs The features to parse the prepared expression with | ||
| */ | ||
| NestedExpression(final CharSequence expr, final JexlNode node, final Scope sc) { | ||
| NestedExpression(final CharSequence expr, final JexlNode node, final Scope sc, final JexlFeatures ftrs) { | ||
| super(expr, node, null); | ||
| this.scope = sc; | ||
| this.features = ftrs; | ||
| if (source != this) { | ||
| throw new IllegalArgumentException("Nested TemplateExpression cannot have a source"); | ||
| } | ||
|
|
@@ -544,7 +548,7 @@ public boolean isImmediate() { | |
| @Override | ||
| protected TemplateExpression prepare(final Interpreter interpreter) { | ||
| final String value = interpreter.interpret(node).toString(); | ||
| final JexlNode dnode = jexl.jxltParse(node.jexlInfo(), noscript, value, scope); | ||
| final JexlNode dnode = jexl.jxltParse(node.jexlInfo(), features, value, scope); | ||
| return new ImmediateExpression(value, dnode, this); | ||
| } | ||
| } | ||
|
|
@@ -912,24 +916,38 @@ public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final Str | |
| } | ||
|
|
||
| public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final String expression, final Scope scope) { | ||
| return createExpression(jexlInfo, expression, scope, null); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an expression that is embedded in a script, an interpolation. | ||
| * | ||
| * @param jexlInfo The source info | ||
| * @param expression The expression source | ||
| * @param scope The scope | ||
| * @param ftrs The features of the script the expression is embedded in, null for those of the engine | ||
| * @return The expression | ||
| */ | ||
| public JxltEngine.Expression createExpression(final JexlInfo jexlInfo, final String expression, final Scope scope, | ||
| final JexlFeatures ftrs) { | ||
| final JexlInfo info = jexlInfo == null ? jexl.createInfo() : jexlInfo; | ||
| Exception xuel = null; | ||
| TemplateExpression stmt = null; | ||
| final JexlFeatures features = noscript ? jexl.expressionFeatures : jexl.scriptFeatures; | ||
| // do not cache interpolation expression, they are stored in AST node | ||
| final JexlFeatures features = features(ftrs); | ||
| // expressions shorter than the cache threshold are cached, keyed by features, symbols and source | ||
| final boolean cached = cache != null && expression.length() < jexl.cacheThreshold; | ||
| final JexlEngine je = Engine.putThreadEngine(jexl); | ||
| try { | ||
| if (!cached) { | ||
| stmt = parseExpression(info, expression, scope); | ||
| stmt = parseExpression(info, expression, scope, features); | ||
| } else { | ||
| final Source source = new Source(features, Scope.getSymbolsMap(scope), expression); | ||
| final Object c = cache.get(source); | ||
| stmt = c instanceof TemplateExpression ? (TemplateExpression) c : null; | ||
| if (stmt != null) { | ||
| return stmt; | ||
| } | ||
| stmt = parseExpression(info, expression, scope); | ||
| stmt = parseExpression(info, expression, scope, features); | ||
| cache.put(source, stmt); | ||
| } | ||
| } catch (final JexlException xjexl) { | ||
|
|
@@ -987,7 +1005,39 @@ char getImmediateChar() { | |
| * @return The unified expression instance | ||
| * @throws JexlException if an error occurs during parsing | ||
| */ | ||
| TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope) { // CSOFF: MethodLength | ||
| TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope) { | ||
| return parseExpression(info, expr, scope, features(null)); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the features used to parse the JEXL expressions of a unified expression. | ||
| * | ||
| * @param ftrs The features of the script the unified expression is embedded in, null for those of the engine | ||
| * @return The features | ||
| */ | ||
| private JexlFeatures features(final JexlFeatures ftrs) { | ||
| if (ftrs == null) { | ||
| return noscript ? jexl.expressionFeatures : jexl.scriptFeatures; | ||
| } | ||
| if (!noscript || !ftrs.supportsScript()) { | ||
| return ftrs; | ||
| } | ||
| // the script was created with the features of the engine, no need to derive them | ||
| return ftrs == jexl.scriptFeatures ? jexl.expressionFeatures : new JexlFeatures(ftrs).script(false); | ||
| } | ||
|
henrib marked this conversation as resolved.
Resolved
Comment on lines
+1018
to
+1027
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a hot path: the features are derived when the parser builds an interpolation node, once per node, and evaluation uses the expression stored in the node. Still, scripts created with the features of the engine did not need a copy before this change; e0f00f2 reuses the engine expression features for them, so only scripts created with their own features derive one. |
||
|
|
||
| /** | ||
| * Parses a unified expression. | ||
| * | ||
| * @param info The source info | ||
| * @param expr The string expression | ||
| * @param scope The template scope | ||
| * @param features The features to parse the JEXL expressions with | ||
| * @return The unified expression instance | ||
| * @throws JexlException if an error occurs during parsing | ||
| */ | ||
| TemplateExpression parseExpression(final JexlInfo info, final String expr, final Scope scope, | ||
| final JexlFeatures features) { // CSOFF: MethodLength | ||
| final int size = expr.length(); | ||
| final ExpressionBuilder builder = new ExpressionBuilder(0); | ||
| final StringBuilder strb = new StringBuilder(size); | ||
|
|
@@ -1058,7 +1108,7 @@ TemplateExpression parseExpression(final JexlInfo info, final String expr, final | |
| final String src = escapeString(strb); | ||
| final JexlInfo srcInfo = info.at(lineno, column); | ||
| final TemplateExpression iexpr = new ImmediateExpression(src, | ||
| jexl.jxltParse(srcInfo, noscript, src, scope), null); | ||
| jexl.jxltParse(srcInfo, features, src, scope), null); | ||
| builder.add(iexpr); | ||
| strb.delete(0, Integer.MAX_VALUE); | ||
| state = ParseState.CONST; | ||
|
|
@@ -1106,12 +1156,12 @@ TemplateExpression parseExpression(final JexlInfo info, final String expr, final | |
| if (nested) { | ||
| dexpr = new NestedExpression( | ||
| escapeString(expr.substring(inested, column + 1)), | ||
| jexl.jxltParse(srcInfo, noscript, src, scope), | ||
| scope); | ||
| jexl.jxltParse(srcInfo, features, src, scope), | ||
| scope, features); | ||
| } else { | ||
| dexpr = new DeferredExpression( | ||
| src, | ||
| jexl.jxltParse(srcInfo, noscript, src, scope)); | ||
| jexl.jxltParse(srcInfo, features, src, scope)); | ||
| } | ||
| builder.add(dexpr); | ||
| strb.delete(0, Integer.MAX_VALUE); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -377,9 +377,19 @@ void testMany() throws Exception { | |
|
|
||
| // attempt to force GC: | ||
| // while we still have a MB free, create & store big objects | ||
| for (int b = 0; b < 1024 && Runtime.getRuntime().freeMemory() > MEGA; ++b) { | ||
| final BigObject big = new BigObject(b); | ||
| stuff.add(new InstanceReference(big, queue)); | ||
| try { | ||
| for (int b = 0; b < 1024 && Runtime.getRuntime().freeMemory() > MEGA; ++b) { | ||
| final BigObject big = new BigObject(b); | ||
| stuff.add(new InstanceReference(big, queue)); | ||
| } | ||
| } catch (final OutOfMemoryError oom) { | ||
| // exhausting the heap is the intent; if the JVM fails an allocation instead of | ||
| // clearing the soft references, release the big objects and carry on | ||
| for (final Reference<?> soft : stuff) { | ||
| if (soft instanceof InstanceReference) { | ||
| soft.clear(); | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+380
to
393
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept as is: what the test asserts is unchanged, a generated class must be collected and reloaded ( |
||
| // hint it... | ||
| System.gc(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.