jdk.internal.dynalink.linker.GuardedInvocation Java Examples

The following examples show how to use jdk.internal.dynalink.linker.GuardedInvocation. 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   Author: bpupadhyaya   File: NashornBottomLinker.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
    case "call":
        throw typeError("not.a.function", "null");
    case "callMethod":
    case "getMethod":
        throw typeError("no.such.function", getArgument(linkRequest), "null");
    case "getProp":
    case "getElem":
        throw typeError("cant.get.property", getArgument(linkRequest), "null");
    case "setProp":
    case "setElem":
        throw typeError("cant.set.property", getArgument(linkRequest), "null");
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #2
Source Project: jdk8u60   Author: chenghanpeng   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 #3
Source Project: openjdk-8-source   Author: keerath   File: TypeConverterFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
MethodHandle createConverter(Class<?> sourceType, Class<?> targetType) throws Exception {
    final MethodType type = MethodType.methodType(targetType, sourceType);
    final MethodHandle identity = IDENTITY_CONVERSION.asType(type);
    MethodHandle last = identity;
    boolean cacheable = true;
    for(int i = factories.length; i-- > 0;) {
        final GuardedTypeConversion next = factories[i].convertToType(sourceType, targetType);
        if(next != null) {
            cacheable = cacheable && next.isCacheable();
            final GuardedInvocation conversionInvocation = next.getConversionInvocation();
            conversionInvocation.assertType(type);
            last = conversionInvocation.compose(last);
        }
    }
    if(last == identity) {
        return IDENTITY_CONVERSION;
    }
    if(cacheable) {
        return last;
    }
    throw new NotCacheableConverter(last);
}
 
Example #4
Source Project: openjdk-8   Author: bpupadhyaya   File: JSObjectLinker.java    License: 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 #5
Source Project: openjdk-8-source   Author: keerath   File: NativeJavaPackage.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Handle creation of new attribute.
 * @param desc the call site descriptor
 * @param request the link request
 * @return Link to be invoked at call site.
 */
@Override
public GuardedInvocation noSuchProperty(final CallSiteDescriptor desc, final LinkRequest request) {
    final String propertyName = desc.getNameToken(2);
    final String fullName     = name.isEmpty() ? propertyName : name + "." + propertyName;

    final Context context = Context.getContextTrusted();

    Class<?> javaClass = null;
    try {
        javaClass = context.findClass(fullName);
    } catch (final NoClassDefFoundError | ClassNotFoundException e) {
        //ignored
    }

    if (javaClass == null) {
        set(propertyName, new NativeJavaPackage(fullName, getProto()), false);
    } else {
        set(propertyName, StaticClass.forClass(javaClass), false);
    }

    return super.lookup(desc, request);
}
 
Example #6
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornBottomLinker.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static GuardedInvocation linkNull(final LinkRequest linkRequest) {
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor)linkRequest.getCallSiteDescriptor();
    final String operator = desc.getFirstOperator();
    switch (operator) {
    case "new":
    case "call":
        throw typeError("not.a.function", "null");
    case "callMethod":
    case "getMethod":
        throw typeError("no.such.function", getArgument(linkRequest), "null");
    case "getProp":
    case "getElem":
        throw typeError("cant.get.property", getArgument(linkRequest), "null");
    case "setProp":
    case "setElem":
        throw typeError("cant.set.property", getArgument(linkRequest), "null");
    default:
        break;
    }
    throw new AssertionError("unknown call type " + desc);
}
 
Example #7
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornLinker.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or
 * Queue or Deque or Collection type.
 * @param sourceType the source type (presumably NativeArray a superclass of it)
 * @param targetType the target type (presumably an array type, or List or Queue, or Deque, or Collection)
 * @return a guarded invocation that converts from the source type to the target type. null is returned if
 * either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array
 * type, List, Queue, Deque, or Collection.
 */
