Why does not jsr-223 evaluate string when is a attribute of a object?
Simple class with only one String attribute:
public class EvalJSR223Bean {
public String evalFnt;
}
Simple evaluation using text and object, and when is used the object, Rhino does not execute eval. But if I concatenate empty javascript string to object property, Rhino eval.
public static void main(String[] args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval("function f2() {println('EXECUTED!!!!!')}; function f1(source) { return eval(source); };");
String evalFnt = "(function(){f2();return '0';})();";
engine.put("evalFnt", evalFnt);
engine.eval("f1(evalFnt);"); // f2 is executed.
EvalJSR223Bean bean = new EvalJSR223Bean();
bean.evalFnt = evalFnt;
engine.put("bean1", bean.evalFnt);
engine.eval("f1(bean1.evalFnt);"); // Why does NOT executed f2 ?!!.
engine.put("bean", bean);
engine.eval("f1(bean.evalFnt);"); // Why does NOT executed f2 ?!!.
engine.put("bean", bean);
engine.eval("f1( ''+bean.evalFnt );"); // And if I concatenate a string, f2 is executed!!!
}
evalignores the string if the string is not of type “string”:So this is likely a consequence of how Rhino treats the property as being of type “object”. When you
puta string into the engine, it must convert it to the value type.This code:
Output: