src/jdk/nashorn/internal/runtime/linker/NashornLinker.java

Print this page




  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 jdk.internal.dynalink.CallSiteDescriptor;
  36 import jdk.internal.dynalink.linker.ConversionComparator;
  37 import jdk.internal.dynalink.linker.GuardedInvocation;
  38 import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
  39 import jdk.internal.dynalink.linker.LinkRequest;
  40 import jdk.internal.dynalink.linker.LinkerServices;
  41 import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
  42 import jdk.internal.dynalink.support.Guards;


  43 import jdk.nashorn.internal.objects.NativeArray;

  44 import jdk.nashorn.internal.runtime.JSType;
  45 import jdk.nashorn.internal.runtime.ScriptFunction;
  46 import jdk.nashorn.internal.runtime.ScriptObject;
  47 import jdk.nashorn.internal.runtime.Undefined;
  48 
  49 /**
  50  * This is the main dynamic linker for Nashorn. It is used for linking all {@link ScriptObject} and its subclasses (this
  51  * includes {@link ScriptFunction} and its subclasses) as well as {@link Undefined}.
  52  */
  53 final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator {
  54     private static final ClassValue<MethodHandle> ARRAY_CONVERTERS = new ClassValue<MethodHandle>() {
  55         @Override
  56         protected MethodHandle computeValue(Class<?> type) {
  57             return createArrayConverter(type);
  58         }
  59     };
  60 
  61     /**
  62      * Returns true if {@code ScriptObject} is assignable from {@code type}, or it is {@code Undefined}.
  63      */


  98         final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
  99         return gi == null ? null : gi.asType(MH.type(targetType, sourceType));
 100     }
 101 
 102     /**
 103      * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't
 104      * care about adapting the method signature; that's done by the invoking method. Returns either a built-in
 105      * conversion to primitive (or primitive wrapper) Java types or to String, or a just-in-time generated converter to
 106      * a SAM type (if the target type is a SAM type).
 107      * @param sourceType the source type
 108      * @param targetType the target type
 109      * @return a guarded invocation that converts from the source type to the target type.
 110      * @throws Exception if something goes wrong
 111      */
 112     private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 113         final MethodHandle mh = JavaArgumentConverters.getConverter(targetType);
 114         if (mh != null) {
 115             return new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : IS_NASHORN_OR_UNDEFINED_TYPE);
 116         }
 117 
 118         GuardedInvocation inv = getArrayConverter(sourceType, targetType);
 119         if(inv != null) {
 120             return inv;





 121         }
 122 
 123         return getSamTypeConverter(sourceType, targetType);
 124     }
 125 
 126     /**
 127      * Returns a guarded invocation that converts from a source type that is ScriptFunction, or a subclass or a
 128      * superclass of it) to a SAM type.
 129      * @param sourceType the source type (presumably ScriptFunction or a subclass or a superclass of it)
 130      * @param targetType the target type (presumably a SAM type)
 131      * @return a guarded invocation that converts from the source type to the target SAM type. null is returned if
 132      * either the source type is neither ScriptFunction, nor a subclass, nor a superclass of it, or if the target type
 133      * is not a SAM type.
 134      * @throws Exception if something goes wrong; generally, if there's an issue with creation of the SAM proxy type
 135      * constructor.
 136      */
 137     private static GuardedInvocation getSamTypeConverter(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 138         // If source type is more generic than ScriptFunction class, we'll need to use a guard
 139         final boolean isSourceTypeGeneric = sourceType.isAssignableFrom(ScriptFunction.class);
 140 


 164             final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
 165             if(targetType.isArray()) {
 166                 return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard);
 167             }
 168             if(targetType == List.class) {
 169                 return new GuardedInvocation(JSType.TO_JAVA_LIST.methodHandle(), guard);
 170             }
 171             if(targetType == Deque.class) {
 172                 return new GuardedInvocation(JSType.TO_JAVA_DEQUE.methodHandle(), guard);
 173             }
 174         }
 175         return null;
 176     }
 177 
 178     private static MethodHandle createArrayConverter(final Class<?> type) {
 179         assert type.isArray();
 180         final MethodHandle converter = MH.insertArguments(JSType.TO_JAVA_ARRAY.methodHandle(), 1, type.getComponentType());
 181         return MH.asType(converter, converter.type().changeReturnType(type));
 182     }
 183 












 184     private static boolean isAutoConvertibleFromFunction(final Class<?> clazz) {
 185         return isAbstractClass(clazz) && !ScriptObject.class.isAssignableFrom(clazz) &&
 186                 JavaAdapterFactory.isAutoConvertibleFromFunction(clazz);
 187     }
 188 
 189     /**
 190      * Utility method used by few other places in the code. Tests if the class has the abstract modifier and is not an
 191      * array class. For some reason, array classes have the abstract modifier set in HotSpot JVM, and we don't want to
 192      * treat array classes as abstract.
 193      * @param clazz the inspected class
 194      * @return true if the class is abstract and is not an array type.
 195      */
 196     static boolean isAbstractClass(final Class<?> clazz) {
 197         return Modifier.isAbstract(clazz.getModifiers()) && !clazz.isArray();
 198     }
 199 
 200 
 201     @Override
 202     public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
 203         if(sourceType == NativeArray.class) {


 218                 return Comparison.TYPE_2_BETTER;
 219             }
 220         }
 221         if(ScriptObject.class.isAssignableFrom(sourceType)) {
 222             // Prefer interfaces
 223             if(targetType1.isInterface()) {
 224                 if(!targetType2.isInterface()) {
 225                     return Comparison.TYPE_1_BETTER;
 226                 }
 227             } else if(targetType2.isInterface()) {
 228                 return Comparison.TYPE_2_BETTER;
 229             }
 230         }
 231         return Comparison.INDETERMINATE;
 232     }
 233 
 234     private static boolean isList(Class<?> clazz) {
 235         return clazz == List.class || clazz == Deque.class;
 236     }
 237 

 238     private static final MethodHandle IS_SCRIPT_FUNCTION = Guards.isInstance(ScriptFunction.class, MH.type(Boolean.TYPE, Object.class));
 239     private static final MethodHandle IS_NATIVE_ARRAY = Guards.isOfClass(NativeArray.class, MH.type(Boolean.TYPE, Object.class));
 240 
 241     private static final MethodHandle IS_NASHORN_OR_UNDEFINED_TYPE = findOwnMH("isNashornTypeOrUndefined",
 242             Boolean.TYPE, Object.class);
 243 
 244     @SuppressWarnings("unused")
 245     private static boolean isNashornTypeOrUndefined(final Object obj) {
 246         return obj instanceof ScriptObject || obj instanceof Undefined;





 247     }
 248 
 249     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 250         return MH.findStatic(MethodHandles.lookup(), NashornLinker.class, name, MH.type(rtype, types));
 251     }
 252 }
 253 


  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      */


 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 


 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) {


 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