private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType) {
    final boolean isSourceTypeNativeArray = sourceType == NativeArray.class;
    // If source type is more generic than NativeArray class, we'll need to use a guard
    final boolean isSourceTypeGeneric = !isSourceTypeNativeArray && sourceType.isAssignableFrom(NativeArray.class);

    if (isSourceTypeNativeArray || isSourceTypeGeneric) {
        final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
        if(targetType.isArray()) {
            return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard);
        } else if(targetType == List.class) {
            return new GuardedInvocation(TO_LIST, guard);
        } else if(targetType == Deque.class) {
            return new GuardedInvocation(TO_DEQUE, guard);
        } else if(targetType == Queue.class) {
            return new GuardedInvocation(TO_QUEUE, guard);
        } else if(targetType == Collection.class) {
            return new GuardedInvocation(TO_COLLECTION, guard);
        }
    }
    return null;
}
 
Example #8
Source Project: openjdk-8-source   Author: keerath   File: StaticClassLinker.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest request, LinkerServices linkerServices)
        throws Exception {
    final GuardedInvocation gi = super.getGuardedInvocation(request, linkerServices);
    if(gi != null) {
        return gi;
    }
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    final String op = desc.getNameToken(CallSiteDescriptor.OPERATOR);
    if("new" == op && constructor != null) {
        final MethodHandle ctorInvocation = constructor.getInvocation(desc, linkerServices);
        if(ctorInvocation != null) {
            return new GuardedInvocation(ctorInvocation, getClassGuard(desc.getMethodType()));
        }
    }
    return null;
}
 
Example #9
Source Project: openjdk-8-source   Author: keerath   File: NashornLinker.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a guarded invocation that converts from a source type that is NativeArray to a Java array or List or
 * Deque type.
 * @param sourceType the source type (presumably NativeArray a superclass of it)
 * @param targetType the target type (presumably an array type, or List or Deque)
 * @return a guarded invocation that converts from the source type to the target type. null is returned if
 * either the source type is neither NativeArray, nor a superclass of it, or if the target type is not an array
 * type, List, or Deque.
 */
private static GuardedInvocation getArrayConverter(final Class<?> sourceType, final Class<?> targetType) {
    final boolean isSourceTypeNativeArray = sourceType == NativeArray.class;
    // If source type is more generic than ScriptFunction class, we'll need to use a guard
    final boolean isSourceTypeGeneric = !isSourceTypeNativeArray && sourceType.isAssignableFrom(NativeArray.class);

    if (isSourceTypeNativeArray || isSourceTypeGeneric) {
        final MethodHandle guard = isSourceTypeGeneric ? IS_NATIVE_ARRAY : null;
        if(targetType.isArray()) {
            return new GuardedInvocation(ARRAY_CONVERTERS.get(targetType), guard);
        }
        if(targetType == List.class) {
            return new GuardedInvocation(JSType.TO_JAVA_LIST.methodHandle(), guard);
        }
        if(targetType == Deque.class) {
            return new GuardedInvocation(JSType.TO_JAVA_DEQUE.methodHandle(), guard);
        }
    }
    return null;
}
 
Example #10
Source Project: TencentKona-8   Author: Tencent   File: BeansLinker.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 CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();
    final int l = callSiteDescriptor.getNameTokenCount();
    // All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn"
    if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) {
        return null;
    }

    final Object receiver = request.getReceiver();
    if(receiver == null) {
        // Can't operate on null
        return null;
    }
    return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices);
}
 
Example #11
Source Project: hottub   Author: dsrg-uoft   File: BeansLinker.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 CallSiteDescriptor callSiteDescriptor = request.getCallSiteDescriptor();
    final int l = callSiteDescriptor.getNameTokenCount();
    // All names conforming to the dynalang MOP should have at least two tokens, the first one being "dyn"
    if(l < 2 || "dyn" != callSiteDescriptor.getNameToken(CallSiteDescriptor.SCHEME)) {
        return null;
    }

    final Object receiver = request.getReceiver();
    if(receiver == null) {
        // Can't operate on null
        return null;
    }
    return getLinkerForClass(receiver.getClass()).getGuardedInvocation(request, linkerServices);
}
 
Example #12
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 #13
Source Project: openjdk-8   Author: bpupadhyaya   File: NashornLinker.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 ScriptObject) {
        inv = ((ScriptObject)self).lookup(desc, request);
    } else if (self instanceof Undefined) {
        inv = Undefined.lookup(desc);
    } else {
        throw new AssertionError(); // Should never reach here.
    }

    return Bootstrap.asType(inv, linkerServices, desc);
}
 
