Java Code Examples for jdk.internal.dynalink.CallSiteDescriptor#getNameTokenCount()

The following examples show how to use jdk.internal.dynalink.CallSiteDescriptor#getNameTokenCount() . 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: JSObjectLinker.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example 2
Source File: AbstractCallSiteDescriptor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if this call site descriptor is equal to the passed call site descriptor.
 * @param csd the other call site descriptor.
 * @return true if they are equal.
 */
public boolean equals(final CallSiteDescriptor csd) {
    if(csd == null) {
        return false;
    }
    if(csd == this) {
        return true;
    }
    final int ntc = getNameTokenCount();
    if(ntc != csd.getNameTokenCount()) {
        return false;
    }
    for(int i = ntc; i-- > 0;) { // Reverse order as variability is higher at the end
        if(!Objects.equals(getNameToken(i), csd.getNameToken(i))) {
            return false;
        }
    }
    if(!getMethodType().equals(csd.getMethodType())) {
        return false;
    }
    return lookupsEqual(getLookup(), csd.getLookup());
}
 
Example 3
Source File: BrowserJSObjectLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    GuardedInvocation inv;
    try {
        inv = nashornBeansLinker.getGuardedInvocation(request, linkerServices);
    } catch (Throwable th) {
        inv = null;
    }

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2? findGetMethod(desc, inv) : findGetIndexMethod(inv);
        case "setProp":
        case "setElem":
            return c > 2? findSetMethod(desc, inv) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        default:
            return null;
    }
}
 
Example 4
Source File: AbstractCallSiteDescriptor.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if this call site descriptor is equal to the passed call site descriptor.
 * @param csd the other call site descriptor.
 * @return true if they are equal.
 */
public boolean equals(final CallSiteDescriptor csd) {
    if(csd == null) {
        return false;
    }
    if(csd == this) {
        return true;
    }
    final int ntc = getNameTokenCount();
    if(ntc != csd.getNameTokenCount()) {
        return false;
    }
    for(int i = ntc; i-- > 0;) { // Reverse order as variability is higher at the end
        if(!Objects.equals(getNameToken(i), csd.getNameToken(i))) {
            return false;
        }
    }
    if(!getMethodType().equals(csd.getMethodType())) {
        return false;
    }
    return lookupsEqual(getLookup(), csd.getLookup());
}
 
Example 5
Source File: JSObjectLinker.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    final int c = desc.getNameTokenCount();
    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            return c > 2 ? findGetMethod(desc) : findGetIndexMethod();
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc, operator);
        case "callMethod":
            return findCallMethodMethod(desc, operator);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example 6
Source File: DynamicMethodLinker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example 7
Source File: Undefined.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup the appropriate method for an invoke dynamic call.
 * @param desc The invoke dynamic callsite descriptor.
 * @return GuardedInvocation to be invoked at call site.
 */
public static GuardedInvocation lookup(final CallSiteDescriptor desc) {
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);

    switch (operator) {
    case "new":
    case "call":
        throw lookupTypeError("cant.call.undefined", desc);
    case "callMethod":
        throw lookupTypeError("cant.read.property.of.undefined", desc);
    // NOTE: we support getElem and setItem as JavaScript doesn't distinguish items from properties. Nashorn itself
    // emits "dyn:getProp:identifier" for "<expr>.<identifier>" and "dyn:getElem" for "<expr>[<expr>]", but we are
    // more flexible here and dispatch not on operation name (getProp vs. getElem), but rather on whether the
    // operation has an associated name or not.
    case "getProp":
    case "getElem":
    case "getMethod":
        if (desc.getNameTokenCount() < 3) {
            return findGetIndexMethod(desc);
        }
        throw lookupTypeError("cant.read.property.of.undefined", desc);
    case "setProp":
    case "setElem":
        if (desc.getNameTokenCount() < 3) {
            return findSetIndexMethod(desc);
        }
        throw lookupTypeError("cant.set.property.of.undefined", desc);
    default:
        break;
    }

    return null;
}
 
Example 8
Source File: DynamicMethodLinker.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) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example 9
Source File: DynamicMethodLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) {
    final Object receiver = linkRequest.getReceiver();
    if(!(receiver instanceof DynamicMethod)) {
        return null;
    }
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if(desc.getNameTokenCount() != 2 && desc.getNameToken(CallSiteDescriptor.SCHEME) != "dyn") {
        return null;
    }
    final String operator = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    final DynamicMethod dynMethod = (DynamicMethod)receiver;
    final boolean constructor = dynMethod.isConstructor();
    final MethodHandle invocation;

    if (operator == "call" && !constructor) {
        invocation = dynMethod.getInvocation(
                CallSiteDescriptorFactory.dropParameterTypes(desc, 0, 1), linkerServices);
    } else if (operator == "new" && constructor) {
        final MethodHandle ctorInvocation = dynMethod.getInvocation(desc, linkerServices);
        if(ctorInvocation == null) {
            return null;
        }

        // Insert null for StaticClass parameter
        invocation = MethodHandles.insertArguments(ctorInvocation, 0, (Object)null);
    } else {
        return null;
    }

    if (invocation != null) {
        return new GuardedInvocation(MethodHandles.dropArguments(invocation, 0,
            desc.getMethodType().parameterType(0)), Guards.getIdentityGuard(receiver));
    }

    return null;
}
 
Example 10
Source File: NashornBottomLinker.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String getArgument(final LinkRequest linkRequest) {
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if (desc.getNameTokenCount() > 2) {
        return desc.getNameToken(2);
    }
    return ScriptRuntime.safeToString(linkRequest.getArguments()[1]);
}
 
Example 11
Source File: AbstractJavaLinker.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocation getCallPropWithThis(final CallSiteDescriptor callSiteDescriptor, final LinkerServices linkerServices) {
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 3: {
            return createGuardedDynamicMethodInvocation(callSiteDescriptor, linkerServices,
                    callSiteDescriptor.getNameToken(CallSiteDescriptor.NAME_OPERAND), methods);
        }
        default: {
            return null;
        }
    }
}
 
Example 12
Source File: WithObject.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    // With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of
    // necessity have a Nashorn descriptor - it is safe to cast.
    final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc;
    FindProperty find = null;
    GuardedInvocation link = null;
    ScriptObject self = null;

    final boolean isNamedOperation;
    final String name;
    if(desc.getNameTokenCount() > 2) {
        isNamedOperation = true;
        name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
    } else {
        isNamedOperation = false;
        name = null;
    }

    self = expression;
    if (isNamedOperation) {
         find = self.findProperty(name, true);
    }

    if (find != null) {
        link = self.lookup(desc, request);

        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    final ScriptObject scope = getProto();
    if (isNamedOperation) {
        find = scope.findProperty(name, true);
    }

    if (find != null) {
        return fixScopeCallSite(scope.lookup(desc, request), name);
    }

    // the property is not found - now check for
    // __noSuchProperty__ and __noSuchMethod__ in expression
    if (self != null) {
        final String fallBack;

        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);

        switch (operator) {
        case "callMethod":
            throw new AssertionError(); // Nashorn never emits callMethod
        case "getMethod":
            fallBack = NO_SUCH_METHOD_NAME;
            break;
        case "getProp":
        case "getElem":
            fallBack = NO_SUCH_PROPERTY_NAME;
            break;
        default:
            fallBack = null;
            break;
        }

        if (fallBack != null) {
            find = self.findProperty(fallBack, true);
            if (find != null) {
                switch (operator) {
                case "getMethod":
                    link = self.noSuchMethod(desc, request);
                    break;
                case "getProp":
                case "getElem":
                    link = self.noSuchProperty(desc, request);
                    break;
                default:
                    break;
                }
            }
        }

        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    // still not found, may be scope can handle with it's own
    // __noSuchProperty__, __noSuchMethod__ etc.
    link = scope.lookup(desc, request);

    if (link != null) {
        return fixScopeCallSite(link, name);
    }

    return null;
}
 
