http://mail.openjdk.java.net/pipermail/nashorn-dev/2014-July/003204.html
Inconsistency about conversion to ScriptObjectMirror with getInterface and invokeFunction makes it difficult know which kind of object is being worked with in Nashorn. Example;
package mirrors;
import java.util.*;
import javax.script.*;
public class Mirrors {
public interface Example {
Object test1(Object arg);
Object test2(Object arg);
}
public static void main(String[] args) throws Exception {
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");
Invocable invocable = (Invocable)engine;
engine.eval("function test1(arg) { print(arg.class); return { arg: arg }; }");
engine.eval("function test2(arg) { print(arg.class); return arg; }");
Map<String, Object> map = new HashMap<>();
map.put("option", true);
Example example = invocable.getInterface(Example.class);
Object value1 = invocable.invokeFunction("test1", map);
Object value2 = example.test1(map);
Object value3 = invocable.invokeFunction("test2", value2);
Object value4 = example.test2(value2);
System.out.println(value1.getClass());
System.out.println(value2.getClass());
System.out.println(value3.getClass());
System.out.println(value4.getClass());
}
}
Output:
class java.util.HashMap
class java.util.HashMap
undefined
undefined
class jdk.nashorn.api.scripting.ScriptObjectMirror
class jdk.nashorn.internal.scripts.JO4
class jdk.nashorn.api.scripting.ScriptObjectMirror
class jdk.nashorn.internal.scripts.JO4