[ 
https://issues.apache.org/jira/browse/JEXL-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17993135#comment-17993135
 ] 

Henri Biestro commented on JEXL-442:
------------------------------------

The culprit is the JXLT expression (ie `${x}`) whose parsing is delayed and for 
which variable capture is limited to its usage scope.
In the example, adding a local variable to the test function that captures x 
and referencing it in the expression 'solves' the problem.
{code}
        String code = "var x = 'hello';\n" +
                "function test() {\n" +
                "  let y = x;\n" +
                "  return `${y}`;\n" +
                "}\n" + "test();";
{code}

> Global variables can't be found in template.
> --------------------------------------------
>
>                 Key: JEXL-442
>                 URL: https://issues.apache.org/jira/browse/JEXL-442
>             Project: Commons JEXL
>          Issue Type: Bug
>            Reporter: Xu Pengcheng
>            Priority: Major
>
> The following code throws an exception: JexlExample.main:24@3:1 variable 'x' 
> is undefined
> {code:java}
> JexlBuilder jexlBuilder = new JexlBuilder();
> JexlEngine jexl = jexlBuilder.create();
> JexlContext context = new MapContext();
> String code = """
>   var x = 'hello';
>   function test() {
>     return `${x}`
>   }
>   test();
> """;
> JexlScript script = jexl.createScript(code);
> Object result = script.execute(context);
> System.out.println("Expression Result: " + result);{code}
> but if I change to 
> {code:java}
> return x; {code}
> which works.



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

Reply via email to