Example 13
Source File: AbstractJavaLinker.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private GuardedInvocationComponent getPropertyGetter(CallSiteDescriptor callSiteDescriptor,
        LinkerServices linkerServices, List<String> ops) throws Exception {
    final MethodType type = callSiteDescriptor.getMethodType();
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Must have exactly two arguments: receiver and name
            assertParameterCount(callSiteDescriptor, 2);

            // What's below is basically:
            //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
            // only with a bunch of method signature adjustments. Basically, retrieve method getter
            // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
            // or delegate to next component's invocation.

            final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
                    AnnotatedDynamicMethod.class));
            final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
                    GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup());
            final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
                    callSiteBoundMethodGetter);
            // Object(AnnotatedDynamicMethod, Object)->R(AnnotatedDynamicMethod, T0)
            final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
                    MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
            // Since it's in the target of a fold, drop the unnecessary second argument
            // R(AnnotatedDynamicMethod, T0)->R(AnnotatedDynamicMethod, T0, T1)
            final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
                    type.parameterType(1));
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, ops);

            final MethodHandle fallbackFolded;
            if(nextComponent == null) {
                // Object(AnnotatedDynamicMethod)->R(AnnotatedDynamicMethod, T0, T1); returns constant null
                fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
                        type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
            } else {
                // R(T0, T1)->R(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to drop the
                // extra argument resulting from fold
                fallbackFolded = MethodHandles.dropArguments(nextComponent.getGuardedInvocation().getInvocation(),
                        0, AnnotatedDynamicMethod.class);
            }

            // fold(R(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
            final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                        IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
            if(nextComponent == null) {
                return getClassGuardedInvocationComponent(compositeGetter, type);
            }
            return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
        }
        case 3: {
            // Must have exactly one argument: receiver
            assertParameterCount(callSiteDescriptor, 1);
            // Fixed name
            final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
                    CallSiteDescriptor.NAME_OPERAND));
            if(annGetter == null) {
                // We have no such property, always delegate to the next component operation
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
            }
            final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
            // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
            // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
            // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
            // we're linking against a field getter, don't make the assumption.
            // NOTE: No delegation to the next component operation if we have a property with this name, even if its
            // value is null.
            final ValidationType validationType = annGetter.validationType;
            // TODO: we aren't using the type that declares the most generic getter here!
            return new GuardedInvocationComponent(linkerServices.asType(getter, type), getGuard(validationType,
                    type), clazz, validationType);
        }
        default: {
            // Can't do anything with more than 3 name components
            return null;
        }
    }
}
 
Example 14
Source File: BoundCallableLinker.java    From jdk8u60 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 15
Source File: BoundCallableLinker.java    From openjdk-jdk8u 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 16
Source File: BeanLinker.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static String getFixedKey(final CallSiteDescriptor callSiteDescriptor) {
    return callSiteDescriptor.getNameTokenCount() == 2 ? null : callSiteDescriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND);
}
 