Example #14
Source Project: jdk8u60   Author: chenghanpeng   File: ContinuousArrayData.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a fast linked array setter, or null if we have to dispatch to super class
 * @param desc     descriptor
 * @param request  link request
 * @return invocation or null if needs to be sent to slow relink
 */
@Override
public GuardedInvocation findFastSetIndexMethod(final Class<? extends ArrayData> clazz, final CallSiteDescriptor desc, final LinkRequest request) { // array, index, value
    final MethodType callType    = desc.getMethodType();
    final Class<?>   indexType   = callType.parameterType(1);
    final Class<?>   elementType = callType.parameterType(2);

    if (ContinuousArrayData.class.isAssignableFrom(clazz) && indexType == int.class) {
        final Object[]        args  = request.getArguments();
        final int             index = (int)args[args.length - 2];

        if (hasRoomFor(index)) {
            MethodHandle setElement = getElementSetter(elementType); //Z(continuousarraydata, int, int), return true if successful
            if (setElement != null) {
                //else we are dealing with a wider type than supported by this callsite
                MethodHandle getArray = ScriptObject.GET_ARRAY.methodHandle();
                getArray   = MH.asType(getArray, getArray.type().changeReturnType(getClass()));
                setElement = MH.filterArguments(setElement, 0, getArray);
                final MethodHandle guard = MH.insertArguments(FAST_ACCESS_GUARD, 0, clazz);
                return new GuardedInvocation(setElement, guard, (SwitchPoint)null, ClassCastException.class); //CCE if not a scriptObject anymore
            }
        }
    }

    return null;
}
 
Example #15
Source Project: nashorn   Author: ynsn   File: Undefined.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 invoke dynamic callsite descriptor
 * @param args arguments
 * @return GuardedInvocation to be invoked at call site.
 */
private static GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final Object... args) {
    final MethodType callType  = desc.getMethodType();
    final Class<?> returnClass = callType.returnType();
    final Class<?> keyClass    = callType.parameterType(1);

    String name = "get";
    if (returnClass.isPrimitive()) {
        //turn e.g. get with a double into getDouble
        final String returnTypeName = returnClass.getName();
        name += Character.toUpperCase(returnTypeName.charAt(0)) + returnTypeName.substring(1, returnTypeName.length());
    }
    MethodHandle methodHandle = findOwnMH(name, returnClass, keyClass);
    methodHandle = MH.asType(methodHandle, methodHandle.type().changeParameterType(0, Object.class));

    return new GuardedInvocation(methodHandle, UNDEFINED_GUARD);
}
 
Example #16
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, final LinkerServices linkerServices)
        throws Exception {
    final Object obj = linkRequest.getReceiver();
    if(obj == null) {
        return null;
    }
    for(TypeBasedGuardingDynamicLinker linker: classToLinker.get(obj.getClass())) {
        final GuardedInvocation invocation = linker.getGuardedInvocation(linkRequest, linkerServices);
        if(invocation != null) {
            return invocation;
        }
    }
    return null;
}
 
Example #17
Source Project: openjdk-8-source   Author: keerath   File: ScriptObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find the appropriate SETINDEX method for an invoke dynamic call.
 *
 * @param callType the method type at the call site
 * @param isStrict are we in strict mode?
 *
 * @return GuardedInvocation to be invoked at call site.
 */
private static GuardedInvocation findSetIndexMethod(final MethodType callType, final boolean isStrict) {
    assert callType.parameterCount() == 3;

    final Class<?>   keyClass   = callType.parameterType(1);
    final Class<?>   valueClass = callType.parameterType(2);

    MethodHandle methodHandle = findOwnMH("set", void.class, keyClass, valueClass, boolean.class);
    methodHandle = MH.insertArguments(methodHandle, 3, isStrict);

    return new GuardedInvocation(methodHandle, getScriptObjectGuard(callType));
}
 
Example #18
Source Project: TencentKona-8   Author: Tencent   File: ArrayBufferView.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final GuardedInvocation inv = getArray().findFastGetIndexMethod(getArray().getClass(), desc, request);
    if (inv != null) {
        return inv;
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example #19
Source Project: jdk8u_nashorn   Author: JetBrains   File: NativeString.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findGetIndexMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    final Object self = request.getReceiver();
    final Class<?> returnType = desc.getMethodType().returnType();

    if (returnType == Object.class && JSType.isString(self)) {
        try {
            return new GuardedInvocation(MH.findStatic(MethodHandles.lookup(), NativeString.class, "get", desc.getMethodType()), NashornGuards.getStringGuard());
        } catch (final LookupException e) {
            //empty. Shouldn't happen. Fall back to super
        }
    }
    return super.findGetIndexMethod(desc, request);
}
 
Example #20
Source Project: nashorn   Author: ynsn   File: ReflectionCheckLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
        throws Exception {
    checkLinkRequest(origRequest);
    // let the next linker deal with actual linking
    return null;
}
 
Example #21
Source Project: TencentKona-8   Author: Tencent   File: NativeArray.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 #22
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a guarded invocation that converts from a source type that is ScriptFunction, or a subclass or a
 * superclass of it) to a SAM type.
 * @param sourceType the source type (presumably ScriptFunction or a subclass or a superclass of it)
 * @param targetType the target type (presumably a SAM type)
 * @return a guarded invocation that converts from the source type to the target SAM type. null is returned if
 * either the source type is neither ScriptFunction, nor a subclass, nor a superclass of it, or if the target type
 * is not a SAM type.
 * @throws Exception if something goes wrong; generally, if there's an issue with creation of the SAM proxy type
 * constructor.
 */
private static GuardedInvocation getSamTypeConverter(final Class<?> sourceType, final Class<?> targetType) throws Exception {
    // If source type is more generic than ScriptFunction class, we'll need to use a guard
    final boolean isSourceTypeGeneric = sourceType.isAssignableFrom(ScriptFunction.class);

    if ((isSourceTypeGeneric || ScriptFunction.class.isAssignableFrom(sourceType)) && isAutoConvertibleFromFunction(targetType)) {
        final MethodHandle ctor = JavaAdapterFactory.getConstructor(ScriptFunction.class, targetType, getCurrentLookup());
        assert ctor != null; // if isAutoConvertibleFromFunction() returned true, then ctor must exist.
        return new GuardedInvocation(ctor, isSourceTypeGeneric ? IS_SCRIPT_FUNCTION : null);
    }
    return null;
}
 
Example #23
Source Project: TencentKona-8   Author: Tencent   File: NashornLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedTypeConversion convertToType(final Class<?> sourceType, final Class<?> targetType) throws Exception {
    GuardedInvocation gi = convertToTypeNoCast(sourceType, targetType);
    if(gi != null) {
        return new GuardedTypeConversion(gi.asType(MH.type(targetType, sourceType)), true);
    }
    gi = getSamTypeConverter(sourceType, targetType);
    if(gi != null) {
        return new GuardedTypeConversion(gi.asType(MH.type(targetType, sourceType)), false);
    }
    return null;
}
 
Example #24
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 #25
Source Project: jdk8u60   Author: chenghanpeng   File: NativeJSAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected GuardedInvocation findSetMethod(final CallSiteDescriptor desc, final LinkRequest request) {
    if (overrides && super.hasOwnProperty(desc.getNameToken(CallSiteDescriptor.NAME_OPERAND))) {
        try {
            final GuardedInvocation inv = super.findSetMethod(desc, request);
            if (inv != null) {
                return inv;
            }
        } catch (final Exception e) {
            //ignored
        }
    }

    return findHook(desc, __put__);
}
 
Example #26
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 #27
Source Project: nashorn   Author: ynsn   File: NashornStaticClassLinker.java    License: 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 #28
Source Project: TencentKona-8   Author: Tencent   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 #29
Source Project: jdk8u_nashorn   Author: JetBrains   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 #30
Source Project: hottub   Author: dsrg-uoft   File: StaticClassLinker.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 Object receiver = request.getReceiver();
    if(receiver instanceof StaticClass) {
        return linkers.get(((StaticClass)receiver).getRepresentedClass()).getGuardedInvocation(request,
                linkerServices);
    }
    return null;
}