diff options
Diffstat (limited to 'src/jdk/nashorn/internal/runtime/linker/Bootstrap.java')
-rw-r--r-- | src/jdk/nashorn/internal/runtime/linker/Bootstrap.java | 211 |
1 files changed, 192 insertions, 19 deletions
diff --git a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java index 48821036..749c4728 100644 --- a/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java +++ b/src/jdk/nashorn/internal/runtime/linker/Bootstrap.java @@ -26,8 +26,10 @@ package jdk.nashorn.internal.runtime.linker; import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; +import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; import java.lang.invoke.CallSite; +import java.lang.invoke.ConstantCallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles.Lookup; @@ -35,14 +37,24 @@ import java.lang.invoke.MethodType; import jdk.internal.dynalink.CallSiteDescriptor; import jdk.internal.dynalink.DynamicLinker; import jdk.internal.dynalink.DynamicLinkerFactory; +import jdk.internal.dynalink.GuardedInvocationFilter; import jdk.internal.dynalink.beans.BeansLinker; import jdk.internal.dynalink.beans.StaticClass; import jdk.internal.dynalink.linker.GuardedInvocation; +import jdk.internal.dynalink.linker.LinkRequest; import jdk.internal.dynalink.linker.LinkerServices; +import jdk.internal.dynalink.linker.MethodTypeConversionStrategy; +import jdk.internal.dynalink.support.TypeUtilities; import jdk.nashorn.api.scripting.JSObject; import jdk.nashorn.internal.codegen.CompilerConstants.Call; +import jdk.nashorn.internal.codegen.ObjectClassGenerator; import jdk.nashorn.internal.codegen.RuntimeCallSite; +import jdk.nashorn.internal.lookup.MethodHandleFactory; +import jdk.nashorn.internal.lookup.MethodHandleFunctionality; +import jdk.nashorn.internal.objects.ScriptFunctionImpl; +import jdk.nashorn.internal.runtime.ECMAException; import jdk.nashorn.internal.runtime.JSType; +import jdk.nashorn.internal.runtime.OptimisticReturnFilters; import jdk.nashorn.internal.runtime.ScriptFunction; import jdk.nashorn.internal.runtime.ScriptRuntime; import jdk.nashorn.internal.runtime.options.Options; @@ -54,6 +66,25 @@ 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); + private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); + + /** + * The default dynalink relink threshold for megamorphisism is 8. In the case + * of object fields only, it is fine. However, with dual fields, in order to get + * performance on benchmarks with a lot of object instantiation and then field + * reassignment, it can take slightly more relinks to become stable with type + * changes swapping out an entire proprety map and making a map guard fail. + * Therefore the relink threshold is set to 16 for dual fields (now the default). + * This doesn't seem to have any other negative performance implication. + * + * See for example octane.gbemu, run with --log=fields:warning to study + * megamorphic behavior + */ + private static final int NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD = + ObjectClassGenerator.OBJECT_FIELDS_ONLY ? + 8 : + 16; + // do not create me!! private Bootstrap() { } @@ -62,18 +93,31 @@ public final class Bootstrap { 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 BoundCallableLinker(), new JavaSuperAdapterLinker(), - jsObjectLinker, + new JSObjectLinker(nashornBeansLinker), + new BrowserJSObjectLinker(nashornBeansLinker), new ReflectionCheckLinker()); factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker()); factory.setSyncOnRelink(true); - final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", -1); + factory.setPrelinkFilter(new GuardedInvocationFilter() { + @Override + public GuardedInvocation filter(final GuardedInvocation inv, final LinkRequest request, final LinkerServices linkerServices) { + final CallSiteDescriptor desc = request.getCallSiteDescriptor(); + return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType()); + } + }); + factory.setAutoConversionStrategy(new MethodTypeConversionStrategy() { + @Override + public MethodHandle asType(final MethodHandle target, final MethodType newType) { + return unboxReturnType(target, newType); + } + }); + final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD); if (relinkThreshold > -1) { factory.setUnstableRelinkThreshold(relinkThreshold); } @@ -95,19 +139,47 @@ public final class Bootstrap { } return obj instanceof ScriptFunction || - ((obj instanceof JSObject) && ((JSObject)obj).isFunction()) || - isDynamicMethod(obj) || + isJSObjectFunction(obj) || + BeansLinker.isDynamicMethod(obj) || + obj instanceof BoundCallable || isFunctionalInterfaceObject(obj) || obj instanceof StaticClass; } /** + * Returns true if the given object is a strict callable + * @param callable the callable object to be checked for strictness + * @return true if the obj is a strict callable, false if it is a non-strict callable. + * @throws ECMAException with {@code TypeError} if the object is not a callable. + */ + public static boolean isStrictCallable(final Object callable) { + if (callable instanceof ScriptFunction) { + return ((ScriptFunction)callable).isStrict(); + } else if (isJSObjectFunction(callable)) { + return ((JSObject)callable).isStrictFunction(); + } else if (callable instanceof BoundCallable) { + return isStrictCallable(((BoundCallable)callable).getCallable()); + } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) { + return false; + } + throw notFunction(callable); + } + + private static ECMAException notFunction(final Object obj) { + return typeError("not.a.function", ScriptRuntime.safeToString(obj)); + } + + private static boolean isJSObjectFunction(final Object obj) { + return obj instanceof JSObject && ((JSObject)obj).isFunction(); + } + + /** * 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); + return BeansLinker.isDynamicMethod(obj instanceof BoundCallable ? ((BoundCallable)obj).getCallable() : obj); } /** @@ -148,6 +220,60 @@ public final class Bootstrap { } /** + * Boostrapper for math calls that may overflow + * @param lookup lookup + * @param name name of operation + * @param type method type + * @param programPoint program point to bind to callsite + * + * @return callsite for a math instrinic node + */ + public static CallSite mathBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int programPoint) { + final MethodHandle mh; + switch (name) { + case "iadd": + mh = JSType.ADD_EXACT.methodHandle(); + break; + case "isub": + mh = JSType.SUB_EXACT.methodHandle(); + break; + case "imul": + mh = JSType.MUL_EXACT.methodHandle(); + break; + case "idiv": + mh = JSType.DIV_EXACT.methodHandle(); + break; + case "irem": + mh = JSType.REM_EXACT.methodHandle(); + break; + case "ineg": + mh = JSType.NEGATE_EXACT.methodHandle(); + break; + case "ladd": + mh = JSType.ADD_EXACT_LONG.methodHandle(); + break; + case "lsub": + mh = JSType.SUB_EXACT_LONG.methodHandle(); + break; + case "lmul": + mh = JSType.MUL_EXACT_LONG.methodHandle(); + break; + case "ldiv": + mh = JSType.DIV_EXACT_LONG.methodHandle(); + break; + case "lrem": + mh = JSType.REM_EXACT_LONG.methodHandle(); + break; + case "lneg": + mh = JSType.NEGATE_EXACT_LONG.methodHandle(); + break; + default: + throw new AssertionError("unsupported math intrinsic"); + } + return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint)); + } + + /** * 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 @@ -250,6 +376,20 @@ public final class Bootstrap { /** * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to + * {@link #createDynamicInvoker(String, Class, Class...)} but with an additional parameter to + * set the call site flags of the dynamic invoker. + * @param opDesc Dynalink dynamic operation descriptor. + * @param flags the call site flags for the operation + * @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 int flags, final Class<?> rtype, final Class<?>... ptypes) { + return bootstrap(MethodHandles.publicLookup(), opDesc, MethodType.methodType(rtype, ptypes), flags).dynamicInvoker(); + } + + /** + * 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. @@ -261,14 +401,22 @@ public final class Bootstrap { } /** - * 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 + * Binds any object Nashorn can use as a [[Callable]] to a receiver and optionally arguments. + * @param callable the callable to bind * @param boundThis the bound "this" value. - * @return a bound dynamic method. + * @param boundArgs the bound arguments. Can be either null or empty array to signify no arguments are bound. + * @return a bound callable. + * @throws ECMAException with {@code TypeError} if the object is not a callable. */ - public static Object bindDynamicMethod(Object dynamicMethod, Object boundThis) { - return new BoundDynamicMethod(dynamicMethod, boundThis); + public static Object bindCallable(final Object callable, final Object boundThis, final Object[] boundArgs) { + if (callable instanceof ScriptFunctionImpl) { + return ((ScriptFunctionImpl)callable).makeBoundFunction(boundThis, boundArgs); + } else if (callable instanceof BoundCallable) { + return ((BoundCallable)callable).bind(boundArgs); + } else if (isCallable(callable)) { + return new BoundCallable(callable, boundThis, boundArgs); + } + throw notFunction(callable); } /** @@ -288,7 +436,7 @@ public final class Bootstrap { * @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) { + public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) { ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic); } @@ -307,16 +455,41 @@ public final class Bootstrap { /** * 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. + * before returning guarded invocations. 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 + * to the aggregating linker 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()); + static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { + return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType()); + } + + /** + * Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing + * conversion. This will ensure that nulls are unwrapped to false or 0. + * @param target the target method handle + * @param newType the desired new type. Note that this method does not adapt the method handle completely to the + * new type, it only adapts the return type; this is allowed as per + * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method + * is used for. + * @return the method handle with adapted return type, if it required an unboxing conversion. + */ + private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) { + final MethodType targetType = target.type(); + final Class<?> oldReturnType = targetType.returnType(); + if (TypeUtilities.isWrapperType(oldReturnType)) { + final Class<?> newReturnType = newType.returnType(); + if (newReturnType.isPrimitive()) { + // The contract of setAutoConversionStrategy is such that the difference between newType and targetType + // can only be JLS method invocation conversions. + assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType); + return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType)); + } + } + return target; } } |