Example 17
Source File: WithObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    if (request.isCallSiteUnstable()) {
        // Fall back to megamorphic invocation which performs a complete lookup each time without further relinking.
        return super.lookup(desc, request);
    }

    // With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of
    // necessity have a Nashorn descriptor - it is safe to cast.
    final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc;
    FindProperty find = null;
    GuardedInvocation link = null;
    ScriptObject self;

    final boolean isNamedOperation;
    final String name;
    if (desc.getNameTokenCount() > 2) {
        isNamedOperation = true;
        name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
    } else {
        isNamedOperation = false;
        name = null;
    }

    self = expression;
    if (isNamedOperation) {
         find = self.findProperty(name, true);
    }

    if (find != null) {
        link = self.lookup(desc, request);
        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    final ScriptObject scope = getProto();
    if (isNamedOperation) {
        find = scope.findProperty(name, true);
    }

    if (find != null) {
        return fixScopeCallSite(scope.lookup(desc, request), name, find.getOwner());
    }

    // the property is not found - now check for
    // __noSuchProperty__ and __noSuchMethod__ in expression
    if (self != null) {
        final String fallBack;

        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);

        switch (operator) {
        case "callMethod":
            throw new AssertionError(); // Nashorn never emits callMethod
        case "getMethod":
            fallBack = NO_SUCH_METHOD_NAME;
            break;
        case "getProp":
        case "getElem":
            fallBack = NO_SUCH_PROPERTY_NAME;
            break;
        default:
            fallBack = null;
            break;
        }

        if (fallBack != null) {
            find = self.findProperty(fallBack, true);
            if (find != null) {
                switch (operator) {
                case "getMethod":
                    link = self.noSuchMethod(desc, request);
                    break;
                case "getProp":
                case "getElem":
                    link = self.noSuchProperty(desc, request);
                    break;
                default:
                    break;
                }
            }
        }

        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    // still not found, may be scope can handle with it's own
    // __noSuchProperty__, __noSuchMethod__ etc.
    link = scope.lookup(desc, request);

    if (link != null) {
        return fixScopeCallSite(link, name, null);
    }

    return null;
}
 
Example 18
Source File: WithObject.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    // With scopes can never be observed outside of Nashorn code, so all call sites that can address it will of
    // necessity have a Nashorn descriptor - it is safe to cast.
    final NashornCallSiteDescriptor ndesc = (NashornCallSiteDescriptor)desc;
    FindProperty find = null;
    GuardedInvocation link = null;
    ScriptObject self = null;

    final boolean isNamedOperation;
    final String name;
    if(desc.getNameTokenCount() > 2) {
        isNamedOperation = true;
        name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
    } else {
        isNamedOperation = false;
        name = null;
    }

    self = expression;
    if (isNamedOperation) {
         find = self.findProperty(name, true);
    }

    if (find != null) {
        link = self.lookup(desc, request);

        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    final ScriptObject scope = getProto();
    if (isNamedOperation) {
        find = scope.findProperty(name, true);
    }

    if (find != null) {
        return fixScopeCallSite(scope.lookup(desc, request), name);
    }

    // the property is not found - now check for
    // __noSuchProperty__ and __noSuchMethod__ in expression
    if (self != null) {
        final String fallBack;

        final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);

        switch (operator) {
        case "callMethod":
            throw new AssertionError(); // Nashorn never emits callMethod
        case "getMethod":
            fallBack = NO_SUCH_METHOD_NAME;
            break;
        case "getProp":
        case "getElem":
            fallBack = NO_SUCH_PROPERTY_NAME;
            break;
        default:
            fallBack = null;
            break;
        }

        if (fallBack != null) {
            find = self.findProperty(fallBack, true);
            if (find != null) {
                switch (operator) {
                case "getMethod":
                    link = self.noSuchMethod(desc, request);
                    break;
                case "getProp":
                case "getElem":
                    link = self.noSuchProperty(desc, request);
                    break;
                default:
                    break;
                }
            }
        }

        if (link != null) {
            return fixExpressionCallSite(ndesc, link);
        }
    }

    // still not found, may be scope can handle with it's own
    // __noSuchProperty__, __noSuchMethod__ etc.
    link = scope.lookup(desc, request);

    if (link != null) {
        return fixScopeCallSite(link, name);
    }

    return null;
}
 
