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

Thu, 24 May 2018 16:39:31 +0800

author
aoqi
date
Thu, 24 May 2018 16:39:31 +0800
changeset 1959
61ffdd1b89f2
parent 1721
bfc671539e50
parent 1490
d85f981c8cf8
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package jdk.nashorn.internal.runtime.linker;
aoqi@0 27
aoqi@0 28 import static jdk.nashorn.internal.lookup.Lookup.MH;
aoqi@0 29
aoqi@0 30 import java.lang.invoke.MethodHandle;
aoqi@0 31 import java.lang.invoke.MethodHandles;
aoqi@0 32 import jdk.internal.dynalink.linker.ConversionComparator;
aoqi@0 33 import jdk.internal.dynalink.linker.GuardedInvocation;
aoqi@0 34 import jdk.internal.dynalink.linker.GuardedTypeConversion;
aoqi@0 35 import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
aoqi@0 36 import jdk.internal.dynalink.linker.LinkRequest;
aoqi@0 37 import jdk.internal.dynalink.linker.LinkerServices;
aoqi@0 38 import jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker;
aoqi@0 39 import jdk.internal.dynalink.support.TypeUtilities;
aoqi@0 40 import jdk.nashorn.internal.objects.Global;
aoqi@0 41 import jdk.nashorn.internal.runtime.ConsString;
attila@1251 42 import jdk.nashorn.internal.runtime.JSType;
attila@963 43 import jdk.nashorn.internal.runtime.ScriptRuntime;
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * Internal linker for String, Boolean, and Number objects, only ever used by Nashorn engine and not exposed to other
aoqi@0 47 * engines. It is used for treatment of strings, boolean, and numbers as JavaScript primitives. Also provides ECMAScript
aoqi@0 48 * primitive type conversions for these types when linking to Java methods.
aoqi@0 49 */
aoqi@0 50 final class NashornPrimitiveLinker implements TypeBasedGuardingDynamicLinker, GuardingTypeConverterFactory, ConversionComparator {
attila@963 51 private static final GuardedTypeConversion VOID_TO_OBJECT = new GuardedTypeConversion(
attila@963 52 new GuardedInvocation(MethodHandles.constant(Object.class, ScriptRuntime.UNDEFINED)), true);
attila@963 53
aoqi@0 54 @Override
aoqi@0 55 public boolean canLinkType(final Class<?> type) {
aoqi@0 56 return canLinkTypeStatic(type);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 private static boolean canLinkTypeStatic(final Class<?> type) {
hannesw@1721 60 return type == String.class || type == Boolean.class || type == ConsString.class || type == Integer.class
hannesw@1721 61 || type == Double.class || type == Float.class || type == Short.class || type == Byte.class;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 @Override
aoqi@0 65 public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
aoqi@0 66 throws Exception {
aoqi@0 67 final LinkRequest request = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context
aoqi@0 68
aoqi@0 69 final Object self = request.getReceiver();
aoqi@0 70 final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor) request.getCallSiteDescriptor();
aoqi@0 71
attila@963 72 return Bootstrap.asTypeSafeReturn(Global.primitiveLookup(request, self), linkerServices, desc);
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 /**
aoqi@0 76 * This implementation of type converter factory will pretty much allow implicit conversions of anything to anything
aoqi@0 77 * else that's allowed among JavaScript primitive types (string to number, boolean to string, etc.)
aoqi@0 78 * @param sourceType the type to convert from
aoqi@0 79 * @param targetType the type to convert to
aoqi@0 80 * @return a conditional converter from source to target type
aoqi@0 81 */
aoqi@0 82 @Override
aoqi@0 83 public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) {
aoqi@0 84 final MethodHandle mh = JavaArgumentConverters.getConverter(targetType);
aoqi@0 85 if (mh == null) {
attila@963 86 if(targetType == Object.class && sourceType == void.class) {
attila@963 87 return VOID_TO_OBJECT;
attila@963 88 }
aoqi@0 89 return null;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 return new GuardedTypeConversion(new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : GUARD_PRIMITIVE).asType(mh.type().changeParameterType(0, sourceType)), true);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 /**
aoqi@0 96 * Implements the somewhat involved prioritization of JavaScript primitive types conversions. Instead of explaining
aoqi@0 97 * it here in prose, just follow the source code comments.
aoqi@0 98 * @param sourceType the source type to convert from
aoqi@0 99 * @param targetType1 one candidate target type
aoqi@0 100 * @param targetType2 another candidate target type
aoqi@0 101 * @return one of {@link jdk.internal.dynalink.linker.ConversionComparator.Comparison} values signifying which
aoqi@0 102 * target type should be favored for conversion.
aoqi@0 103 */
aoqi@0 104 @Override
aoqi@0 105 public Comparison compareConversion(final Class<?> sourceType, final Class<?> targetType1, final Class<?> targetType2) {
aoqi@0 106 final Class<?> wrapper1 = getWrapperTypeOrSelf(targetType1);
aoqi@0 107 if (sourceType == wrapper1) {
aoqi@0 108 // Source type exactly matches target 1
aoqi@0 109 return Comparison.TYPE_1_BETTER;
aoqi@0 110 }
aoqi@0 111 final Class<?> wrapper2 = getWrapperTypeOrSelf(targetType2);
aoqi@0 112 if (sourceType == wrapper2) {
aoqi@0 113 // Source type exactly matches target 2
aoqi@0 114 return Comparison.TYPE_2_BETTER;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 if (Number.class.isAssignableFrom(sourceType)) {
aoqi@0 118 // If exactly one of the targets is a number, pick it.
aoqi@0 119 if (Number.class.isAssignableFrom(wrapper1)) {
aoqi@0 120 if (!Number.class.isAssignableFrom(wrapper2)) {
aoqi@0 121 return Comparison.TYPE_1_BETTER;
aoqi@0 122 }
aoqi@0 123 } else if (Number.class.isAssignableFrom(wrapper2)) {
aoqi@0 124 return Comparison.TYPE_2_BETTER;
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 // If exactly one of the targets is a character, pick it. Numbers can be reasonably converted to chars using
aoqi@0 128 // the UTF-16 values.
aoqi@0 129 if (Character.class == wrapper1) {
aoqi@0 130 return Comparison.TYPE_1_BETTER;
aoqi@0 131 } else if (Character.class == wrapper2) {
aoqi@0 132 return Comparison.TYPE_2_BETTER;
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 // For all other cases, we fall through to the next if statement - not that we repeat the condition in it
aoqi@0 136 // too so if we entered this branch, we'll enter the below if statement too.
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 if (sourceType == String.class || sourceType == Boolean.class || Number.class.isAssignableFrom(sourceType)) {
aoqi@0 140 // Treat wrappers as primitives.
aoqi@0 141 final Class<?> primitiveType1 = getPrimitiveTypeOrSelf(targetType1);
aoqi@0 142 final Class<?> primitiveType2 = getPrimitiveTypeOrSelf(targetType2);
aoqi@0 143 // Basically, choose the widest possible primitive type. (First "if" returning TYPE_2_BETTER is correct;
aoqi@0 144 // when faced with a choice between double and int, choose double).
aoqi@0 145 if (TypeUtilities.isMethodInvocationConvertible(primitiveType1, primitiveType2)) {
aoqi@0 146 return Comparison.TYPE_2_BETTER;
aoqi@0 147 } else if (TypeUtilities.isMethodInvocationConvertible(primitiveType2, primitiveType1)) {
aoqi@0 148 return Comparison.TYPE_1_BETTER;
aoqi@0 149 }
aoqi@0 150 // Ok, at this point we're out of possible number conversions, so try strings. A String can represent any
aoqi@0 151 // value without loss, so if one of the potential targets is string, go for it.
aoqi@0 152 if (targetType1 == String.class) {
aoqi@0 153 return Comparison.TYPE_1_BETTER;
aoqi@0 154 }
aoqi@0 155 if (targetType2 == String.class) {
aoqi@0 156 return Comparison.TYPE_2_BETTER;
aoqi@0 157 }
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 return Comparison.INDETERMINATE;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 private static Class<?> getPrimitiveTypeOrSelf(final Class<?> type) {
aoqi@0 164 final Class<?> primitive = TypeUtilities.getPrimitiveType(type);
aoqi@0 165 return primitive == null ? type : primitive;
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 private static Class<?> getWrapperTypeOrSelf(final Class<?> type) {
aoqi@0 169 final Class<?> wrapper = TypeUtilities.getWrapperType(type);
aoqi@0 170 return wrapper == null ? type : wrapper;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 @SuppressWarnings("unused")
aoqi@0 174 private static boolean isJavaScriptPrimitive(final Object o) {
hannesw@1721 175 return JSType.isString(o) || o instanceof Boolean || JSType.isNumber(o) || o == null;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 private static final MethodHandle GUARD_PRIMITIVE = findOwnMH("isJavaScriptPrimitive", boolean.class, Object.class);
aoqi@0 179
aoqi@0 180 private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
aoqi@0 181 return MH.findStatic(MethodHandles.lookup(), NashornPrimitiveLinker.class, name, MH.type(rtype, types));
aoqi@0 182 }
aoqi@0 183 }

mercurial