// ...
public Object run(String script) {
return rhinoContext.evaluateString(sharedJsScope, script, "A label here", 1, null);
}
public Object invokeFn(Scriptable o, String fn, Object... args) {
Function function = (Function) ScriptableObject.getProperty(o, fn);
return function.call(this.rhinoContext, this.sharedJsScope, o, args);
}
public Date jsToDate(Object o) {
return (Date) this.rhinoContext.jsToJava(o, Date.class);
}
// ...
// Create a reference to the car object with all its protoypes and state
// Note that by assigning it to an object (without "var") it's returning the object
// as a scriptable, otherwise it would return undefined.
Scriptable car = (Scriptable) jsEngine.run("car = new Car()", "");
// Invoke an instance method on car
double numDoors = (double) jsEngine.invokeFn(car, "getDoors");
// Get a property on the car
Date created = jsEngine.jsToDate(car.get("created", car));
System.out.println("Number of doors: " + numDoors);
System.out.println("Car was created: " + created);
function Car() {
this.created = new Date();
}
Car.prototype.getDoors = function() {
return 4;
}
@snezdoliy