Example 19
Source File: AbstractJavaLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private GuardedInvocationComponent getPropertyGetter(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> ops) throws Exception {
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Since we can't know what kind of a getter we'll get back on different invocations, we'll just
            // conservatively presume Object. Note we can't just coerce to a narrower call site type as the linking
            // runtime might not allow coercing at that call site.
            final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
            // Must have exactly two arguments: receiver and name
            assertParameterCount(callSiteDescriptor, 2);

            // What's below is basically:
            //   foldArguments(guardWithTest(isNotNull, invoke(get_handle), null|nextComponent.invocation), get_getter_handle)
            // only with a bunch of method signature adjustments. Basically, retrieve method getter
            // AnnotatedDynamicMethod; if it is non-null, invoke its "handle" field, otherwise either return null,
            // or delegate to next component's invocation.

            final MethodHandle typedGetter = linkerServices.asType(getPropertyGetterHandle, type.changeReturnType(
                    AnnotatedDynamicMethod.class));
            final MethodHandle callSiteBoundMethodGetter = MethodHandles.insertArguments(
                    GET_ANNOTATED_METHOD, 1, callSiteDescriptor.getLookup(), linkerServices);
            final MethodHandle callSiteBoundInvoker = MethodHandles.filterArguments(GETTER_INVOKER, 0,
                    callSiteBoundMethodGetter);
            // Object(AnnotatedDynamicMethod, Object)->Object(AnnotatedDynamicMethod, T0)
            final MethodHandle invokeHandleTyped = linkerServices.asType(callSiteBoundInvoker,
                    MethodType.methodType(type.returnType(), AnnotatedDynamicMethod.class, type.parameterType(0)));
            // Since it's in the target of a fold, drop the unnecessary second argument
            // Object(AnnotatedDynamicMethod, T0)->Object(AnnotatedDynamicMethod, T0, T1)
            final MethodHandle invokeHandleFolded = MethodHandles.dropArguments(invokeHandleTyped, 2,
                    type.parameterType(1));
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, ops);

            final MethodHandle fallbackFolded;
            if(nextComponent == null) {
                // Object(AnnotatedDynamicMethod)->Object(AnnotatedDynamicMethod, T0, T1); returns constant null
                fallbackFolded = MethodHandles.dropArguments(CONSTANT_NULL_DROP_ANNOTATED_METHOD, 1,
                        type.parameterList()).asType(type.insertParameterTypes(0, AnnotatedDynamicMethod.class));
            } else {
                // Object(T0, T1)->Object(AnnotatedDynamicMethod, T0, T1); adapts the next component's invocation to
                // drop the extra argument resulting from fold and to change its return type to Object.
                final MethodHandle nextInvocation = nextComponent.getGuardedInvocation().getInvocation();
                final MethodType nextType = nextInvocation.type();
                fallbackFolded = MethodHandles.dropArguments(nextInvocation.asType(
                        nextType.changeReturnType(Object.class)), 0, AnnotatedDynamicMethod.class);
            }

            // fold(Object(AnnotatedDynamicMethod, T0, T1), AnnotatedDynamicMethod(T0, T1))
            final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                        IS_ANNOTATED_METHOD_NOT_NULL, invokeHandleFolded, fallbackFolded), typedGetter);
            if(nextComponent == null) {
                return getClassGuardedInvocationComponent(compositeGetter, type);
            }
            return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
        }
        case 3: {
            // Must have exactly one argument: receiver
            assertParameterCount(callSiteDescriptor, 1);
            // Fixed name
            final AnnotatedDynamicMethod annGetter = propertyGetters.get(callSiteDescriptor.getNameToken(
                    CallSiteDescriptor.NAME_OPERAND));
            if(annGetter == null) {
                // We have no such property, always delegate to the next component operation
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
            }
            final MethodHandle getter = annGetter.getInvocation(callSiteDescriptor, linkerServices);
            // NOTE: since property getters (not field getters!) are no-arg, we don't have to worry about them being
            // overloaded in a subclass. Therefore, we can discover the most abstract superclass that has the
            // method, and use that as the guard with Guards.isInstance() for a more stably linked call site. If
            // we're linking against a field getter, don't make the assumption.
            // NOTE: No delegation to the next component operation if we have a property with this name, even if its
            // value is null.
            final ValidationType validationType = annGetter.validationType;
            // TODO: we aren't using the type that declares the most generic getter here!
            return new GuardedInvocationComponent(getter, getGuard(validationType,
                    callSiteDescriptor.getMethodType()), clazz, validationType);
        }
        default: {
            // Can't do anything with more than 3 name components
            return null;
        }
    }
}
 
