Java Code Examples for jdk.internal.dynalink.linker.GuardedInvocation#replaceMethods()

The following examples show how to use jdk.internal.dynalink.linker.GuardedInvocation#replaceMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WithObject.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
    // If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
    // expression.
    if(!"getMethod".equals(desc.getFirstOperator())) {
        return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
    }

    final MethodHandle linkInvocation = link.getInvocation();
    final MethodType linkType = linkInvocation.type();
    final boolean linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());
    return link.replaceMethods(
            // Make sure getMethod will bind the script functions it receives to WithObject.expression
            MH.foldArguments(linkReturnsFunction ? BIND_TO_EXPRESSION_FN : BIND_TO_EXPRESSION_OBJ,
                    filter(linkInvocation.asType(linkType.changeReturnType(
                            linkReturnsFunction ? ScriptFunction.class : Object.class)), WITHEXPRESSIONFILTER)),
            // No clever things for the guard -- it is still identically filtered.
            filterGuard(link, WITHEXPRESSIONFILTER));
}
 
Example 2
Source File: BrowserJSObjectLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = requestWithoutContext.getReceiver();
    final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
    checkJSObjectClass();

    if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
        // We only support standard "dyn:*[:*]" operations
        return null;
    }

    GuardedInvocation inv;
    if (jsObjectClass.isInstance(self)) {
        inv = lookup(desc, request, linkerServices);
        inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard());
    } else {
        throw new AssertionError(); // Should never reach here.
    }

    return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
}
 
Example 3
Source File: BrowserJSObjectLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = requestWithoutContext.getReceiver();
    final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();
    checkJSObjectClass();

    if (desc.getNameTokenCount() < 2 || !"dyn".equals(desc.getNameToken(CallSiteDescriptor.SCHEME))) {
        // We only support standard "dyn:*[:*]" operations
        return null;
    }

    GuardedInvocation inv;
    if (jsObjectClass.isInstance(self)) {
        inv = lookup(desc, request, linkerServices);
        inv = inv.replaceMethods(linkerServices.filterInternalObjects(inv.getInvocation()), inv.getGuard());
    } else {
        throw new AssertionError(); // Should never reach here.
    }

    return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
}
 
Example 4
Source File: OptimisticReturnFilters.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given a guarded invocation and a callsite descriptor, perform return value filtering
 * according to the optimistic type coercion rules, using the return value from the descriptor
 * @param inv the invocation
 * @param desc the descriptor
 * @return filtered invocation
 */
public static GuardedInvocation filterOptimisticReturnValue(final GuardedInvocation inv, final CallSiteDescriptor desc) {
    if(!NashornCallSiteDescriptor.isOptimistic(desc)) {
        return inv;
    }
    return inv.replaceMethods(filterOptimisticReturnValue(inv.getInvocation(), desc.getMethodType().returnType(),
            NashornCallSiteDescriptor.getProgramPoint(desc)), inv.getGuard());
}
 
Example 5
Source File: NashornBeansLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object self = linkRequest.getReceiver();
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if (self instanceof ConsString) {
        // In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
        final Object[] arguments = linkRequest.getArguments();
        arguments[0] = "";
        final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
        final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
        // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
        return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
    }

    if (self != null && "call".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // Support dyn:call on any object that supports some @FunctionalInterface
        // annotated interface. This way Java method, constructor references or
        // implementations of java.util.function.* interfaces can be called as though
        // those are script functions.
        final String name = getFunctionalInterfaceMethodName(self.getClass());
        if (name != null) {
            final MethodType callType = desc.getMethodType();
            // drop callee (Undefined ScriptFunction) and change the request to be dyn:callMethod:<name>
            final NashornCallSiteDescriptor newDesc = NashornCallSiteDescriptor.get(desc.getLookup(),
                    "dyn:callMethod:" + name, desc.getMethodType().dropParameterTypes(1, 2),
                    NashornCallSiteDescriptor.getFlags(desc));
            final GuardedInvocation gi = getGuardedInvocation(beansLinker,
                    linkRequest.replaceArguments(newDesc, linkRequest.getArguments()),
                    new NashornBeansLinkerServices(linkerServices));

            // drop 'thiz' passed from the script.
            return gi.replaceMethods(
                MH.dropArguments(linkerServices.filterInternalObjects(gi.getInvocation()), 1, callType.parameterType(1)),
                gi.getGuard());
        }
    }
    return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
}
 
