java - Rhino Javascript: How to convert Object to a Javascript primitive? -


I am using javax.scripting with Rhino in this project.

I have a Java method that gives a Java object (double, long, figure, etc.). I want to call that method from javascript and reference the result as javascript primitive type. However, javacript recognizes the return type as an object.

How do I apply it to convert to a JavaScript primitive?

This question is very similar

How do I get the context of reference and rapfactry with that problem?

Sample code:

  public class data {double value = 1.0d; Get public number () {return value; }} Public static zero main (string [] args) {ScriptEngine engine = new scriptEngineManager (). GetEngineByName ("rhino"); Data data = new data (); {Engine.eval ("Function Test (Data) {Return Data. GetD ('value1') + 5;};"); System.out.println ("Result:" + ((Incognitive engine). Invoch function ("test", data)); } Hold (exception e) {e.printStackTrace (); }}  

Output: Results: 15

try the following

  Public static zero master throws an exception {scriptEngine engine = new scriptEngineManager (). GetEngineByName ("rhino"); Data data = new data (); Context.enter () getWrapFactory () setJavaPrimitiveWrap (wrong) .. Try {engine.eval ("Function Test (Data) {try data.get ('value1') + 5;};" "); System.out.println ("Result:" + ((Incognitive engine). Invoch function ("test", data)); } Hold (exception e) {e.printStackTrace (); }} Public Static Class Data {Double Value = 1.0D; Public number received (string AGR) {return value; }}  

Alternatively, you can modify the javascript function to explicitly insert a value for a number:

  Function Test (Data) {Return ParseInt (data.get ('value1'), 10) + 5;}  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -