Java Code Examples for jdk.internal.dynalink.CallSiteDescriptor

The following examples show how to use jdk.internal.dynalink.CallSiteDescriptor. These examples are extracted from open source projects. 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 Project: openjdk-8-source   Author: keerath   File: JSObjectLinker.java    License: 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();

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

    final GuardedInvocation inv;
    if (self instanceof JSObject) {
        inv = lookup(desc);
    } else {
        throw new AssertionError(); // Should never reach here.
    }

    return Bootstrap.asType(inv, linkerServices, desc);
}
 
Example #2
Source Project: jdk8u60   Author: chenghanpeng   File: BrowserJSObjectLinker.java    License: 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;
    }

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

    return Bootstrap.asTypeSafeReturn(inv, linkerServices, desc);
}
 
Example #3
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ScriptObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the appropriate GETINDEX method for an invoke dynamic call.
 *
 * @param desc    the call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final MethodType callType                = desc.getMethodType();
    final Class<?>   returnType              = callType.returnType();
    final Class<?>   returnClass             = returnType.isPrimitive() ? returnType : Object.class;
    final Class<?>   keyClass                = callType.parameterType(1);
    final boolean    explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);

    final String name;
    if (returnClass.isPrimitive()) {
        //turn e.g. get with a double into getDouble
        final String returnTypeName = returnClass.getName();
        name = "get" + Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length());
    } else {
        name = "get";
    }

    final MethodHandle mh = findGetIndexMethodHandle(returnClass, name, keyClass, desc);
    return new GuardedInvocation(mh, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example #4
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: JSObjectLinker.java    License: 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();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #5
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: JSObjectLinker.java    License: 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();

    switch (operator) {
        case "getProp":
        case "getElem":
        case "getMethod":
            if (c > 2) {
                return findGetMethod(desc);
            }
        // For indexed get, we want get GuardedInvocation beans linker and pass it.
        // JSObjectLinker.get uses this fallback getter for explicit signature method access.
        return findGetIndexMethod(nashornBeansLinker.getGuardedInvocation(request, linkerServices));
        case "setProp":
        case "setElem":
            return c > 2 ? findSetMethod(desc) : findSetIndexMethod();
        case "call":
            return findCallMethod(desc);
        case "new":
            return findNewMethod(desc);
        default:
            return null;
    }
}
 
Example #6
Source Project: jdk8u60   Author: chenghanpeng   File: ScriptObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the appropriate GETINDEX method for an invoke dynamic call.
 *
 * @param desc    the call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation to be invoked at call site.
 */
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final MethodType callType                = desc.getMethodType();
    final Class<?>   returnType              = callType.returnType();
    final Class<?>   returnClass             = returnType.isPrimitive() ? returnType : Object.class;
    final Class<?>   keyClass                = callType.parameterType(1);
    final boolean    explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);

    final String name;
    if (returnClass.isPrimitive()) {
        //turn e.g. get with a double into getDouble
        final String returnTypeName = returnClass.getName();
        name = "get" + Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length());
    } else {
        name = "get";
    }

    final MethodHandle mh = findGetIndexMethodHandle(returnClass, name, keyClass, desc);
    return new GuardedInvocation(mh, getScriptObjectGuard(callType, explicitInstanceOfCheck), (SwitchPoint)null, explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example #7
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: ScriptObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fall back if a function property is not found.
 * @param desc The call site descriptor
 * @param request the link request
 * @return GuardedInvocation to be invoked at call site.
 */
public GuardedInvocation noSuchMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final String       name      = desc.getNameToken(2);
    final FindProperty find      = findProperty(NO_SUCH_METHOD_NAME, true);
    final boolean      scopeCall = isScope() && NashornCallSiteDescriptor.isScope(desc);

    if (find == null) {
        return noSuchProperty(desc, request);
    }

    final boolean explicitInstanceOfCheck = explicitInstanceOfCheck(desc, request);

    final Object value = find.getObjectValue();
    if (!(value instanceof ScriptFunction)) {
        return createEmptyGetter(desc, explicitInstanceOfCheck, name);
    }

    final ScriptFunction func = (ScriptFunction)value;
    final Object         thiz = scopeCall && func.isStrict() ? UNDEFINED : this;
    // TODO: It'd be awesome if we could bind "name" without binding "this".
    // Since we're binding this we must use an identity guard here.
    return new GuardedInvocation(
            MH.dropArguments(
                    MH.constant(
                            ScriptFunction.class,
                            func.createBound(thiz, new Object[] { name })),
                    0,
                    Object.class),
            NashornGuards.combineGuards(
                    NashornGuards.getIdentityGuard(this),
                    NashornGuards.getMapGuard(getMap(), true)));
}
 
Example #8
Source Project: TencentKona-8   Author: Tencent   File: NativeJSAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findCallMethodMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    if (overrides && super.hasOwnProperty(desc.getNameToken(2))) {
        try {
            final GuardedInvocation inv = super.findCallMethodMethod(desc, request);
            if (inv != null) {
                return inv;
            }
        } catch (final Exception e) {
            //ignored
        }
    }

    return findHook(desc, __call__);
}
 
Example #9
Source Project: nashorn   Author: ynsn   File: BeanLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static MethodHandle convertArgToInt(MethodHandle mh, LinkerServices ls, CallSiteDescriptor desc) {
    final Class<?> sourceType = desc.getMethodType().parameterType(1);
    if(TypeUtilities.isMethodInvocationConvertible(sourceType, Number.class)) {
        return mh;
    } else if(ls.canConvert(sourceType, Number.class)) {
        final MethodHandle converter = ls.getTypeConverter(sourceType, Number.class);
        return MethodHandles.filterArguments(mh, 1, converter.asType(converter.type().changeReturnType(
                mh.type().parameterType(1))));
    }
    return mh;
}
 
Example #10
Source Project: jdk8u60   Author: chenghanpeng   File: BeanLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocationComponent getLengthGetter(final CallSiteDescriptor callSiteDescriptor) {
    assertParameterCount(callSiteDescriptor, 1);
    final MethodType callSiteType = callSiteDescriptor.getMethodType();
    final Class<?> declaredType = callSiteType.parameterType(0);
    // If declared type of receiver at the call site is already an array, collection, or map, bind without guard.
    // Thing is, it'd be quite stupid of a call site creator to go though invokedynamic when it knows in advance
    // they're dealing with an array, collection, or map, but hey...
    if(declaredType.isArray()) {
        return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType));
    } else if(Collection.class.isAssignableFrom(declaredType)) {
        return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType));
    } else if(Map.class.isAssignableFrom(declaredType)) {
        return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType));
    }

    // Otherwise, create a binding based on the actual type of the argument with an appropriate guard.
    if(clazz.isArray()) {
        return new GuardedInvocationComponent(GET_ARRAY_LENGTH.asType(callSiteType), Guards.isArray(0,
                callSiteType), ValidationType.IS_ARRAY);
    } if(Collection.class.isAssignableFrom(clazz)) {
        return new GuardedInvocationComponent(GET_COLLECTION_LENGTH.asType(callSiteType), Guards.asType(
                COLLECTION_GUARD, callSiteType), Collection.class, ValidationType.INSTANCE_OF);
    } if(Map.class.isAssignableFrom(clazz)) {
        return new GuardedInvocationComponent(GET_MAP_LENGTH.asType(callSiteType), Guards.asType(MAP_GUARD,
                callSiteType), Map.class, ValidationType.INSTANCE_OF);
    }
    // Can't retrieve length for objects that are neither arrays, nor collections, nor maps.
    return null;
}
 
Example #11
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NashornBeansLinker.java    License: 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 #12
Source Project: hottub   Author: dsrg-uoft   File: OptimisticReturnFilters.java    License: 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 #13
Source Project: jdk8u60   Author: chenghanpeng   File: DynamicMethodLinker.java    License: 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 #14
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: DynamicMethodLinker.java    License: 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 #15
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: ArrayBufferView.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findSetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final GuardedInvocation inv = getArray().findFastSetIndexMethod(getArray().getClass(), desc, request);
    if (inv != null) {
        return inv;
    }
    return super.findSetIndexMethod(desc, request);
}
 
Example #16
Source Project: hottub   Author: dsrg-uoft   File: NashornLinker.java    License: GNU General Public License v2.0 5 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();

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

    return Bootstrap.asTypeSafeReturn(getGuardedInvocation(self,  request, desc), linkerServices, desc);
}
 
Example #17
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: TypedArrayData.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation findFastGetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) {
    final GuardedInvocation inv = super.findFastGetIndexMethod(clazz, desc, request);

    if (inv != null) {
        return inv;
    }

    return null;
}
 
Example #18
Source Project: hottub   Author: dsrg-uoft   File: TypedArrayData.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation findFastGetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) {
    final GuardedInvocation inv = super.findFastGetIndexMethod(clazz, desc, request);

    if (inv != null) {
        return inv;
    }

    return null;
}
 
