- Loading...
...
If you a java method called from script accepts ScriptObjectMirror as a parameter, nashorn converts script object to ScriptObjectMirror - so that your method can manipulate script objects via method of the class ScriptObjectMirror interface. Note that this auto conversion from script object to ScriptObjectMirror won't happen if you use "Object" as the param type of your Java method. Also, if you put script object into a Java Object[] or any other Collection like a List, Map, script object conversion won't happen. If you convert a script array of script objects via Java.to API to ScriptObjectMirror[], then the script object to mirror conversion will happen. With jdk8u40 onwards, script objects are converted to ScriptObjectMirror whenever script objects are passed to Java layer - even with Object type params or assigned to a Object[] element. Such wrapped mirror instances are automatically unwrapped when execution crosses to script boundary. i.e., say a Java method returns Object type value which happens to be ScriptObjectMirror object, then script caller will see it a ScriptObject instance (mirror gets unwrapped automatically)
If you want to explicitly wrap a script object to a ScriptObjectMirror instance and pass along to any API (which may even accept Object), you can use jdk.nashorn.api.scripting.ScriptUtils class's wrap method. Similarly you can explicitly unwrap a ScriptObjectMirror to a script object via unwrap method. Note that unwrap won't unwrap the object if it is not a wrapper of object created with the current global object. In most instances you won't have to wrap/unwrap manually. In Java code, always operate on ScriptObjectMirror instances and in script you'll always see normal ScriptObject (unwrapped) instances automatically.
...