[
https://issues.apache.org/jira/browse/JEXL-307?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17020834#comment-17020834
]
Dmitri Blinov edited comment on JEXL-307 at 1/23/20 9:07 AM:
-------------------------------------------------------------
And one more thing... hoisted variables are not resolved in nested blocks -
_*variable 'x' is undefined*_
{code:java}
@Test
public void testHoisted() throws Exception {
JexlFeatures f = new JexlFeatures();
f.lexical(true);
JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
JexlScript script = jexl.createScript("var x = 10; var a = function(var
b) {for (var q : 1 ..10) {return x + b}}; a(32)");
JexlContext jc = new MapContext();
Object result = script.execute(null);
Assert.assertEquals(result, 42);
}
{code}
If the lexical feature is switched off everything works OK.
The problem stems from the fact that hoisted variables *may* change their frame
pointer index (aka symbol) between frames. So when we check identifier inside
{{JexlParser.checkVariable()}} we can not simply check for all lexical units
for the identifier to be declared by its *symbol*. We either need to keep track
of Frame-LexicalUnit relation when we traverse the lexical stack, or to check
for variable by identifier *name* inside LexicalUnit.
PS. Technically, what we call a *hoisted* variable in JEXL is a *captured*
variable. Maybe its a good point to rebrand this term to make things clear not
only from implementation point, but from its definition?
was (Author: dmitri_blinov):
And one more thing... hoisted variables are not resolved in nested blocks -
_*variable 'x' is undefined*_
{code:java}
@Test
public void testHoisted() throws Exception {
JexlFeatures f = new JexlFeatures();
f.lexical(true);
JexlEngine jexl = new JexlBuilder().strict(true).features(f).create();
JexlScript script = jexl.createScript("var x = 10; var a = function(var
b) {for (var q : 1 ..10) {return x + b}}; a(32)");
JexlContext jc = new MapContext();
Object result = script.execute(null);
Assert.assertEquals(result, 42);
}
{code}
If the lexical feature is switched off everything works OK.
The problem stems from the fact that hoisted variables *may* change their frame
pointer index (aka symbol) between frames. So when we check identifier inside
{{JexlParser.checkVariable()}} we can not simply check for all lexical units
for the identifier to be declared by its *symbol*. We either need to keep track
of Frame-LexicalUnit relation when we traverse the lexical stack, or to check
for variable by identifier *name* inside LexicalUnit.
> Variable redeclaration option
> -----------------------------
>
> Key: JEXL-307
> URL: https://issues.apache.org/jira/browse/JEXL-307
> Project: Commons JEXL
> Issue Type: New Feature
> Affects Versions: 3.1
> Reporter: Dmitri Blinov
> Assignee: Henri Biestro
> Priority: Minor
> Fix For: 3.2
>
>
> As of now, JEXL allows a script writer to redeclare a local variable during
> script evaluation.
> {code:java}
> var a = 1; var a = 2;{code}
> This may lead to potential errors with misspelled names and clashed
> variables. Checking for already defined variable is a common feature of many
> languages. This feature can be implemented in JEXL as an additional option of
> JexlFeatures class, enabled by default, thus allowing compatibility with
> existing code.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)