Example #19
Source Project: nashorn   Author: ynsn   File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetMethod(final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
    final String name = desc.getNameToken(2);

    // if str.length(), then let the bean linker handle it
    if ("length".equals(name) && "getMethod".equals(operator)) {
        return null;
    }

    return super.findGetMethod(desc, request, operator);
}
 
Example #20
Source Project: jdk8u60   Author: chenghanpeng   File: CallSiteDescriptorFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static CallSiteDescriptor createPublicCallSiteDescriptor(final String[] tokenizedName, final MethodType methodType) {
    final int l = tokenizedName.length;
    if(l > 0 && tokenizedName[0] == "dyn") {
        if(l == 2) {
            return new UnnamedDynCallSiteDescriptor(tokenizedName[1], methodType);
        } if (l == 3) {
            return new NamedDynCallSiteDescriptor(tokenizedName[1], tokenizedName[2], methodType);
        }
    }
    return new DefaultCallSiteDescriptor(tokenizedName, methodType);
}
 
Example #21
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean canLink(final Object self, final CallSiteDescriptor desc, final LinkRequest request) {
    try {
        //check that it's a char sequence or throw cce
        final CharSequence cs = (CharSequence)self;
        //check that the index, representable as an int, is inside the array
        final int intIndex = JSType.toInteger(request.getArguments()[2]);
        return intIndex >= 0 && intIndex < cs.length(); //can link
    } catch (final ClassCastException | IndexOutOfBoundsException e) {
        //fallthru
    }
    return false;
}
 
Example #22
Source Project: hottub   Author: dsrg-uoft   File: DynamicMethodLinker.java    License: 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 #23
Source Project: hottub   Author: dsrg-uoft   File: ScriptObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
private GuardedInvocation createEmptySetMethod(final CallSiteDescriptor desc, final boolean explicitInstanceOfCheck, final String strictErrorMessage, final boolean canBeFastScope) {
    final String  name = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
    if (NashornCallSiteDescriptor.isStrict(desc)) {
        throw typeError(strictErrorMessage, name, ScriptRuntime.safeToString(this));
    }
    assert canBeFastScope || !NashornCallSiteDescriptor.isFastScope(desc);
    return new GuardedInvocation(
            Lookup.EMPTY_SETTER,
            NashornGuards.getMapGuard(getMap(), explicitInstanceOfCheck),
            getProtoSwitchPoints(name, null),
            explicitInstanceOfCheck ? null : ClassCastException.class);
}
 
Example #24
Source Project: jdk8u60   Author: chenghanpeng   File: DefaultCallSiteDescriptor.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public CallSiteDescriptor changeMethodType(final MethodType newMethodType) {
    return CallSiteDescriptorFactory.getCanonicalPublicDescriptor(new DefaultCallSiteDescriptor(tokenizedName,
            newMethodType));
}
 
Example #25
Source Project: TencentKona-8   Author: Tencent   File: ScriptObject.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Fall back if a property is not found.
 * @param desc the call site descriptor.
 * @param request the link request
 * @return GuardedInvocation to be invoked at call site.
 */
public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
    final String       name        = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
    final FindProperty find        = findProperty(NO_SUCH_PROPERTY_NAME, true);
    final boolean      scopeAccess = isScope() && NashornCallSiteDescriptor.isScope(desc);

    if (find != null) {
        final Object   value = find.getObjectValue();
        ScriptFunction func  = null;
        MethodHandle   mh    = null;

        if (value instanceof ScriptFunction) {
            func = (ScriptFunction)value;
            mh   = getCallMethodHandle(func, desc.getMethodType(), name);
        }

        if (mh != null) {
            assert func != null;
            if (scopeAccess && func.isStrict()) {
                mh = bindTo(mh, UNDEFINED);
            }

            return new GuardedInvocation(
                    mh,
                    find.isSelf()?
                        getKnownFunctionPropertyGuardSelf(
                            getMap(),
                            find.getGetter(Object.class, INVALID_PROGRAM_POINT, request),
                            func)
                        :
                        //TODO this always does a scriptobject check
                        getKnownFunctionPropertyGuardProto(
                            getMap(),
                            find.getGetter(Object.class, INVALID_PROGRAM_POINT, request),
                            find.getProtoChainLength(),
                            func),
                    getProtoSwitchPoints(NO_SUCH_PROPERTY_NAME, find.getOwner()),
                    //TODO this doesn't need a ClassCastException as guard always checks script object
                    null);
        }
    }

    if (scopeAccess) {
        throw referenceError("not.defined", name);
    }

    return createEmptyGetter(desc, explicitInstanceOfCheck(desc, request), name);
}
 
