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 2019/11/28 16:11:14 UTC

[commons-jexl] branch master updated: JEXL-307: handle top-level lambda creation consistently Task #JEXL-307 - Variable redeclaration option

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 4f1a6d1  JEXL-307: handle top-level lambda creation consistently Task #JEXL-307 - Variable redeclaration option
4f1a6d1 is described below

commit 4f1a6d15e8a9a837b4e2326f74a44a8632163559
Author: henrib <he...@apache.org>
AuthorDate: Thu Nov 28 17:09:55 2019 +0100

    JEXL-307: handle top-level lambda creation consistently
    Task #JEXL-307 - Variable redeclaration option
---
 .../java/org/apache/commons/jexl3/JexlEngine.java    | 12 ++++++------
 .../org/apache/commons/jexl3/internal/Engine.java    |  7 +++----
 .../java/org/apache/commons/jexl3/LexicalTest.java   | 20 ++++++++++++++++++++
 3 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/JexlEngine.java b/src/main/java/org/apache/commons/jexl3/JexlEngine.java
index 7b9d198..a5cfcee 100644
--- a/src/main/java/org/apache/commons/jexl3/JexlEngine.java
+++ b/src/main/java/org/apache/commons/jexl3/JexlEngine.java
@@ -385,14 +385,14 @@ public abstract class JexlEngine {
      * Creates a Script from a String containing valid JEXL syntax.
      * This method parses the script and validates the syntax.
      *
-     * @param scriptText A String containing valid JEXL syntax
+     * @param source A String containing valid JEXL syntax
      * @param names      The script parameter names used during parsing; a corresponding array of arguments containing
      * values should be used during evaluation
      * @return A {@link JexlScript} which can be executed using a {@link JexlContext}
      * @throws JexlException if there is a problem parsing the script
      */
-    public final JexlScript createScript(String scriptText, String... names) {
-        return createScript(null, null, scriptText, names);
+    public final JexlScript createScript(String source, String... names) {
+        return createScript(null, null, source, names);
     }
 
     /**
@@ -432,7 +432,7 @@ public abstract class JexlEngine {
      * @return A {@link JexlScript} which can be executed with a {@link JexlContext}.
      * @throws JexlException if there is a problem reading or parsing the script.
      */
-    public final JexlScript createScript(JexlInfo info, File scriptFile, String[] names) {
+    public final JexlScript createScript(JexlInfo info, File scriptFile, String... names) {
         return createScript(null, info, readSource(scriptFile), names);
     }
 
@@ -458,7 +458,7 @@ public abstract class JexlEngine {
      * @return A {@link JexlScript} which can be executed with a {@link JexlContext}.
      * @throws JexlException if there is a problem reading or parsing the script.
      */
-    public final JexlScript createScript(URL scriptUrl, String[] names) {
+    public final JexlScript createScript(URL scriptUrl, String... names) {
         return createScript(null, null, readSource(scriptUrl), names);
     }
 
@@ -473,7 +473,7 @@ public abstract class JexlEngine {
      * @return A {@link JexlScript} which can be executed with a {@link JexlContext}.
      * @throws JexlException if there is a problem reading or parsing the script.
      */
-    public final JexlScript createScript(JexlInfo info, URL scriptUrl, String[] names) {
+    public final JexlScript createScript(JexlInfo info, URL scriptUrl, String... names) {
         return createScript(null, info, readSource(scriptUrl), names);
     }
 
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 0296777..2b96b98 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Engine.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Engine.java
@@ -315,9 +315,8 @@ public class Engine extends JexlEngine {
             if (jexlo != null) {
                 return jexlo.isSharedInstance()? jexlo : jexlo.copy();
             } 
-        }
-        // The following block for compatibility between 3.1 and 3.2
-        else if (context instanceof JexlEngine.Options) {
+        } else if (context instanceof JexlEngine.Options) {
+            // This condition and block for compatibility between 3.1 and 3.2
             JexlOptions jexlo = options.copy();
             JexlEngine jexl = this;
             JexlEngine.Options opts = (JexlEngine.Options) context;
@@ -422,7 +421,7 @@ public class Engine extends JexlEngine {
             throw new NullPointerException("source is null");
         }
         String source = trimSource(scriptText);
-        Scope scope = names == null ? null : new Scope(null, names);
+        Scope scope = names == null || names.length == 0? null : new Scope(null, names);
         JexlFeatures ftrs = features == null? scriptFeatures : features;
         ASTJexlScript tree = parse(info, ftrs, source, scope);
         return new Script(this, source, tree);
diff --git a/src/test/java/org/apache/commons/jexl3/LexicalTest.java b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
index 2c6b2f7..8a6273f 100644
--- a/src/test/java/org/apache/commons/jexl3/LexicalTest.java
+++ b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
@@ -460,4 +460,24 @@ public class LexicalTest {
         // result is exception!
         Assert.assertTrue(result instanceof JexlException.Variable);
     }
+    
+    @Test
+    public void testParameter0() throws Exception {
+        String str = "function(u) {}";
+        JexlEngine jexl = new JexlBuilder().create();
+        JexlScript e = jexl.createScript(str);
+        Assert.assertEquals(1, e.getParameters().length);
+        e = jexl.createScript(new JexlInfo("TestScript", 1, 1), str);
+        Assert.assertEquals(1, e.getParameters().length);
+    } 
+    
+    @Test
+    public void testParameter1() throws Exception {
+        JexlEngine jexl = new JexlBuilder().strict(true).lexical(true).create();
+        JexlContext jc = new MapContext();
+        String strs = "var s = function(x) { for (var i : 1..3) {if (i > 2) return x}}; s(42)";
+        JexlScript s42 = jexl.createScript(strs);
+        Object result = s42.execute(jc);
+        Assert.assertEquals(42, result);
+    }
 }