I am new to JSR-223 Java Scripting, actually I'm switching from MVEL to standard Mozilla Rhino JS. I have read all documentation, but get stuck. I have tried to reference some Java objects from script by bindings just like in tutorial:
// my object
public class MyBean {
public String getStringValue() { return "abc" };
}
// initialization
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// add bindings
engine.put("bean", new MyBean());
// evaluate script, output is "abc"
engine.eval("print(bean.stringValue)");
Java object is referenced from script as property bean
. So far so good.
But I want to reference my object in script as this
, I want to use its properties and methods without any prefix or explicitely with prefix this
. Just like this:
// add bindings
engine.put(....., new MyBean()); // or whatever ???
// evaluate scripts, all have the same output "abc"
engine.eval("print(stringValue)");
engine.eval("print(this.stringValue)");
I know that this
in JavaScript has special meaning (as in Java) but in MVEL scripting that could be done by using custom ParserContext
and custom PropertyHandler
.
Is something like this possible in Rhino?
Thanks a lot.
Well, in JavaScript it really only makes sense to think about
this
being set in the context of a function being invoked. Thus I think you should be able to use the "invoke" method on the ScriptEngine (which has to be cast to "Invocable"):Now the "objectForThis" reference is (in my experience) generally something that was returned from a prior call to "eval()" (or "invokeMethod" I guess); in other words, it's supposed to be an object in the appropriate language for the script engine. Whether you could pass in a Java object there (and have it work out), I don't know for sure.
I try to implement this idea from answer from Pointy (thanks again), but this workaround doesn't work for properties without
this
prefix, which seems to be IMHO the very same. Instead of using Rhino 1.5 from Java API, there is original Rhino 1.7 from Mozilla. Test case is here:Why
this.getStringValue()/this.stringValue
works andgetStringValue()/stringValue
doesn't? Have some point overlooked? Pointy?