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 9e8257e JEXL-307: declared variable default value (re)set to null
Task #JEXL-307 - Variable redeclaration option
9e8257e is described below
commit 9e8257ef6d1efec445237ab098cc654d8ba26a7c
Author: Henri Biestro <[email protected]>
AuthorDate: Tue Oct 29 22:46:38 2019 +0100
JEXL-307: declared variable default value (re)set to null
Task #JEXL-307 - Variable redeclaration option
---
.../apache/commons/jexl3/internal/Interpreter.java | 20 ++++-------
.../java/org/apache/commons/jexl3/LexicalTest.java | 39 +++++++++++++++-------
2 files changed, 34 insertions(+), 25 deletions(-)
diff --git a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
index 86eeb75..72a42b6 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java
@@ -120,7 +120,7 @@ public class Interpreter extends InterpreterBase {
protected final Frame frame;
/** Block micro-frames. */
protected LexicalScope block = null;
-
+
/**
* The thread local interpreter.
*/
@@ -158,7 +158,7 @@ public class Interpreter extends InterpreterBase {
INTER.set(inter);
return pinter;
}
-
+
/**
* Interpret the given script/expression.
* <p>
@@ -605,18 +605,12 @@ public class Interpreter extends InterpreterBase {
@Override
protected Object visit(ASTVar node, Object data) {
int symbol = node.getSymbol();
- // if we have a var, we have a scope thus a frame
- Object value;
- if (frame.has(symbol)) {
- value = frame.get(symbol);
- } else {
- frame.set(symbol, null);
- value = null;
- }
if (options.isLexical() && !block.declareSymbol(symbol)) {
return redefinedVariable(node, node.getName());
}
- return value;
+ // if we have a var, we have a scope thus a frame
+ frame.set(symbol, null);
+ return null;
}
@Override
@@ -633,7 +627,7 @@ public class Interpreter extends InterpreterBase {
block = lexical;
}
}
-
+
/**
* Base visitation for blocks.
* @param node the block
@@ -676,7 +670,7 @@ public class Interpreter extends InterpreterBase {
final int symbol = loopVariable.getSymbol();
final LexicalScope lexical = block;
if (options.isLexical()) {
- // the iteration variable can not be declared in parent block
+ // the iteration variable can not be declared in parent block
if (symbol >= 0 && block.hasSymbol(symbol)) {
return redefinedVariable(node, loopVariable.getName());
}
diff --git a/src/test/java/org/apache/commons/jexl3/LexicalTest.java
b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
index 3520c16..5cf9bfd 100644
--- a/src/test/java/org/apache/commons/jexl3/LexicalTest.java
+++ b/src/test/java/org/apache/commons/jexl3/LexicalTest.java
@@ -23,7 +23,7 @@ import org.junit.Test;
* Test cases for lexical option and feature.
*/
public class LexicalTest {
-
+
@Test
public void testLexical0a() throws Exception {
runLexical0(false);
@@ -112,7 +112,7 @@ public class LexicalTest {
script.execute(ctxt, 42);
}
}
-
+
@Test
public void testLexical1a() throws Exception {
runLexical1(false);
@@ -156,7 +156,7 @@ public class LexicalTest {
if (!shade) {
throw xany;
}
- }
+ }
try {
// if local shade, x = 42 is undefined
script = jexl.createScript("{ var x = 0; } y = 42");
@@ -177,7 +177,7 @@ public class LexicalTest {
// y being defined as global
ctxt.set("y", 4242);
try {
- // if no shade and global y being defined,
+ // if no shade and global y being defined,
script = jexl.createScript("{ var y = 0; } y = 42");
result = script.execute(ctxt);
if (!shade) {
@@ -189,9 +189,9 @@ public class LexicalTest {
if (!shade) {
throw xany;
}
- }
+ }
}
-
+
@Test
public void testLexical1() throws Exception {
JexlEngine jexl = new JexlBuilder().strict(true).create();
@@ -201,7 +201,7 @@ public class LexicalTest {
options.setLexical(true);
JexlScript script;
Object result;
-
+
script = jexl.createScript("var x = 0; for(var y : [1]) { var x = 42;
return x; };");
try {
result = script.execute(ctxt);
@@ -210,7 +210,7 @@ public class LexicalTest {
} catch (JexlException xany) {
String ww = xany.toString();
}
-
+
try {
script = jexl.createScript("(x)->{ if (x) { var x = 7 * (x + x);
x; } }");
result = script.execute(ctxt, 3);
@@ -218,11 +218,26 @@ public class LexicalTest {
} catch (JexlException xany) {
String ww = xany.toString();
}
-
+
script = jexl.createScript("{ var x = 0; } var x = 42; x");
result = script.execute(ctxt, 21);
- Assert.assertEquals(42, result);
-
+ Assert.assertEquals(42, result);
+ }
+
+ @Test
+ public void testLexical2() throws Exception {
+ JexlEngine jexl = new JexlBuilder().strict(true).create();
+ JexlEvalContext ctxt = new JexlEvalContext();
+ JexlOptions options = ctxt.getEngineOptions();
+ // ensure errors will throw
+ options.setLexical(true);
+
+ JexlScript script = jexl.createScript("{var x = 42}; {var x; return x;
}");
+ try {
+ Object result = script.execute(ctxt);
+ Assert.assertNull(result);
+ } catch (JexlException xany) {
+ String ww = xany.toString();
+ }
}
-
}