Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
3.1
-
None
Description
Consider the following example
x = 1; if (false) var x = 2; x
In this example the null is returned, which is the value of uninitialized local variable x. The expected behaviour is to return 1.
The reason is the variable declaration is a statement, so one can expect that, as a statement, it should be evaluated according to its position within the script structure.
The suggestion is to move the Scope.declareVariable() call from parse time to interpretation time. Also, the resolution whether an identifier inside the script referes to the local variable or to the contextual variable should be moved from parse time to the interpretation time.
I think we can also reconsider the variable resolution during a variable initialization:
sum = 0; var sum = sum + 1
As of now, Jexl evaluates the local variable sum as 1, as it considers the sum+1 an expression referencing the local variable sum, which is, sort of, pointless. We can change this to referencing a contextual variable instead, so that the result would be 2. It can be useful in plain-vanilla Jexl (without an access differentiator between local and contextual variables) to allow one to initialize a local variable foo with the value of contextual variable foo without using temporary variable.