NashornBottomLinker.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; 29import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 30import static jdk.nashorn.internal.runtime.JSType.GET_UNDEFINED; 31import static jdk.nashorn.internal.runtime.JSType.TYPE_OBJECT_INDEX; 32import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 33 34import java.lang.invoke.MethodHandle; 35import java.lang.invoke.MethodHandles; 36import java.util.HashMap; 37import java.util.Map; 38import java.util.function.Supplier; 39import jdk.internal.dynalink.CallSiteDescriptor; 40import jdk.internal.dynalink.beans.BeansLinker; 41import jdk.internal.dynalink.linker.GuardedInvocation; 42import jdk.internal.dynalink.linker.GuardingDynamicLinker; 43import jdk.internal.dynalink.linker.GuardingTypeConverterFactory; 44import jdk.internal.dynalink.linker.LinkRequest; 45import jdk.internal.dynalink.linker.LinkerServices; 46import jdk.internal.dynalink.support.Guards; 47import jdk.nashorn.internal.codegen.types.Type; 48import jdk.nashorn.internal.runtime.JSType; 49import jdk.nashorn.internal.runtime.ScriptRuntime; 50import jdk.nashorn.internal.runtime.UnwarrantedOptimismException; 51 52/** 53 * Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other 54 * linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw 55 * appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and 56 * setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for 57 * attempts to invoke arbitrary Java objects as functions or constructors. 58 */ 59final class NashornBottomLinker implements GuardingDynamicLinker, GuardingTypeConverterFactory { 60 61 @Override 62 public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) 63 throws Exception { 64 final Object self = linkRequest.getReceiver(); 65 66 if (self == null) { 67 return linkNull(linkRequest); 68 } 69 70 // None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below 71 // this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug. 72 assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName(); 73 74 return linkBean(linkRequest, linkerServices); 75 } 76 77 private static final MethodHandle EMPTY_PROP_GETTER = 78 MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class); 79 private static final MethodHandle EMPTY_ELEM_GETTER = 80 MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class); 81 private static final MethodHandle EMPTY_PROP_SETTER = 82 MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class)); 83 private static final MethodHandle EMPTY_ELEM_SETTER = 84 MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class); 85 86 private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception { 87 final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); 88 final Object self = linkRequest.getReceiver(); 89 final String operator = desc.getFirstOperator(); 90 switch (operator) { 91 case "new": 92 if(BeansLinker.isDynamicConstructor(self)) { 93 throw typeError("no.constructor.matches.args", ScriptRuntime.safeToString(self)); 94 } 95 if(BeansLinker.isDynamicMethod(self)) { 96 throw typeError("method.not.constructor", ScriptRuntime.safeToString(self)); 97 } 98 throw typeError("not.a.function", desc.getFunctionErrorMessage(self)); 99 case "call": 100 if(BeansLinker.isDynamicConstructor(self)) { 101 throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self)); 102 } 103 if(BeansLinker.isDynamicMethod(self)) { 104 throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self)); 105 } 106 throw typeError("not.a.function", desc.getFunctionErrorMessage(self)); 107 case "callMethod": 108 throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self)); 109 case "getMethod": 110 // evaluate to undefined, later on Undefined will take care of throwing TypeError 111 return getInvocation(MH.dropArguments(GET_UNDEFINED.get(TYPE_OBJECT_INDEX), 0, Object.class), self, linkerServices, desc); 112 case "getProp": 113 case "getElem": 114 if(NashornCallSiteDescriptor.isOptimistic(desc)) { 115 throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT); 116 } 117 if (desc.getOperand() != null) { 118 return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc); 119 } 120 return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc); 121 case "setProp": 122 case "setElem": { 123 final boolean strict = NashornCallSiteDescriptor.isStrict(desc); 124 if (strict) { 125 throw typeError("cant.set.property", getArgument(linkRequest), ScriptRuntime.safeToString(self)); 126 } 127 if (desc.getOperand() != null) { 128 return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc); 129 } 130 return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc); 131 } 132 default: 133 break; 134 } 135 throw new AssertionError("unknown call type " + desc); 136 } 137 138 @Override 139 public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType, final Supplier<MethodHandles.Lookup> lookupSupplier) throws Exception { 140 final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType); 141 return gi == null ? null : gi.asType(MH.type(targetType, sourceType)); 142 } 143 144 /** 145 * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't 146 * care about adapting the method signature; that's done by the invoking method. Returns conversion from Object to String/number/boolean (JS primitive types). 147 * @param sourceType the source type 148 * @param targetType the target type 149 * @return a guarded invocation that converts from the source type to the target type. 150 * @throws Exception if something goes wrong 151 */ 152 private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception { 153 final MethodHandle mh = CONVERTERS.get(targetType); 154 if (mh != null) { 155 return new GuardedInvocation(mh); 156 } 157 158 return null; 159 } 160 161 private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) { 162 return Bootstrap.asTypeSafeReturn(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc); 163 } 164 165 // Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects 166 // linked by NashornLinker should never reach here. 167 private static boolean isExpectedObject(final Object obj) { 168 return !(NashornLinker.canLinkTypeStatic(obj.getClass())); 169 } 170 171 private static GuardedInvocation linkNull(final LinkRequest linkRequest) { 172 final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor(); 173 final String operator = desc.getFirstOperator(); 174 switch (operator) { 175 case "new": 176 case "call": 177 throw typeError("not.a.function", "null"); 178 case "callMethod": 179 case "getMethod": 180 throw typeError("no.such.function", getArgument(linkRequest), "null"); 181 case "getProp": 182 case "getElem": 183 throw typeError("cant.get.property", getArgument(linkRequest), "null"); 184 case "setProp": 185 case "setElem": 186 throw typeError("cant.set.property", getArgument(linkRequest), "null"); 187 default: 188 break; 189 } 190 throw new AssertionError("unknown call type " + desc); 191 } 192 193 private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>(); 194 static { 195 CONVERTERS.put(boolean.class, JSType.TO_BOOLEAN.methodHandle()); 196 CONVERTERS.put(double.class, JSType.TO_NUMBER.methodHandle()); 197 CONVERTERS.put(int.class, JSType.TO_INTEGER.methodHandle()); 198 CONVERTERS.put(long.class, JSType.TO_LONG.methodHandle()); 199 CONVERTERS.put(String.class, JSType.TO_STRING.methodHandle()); 200 } 201 202 private static String getArgument(final LinkRequest linkRequest) { 203 final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor(); 204 if (desc.getNameTokenCount() > 2) { 205 return desc.getNameToken(2); 206 } 207 return ScriptRuntime.safeToString(linkRequest.getArguments()[1]); 208 } 209} 210