Example 20
Source File: JavaSuperAdapterLinker.java    From TencentKona-8 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 objSuperAdapter = linkRequest.getReceiver();
    if(!(objSuperAdapter instanceof JavaSuperAdapter)) {
        return null;
    }

    final CallSiteDescriptor descriptor = linkRequest.getCallSiteDescriptor();
    if(!CallSiteDescriptorFactory.tokenizeOperators(descriptor).contains(GET_METHOD)) {
        // We only handle getMethod
        return null;
    }

    final Object adapter = ((JavaSuperAdapter)objSuperAdapter).getAdapter();

    // Replace argument (javaSuperAdapter, ...) => (adapter, ...) when delegating to BeansLinker
    final Object[] args = linkRequest.getArguments();
    args[0] = adapter;

    // Use R(T0, ...) => R(adapter.class, ...) call site type when delegating to BeansLinker.
    final MethodType type = descriptor.getMethodType();
    final Class<?> adapterClass = adapter.getClass();
    final boolean hasFixedName = descriptor.getNameTokenCount() > 2;
    final String opName = hasFixedName ? (DYN_GET_METHOD_FIXED + descriptor.getNameToken(
            CallSiteDescriptor.NAME_OPERAND)) : DYN_GET_METHOD;

    final CallSiteDescriptor newDescriptor = NashornCallSiteDescriptor.get(descriptor.getLookup(), opName,
            type.changeParameterType(0, adapterClass), 0);

    // Delegate to BeansLinker
    final GuardedInvocation guardedInv = NashornBeansLinker.getGuardedInvocation(
            BeansLinker.getLinkerForClass(adapterClass), linkRequest.replaceArguments(newDescriptor, args),
            linkerServices);

    final MethodHandle guard = IS_ADAPTER_OF_CLASS.bindTo(adapterClass);
    if(guardedInv == null) {
        // Short circuit the lookup here for non-existent methods by linking an empty getter. If we just returned
        // null instead, BeansLinker would find final methods on the JavaSuperAdapter instead: getClass() and
        // wait().
        return new GuardedInvocation(MethodHandles.dropArguments(EMPTY_GETTER, 1,type.parameterList().subList(1,
                type.parameterCount())), guard).asType(descriptor);
    }

    final MethodHandle invocation = guardedInv.getInvocation();
    final MethodType invType = invocation.type();
    // For invocation typed R(T0, ...) create a dynamic method binder of type Object(R, T0)
    final MethodHandle typedBinder = BIND_DYNAMIC_METHOD.asType(MethodType.methodType(Object.class,
            invType.returnType(), invType.parameterType(0)));
    // For invocation typed R(T0, T1, ...) create a dynamic method binder of type Object(R, T0, T1, ...)
    final MethodHandle droppingBinder = MethodHandles.dropArguments(typedBinder, 2,
            invType.parameterList().subList(1, invType.parameterCount()));
    // Finally, fold the invocation into the binder to produce a method handle that will bind every returned
    // DynamicMethod object from dyn:getMethod calls to the actual receiver
    // Object(R(T0, T1, ...), T0, T1, ...)
    final MethodHandle bindingInvocation = MethodHandles.foldArguments(droppingBinder, invocation);

    final MethodHandle typedGetAdapter = asFilterType(GET_ADAPTER, 0, invType, type);
    final MethodHandle adaptedInvocation;
    if(hasFixedName) {
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter);
    } else {
        // Add a filter that'll prepend "super$" to each name passed to the variable-name "dyn:getMethod".
        final MethodHandle typedAddPrefix = asFilterType(ADD_PREFIX_TO_METHOD_NAME, 1, invType, type);
        adaptedInvocation = MethodHandles.filterArguments(bindingInvocation, 0, typedGetAdapter, typedAddPrefix);
    }

    return guardedInv.replaceMethods(adaptedInvocation, guard).asType(descriptor);
}