0

Executing the following code in Java7

ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");
Bindings b = scriptEngine.createBindings();
b.put("x", true);
scriptEngine.eval("x&y", b);

I get the error

sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "b" is not defined. (<Unknown Source>#1) in <Unknown Source> at line number 1

Is there an option to evaluate to null/false for undefined objects, like in JavaScript?

I know that an option will be to do something like "this.x&this.y" instead of "x&y", but I don't have control over that string (user entered).

Adrian Ber
  • 17,162
  • 9
  • 57
  • 99

1 Answers1

0

I browsed a little bit through the Rhino code and it seems that there's no such option.

In the end I will append "this." in front of each variable. This is not by far a desirable solution (I will not even accept my own answer :) ), but for the time being I have no other.

Adrian Ber
  • 17,162
  • 9
  • 57
  • 99