Example 6
Source File: NashornStaticClassLinker.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();
    Bootstrap.checkReflectionAccess(receiverClass);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass.getName());

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null);
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example 7
Source File: NashornStaticClassLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();
    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example 8
Source File: ScriptFunction.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocation createVarArgApplyOrCallCall(final boolean isApply, final CallSiteDescriptor desc,
        final LinkRequest request, final Object[] args) {
    final MethodType descType = desc.getMethodType();
    final int paramCount = descType.parameterCount();
    final Object[] varArgs = (Object[]) args[paramCount - 1];
    // -1 'cause we're not passing the vararg array itself
    final int copiedArgCount = args.length - 1;
    final int varArgCount = varArgs.length;

    // Spread arguments for the delegate createApplyOrCallCall invocation.
    final Object[] spreadArgs = new Object[copiedArgCount + varArgCount];
    System.arraycopy(args, 0, spreadArgs, 0, copiedArgCount);
    System.arraycopy(varArgs, 0, spreadArgs, copiedArgCount, varArgCount);

    // Spread call site descriptor for the delegate createApplyOrCallCall invocation. We drop vararg array and
    // replace it with a list of Object.class.
    final MethodType spreadType = descType.dropParameterTypes(paramCount - 1, paramCount).appendParameterTypes(
            Collections.<Class<?>>nCopies(varArgCount, Object.class));
    final CallSiteDescriptor spreadDesc = desc.changeMethodType(spreadType);

    // Delegate back to createApplyOrCallCall with the spread (that is, reverted to non-vararg) request/
    final LinkRequest spreadRequest = request.replaceArguments(spreadDesc, spreadArgs);
    final GuardedInvocation spreadInvocation = createApplyOrCallCall(isApply, spreadDesc, spreadRequest, spreadArgs);

    // Add spreader combinators to returned invocation and guard.
    return spreadInvocation.replaceMethods(
            // Use standard ScriptObject.pairArguments on the invocation
            pairArguments(spreadInvocation.getInvocation(), descType),
            // Use our specialized spreadGuardArguments on the guard (see below).
            spreadGuardArguments(spreadInvocation.getGuard(), descType));
}
 
Example 9
Source File: WithObject.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
    // If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
    // expression.
    if (!"getMethod".equals(desc.getFirstOperator())) {
        return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
    }

    final MethodHandle linkInvocation      = link.getInvocation();
    final MethodType   linkType            = linkInvocation.type();
    final boolean      linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());

    return link.replaceMethods(
            // Make sure getMethod will bind the script functions it receives to WithObject.expression
            MH.foldArguments(
                    linkReturnsFunction ?
                            BIND_TO_EXPRESSION_FN :
                            BIND_TO_EXPRESSION_OBJ,
                    filterReceiver(
                            linkInvocation.asType(
                                    linkType.changeReturnType(
                                            linkReturnsFunction ?
                                                    ScriptFunction.class :
                                                    Object.class).
                                                        changeParameterType(
                                                                0,
                                                                Object.class)),
                                    WITHEXPRESSIONFILTER)),
                     filterGuardReceiver(link, WITHEXPRESSIONFILTER));
 // No clever things for the guard -- it is still identically filtered.

}
 
Example 10
Source File: NashornBeansLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object self = linkRequest.getReceiver();
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if (self instanceof ConsString) {
        // In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
        final Object[] arguments = linkRequest.getArguments();
        arguments[0] = "";
        final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
        final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
        // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
        return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
    }

    if (self != null && "call".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // Support dyn:call on any object that supports some @FunctionalInterface
        // annotated interface. This way Java method, constructor references or
        // implementations of java.util.function.* interfaces can be called as though
        // those are script functions.
        final String name = getFunctionalInterfaceMethodName(self.getClass());
        if (name != null) {
            final MethodType callType = desc.getMethodType();
            // drop callee (Undefined ScriptFunction) and change the request to be dyn:callMethod:<name>
            final NashornCallSiteDescriptor newDesc = NashornCallSiteDescriptor.get(desc.getLookup(),
                    "dyn:callMethod:" + name, desc.getMethodType().dropParameterTypes(1, 2),
                    NashornCallSiteDescriptor.getFlags(desc));
            final GuardedInvocation gi = getGuardedInvocation(beansLinker,
                    linkRequest.replaceArguments(newDesc, linkRequest.getArguments()),
                    new NashornBeansLinkerServices(linkerServices));

            // drop 'thiz' passed from the script.
            return gi.replaceMethods(
                MH.dropArguments(linkerServices.filterInternalObjects(gi.getInvocation()), 1, callType.parameterType(1)),
                gi.getGuard());
        }
    }
    return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
}
 
Example 11
Source File: WithObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static GuardedInvocation fixExpressionCallSite(final NashornCallSiteDescriptor desc, final GuardedInvocation link) {
    // If it's not a getMethod, just add an expression filter that converts WithObject in "this" position to its
    // expression.
    if (!"getMethod".equals(desc.getFirstOperator())) {
        return fixReceiverType(link, WITHEXPRESSIONFILTER).filterArguments(0, WITHEXPRESSIONFILTER);
    }

    final MethodHandle linkInvocation      = link.getInvocation();
    final MethodType   linkType            = linkInvocation.type();
    final boolean      linkReturnsFunction = ScriptFunction.class.isAssignableFrom(linkType.returnType());

    return link.replaceMethods(
            // Make sure getMethod will bind the script functions it receives to WithObject.expression
            MH.foldArguments(
                    linkReturnsFunction ?
                            BIND_TO_EXPRESSION_FN :
                            BIND_TO_EXPRESSION_OBJ,
                    filterReceiver(
                            linkInvocation.asType(
                                    linkType.changeReturnType(
                                            linkReturnsFunction ?
                                                    ScriptFunction.class :
                                                    Object.class).
                                                        changeParameterType(
                                                                0,
                                                                Object.class)),
                                    WITHEXPRESSIONFILTER)),
                     filterGuardReceiver(link, WITHEXPRESSIONFILTER));
 // No clever things for the guard -- it is still identically filtered.

}
 
Example 12
Source File: ScriptFunction.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocation createVarArgApplyOrCallCall(final boolean isApply, final CallSiteDescriptor desc,
        final LinkRequest request, final Object[] args) {
    final MethodType descType = desc.getMethodType();
    final int paramCount = descType.parameterCount();
    final Object[] varArgs = (Object[]) args[paramCount - 1];
    // -1 'cause we're not passing the vararg array itself
    final int copiedArgCount = args.length - 1;
    final int varArgCount = varArgs.length;

    // Spread arguments for the delegate createApplyOrCallCall invocation.
    final Object[] spreadArgs = new Object[copiedArgCount + varArgCount];
    System.arraycopy(args, 0, spreadArgs, 0, copiedArgCount);
    System.arraycopy(varArgs, 0, spreadArgs, copiedArgCount, varArgCount);

    // Spread call site descriptor for the delegate createApplyOrCallCall invocation. We drop vararg array and
    // replace it with a list of Object.class.
    final MethodType spreadType = descType.dropParameterTypes(paramCount - 1, paramCount).appendParameterTypes(
            Collections.<Class<?>>nCopies(varArgCount, Object.class));
    final CallSiteDescriptor spreadDesc = desc.changeMethodType(spreadType);

    // Delegate back to createApplyOrCallCall with the spread (that is, reverted to non-vararg) request/
    final LinkRequest spreadRequest = request.replaceArguments(spreadDesc, spreadArgs);
    final GuardedInvocation spreadInvocation = createApplyOrCallCall(isApply, spreadDesc, spreadRequest, spreadArgs);

    // Add spreader combinators to returned invocation and guard.
    return spreadInvocation.replaceMethods(
            // Use standard ScriptObject.pairArguments on the invocation
            pairArguments(spreadInvocation.getInvocation(), descType),
            // Use our specialized spreadGuardArguments on the guard (see below).
            spreadGuardArguments(spreadInvocation.getGuard(), descType));
}
 
