ScriptObjectMirror
has an unwrap
method that takes a Global
object. The problem is that I can't figure out how to send the global object because there doesn't seem to be an easy way to access it. This means I always get the mirror and not the raw object (decompiled code from ScriptObjectMirror.class
):
public static Object unwrap(Object obj, Object homeGlobal) {
if(obj instanceof ScriptObjectMirror) {
ScriptObjectMirror mirror = (ScriptObjectMirror) obj;
return mirror.global == homeGlobal ? mirror.sobj : obj;
} else {
return obj instanceof JSONListAdapter?((JSONListAdapter)obj).unwrap(homeGlobal):obj;
}
}
How do I pass in the correct global object?
In Nashorn you cannot unwrap ScriptObjectMirror
instances from a foreign context. That is, if the current global (the JavaScript "global" object) is not the same as the global of the object being unwrapped, Nashorn will not unwrap it. There also doesn't appear to be a way to access a JavaScript object's current global.
There is a ScriptUtils#unwrap
that doesn't take in a global, but it also shows the same behavior.