Example #26
Source Project: nashorn   Author: ynsn   File: AbstractCallSiteDescriptor.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean equals(Object obj) {
    return obj instanceof CallSiteDescriptor && equals((CallSiteDescriptor)obj);
}
 
Example #27
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: AbstractJavaLinker.java    License: GNU General Public License v2.0 4 votes vote down vote up
private GuardedInvocationComponent getMethodGetter(final CallSiteDescriptor callSiteDescriptor,
        final LinkerServices linkerServices, final List<String> ops) throws Exception {
    // The created method handle will always return a DynamicMethod (or null), but since we don't want that type to
    // be visible outside of this linker, declare it to return Object.
    final MethodType type = callSiteDescriptor.getMethodType().changeReturnType(Object.class);
    switch(callSiteDescriptor.getNameTokenCount()) {
        case 2: {
            // Must have exactly two arguments: receiver and name
            assertParameterCount(callSiteDescriptor, 2);
            final GuardedInvocationComponent nextComponent = getGuardedInvocationComponent(callSiteDescriptor,
                    linkerServices, ops);
            if(nextComponent == null || !TypeUtilities.areAssignable(DynamicMethod.class,
                    nextComponent.getGuardedInvocation().getInvocation().type().returnType())) {
                // No next component operation, or it can never produce a dynamic method; just return a component
                // for this operation.
                return getClassGuardedInvocationComponent(linkerServices.asType(getDynamicMethod, type), type);
            }

            // What's below is basically:
            // foldArguments(guardWithTest(isNotNull, identity, nextComponent.invocation), getter) only with a
            // bunch of method signature adjustments. Basically, execute method getter; if it returns a non-null
            // DynamicMethod, use identity to return it, otherwise delegate to nextComponent's invocation.

            final MethodHandle typedGetter = linkerServices.asType(getDynamicMethod, type);
            // Since it is part of the foldArgument() target, it will have extra args that we need to drop.
            final MethodHandle returnMethodHandle = linkerServices.asType(MethodHandles.dropArguments(
                    OBJECT_IDENTITY, 1, type.parameterList()), type.insertParameterTypes(0, Object.class));
            final MethodHandle nextComponentInvocation = nextComponent.getGuardedInvocation().getInvocation();
            // The assumption is that getGuardedInvocationComponent() already asType()'d it correctly modulo the
            // return type.
            assert nextComponentInvocation.type().changeReturnType(type.returnType()).equals(type);
            // Since it is part of the foldArgument() target, we have to drop an extra arg it receives.
            final MethodHandle nextCombinedInvocation = MethodHandles.dropArguments(nextComponentInvocation, 0,
                    Object.class);
            // Assemble it all into a fold(guard(isNotNull, identity, nextInvocation), get)
            final MethodHandle compositeGetter = MethodHandles.foldArguments(MethodHandles.guardWithTest(
                    IS_DYNAMIC_METHOD, returnMethodHandle, nextCombinedInvocation), typedGetter);

            return nextComponent.compose(compositeGetter, getClassGuard(type), clazz, ValidationType.EXACT_CLASS);
        }
        case 3: {
            // Must have exactly one argument: receiver
            assertParameterCount(callSiteDescriptor, 1);
            final DynamicMethod method = getDynamicMethod(callSiteDescriptor.getNameToken(
                    CallSiteDescriptor.NAME_OPERAND));
            if(method == null) {
                // We have no such method, always delegate to the next component
                return getGuardedInvocationComponent(callSiteDescriptor, linkerServices, ops);
            }
            // No delegation to the next component of the composite operation; if we have a method with that name,
            // we'll always return it at this point.
            return getClassGuardedInvocationComponent(linkerServices.asType(MethodHandles.dropArguments(
                    MethodHandles.constant(Object.class, method), 0, type.parameterType(0)), type), type);
        }
        default: {
            // Can't do anything with more than 3 name components
            return null;
        }
    }
}
 
Example #28
Source Project: nashorn   Author: ynsn   File: WithObject.java    License: 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 #29
Source Project: TencentKona-8   Author: Tencent   File: Global.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    return filterInvocation(super.findSetMethod(desc, request));
}
 
Example #30
Source Project: openjdk-8   Author: bpupadhyaya   File: ScriptObject.java    License: GNU General Public License v2.0 4 votes vote down vote up
private static GuardedInvocation findMegaMorphicGetMethod(final CallSiteDescriptor desc, final String name, final boolean isMethod) {
    final MethodHandle invoker = MH.insertArguments(MEGAMORPHIC_GET, 1, name, isMethod);
    final MethodHandle guard = getScriptObjectGuard(desc.getMethodType());
    return new GuardedInvocation(invoker, guard);
}