Example 13
Source File: NashornStaticClassLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example 14
Source File: WithObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocation fixScopeCallSite(final GuardedInvocation link, final String name, final ScriptObject owner) {
    final GuardedInvocation newLink             = fixReceiverType(link, WITHSCOPEFILTER);
    final MethodHandle      expressionGuard     = expressionGuard(name, owner);
    final MethodHandle      filterGuardReceiver = filterGuardReceiver(newLink, WITHSCOPEFILTER);
    return link.replaceMethods(
            filterReceiver(
                    newLink.getInvocation(),
                    WITHSCOPEFILTER),
            NashornGuards.combineGuards(
                    expressionGuard,
                    filterGuardReceiver));
}
 
Example 15
Source File: ScriptObject.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an implementation for a "dyn:callMethod" operation. Note that Nashorn internally never uses
 * "dyn:callMethod", but instead always emits two call sites in bytecode, one for "dyn:getMethod", and then another
 * one for "dyn:call". Explicit support for "dyn:callMethod" is provided for the benefit of potential external
 * callers. The implementation itself actually folds a "dyn:getMethod" method handle into a "dyn:call" method handle.
 *
 * @param desc    the call site descriptor.
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    // R(P0, P1, ...)
    final MethodType callType = desc.getMethodType();
    // use type Object(P0) for the getter
    final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0)));
    final GuardedInvocation getter = findGetMethod(getterType, request, "getMethod");

    // Object(P0) => Object(P0, P1, ...)
    final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount()));
    // R(Object, P0, P1, ...)
    final MethodHandle invoker = Bootstrap.createDynamicInvoker("dyn:call", callType.insertParameterTypes(0, argDroppingGetter.type().returnType()));
    // Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...)
    return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard());
}
 
Example 16
Source File: ScriptObject.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find an implementation for a "dyn:callMethod" operation. Note that Nashorn internally never uses
 * "dyn:callMethod", but instead always emits two call sites in bytecode, one for "dyn:getMethod", and then another
 * one for "dyn:call". Explicit support for "dyn:callMethod" is provided for the benefit of potential external
 * callers. The implementation itself actually folds a "dyn:getMethod" method handle into a "dyn:call" method handle.
 *
 * @param desc    the call site descriptor.
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    // R(P0, P1, ...)
    final MethodType callType = desc.getMethodType();
    // use type Object(P0) for the getter
    final CallSiteDescriptor getterType = desc.changeMethodType(MethodType.methodType(Object.class, callType.parameterType(0)));
    final GuardedInvocation getter = findGetMethod(getterType, request, "getMethod");

    // Object(P0) => Object(P0, P1, ...)
    final MethodHandle argDroppingGetter = MH.dropArguments(getter.getInvocation(), 1, callType.parameterList().subList(1, callType.parameterCount()));
    // R(Object, P0, P1, ...)
    final MethodHandle invoker = Bootstrap.createDynamicInvoker("dyn:call", callType.insertParameterTypes(0, argDroppingGetter.type().returnType()));
    // Fold Object(P0, P1, ...) into R(Object, P0, P1, ...) => R(P0, P1, ...)
    return getter.replaceMethods(MH.foldArguments(invoker, argDroppingGetter), getter.getGuard());
}
 
Example 17
Source File: BrowserJSObjectLinker.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) {
    final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation());
    return inv.replaceMethods(getter, inv.getGuard());
}
 
