/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package jdk.nashorn.internal.runtime.linker; import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import jdk.internal.dynalink.CallSiteDescriptor; import jdk.internal.dynalink.DynamicLinker; import jdk.internal.dynalink.DynamicLinkerFactory; import jdk.internal.dynalink.beans.BeansLinker; import jdk.internal.dynalink.beans.StaticClass; import jdk.internal.dynalink.linker.GuardedInvocation; import jdk.internal.dynalink.linker.LinkerServices; import jdk.nashorn.api.scripting.JSObject; import jdk.nashorn.internal.codegen.CompilerConstants.Call; import jdk.nashorn.internal.codegen.RuntimeCallSite; import jdk.nashorn.internal.runtime.JSType; import jdk.nashorn.internal.runtime.ScriptFunction; import jdk.nashorn.internal.runtime.ScriptRuntime; import jdk.nashorn.internal.runtime.options.Options; /** * This class houses bootstrap method for invokedynamic instructions generated by compiler. */ public final class Bootstrap { /** Reference to the seed boostrap function */ public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class); // do not create me!! private Bootstrap() { } private static final DynamicLinker dynamicLinker; static { final DynamicLinkerFactory factory = new DynamicLinkerFactory(); final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker(); final JSObjectLinker jsObjectLinker = new JSObjectLinker(nashornBeansLinker); factory.setPrioritizedLinkers(new NashornLinker(), new NashornPrimitiveLinker(), new NashornStaticClassLinker(), new BoundDynamicMethodLinker(), new JavaSuperAdapterLinker(), jsObjectLinker, new ReflectionCheckLinker()); factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker()); factory.setSyncOnRelink(true); final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1); if (relinkThreshold > -1) { factory.setUnstableRelinkThreshold(relinkThreshold); } // Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory. factory.setClassLoader(Bootstrap.class.getClassLoader()); dynamicLinker = factory.createLinker(); } /** * Returns if the given object is a "callable" * @param obj object to be checked for callability * @return true if the obj is callable */ public static boolean isCallable(final Object obj) { if (obj == ScriptRuntime.UNDEFINED || obj == null) { return false; } return obj instanceof ScriptFunction || ((obj instanceof JSObject) && ((JSObject)obj).isFunction()) || isDynamicMethod(obj) || isFunctionalInterfaceObject(obj) || obj instanceof StaticClass; } /** * Returns if the given object is a dynalink Dynamic method * @param obj object to be checked * @return true if the obj is a dynamic method */ public static boolean isDynamicMethod(final Object obj) { return obj instanceof BoundDynamicMethod || BeansLinker.isDynamicMethod(obj); } /** * Returns if the given object is an instance of an interface annotated with * java.lang.FunctionalInterface * @param obj object to be checked * @return true if the obj is an instance of @FunctionalInterface interface */ public static boolean isFunctionalInterfaceObject(final Object obj) { return !JSType.isPrimitive(obj) && (NashornBottomLinker.getFunctionalInterfaceMethod(obj.getClass()) != null); } /** * Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap * method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all * invokedynamic instructions. * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup. * @param opDesc Dynalink dynamic operation descriptor. * @param type Method type. * @param flags flags for call type, trace/profile etc. * @return CallSite with MethodHandle to appropriate method or null if not found. */ public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) { return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags)); } /** * Bootstrapper for a specialized Runtime call * * @param lookup lookup * @param initialName initial name for callsite * @param type method type for call site * * @return callsite for a runtime node */ public static CallSite runtimeBootstrap(final MethodHandles.Lookup lookup, final String initialName, final MethodType type) { return new RuntimeCallSite(type, initialName); } /** * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of * available dynamic operations can be found in the * Dynalink User Guide, but we'll show few * examples here: * * Few additional remarks: * * @param opDesc Dynalink dynamic operation descriptor. * @param rtype the return type for the operation * @param ptypes the parameter types for the operation * @return MethodHandle for invoking the operation. */ public static MethodHandle createDynamicInvoker(final String opDesc, final Class rtype, final Class... ptypes) { return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes)); } /** * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to * {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a * method type in the signature. See the discussion of that method for details. * @param opDesc Dynalink dynamic operation descriptor. * @param type the method type for the operation * @return MethodHandle for invoking the operation. */ public static MethodHandle createDynamicInvoker(final String opDesc, final MethodType type) { return bootstrap(MethodHandles.publicLookup(), opDesc, type, 0).dynamicInvoker(); } /** * Binds a bean dynamic method (returned by invoking {@code dyn:getMethod} on an object linked with * {@code BeansLinker} to a receiver. * @param dynamicMethod the dynamic method to bind * @param boundThis the bound "this" value. * @return a bound dynamic method. */ public static Object bindDynamicMethod(Object dynamicMethod, Object boundThis) { return new BoundDynamicMethod(dynamicMethod, boundThis); } /** * Creates a super-adapter for an adapter, that is, an adapter to the adapter that allows invocation of superclass * methods on it. * @param adapter the original adapter * @return a new adapter that can be used to invoke super methods on the original adapter. */ public static Object createSuperAdapter(final Object adapter) { return new JavaSuperAdapter(adapter); } /** * If the given class is a reflection-specific class (anything in {@code java.lang.reflect} and * {@code java.lang.invoke} package, as well a {@link Class} and any subclass of {@link ClassLoader}) and there is * a security manager in the system, then it checks the {@code nashorn.JavaReflection} {@code RuntimePermission}. * @param clazz the class being tested * @param isStatic is access checked for static members (or instance members) */ public static void checkReflectionAccess(Class clazz, boolean isStatic) { ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic); } /** * Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a * linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker * invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should * only resort to retrieving a linker services object using this method when you need some linker services (e.g. * type converter method handles) outside of a code path that is linking a call site. * @return Nashorn's internal dynamic linker's services object. */ public static LinkerServices getLinkerServices() { return dynamicLinker.getLinkerServices(); } /** * Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using * all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step * before returning guarded invocations to the callers. Most of the code used to produce the guarded invocations * does not make an effort to coordinate types of the methods, and so a final type adjustment before a guarded * invocation is returned is the responsibility of the linkers themselves. * @param inv the guarded invocation that needs to be type-converted. Can be null. * @param linkerServices the linker services object providing the type conversions. * @param desc the call site descriptor to whose method type the invocation needs to conform. * @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation * already conforms to the requested type, it is returned unchanged. */ static GuardedInvocation asType(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { return inv == null ? null : inv.asType(linkerServices, desc.getMethodType()); } }