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