< prev index next >
src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/linker/JavaAdapterServices.java
Print this page
*** 37,46 ****
--- 37,47 ----
import java.lang.invoke.ConstantCallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
+ import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.Permissions;
import java.security.PrivilegedAction;
*** 49,58 ****
--- 50,60 ----
import java.util.Objects;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.org.objectweb.asm.Type;
import jdk.internal.org.objectweb.asm.commons.InstructionAdapter;
+ import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.objects.Global;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.ECMAException;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.ScriptFunction;
*** 174,183 ****
--- 176,202 ----
// fixed "toString" argument.
return sobj.getMap().findProperty("toString") != null;
}
/**
+ * Returns the ScriptObject or Global field value from a ScriptObjectMirror using reflection.
+ *
+ * @param mirror the mirror object
+ * @param getGlobal true if we want the global object, false to return the script object
+ * @return the script object or global object
+ */
+ public static ScriptObject unwrapMirror(final Object mirror, final boolean getGlobal) {
+ assert mirror instanceof ScriptObjectMirror;
+ try {
+ final Field field = getGlobal ? MirrorFieldHolder.GLOBAL_FIELD : MirrorFieldHolder.SOBJ_FIELD;
+ return (ScriptObject) field.get(mirror);
+ } catch (final IllegalAccessException x) {
+ throw new RuntimeException(x);
+ }
+ }
+
+ /**
* Delegate to {@link Bootstrap#bootstrap(Lookup, String, MethodType, int)}.
* @param lookup MethodHandle lookup.
* @param opDesc Dynalink dynamic operation descriptor.
* @param type Method type.
* @param flags flags for call type, trace/profile etc.
*** 289,294 ****
--- 308,333 ----
return new ConstantCallSite(
MethodHandles.identity(Object[].class)
.asCollector(Object[].class, type.parameterCount())
.asType(type));
}
+
+ // Initialization on demand holder for accessible ScriptObjectMirror fields
+ private static class MirrorFieldHolder {
+
+ private static final Field SOBJ_FIELD = getMirrorField("sobj");
+ private static final Field GLOBAL_FIELD = getMirrorField("global");
+
+ private static Field getMirrorField(final String fieldName) {
+ try {
+ final Field field = ScriptObjectMirror.class.getDeclaredField(fieldName);
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+ field.setAccessible(true);
+ return null;
+ });
+ return field;
+ } catch (final NoSuchFieldException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
}
< prev index next >