Uploaded image for project: 'Commons JEXL'
  1. Commons JEXL
  2. JEXL-287

Wrong resolution of local variables

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.1
    • 3.2
    • 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.

      Attachments

        Activity

          People

            henrib Henri Biestro
            dmitri_blinov Dmitri Blinov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: