You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2023/03/17 14:19:44 UTC

[commons-jexl] branch master updated: JEXL-393: stop searching for a symbol constness when its declaration is found in a lexical scope;

This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new a3fe9a60 JEXL-393: stop searching for a symbol constness when its declaration is found in a lexical scope;
a3fe9a60 is described below

commit a3fe9a60d2717c409ee5694747a2896b4bc58d56
Author: henrib <he...@apache.org>
AuthorDate: Fri Mar 17 15:19:35 2023 +0100

    JEXL-393: stop searching for a symbol constness when its declaration is found in a lexical scope;
---
 .../org/apache/commons/jexl3/internal/Engine.java  | 13 ++++++---
 .../apache/commons/jexl3/parser/JexlParser.java    |  8 ++---
 .../org/apache/commons/jexl3/Issues300Test.java    |  1 -
 .../java/org/apache/commons/jexl3/LexicalTest.java | 34 ++++++++++++++++++++++
 4 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/internal/Engine.java b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
index ff228295..43266cf9 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Engine.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
@@ -459,18 +459,23 @@ public class Engine extends JexlEngine {
                         ns = new LinkedHashMap<>();
                     }
                     processPragmaNamespace(ns, key, value);
+                    if (!ns.isEmpty()) {
+                        opts.setNamespaces(ns);
+                    }
                 } else if (key.startsWith(PRAGMA_MODULE)) {
                     if (ns == null)  {
                         ns = new LinkedHashMap<>();
                     }
-                    processModulePragma(ns, key, value, script.jexlInfo(), context);
+                    processPragmaModule(ns, key, value, script.jexlInfo(), context);
+                    if (!ns.isEmpty()) {
+                        opts.setNamespaces(ns);
+                    }
                 }
+                // user defined processor may alter options
                 if (processor != null) {
-                    opts.setNamespaces(ns);
                     processor.processPragma(opts, key, value);
                 }
             }
-            opts.setNamespaces(ns);
         }
     }
 
@@ -522,7 +527,7 @@ public class Engine extends JexlEngine {
      * @param info the expression info
      * @param context the value-as-expression evaluation context
      */
-    private void processModulePragma(Map<String, Object> ns, String key, Object value, JexlInfo info, JexlContext context) {
+    private void processPragmaModule(Map<String, Object> ns, String key, Object value, JexlInfo info, JexlContext context) {
         // jexl.module.***
         final String module = key.substring(PRAGMA_MODULE.length());
         if (module.isEmpty()) {
diff --git a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
index eb2cbc0d..6dd134f4 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -691,8 +691,8 @@ public abstract class JexlParser extends StringParser {
      */
     private boolean isConstant(int symbol) {
         if (symbol >= 0) {
-            if (block != null && block.isConstant(symbol)) {
-                return true;
+            if (block != null && block.hasSymbol(symbol)) {
+                return block.isConstant(symbol);
             }
             Scope blockScope = blockScopes.get(block);
             int lexical = symbol;
@@ -708,8 +708,8 @@ public abstract class JexlParser extends StringParser {
                         blockScope = unitScope;
                     }
                 }
-                if (unit.isConstant(lexical)) {
-                    return true;
+                if (unit.hasSymbol(lexical)) {
+                    return unit.isConstant(lexical);
                 }
             }
         }
diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
index f3d087ba..a63239d4 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
@@ -1284,5 +1284,4 @@ public class Issues300Test {
         Assert.assertEquals("foo", x.toString());
     }
 
-
 }
diff --git a/src/test/java/org/apache/commons/jexl3/LexicalTest.java b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
index 62ecfbc3..35462764 100644
--- a/src/test/java/org/apache/commons/jexl3/LexicalTest.java
+++ b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
@@ -1017,6 +1017,40 @@ public class LexicalTest {
         }
     }
 
+    @Test public void testConst3a() {
+        JexlEngine jexl = new JexlBuilder().create();
+        List<String> srcs = Arrays.asList(
+                "const f = ()->{ var foo = 3; foo = 5; }",
+                "const y = '42'; const f = (let y)->{ var foo = 3; foo = 5; }",
+                "const foo = '34'; const f = ()->{ var foo = 3; foo = 5; };",
+                "const bar = '34'; const f = ()->{ var f = 3; f = 5; };",
+                "const bar = '34'; const f = ()->{ var bar = 3; z ->{ bar += z; } };");
+        for(String src: srcs) {
+            JexlScript script = jexl.createScript(src);
+            Object result = script.execute(null);
+            Assert.assertNotNull(src, result);
+        }
+    }
+
+    @Test public void testConst3b() {
+        JexlEngine jexl = new JexlBuilder().create();
+        List<String> srcs = Arrays.asList(
+                "const f = ()->{ var foo = 3; f = 5; }",
+                "const y = '42'; const f = (let z)->{ y += z; }",
+                "const foo = '34'; const f = ()->{ foo = 3; };",
+                "const bar = '34'; const f = ()->{  bar = 3; z ->{ bar += z; } };",
+                "let bar = '34'; const f = ()->{  const bar = 3; z ->{ bar += z; } };");
+        for(String src: srcs) {
+            try {
+                JexlScript script = jexl.createScript(src);
+                Object result = script.execute(null);
+                Assert.fail(src);
+            } catch(JexlException.Assignment xassign) {
+                Assert.assertNotNull(src, xassign); // debug breakpoint
+            }
+        }
+    }
+
     @Test
     public void testSingleStatementDeclFail() {
         List<String> srcs = Arrays.asList(