NashornLinker.java revision 1481:e6bb9489faac
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package jdk.nashorn.internal.runtime.linker; 27 28import static jdk.nashorn.internal.lookup.Lookup.MH; 29 30import java.lang.invoke.MethodHandle; 31import java.lang.invoke.MethodHandles; 32import java.lang.invoke.MethodType; 33import java.lang.reflect.Modifier; 34import java.security.AccessControlContext; 35import java.security.AccessController; 36import java.security.PrivilegedAction; 37import java.util.Collection; 38import java.util.Deque; 39import java.util.List; 40import java.util.Map; 41import java.util.Queue; 42import java.util.function.Supplier; 43import javax.script.Bindings; 44import jdk.internal.dynalink.CallSiteDescriptor; 45import jdk.internal.dynalink.linker.ConversionComparator; 46import jdk.internal.dynalink.linker.GuardedInvocation; 47import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 48import jdk.internal.dynalink.linker.LinkRequest; 49import jdk.internal.dynalink.linker.LinkerServices; 50import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 51import jdk.internal.dynalink.linker.support.Guards; 52import jdk.internal.dynalink.linker.support.Lookup; 53import jdk.nashorn.api.scripting.JSObject; 54import jdk.nashorn.api.scripting.ScriptObjectMirror; 55import jdk.nashorn.api.scripting.ScriptUtils; 56import jdk.nashorn.internal.objects.NativeArray; 57import jdk.nashorn.internal.runtime.AccessControlContextFactory; 58import jdk.nashorn.internal.runtime.JSType; 59import jdk.nashorn.internal.runtime.ListAdapter; 60import jdk.nashorn.internal.runtime.ScriptFunction; 61import jdk.nashorn.internal.runtime.ScriptObject; 62import jdk.nashorn.internal.runtime.Undefined; 63 64/** 65 * This is the main dynamic linker for Nashorn. It is used for linking all {@link ScriptObject} and its subclasses (this 66 * includes {@link ScriptFunction} and its subclasses) as well as {@link Undefined}. 67 */ 68final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator { 69 private static final AccessControlContext GET_LOOKUP_PERMISSION_CONTEXT = 70 AccessControlContextFactory.createAccessControlContext(CallSiteDescriptor.GET_LOOKUP_PERMISSION); 71 72 private static final ClassValue<MethodHandle> ARRAY_CONVERTERS = new ClassValue<MethodHandle>() { 73 @Override 74 protected MethodHandle computeValue(final Class<?> type) { 75 return createArrayConverter(type); 76 } 77 }; 78 79 /** 80 * Returns true if {@code ScriptObject} is assignable from {@code type}, or it is {@code Undefined}. 81 */ 82 @Override 83 public boolean canLinkType(final Class<?> type) { 84 return canLinkTypeStatic(type); 85 } 86 87 static boolean canLinkTypeStatic(final Class<?> type) { 88 return ScriptObject.class.isAssignableFrom(type) || Undefined.class == type; 89 } 90 91 @Override 92 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception { 93 final Object self = request.getReceiver(); 94 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 95 96 if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) { 97 // We only support standard "dyn:*[:*]" operations 98 return null; 99 } 100 101 return Bootstrap.asTypeSafeReturn(getGuardedInvocation(self, request, desc), linkerServices, desc); 102 } 103 104 private static GuardedInvocation getGuardedInvocation(final Object self, final LinkRequest request, final CallSiteDescriptor desc) { 105 final GuardedInvocation inv; 106 if (self instanceof ScriptObject) { 107 inv = ((ScriptObject)self).lookup(desc, request); 108 } else if (self instanceof Undefined) { 109 inv = Undefined.lookup(desc); 110 } else { 111 throw new AssertionError(self.getClass().getName()); // Should never reach here. 112 } 113 114 return inv; 115 } 116 117 @Override 118 public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) throws Exception { 119 GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType); 120 if(gi == null) { 121 gi = getSamTypeConverter(sourceType, targetType, lookupSupplier); 122 } 123 return gi == null ? null : gi.asType(MH.type(targetType, sourceType)); 124 } 125 126 /** 127 * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't 128 * care about adapting the method signature; that's done by the invoking method. Returns either a built-in 129 * conversion to primitive (or primitive wrapper) Java types or to String, or a just-in-time generated converter to 130 * a SAM type (if the target type is a SAM type). 131 * @param sourceType the source type 132 * @param targetType the target type 133 * @return a guarded invocation that converts from the source type to the target type. 134 * @throws Exception if something goes wrong 135 */ 136 private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception { 137 final MethodHandle mh = JavaArgumentConverters.getConverter(targetType); 138 if (mh != null) { 139 return new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : IS_NASHORN_OR_UNDEFINED_TYPE); 140 } 141 142 final GuardedInvocation arrayConverter = getArrayConverter(sourceType, targetType); 143 if(arrayConverter != null) { 144 return arrayConverter; 145 } 146 147 return getMirrorConverter(sourceType, targetType); 148 } 149 150 /** 151 * Returns a guarded invocation that converts from a source type that is ScriptFunction, or a subclass or a 152 * superclass of it) to a SAM type. 153 * @param sourceType the source type (presumably ScriptFunction or a subclass or a superclass of it) 154 * @param targetType the target type (presumably a SAM type) 155 * @return a guarded invocation that converts from the source type to the target SAM type. null is returned if 156 * either the source type is neither ScriptFunction, nor a subclass, nor a superclass of it, or if the target type 157 * is not a SAM type. 158 * @throws Exception if something goes wrong; generally, if there's an issue with creation of the SAM proxy type 159 * constructor. 160 */ 161 private static GuardedInvocation getSamTypeConverter(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) throws Exception { 162 // If source type is more generic than ScriptFunction class, we'll need to use a guard 163 final boolean isSourceTypeGeneric = sourceType.isAssignableFrom(ScriptFunction.class); 164 165 if ((isSourceTypeGeneric || ScriptFunction.class.isAssignableFrom(sourceType)) && isAutoConvertibleFromFunction(targetType)) { 166 final MethodHandle ctor = JavaAdapterFactory.getConstructor(ScriptFunction.class, targetType, getCurrentLookup(lookupSupplier)); 167 assert ctor != null; // if isAutoConvertibleFromFunction() returned true, then ctor must exist. 168 return new GuardedInvocation(ctor, isSourceTypeGeneric ? IS_SCRIPT_FUNCTION : null); 169 } 170 return null; 171 } 172 173 private static MethodHandles.Lookup getCurrentLookup(final Supplier<MethodHandles.Lookup> lookupSupplier) { 174 return AccessController.doPrivileged(new PrivilegedAction<MethodHandles.Lookup>() { 175 @Override 176 public MethodHandles.Lookup run() { 177 return lookupSupplier.get(); 178 } 179 }, GET_LOOKUP_PERMISSION_CONTEXT); 180 } 181 182 /** 183 * Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or 184 * Queue or Deque or Collection type. 185 * @param sourceType the source type (presumably NativeArray a superclass of it) 186 * @param targetType the target type (presumably an array type, or List or Queue, or Deque, or Collection) 187 * @return a guarded invocation that converts from the source type to the target type. null is returned if 188 * either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array 189 * type, List, Queue, Deque, or Collection. 190 */ 191 private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType) { 192 final boolean isSourceTypeNativeArray = sourceType == NativeArray.class; 193 // If source type is more generic than NativeArray class, we'll need to use a guard 194 final boolean isSourceTypeGeneric = !isSourceTypeNativeArray && sourceType.isAssignableFrom(NativeArray.class); 195 196 if (isSourceTypeNativeArray || isSourceTypeGeneric) { 197 final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null; 198 if(targetType.isArray()) { 199 return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard); 200 } else if(targetType == List.class) { 201 return new GuardedInvocation(TO_LIST, guard); 202 } else if(targetType == Deque.class) { 203 return new GuardedInvocation(TO_DEQUE, guard); 204 } else if(targetType == Queue.class) { 205 return new GuardedInvocation(TO_QUEUE, guard); 206 } else if(targetType == Collection.class) { 207 return new GuardedInvocation(TO_COLLECTION, guard); 208 } 209 } 210 return null; 211 } 212 213 private static MethodHandle createArrayConverter(final Class<?> type) { 214 assert type.isArray(); 215 final MethodHandle converter = MH.insertArguments(JSType.TO_JAVA_ARRAY.methodHandle(), 1, type.getComponentType()); 216 return MH.asType(converter, converter.type().changeReturnType(type)); 217 } 218 219 private static GuardedInvocation getMirrorConverter(final Class<?> sourceType, final Class<?> targetType) { 220 // Could've also used (targetType.isAssignableFrom(ScriptObjectMirror.class) && targetType != Object.class) but 221 // it's probably better to explicitly spell out the supported target types 222 if (targetType == Map.class || targetType == Bindings.class || targetType == JSObject.class || targetType == ScriptObjectMirror.class) { 223 if (ScriptObject.class.isAssignableFrom(sourceType)) { 224 return new GuardedInvocation(CREATE_MIRROR); 225 } else if (sourceType.isAssignableFrom(ScriptObject.class) || sourceType.isInterface()) { 226 return new GuardedInvocation(CREATE_MIRROR, IS_SCRIPT_OBJECT); 227 } 228 } 229 return null; 230 } 231 232 private static boolean isAutoConvertibleFromFunction(final Class<?> clazz) { 233 return isAbstractClass(clazz) && !ScriptObject.class.isAssignableFrom(clazz) && 234 JavaAdapterFactory.isAutoConvertibleFromFunction(clazz); 235 } 236 237 /** 238 * Utility method used by few other places in the code. Tests if the class has the abstract modifier and is not an 239 * array class. For some reason, array classes have the abstract modifier set in HotSpot JVM, and we don't want to 240 * treat array classes as abstract. 241 * @param clazz the inspected class 242 * @return true if the class is abstract and is not an array type. 243 */ 244 static boolean isAbstractClass(final Class<?> clazz) { 245 return Modifier.isAbstract(clazz.getModifiers()) && !clazz.isArray(); 246 } 247 248 249 @Override 250 public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) { 251 if(sourceType == NativeArray.class) { 252 // Prefer lists, as they're less costly to create than arrays. 253 if(isList(targetType1)) { 254 if(!isList(targetType2)) { 255 return Comparison.TYPE_1_BETTER; 256 } 257 } else if(isList(targetType2)) { 258 return Comparison.TYPE_2_BETTER; 259 } 260 // Then prefer arrays 261 if(targetType1.isArray()) { 262 if(!targetType2.isArray()) { 263 return Comparison.TYPE_1_BETTER; 264 } 265 } else if(targetType2.isArray()) { 266 return Comparison.TYPE_2_BETTER; 267 } 268 } 269 if(ScriptObject.class.isAssignableFrom(sourceType)) { 270 // Prefer interfaces 271 if(targetType1.isInterface()) { 272 if(!targetType2.isInterface()) { 273 return Comparison.TYPE_1_BETTER; 274 } 275 } else if(targetType2.isInterface()) { 276 return Comparison.TYPE_2_BETTER; 277 } 278 } 279 return Comparison.INDETERMINATE; 280 } 281 282 private static boolean isList(final Class<?> clazz) { 283 return clazz == List.class || clazz == Deque.class; 284 } 285 286 private static final MethodHandle IS_SCRIPT_OBJECT = Guards.isInstance(ScriptObject.class, MH.type(Boolean.TYPE, Object.class)); 287 private static final MethodHandle IS_SCRIPT_FUNCTION = Guards.isInstance(ScriptFunction.class, MH.type(Boolean.TYPE, Object.class)); 288 private static final MethodHandle IS_NATIVE_ARRAY = Guards.isOfClass(NativeArray.class, MH.type(Boolean.TYPE, Object.class)); 289 290 private static final MethodHandle IS_NASHORN_OR_UNDEFINED_TYPE = findOwnMH("isNashornTypeOrUndefined", Boolean.TYPE, Object.class); 291 private static final MethodHandle CREATE_MIRROR = findOwnMH("createMirror", Object.class, Object.class); 292 293 private static final MethodHandle TO_COLLECTION; 294 private static final MethodHandle TO_DEQUE; 295 private static final MethodHandle TO_LIST; 296 private static final MethodHandle TO_QUEUE; 297 static { 298 final MethodHandle listAdapterCreate = new Lookup(MethodHandles.lookup()).findStatic( 299 ListAdapter.class, "create", MethodType.methodType(ListAdapter.class, Object.class)); 300 TO_COLLECTION = asReturning(listAdapterCreate, Collection.class); 301 TO_DEQUE = asReturning(listAdapterCreate, Deque.class); 302 TO_LIST = asReturning(listAdapterCreate, List.class); 303 TO_QUEUE = asReturning(listAdapterCreate, Queue.class); 304 } 305 306 private static MethodHandle asReturning(final MethodHandle mh, final Class<?> nrtype) { 307 return mh.asType(mh.type().changeReturnType(nrtype)); 308 } 309 310 @SuppressWarnings("unused") 311 private static boolean isNashornTypeOrUndefined(final Object obj) { 312 return obj instanceof ScriptObject || obj instanceof Undefined; 313 } 314 315 @SuppressWarnings("unused") 316 private static Object createMirror(final Object obj) { 317 return obj instanceof ScriptObject? ScriptUtils.wrap((ScriptObject)obj) : obj; 318 } 319 320 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 321 return MH.findStatic(MethodHandles.lookup(), NashornLinker.class, name, MH.type(rtype, types)); 322 } 323} 324 325