I am utilizing the javax.scripting with Rhino in this project.
I have a Java method that returns a Java Object (Double, Long, Integer, etc). I want to call that method from javascript and reference the result as a Javascript primitive type. However, javacript recognizes the return type as an Object.
How do I force it to convert to a javascript primitive?
This question is very similar to http://groups.google.com/group/mozilla.dev.tech.js-engine.rhino/browse%5Fthread/thread/2f3d43bb49d5288a/dde79e1aa72e1301
The problem with that is how do I get a reference to the context and the WrapFactory?
Sample Code:
public class data
{
Double value = 1.0d;
public Number get() { return value; }
}
public static void main(String[] args)
{
ScriptEngine engine = new ScriptEngineManager().getEngineByName ("rhino");
data data = new data();
try
{
engine.eval("function test(data) { return data.getD('value1') + 5;};");
System.out.println("Result:" + ((Invocable)engine).invokeFunction("test", data));
}
catch (Exception e)
{
e.printStackTrace();
}
}
Output: Result: 15