Rhino Javascript: how to convert an object to a Javascript primitive?

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

I have a Java method that returns a Java object (Double, Long, Integer, etc.). I want to call this method from javascript and reference the result as a primitive Javascript type. However, javacript recognizes the return type as an object.

How do I get 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 this is how to get the context and WrapFactory reference?

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(); 
            } 
    } 

      

Conclusion: Result: 15

+2


source to share


3 answers


Try to run

 public static void main(String [] args) throws Exception {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName ("rhino"); 
        data data = new data();
        Context.enter().getWrapFactory().setJavaPrimitiveWrap(false);

        try 
        { 
                engine.eval("function test(data) { return data.get('value1') + 5;};"); 
                System.out.println("Result:" + ((Invocable)engine).invokeFunction("test", data)); 
        } 
        catch (Exception e) 
        { 
                e.printStackTrace(); 
        } 
    }

    public static class data 
    { 
       Double value = 1.0d; 
       public Number get(String arg)  {  return value; } 
    } 

      




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

function test(data) { return parseInt(data.get('value1'), 10) + 5;}

      

+3


source


Use the following as a result in JavaScript code:



function aFunction(data)
{
  return parseInt(data);
}

      

0


source


I developed this application in which you enter a set of mathematical formulas that will be evaluated by the JavaScript engine built into Java 6, which I consider to be a port of rhino. The idea was that we would have a set of maps and these maps would contain variables like:

MAP["VALUE 1"]
MAP["VALUE 2"]

      

I used this approach as some of the formula expressions came from variables that I was in control of that might not be valid JS IDs. My first approach was to declare the map and add it to the JS engine, but it failed just like it did with it - it interpreted it as a string, not a number.

The solution was to parse the formula, figure out what variables are used by it, and then declare the object inside the JS engine.

Something like that:

var MAP = new Object();
MAP["VALUE 1"] = 1;
MAP["VALUE 2"] = 2;

      

And then it MAP["VALUE 1"] + MAP["VALUE 2"]

will return 3

, not 12

.

There might be a better solution out there, but once you start using the initial coding / parsing it will always work. In our case, there is a phase in which we execute "declarative" statements, and another phase where we execute formulas. The JS engine is REALLY fast so this is not a problem for us.

0


source







All Articles