NashornPrimitiveLinker.java revision 1475:1faacf3cd85f
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.util.function.Supplier; 33import jdk.internal.dynalink.linker.ConversionComparator; 34import jdk.internal.dynalink.linker.GuardedInvocation; 35import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 36import jdk.internal.dynalink.linker.LinkRequest; 37import jdk.internal.dynalink.linker.LinkerServices; 38import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker; 39import jdk.internal.dynalink.support.TypeUtilities; 40import jdk.nashorn.internal.objects.Global; 41import jdk.nashorn.internal.runtime.ConsString; 42import jdk.nashorn.internal.runtime.JSType; 43import jdk.nashorn.internal.runtime.ScriptRuntime; 44 45/** 46 * Internal linker for String, Boolean, and Number objects, only ever used by Nashorn engine and not exposed to other 47 * engines. It is used for treatment of strings, boolean, and numbers as JavaScript primitives. Also provides ECMAScript 48 * primitive type conversions for these types when linking to Java methods. 49 */ 50final class NashornPrimitiveLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator { 51 private static final GuardedInvocation VOID_TO_OBJECT = 52 new GuardedInvocation(MethodHandles.constant(Object.class, ScriptRuntime.UNDEFINED)); 53 54 @Override 55 public boolean canLinkType(final Class<?> type) { 56 return canLinkTypeStatic(type); 57 } 58 59 private static boolean canLinkTypeStatic(final Class<?> type) { 60 return type == String.class || type == Boolean.class || type == ConsString.class || Number.class.isAssignableFrom(type); 61 } 62 63 @Override 64 public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) 65 throws Exception { 66 final Object self = request.getReceiver(); 67 final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor) request.getCallSiteDescriptor(); 68 69 return Bootstrap.asTypeSafeReturn(Global.primitiveLookup(request, self), linkerServices, desc); 70 } 71 72 /** 73 * This implementation of type converter factory will pretty much allow implicit conversions of anything to anything 74 * else that's allowed among JavaScript primitive types (string to number, boolean to string, etc.) 75 * @param sourceType the type to convert from 76 * @param targetType the type to convert to 77 * @return a conditional converter from source to target type 78 */ 79 @Override 80 public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) { 81 final MethodHandle mh = JavaArgumentConverters.getConverter(targetType); 82 if (mh == null) { 83 if(targetType == Object.class && sourceType == void.class) { 84 return VOID_TO_OBJECT; 85 } 86 return null; 87 } 88 89 return new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : GUARD_PRIMITIVE).asType(mh.type().changeParameterType(0, sourceType)); 90 } 91 92 /** 93 * Implements the somewhat involved prioritization of JavaScript primitive types conversions. Instead of explaining 94 * it here in prose, just follow the source code comments. 95 * @param sourceType the source type to convert from 96 * @param targetType1 one candidate target type 97 * @param targetType2 another candidate target type 98 * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which 99 * target type should be favored for conversion. 100 */ 101 @Override 102 public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) { 103 final Class<?> wrapper1 = getWrapperTypeOrSelf(targetType1); 104 if (sourceType == wrapper1) { 105 // Source type exactly matches target 1 106 return Comparison.TYPE_1_BETTER; 107 } 108 final Class<?> wrapper2 = getWrapperTypeOrSelf(targetType2); 109 if (sourceType == wrapper2) { 110 // Source type exactly matches target 2 111 return Comparison.TYPE_2_BETTER; 112 } 113 114 if (Number.class.isAssignableFrom(sourceType)) { 115 // If exactly one of the targets is a number, pick it. 116 if (Number.class.isAssignableFrom(wrapper1)) { 117 if (!Number.class.isAssignableFrom(wrapper2)) { 118 return Comparison.TYPE_1_BETTER; 119 } 120 } else if (Number.class.isAssignableFrom(wrapper2)) { 121 return Comparison.TYPE_2_BETTER; 122 } 123 124 // If exactly one of the targets is a character, pick it. Numbers can be reasonably converted to chars using 125 // the UTF-16 values. 126 if (Character.class == wrapper1) { 127 return Comparison.TYPE_1_BETTER; 128 } else if (Character.class == wrapper2) { 129 return Comparison.TYPE_2_BETTER; 130 } 131 132 // For all other cases, we fall through to the next if statement - not that we repeat the condition in it 133 // too so if we entered this branch, we'll enter the below if statement too. 134 } 135 136 if (sourceType == String.class || sourceType == Boolean.class || Number.class.isAssignableFrom(sourceType)) { 137 // Treat wrappers as primitives. 138 final Class<?> primitiveType1 = getPrimitiveTypeOrSelf(targetType1); 139 final Class<?> primitiveType2 = getPrimitiveTypeOrSelf(targetType2); 140 // Basically, choose the widest possible primitive type. (First "if" returning TYPE_2_BETTER is correct; 141 // when faced with a choice between double and int, choose double). 142 if (TypeUtilities.isMethodInvocationConvertible(primitiveType1, primitiveType2)) { 143 return Comparison.TYPE_2_BETTER; 144 } else if (TypeUtilities.isMethodInvocationConvertible(primitiveType2, primitiveType1)) { 145 return Comparison.TYPE_1_BETTER; 146 } 147 // Ok, at this point we're out of possible number conversions, so try strings. A String can represent any 148 // value without loss, so if one of the potential targets is string, go for it. 149 if (targetType1 == String.class) { 150 return Comparison.TYPE_1_BETTER; 151 } 152 if (targetType2 == String.class) { 153 return Comparison.TYPE_2_BETTER; 154 } 155 } 156 157 return Comparison.INDETERMINATE; 158 } 159 160 private static Class<?> getPrimitiveTypeOrSelf(final Class<?> type) { 161 final Class<?> primitive = TypeUtilities.getPrimitiveType(type); 162 return primitive == null ? type : primitive; 163 } 164 165 private static Class<?> getWrapperTypeOrSelf(final Class<?> type) { 166 final Class<?> wrapper = TypeUtilities.getWrapperType(type); 167 return wrapper == null ? type : wrapper; 168 } 169 170 @SuppressWarnings("unused") 171 private static boolean isJavaScriptPrimitive(final Object o) { 172 return JSType.isString(o) || o instanceof Boolean || o instanceof Number || o == null; 173 } 174 175 private static final MethodHandle GUARD_PRIMITIVE = findOwnMH("isJavaScriptPrimitive", boolean.class, Object.class); 176 177 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { 178 return MH.findStatic(MethodHandles.lookup(), NashornPrimitiveLinker.class, name, MH.type(rtype, types)); 179 } 180} 181