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.ScriptRuntime.UNDEFINED;
  31 
  32 import java.lang.invoke.MethodHandle;
  33 import java.lang.invoke.MethodType;
  34 import java.lang.reflect.Method;
  35 import java.lang.reflect.Modifier;
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import jdk.internal.dynalink.CallSiteDescriptor;
  39 import jdk.internal.dynalink.beans.BeansLinker;
  40 import jdk.internal.dynalink.linker.GuardedInvocation;
  41 import jdk.internal.dynalink.linker.GuardedTypeConversion;
  42 import jdk.internal.dynalink.linker.GuardingDynamicLinker;
  43 import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
  44 import jdk.internal.dynalink.linker.LinkRequest;
  45 import jdk.internal.dynalink.linker.LinkerServices;
  46 import jdk.internal.dynalink.support.Guards;
  47 import jdk.nashorn.internal.codegen.types.Type;
  48 import jdk.nashorn.internal.runtime.Context;
  49 import jdk.nashorn.internal.runtime.JSType;
  50 import jdk.nashorn.internal.runtime.ScriptRuntime;
  51 import jdk.nashorn.internal.runtime.UnwarrantedOptimismException;
  52 
  53 /**
  54  * Nashorn bottom linker; used as a last-resort catch-all linker for all linking requests that fall through all other
  55  * linkers (see how {@link Bootstrap} class configures the dynamic linker in its static initializer). It will throw
  56  * appropriate ECMAScript errors for attempts to invoke operations on {@code null}, link no-op property getters and
  57  * setters for Java objects that couldn't be linked by any other linker, and throw appropriate ECMAScript errors for
  58  * attempts to invoke arbitrary Java objects as functions or constructors.
  59  */
  60 final class NashornBottomLinker implements GuardingDynamicLinker, GuardingTypeConverterFactory {
  61 
  62     @Override
  63     public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices)
  64             throws Exception {
  65         final Object self = linkRequest.getReceiver();
  66 
  67         if (self == null) {
  68             return linkNull(linkRequest);
  69         }
  70 
  71         // None of the objects that can be linked by NashornLinker should ever reach here. Basically, anything below
  72         // this point is a generic Java bean. Therefore, reaching here with a ScriptObject is a Nashorn bug.
  73         assert isExpectedObject(self) : "Couldn't link " + linkRequest.getCallSiteDescriptor() + " for " + self.getClass().getName();
  74 
  75         return linkBean(linkRequest, linkerServices);
  76     }
  77 
  78     private static final MethodHandle EMPTY_PROP_GETTER =
  79             MH.dropArguments(MH.constant(Object.class, UNDEFINED), 0, Object.class);
  80     private static final MethodHandle EMPTY_ELEM_GETTER =
  81             MH.dropArguments(EMPTY_PROP_GETTER, 0, Object.class);
  82     private static final MethodHandle EMPTY_PROP_SETTER =
  83             MH.asType(EMPTY_ELEM_GETTER, EMPTY_ELEM_GETTER.type().changeReturnType(void.class));
  84     private static final MethodHandle EMPTY_ELEM_SETTER =
  85             MH.dropArguments(EMPTY_PROP_SETTER, 0, Object.class);
  86 
  87     private static GuardedInvocation linkBean(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
  88         final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
  89         final Object self = linkRequest.getReceiver();
  90         final String operator = desc.getFirstOperator();
  91         switch (operator) {
  92         case "new":
  93             if(BeansLinker.isDynamicMethod(self)) {
  94                 throw typeError("method.not.constructor", ScriptRuntime.safeToString(self));
  95             }
  96             throw typeError("not.a.function", ScriptRuntime.safeToString(self));
  97         case "call":
  98             // Support dyn:call on any object that supports some @FunctionalInterface
  99             // annotated interface. This way Java method, constructor references or
 100             // implementations of java.util.function.* interfaces can be called as though
 101             // those are script functions.
 102             final Method m = getFunctionalInterfaceMethod(self.getClass());
 103             if (m != null) {
 104                 final MethodType callType = desc.getMethodType();
 105                 // 'callee' and 'thiz' passed from script + actual arguments
 106                 if (callType.parameterCount() != m.getParameterCount() + 2) {
 107                     throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
 108                 }
 109                 return Bootstrap.asTypeSafeReturn(new GuardedInvocation(
 110                         // drop 'thiz' passed from the script.
 111                         MH.dropArguments(desc.getLookup().unreflect(m), 1, callType.parameterType(1)),
 112                         Guards.getInstanceOfGuard(m.getDeclaringClass())), linkerServices, desc);
 113             }
 114             if(BeansLinker.isDynamicConstructor(self)) {
 115                 throw typeError("constructor.requires.new", ScriptRuntime.safeToString(self));
 116             }
 117             if(BeansLinker.isDynamicMethod(self)) {
 118                 throw typeError("no.method.matches.args", ScriptRuntime.safeToString(self));
 119             }
 120             throw typeError("not.a.function", ScriptRuntime.safeToString(self));
 121         case "callMethod":
 122         case "getMethod":
 123             throw typeError("no.such.function", getArgument(linkRequest), ScriptRuntime.safeToString(self));
 124         case "getProp":
 125         case "getElem":
 126             if(NashornCallSiteDescriptor.isOptimistic(desc)) {
 127                 throw new UnwarrantedOptimismException(UNDEFINED, NashornCallSiteDescriptor.getProgramPoint(desc), Type.OBJECT);
 128             }
 129             if (desc.getOperand() != null) {
 130                 return getInvocation(EMPTY_PROP_GETTER, self, linkerServices, desc);
 131             }
 132             return getInvocation(EMPTY_ELEM_GETTER, self, linkerServices, desc);
 133         case "setProp":
 134         case "setElem":
 135             if (desc.getOperand() != null) {
 136                 return getInvocation(EMPTY_PROP_SETTER, self, linkerServices, desc);
 137             }
 138             return getInvocation(EMPTY_ELEM_SETTER, self, linkerServices, desc);
 139         default:
 140             break;
 141         }
 142         throw new AssertionError("unknown call type " + desc);
 143     }
 144 
 145     @Override
 146     public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 147         final GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
 148         return gi == null ? null : new GuardedTypeConversion(gi.asType(MH.type(targetType, sourceType)), true);
 149     }
 150 
 151     /**
 152      * Main part of the implementation of {@link GuardingTypeConverterFactory#convertToType(Class, Class)} that doesn't
 153      * care about adapting the method signature; that's done by the invoking method. Returns conversion from Object to String/number/boolean (JS primitive types).
 154      * @param sourceType the source type
 155      * @param targetType the target type
 156      * @return a guarded invocation that converts from the source type to the target type.
 157      * @throws Exception if something goes wrong
 158      */
 159     private static GuardedInvocation convertToTypeNoCast(final Class<?> sourceType, final Class<?> targetType) throws Exception {
 160         final MethodHandle mh = CONVERTERS.get(targetType);
 161         if (mh != null) {
 162             return new GuardedInvocation(mh);
 163         }
 164 
 165         return null;
 166     }
 167 
 168     private static GuardedInvocation getInvocation(final MethodHandle handle, final Object self, final LinkerServices linkerServices, final CallSiteDescriptor desc) {
 169         return Bootstrap.asTypeSafeReturn(new GuardedInvocation(handle, Guards.getClassGuard(self.getClass())), linkerServices, desc);
 170     }
 171 
 172     // Used solely in an assertion to figure out if the object we get here is something we in fact expect. Objects
 173     // linked by NashornLinker should never reach here.
 174     private static boolean isExpectedObject(final Object obj) {
 175         return !(NashornLinker.canLinkTypeStatic(obj.getClass()));
 176     }
 177 
 178     private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
 179         final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
 180         final String operator = desc.getFirstOperator();
 181         switch (operator) {
 182         case "new":
 183         case "call":
 184             throw typeError("not.a.function", "null");
 185         case "callMethod":
 186         case "getMethod":
 187             throw typeError("no.such.function", getArgument(linkRequest), "null");
 188         case "getProp":
 189         case "getElem":
 190             throw typeError("cant.get.property", getArgument(linkRequest), "null");
 191         case "setProp":
 192         case "setElem":
 193             throw typeError("cant.set.property", getArgument(linkRequest), "null");
 194         default:
 195             break;
 196         }
 197         throw new AssertionError("unknown call type " + desc);
 198     }
 199 
 200     private static final Map<Class<?>, MethodHandle> CONVERTERS = new HashMap<>();
 201     static {
 202         CONVERTERS.put(boolean.class, JSType.TO_BOOLEAN.methodHandle());
 203         CONVERTERS.put(double.class, JSType.TO_NUMBER.methodHandle());
 204         CONVERTERS.put(int.class, JSType.TO_INTEGER.methodHandle());
 205         CONVERTERS.put(long.class, JSType.TO_LONG.methodHandle());
 206         CONVERTERS.put(String.class, JSType.TO_STRING.methodHandle());
 207     }
 208 
 209     private static String getArgument(final LinkRequest linkRequest) {
 210         final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
 211         if (desc.getNameTokenCount() > 2) {
 212             return desc.getNameToken(2);
 213         }
 214         return ScriptRuntime.safeToString(linkRequest.getArguments()[1]);
 215     }
 216 
 217     // cache of @FunctionalInterface method of implementor classes
 218     private static final ClassValue<Method> FUNCTIONAL_IFACE_METHOD = new ClassValue<Method>() {
 219         @Override
 220         protected Method computeValue(final Class<?> type) {
 221             return findFunctionalInterfaceMethod(type);
 222         }
 223 
 224         private Method findFunctionalInterfaceMethod(final Class<?> clazz) {
 225             if (clazz == null) {
 226                 return null;
 227             }
 228 
 229             for (final Class<?> iface : clazz.getInterfaces()) {
 230                 // check accessiblity up-front
 231                 if (! Context.isAccessibleClass(iface)) {
 232                     continue;
 233                 }
 234 
 235                 // check for @FunctionalInterface
 236                 if (iface.isAnnotationPresent(FunctionalInterface.class)) {
 237                     // return the first abstract method
 238                     for (final Method m : iface.getMethods()) {
 239                         if (Modifier.isAbstract(m.getModifiers())) {
 240                             return m;
 241                         }
 242                     }
 243                 }
 244             }
 245 
 246             // did not find here, try super class
 247             return findFunctionalInterfaceMethod(clazz.getSuperclass());
 248         }
 249     };
 250 
 251     // Returns @FunctionalInterface annotated interface's single abstract
 252     // method. If not found, returns null.
 253     static Method getFunctionalInterfaceMethod(final Class<?> clazz) {
 254         return FUNCTIONAL_IFACE_METHOD.get(clazz);
 255     }
 256 }