eval
ignores the string if the string is not of type "string":
eval(new String('console.log("foo");'));
So this is likely a consequence of how Rhino treats the property as being of type "object". When you put
a string into the engine, it must convert it to the value type.
This code:
import javax.script.*;
public class ScriptDemo {
public static class Bar {
public String bar = "bar";
}
public static void main(String[] args) throws ScriptException {
ScriptEngine engine =
new ScriptEngineManager().getEngineByName("JavaScript");
engine.put("foo", "foo");
engine.put("bar", new Bar());
engine.eval("println(typeof foo);");
engine.eval("println(typeof bar.bar);");
engine.eval("println(typeof String(bar.bar));");
engine.eval("println(typeof new String(bar.bar));");
}
}
Output:
string
object
string
object