Example 18
Source File: BoundCallableLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object objBoundCallable = linkRequest.getReceiver();
    if(!(objBoundCallable instanceof BoundCallable)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if (descriptor.getNameTokenCount() < 2 || !"dyn".equals(descriptor.getNameToken(CallSiteDescriptor.SCHEME))) {
        return null;
    }
    final String operation = descriptor.getNameToken(CallSiteDescriptor.OPERATOR);
    // We need to distinguish "dyn:new" from "dyn:call" because "dyn:call" sites have parameter list of the form
    // "callee, this, args", while "dyn:call" sites have "callee, args" -- they lack the "this" parameter.
    final boolean isCall;
    if ("new".equals(operation)) {
        isCall = false;
    } else if ("call".equals(operation)) {
        isCall = true;
    } else {
        // Only dyn:call and dyn:new are supported.
        return null;
    }
    final BoundCallable boundCallable = (BoundCallable)objBoundCallable;
    final Object callable = boundCallable.getCallable();
    final Object boundThis = boundCallable.getBoundThis();

    // We need to ask the linker services for a delegate invocation on the target callable.

    // Replace arguments (boundCallable[, this], args) => (callable[, boundThis], boundArgs, args) when delegating
    final Object[] args = linkRequest.getArguments();
    final Object[] boundArgs = boundCallable.getBoundArgs();
    final int argsLen = args.length;
    final int boundArgsLen = boundArgs.length;
    final Object[] newArgs = new Object[argsLen + boundArgsLen];
    newArgs[0] = callable;
    final int firstArgIndex;
    if (isCall) {
        newArgs[1] = boundThis;
        firstArgIndex = 2;
    } else {
        firstArgIndex = 1;
    }
    System.arraycopy(boundArgs, 0, newArgs, firstArgIndex, boundArgsLen);
    System.arraycopy(args, firstArgIndex, newArgs, firstArgIndex + boundArgsLen, argsLen - firstArgIndex);

    // Use R(T0, T1, T2, ...) => R(callable.class, boundThis.class, boundArg0.class, ..., boundArgn.class, T2, ...)
    // call site type when delegating to underlying linker (for dyn:new, there's no this).
    final MethodType type = descriptor.getMethodType();
    // Use R(T0, ...) => R(callable.class, ...)
    MethodType newMethodType = descriptor.getMethodType().changeParameterType(0, callable.getClass());
    if (isCall) {
        // R(callable.class, T1, ...) => R(callable.class, boundThis.class, ...)
        newMethodType = newMethodType.changeParameterType(1, boundThis == null? Object.class : boundThis.getClass());
    }
    // R(callable.class[, boundThis.class], T2, ...) => R(callable.class[, boundThis.class], boundArg0.class, ..., boundArgn.class, T2, ...)
    for(int i = boundArgs.length; i-- > 0;) {
        newMethodType = newMethodType.insertParameterTypes(firstArgIndex, boundArgs[i] == null ? Object.class : boundArgs[i].getClass());
    }
    final CallSiteDescriptor newDescriptor = descriptor.changeMethodType(newMethodType);

    // Delegate to target's linker
    final GuardedInvocation inv = linkerServices.getGuardedInvocation(linkRequest.replaceArguments(newDescriptor, newArgs));
    if(inv == null) {
        return null;
    }

    // Bind (callable[, boundThis], boundArgs) to the delegate handle
    final MethodHandle boundHandle = MethodHandles.insertArguments(inv.getInvocation(), 0,
            Arrays.copyOf(newArgs, firstArgIndex + boundArgs.length));
    final Class<?> p0Type = type.parameterType(0);
    final MethodHandle droppingHandle;
    if (isCall) {
        // Ignore incoming boundCallable and this
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type, type.parameterType(1));
    } else {
        // Ignore incoming boundCallable
        droppingHandle = MethodHandles.dropArguments(boundHandle, 0, p0Type);
    }
    // Identity guard on boundCallable object
    final MethodHandle newGuard = Guards.getIdentityGuard(boundCallable);
    return inv.replaceMethods(droppingHandle, newGuard.asType(newGuard.type().changeParameterType(0, p0Type)));
}
 
Example 19
Source File: BrowserJSObjectLinker.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) {
    final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation());
    return inv.replaceMethods(getter, inv.getGuard());
}
 
Example 20
Source File: BrowserJSObjectLinker.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation findGetIndexMethod(final GuardedInvocation inv) {
    final MethodHandle getter = MH.insertArguments(JSOBJECTLINKER_GET, 0, inv.getInvocation());
    return inv.replaceMethods(getter, inv.getGuard());
}