Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java Primitive Wrapped Object to Javascrip Primitive

2 views
Skip to first unread message

Johnia

unread,
Oct 16, 2009, 2:33:24 PM10/16/09
to
Hi, I am new to Rhino. 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_thread/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

0 new messages