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 
  26 package jdk.nashorn.internal.runtime.linker;
  27 
  28 import static jdk.nashorn.internal.lookup.Lookup.MH;
  29 
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.reflect.Modifier;
  33 import java.util.Deque;
  34 import java.util.List;
  35 import java.util.Map;
  36 import javax.script.Bindings;
  37 import jdk.internal.dynalink.CallSiteDescriptor;
  38 import jdk.internal.dynalink.linker.ConversionComparator;
  39 import jdk.internal.dynalink.linker.GuardedInvocation;
  40 import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
  41 import jdk.internal.dynalink.linker.LinkRequest;
  42 import jdk.internal.dynalink.linker.LinkerServices;
  43 import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
  44 import jdk.internal.dynalink.support.Guards;
  45 import jdk.nashorn.api.scripting.JSObject;
  46 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  47 import jdk.nashorn.internal.objects.NativeArray;
  48 import jdk.nashorn.internal.runtime.Context;
  49 import jdk.nashorn.internal.runtime.JSType;
  50 import jdk.nashorn.internal.runtime.ScriptFunction;
  51 import jdk.nashorn.internal.runtime.ScriptObject;
  52 import jdk.nashorn.internal.runtime.Undefined;
  53 
  54 /**
  55  * This is the main dynamic linker for Nashorn. It is used for linking all {@link ScriptObject} and its subclasses (this
  56  * includes {@link ScriptFunction} and its subclasses) as well as {@link Undefined}.
  57  */
  58 final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator {
  59     private static final ClassValue<MethodHandle> ARRAY_CONVERTERS = new ClassValue<MethodHandle>() {
  60         @Override
  61         protected MethodHandle computeValue(Class<?> type) {
  62             return createArrayConverter(type);
  63         }
  64     };
  65 
  66     /**
  67      * Returns true if {@code ScriptObject} is assignable from {@code type}, or it is {@code Undefined}.
  68      */
  69     @Override
  70     public boolean canLinkType(final Class<?> type) {
  71         return canLinkTypeStatic(type);
  72     }
  73 
  74     static boolean canLinkTypeStatic(final Class<?> type) {
  75         return ScriptObject.class.isAssignableFrom(type) || Undefined.class == type;
  76     }
  77 
  78     @Override
  79     public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
  80         final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
  81         final Object self = requestWithoutContext.getReceiver();
  82         final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
  83 
  84         if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
  85             // We only support standard "dyn:*[:*]" operations
  86             return null;
  87         }
  88 
  89         final GuardedInvocation inv;
  90         if (self instanceof ScriptObject) {
  91             inv = ((ScriptObject)self).lookup(desc, request);
  92         } else if (self instanceof Undefined) {
  93             inv = Undefined.lookup(desc);
  94         } else {
  95             throw new AssertionError(); // Should never reach here.
  96         }
  97 
  98         return Bootstrap.asType(inv, linkerServices, desc);
  99     }
 100 
 101     @Override
 102     public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 103         final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
 104         return gi == null ? null : gi.asType(MH.type(targetType, sourceType));
 105     }
 106 
 107     /**
 108      * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't
 109      * care about adapting the method signature; that's done by the invoking method. Returns either a built-in
 110      * conversion to primitive (or primitive wrapper) Java types or to String, or a just-in-time generated converter to
 111      * a SAM type (if the target type is a SAM type).
 112      * @param sourceType the source type
 113      * @param targetType the target type
 114      * @return a guarded invocation that converts from the source type to the target type.
 115      * @throws Exception if something goes wrong
 116      */
 117     private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 118         final MethodHandle mh = JavaArgumentConverters.getConverter(targetType);
 119         if (mh != null) {
 120             return new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : IS_NASHORN_OR_UNDEFINED_TYPE);
 121         }
 122 
 123         final GuardedInvocation arrayConverter = getArrayConverter(sourceType, targetType);
 124         if(arrayConverter != null) {
 125             return arrayConverter;
 126         }
 127 
 128         final GuardedInvocation mapConverter = getMirrorConverter(sourceType, targetType);
 129         if(mapConverter != null) {
 130             return mapConverter;
 131         }
 132  
 133         return getSamTypeConverter(sourceType, targetType);
 134     }
 135 
 136     /**
 137      * Returns a guarded invocation that converts from a source type that is ScriptFunction, or a subclass or a
 138      * superclass of it) to a SAM type.
 139      * @param sourceType the source type (presumably ScriptFunction or a subclass or a superclass of it)
 140      * @param targetType the target type (presumably a SAM type)
 141      * @return a guarded invocation that converts from the source type to the target SAM type. null is returned if
 142      * either the source type is neither ScriptFunction, nor a subclass, nor a superclass of it, or if the target type
 143      * is not a SAM type.
 144      * @throws Exception if something goes wrong; generally, if there's an issue with creation of the SAM proxy type
 145      * constructor.
 146      */
 147     private static GuardedInvocation getSamTypeConverter(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 148         // If source type is more generic than ScriptFunction class, we'll need to use a guard
 149         final boolean isSourceTypeGeneric = sourceType.isAssignableFrom(ScriptFunction.class);
 150 
 151         if ((isSourceTypeGeneric || ScriptFunction.class.isAssignableFrom(sourceType)) && isAutoConvertibleFromFunction(targetType)) {
 152             final MethodHandle ctor = JavaAdapterFactory.getConstructor(ScriptFunction.class, targetType);
 153             assert ctor != null; // if isAutoConvertibleFromFunction() returned true, then ctor must exist.
 154             return new GuardedInvocation(ctor, isSourceTypeGeneric ? IS_SCRIPT_FUNCTION : null);
 155         }
 156         return null;
 157     }
 158 
 159     /**
 160      * Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or
 161      * Deque type.
 162      * @param sourceType the source type (presumably NativeArray a superclass of it)
 163      * @param targetType the target type (presumably an array type, or List or Deque)
 164      * @return a guarded invocation that converts from the source type to the target type. null is returned if
 165      * either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array
 166      * type, List, or Deque.
 167      */
 168     private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType) {
 169         final boolean isSourceTypeNativeArray = sourceType == NativeArray.class;
 170         // If source type is more generic than ScriptFunction class, we'll need to use a guard
 171         final boolean isSourceTypeGeneric = !isSourceTypeNativeArray && sourceType.isAssignableFrom(NativeArray.class);
 172 
 173         if (isSourceTypeNativeArray || isSourceTypeGeneric) {
 174             final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
 175             if(targetType.isArray()) {
 176                 return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard);
 177             }
 178             if(targetType == List.class) {
 179                 return new GuardedInvocation(JSType.TO_JAVA_LIST.methodHandle(), guard);
 180             }
 181             if(targetType == Deque.class) {
 182                 return new GuardedInvocation(JSType.TO_JAVA_DEQUE.methodHandle(), guard);
 183             }
 184         }
 185         return null;
 186     }
 187 
 188     private static MethodHandle createArrayConverter(final Class<?> type) {
 189         assert type.isArray();
 190         final MethodHandle converter = MH.insertArguments(JSType.TO_JAVA_ARRAY.methodHandle(), 1, type.getComponentType());
 191         return MH.asType(converter, converter.type().changeReturnType(type));
 192     }
 193 
 194     private static GuardedInvocation getMirrorConverter(Class<?> sourceType, Class<?> targetType) {
 195         // Could've also used (targetType.isAssignableFrom(ScriptObjectMirror.class) && targetType != Object.class) but
 196         // it's probably better to explicitly spell out the supported target types
 197         if (targetType == Map.class || targetType == Bindings.class || targetType == JSObject.class || targetType == ScriptObjectMirror.class) {
 198             if(ScriptObject.class.isAssignableFrom(sourceType)) {
 199                 return new GuardedInvocation(CREATE_MIRROR, null);
 200             }
 201             return new GuardedInvocation(CREATE_MIRROR, IS_SCRIPT_OBJECT);
 202         }
 203         return null;
 204     }
 205 
 206     private static boolean isAutoConvertibleFromFunction(final Class<?> clazz) {
 207         return isAbstractClass(clazz) && !ScriptObject.class.isAssignableFrom(clazz) &&
 208                 JavaAdapterFactory.isAutoConvertibleFromFunction(clazz);
 209     }
 210 
 211     /**
 212      * Utility method used by few other places in the code. Tests if the class has the abstract modifier and is not an
 213      * array class. For some reason, array classes have the abstract modifier set in HotSpot JVM, and we don't want to
 214      * treat array classes as abstract.
 215      * @param clazz the inspected class
 216      * @return true if the class is abstract and is not an array type.
 217      */
 218     static boolean isAbstractClass(final Class<?> clazz) {
 219         return Modifier.isAbstract(clazz.getModifiers()) && !clazz.isArray();
 220     }
 221 
 222 
 223     @Override
 224     public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
 225         if(sourceType == NativeArray.class) {
 226             // Prefer lists, as they're less costly to create than arrays.
 227             if(isList(targetType1)) {
 228                 if(!isList(targetType2)) {
 229                     return Comparison.TYPE_1_BETTER;
 230                 }
 231             } else if(isList(targetType2)) {
 232                 return Comparison.TYPE_2_BETTER;
 233             }
 234             // Then prefer arrays
 235             if(targetType1.isArray()) {
 236                 if(!targetType2.isArray()) {
 237                     return Comparison.TYPE_1_BETTER;
 238                 }
 239             } else if(targetType2.isArray()) {
 240                 return Comparison.TYPE_2_BETTER;
 241             }
 242         }
 243         if(ScriptObject.class.isAssignableFrom(sourceType)) {
 244             // Prefer interfaces
 245             if(targetType1.isInterface()) {
 246                 if(!targetType2.isInterface()) {
 247                     return Comparison.TYPE_1_BETTER;
 248                 }
 249             } else if(targetType2.isInterface()) {
 250                 return Comparison.TYPE_2_BETTER;
 251             }
 252         }
 253         return Comparison.INDETERMINATE;
 254     }
 255 
 256     private static boolean isList(Class<?> clazz) {
 257         return clazz == List.class || clazz == Deque.class;
 258     }
 259 
 260     private static final MethodHandle IS_SCRIPT_OBJECT = Guards.isInstance(ScriptObject.class, MH.type(Boolean.TYPE, Object.class));
 261     private static final MethodHandle IS_SCRIPT_FUNCTION = Guards.isInstance(ScriptFunction.class, MH.type(Boolean.TYPE, Object.class));
 262     private static final MethodHandle IS_NATIVE_ARRAY = Guards.isOfClass(NativeArray.class, MH.type(Boolean.TYPE, Object.class));
 263 
 264     private static final MethodHandle IS_NASHORN_OR_UNDEFINED_TYPE = findOwnMH("isNashornTypeOrUndefined", Boolean.TYPE, Object.class);
 265     private static final MethodHandle CREATE_MIRROR = findOwnMH("createMirror", Object.class, Object.class);
 266 
 267     @SuppressWarnings("unused")
 268     private static boolean isNashornTypeOrUndefined(final Object obj) {
 269         return obj instanceof ScriptObject || obj instanceof Undefined;
 270     }
 271 
 272     @SuppressWarnings("unused")
 273     private static Object createMirror(final Object obj) {
 274         return ScriptObjectMirror.wrap(obj, Context.getGlobal());
 275     }
 276 
 277     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 278         return MH.findStatic(MethodHandles.lookup(), NashornLinker.class, name, MH.type(rtype, types));
 279     }
 280 }
 281 
--- EOF ---