You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Xu Pengcheng (Jira)" <ji...@apache.org> on 2024/08/24 06:27:00 UTC

[jira] [Created] (JEXL-426) Value of Captured Variable

Xu Pengcheng created JEXL-426:
---------------------------------

             Summary: Value of Captured Variable 
                 Key: JEXL-426
                 URL: https://issues.apache.org/jira/browse/JEXL-426
             Project: Commons JEXL
          Issue Type: Improvement
            Reporter: Xu Pengcheng


For the code
{code:java}
let x = 10;
function foo() {
  x += 2;
}
foo();
return x;{code}
By default the return value of x is 10 because `x` inside `foo` is a captured value, re-assign value not affect the variable outside `foo`, which some times confuse the developer, therefore a new feature `constCapture` was introduced to prevent such kind of re-assignment, which is the same behavior of Java code.

But the problem is variable `x` outside of function is not const, for the following code

 
{code:java}
1:  let x = 10;
2:  function foo() {
3:    someCall(x);
4:  }
5:  x = 20;
6:  foo();
7:  return x; {code}
This code is valid with constCapture = true (similar code in java is invalid because x needs to be 'final'), but the value inside `foo` is 10, not 20, I checked the JEXL source codes and found the value inside `foo` is captured when defining `foo` function (line 3), which is 10, even when calling `foo` in line 6 the value of x is already be 20, this case is also somehow confuse.

 

I tried extending Interpreter to change the behavior of captured variable to:
 # re-assign the captured variable to latest value from parent frame before executing.
 # populate the value to parent scope while assigning new value to a captured variable

with this interpreter, the value of x in line 3 and 7 are 22, which I think is more easy to understand.
{code:java}
1:  let x = 10;
2:  function foo() {
3:    x += 2; // here x = 22
4:  }
5:  x = 20;
6:  foo();
7:  return x; // here x = 22{code}
 

Is it possible to add a new feature to support this behavior?

or provide more apis, i.e.
 # api to get whole frame stack (now I store it in JexlContext)
 # read/write value by variable name for a frame (now I reflect the get/set/getSymbol methods of Frame class)
 # get parent of Scope.

Thanks very much!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)