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

Xu Pengcheng commented on JEXL-442:
-----------------------------------

Here is my fix by customizing TemplateInterpreter and JexlContext
{code:java}
@Override
protected Object getVariable(Frame frame, LexicalScope block, ASTIdentifier 
identifier) {
  if (identifier.getSymbol() < 0) {
    MyContext ctx = (MyContext) this.context;
    String name = identifier.getName();
    if (ctx.hasVariableValue(name)) {
      return ctx.getVariableValue(name);
    }
  }
  return super.getVariable(frame, block, identifier);
}{code}
MyContext is my customized JexlContext which hold the call frame stack, if the 
symbol < 0, I will find it from the parent call frame. 

> 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