Search code examples
javajavascriptarraysjava-8nashorn

how to get the array output from a js function in java 8?


I have the following method in the file test.js:

function avg(input, period) {
var output = []; 
if (input === undefined) {
      return output;
} 
var i,j=0;
 for (i = 0; i < input.length- period; i++) {
    var sum =0;
    for (j = 0; j < period; j++) {
        //print (i+j)
        sum =sum + input[i+j];
        }
    //print (sum + " -- " + sum/period)
    output[i]=sum/period;        
}

 return output;
 }

I want to pass an array from java to this function and to get the js output array in java. I used the following java code:

double[] srcC = new double[] { 1.141, 1.12, 1.331, 1.44, 1.751, 1.66, 1.971, 1.88, 1.191, 1.101 };

    try {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn");

        String location = "test.js";
        engine.eval("load(\"" + location + "\");");
        Invocable invocable = (Invocable) engine;
        // double[] a = (double[]) invocable.invokeFunction("avg", srcC, 2);

        System.out.println("obj " + invocable.invokeFunction("avg", srcC, 2));
    } catch (Exception ex) {
        LOGGER.error(ex.getLocalizedMessage());
    }

I am able to see the output of the avg js function but I do not know how to get the js output arrays from the js avg function in java

Any support is appreciated.

Best Regards, Aurelian


Solution

  • The return type from Invocable.invokeFunction is implementation defined. The Nashorn scripting engine returns an instance of an object that implements jdk.nashorn.api.scripting.JSObject

    This interface has a method Collection<Object> values(), so the only required changes are to cast the result of invokeFunction and then extract the collection of values:

     JSObject obj = (JSObject)invocable.invokeFunction("avg", srcC, 2);
     Collection result = obj.values();
     for (Object o : result) {
          System.out.println(o);
     }
    

    output:

    1.1305
    1.2255
    1.3855
    1.5955
    1.7054999999999998
    1.8155000000000001
    1.9255
    1.5354999999999999