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 File: BeansLinker.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final 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 #2
Source File: NashornBottomLinker.java    From openjdk-jdk8u-backup with 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 #3
Source File: NashornLinker.java    From openjdk-jdk8u-backup with 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 #4
Source File: BeansLinker.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices)
        throws Exception {
    final 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 #5
Source File: NativeJavaPackage.java    From openjdk-8-source with 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 File: JSObjectLinker.java    From openjdk-jdk8u-backup 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();

    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 #7
Source File: NashornLinker.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest request, final LinkerServices linkerServices) throws Exception {
    final LinkRequest requestWithoutContext = request.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = requestWithoutContext.getReceiver();
    final CallSiteDescriptor desc = requestWithoutContext.getCallSiteDescriptor();

    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 #8
Source File: JSObjectLinker.java    From openjdk-8 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 #9
Source File: TypeConverterFactory.java    From openjdk-8-source with 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 #10
Source File: NashornLinker.java    From openjdk-8-source with 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 #11
Source File: JSObjectLinker.java    From jdk8u60 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();

    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 #12
Source File: NashornBottomLinker.java    From openjdk-8 with 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 #13
Source File: ContinuousArrayData.java    From jdk8u60 with 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 #14
Source File: Undefined.java    From nashorn with 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 #15
Source File: StaticClassLinker.java    From openjdk-8-source with 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 #16
Source File: LinkerServicesImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest) throws Exception {
    final LinkRequest prevLinkRequest = threadLinkRequest.get();
    threadLinkRequest.set(linkRequest);
    try {
        return topLevelLinker.getGuardedInvocation(linkRequest, this);
    } finally {
        threadLinkRequest.set(prevLinkRequest);
    }
}
 
Example #17
Source File: NashornPrimitiveLinker.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation of type converter factory will pretty much allow implicit conversions of anything to anything
 * else that's allowed among JavaScript primitive types (string to number, boolean to string, etc.)
 * @param sourceType the type to convert from
 * @param targetType the type to convert to
 * @return a conditional converter from source to target type
 */
@Override
public GuardedInvocation convertToType(final Class<?> sourceType, final Class<?> targetType) {
    final MethodHandle mh = JavaArgumentConverters.getConverter(targetType);
    if (mh == null) {
        return null;
    }

    return new GuardedInvocation(mh, canLinkTypeStatic(sourceType) ? null : GUARD_PRIMITIVE).asType(mh.type().changeParameterType(0, sourceType));
}
 
Example #18
Source File: ArrayBufferView.java    From hottub with 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 File: ScriptObject.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup method that, given a CallSiteDescriptor, looks up the target
 * MethodHandle and creates a GuardedInvocation
 * with the appropriate guard(s).
 *
 * @param desc call site descriptor
 * @param request the link request
 *
 * @return GuardedInvocation for the callsite
 */
public GuardedInvocation lookup(final CallSiteDescriptor desc, final LinkRequest request) {
    final int c = desc.getNameTokenCount();
    // JavaScript is "immune" to all currently defined Dynalink composite operation - getProp is the same as getElem
    // is the same as getMethod as JavaScript objects have a single namespace for all three. Therefore, we don't
    // care about them, and just link to whatever is the first operation.
    final String operator = CallSiteDescriptorFactory.tokenizeOperators(desc).get(0);
    // 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.
    switch (operator) {
    case "getProp":
    case "getElem":
    case "getMethod":
        return c > 2 ? findGetMethod(desc, request, operator) : findGetIndexMethod(desc, request);
    case "setProp":
    case "setElem":
        return c > 2 ? findSetMethod(desc, request) : findSetIndexMethod(desc, request);
    case "call":
        return findCallMethod(desc, request);
    case "new":
        return findNewMethod(desc, request);
    case "callMethod":
        return findCallMethodMethod(desc, request);
    default:
        return null;
    }
}
 
Example #20
Source File: NashornLinker.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static GuardedInvocation getMirrorConverter(final Class<?> sourceType, final Class<?> targetType) {
    // Could've also used (targetType.isAssignableFrom(ScriptObjectMirror.class) && targetType != Object.class) but
    // it's probably better to explicitly spell out the supported target types
    if (targetType == Map.class || targetType == Bindings.class || targetType == JSObject.class || targetType == ScriptObjectMirror.class) {
        if (ScriptObject.class.isAssignableFrom(sourceType)) {
            return new GuardedInvocation(CREATE_MIRROR);
        } else if (sourceType.isAssignableFrom(ScriptObject.class) || sourceType.isInterface()) {
            return new GuardedInvocation(CREATE_MIRROR, IS_SCRIPT_OBJECT);
        }
    }
    return null;
}
 
Example #21
Source File: NashornBeansLinker.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final Object self = linkRequest.getReceiver();
    final CallSiteDescriptor desc = linkRequest.getCallSiteDescriptor();
    if (self instanceof ConsString) {
        // In order to treat ConsString like a java.lang.String we need a link request with a string receiver.
        final Object[] arguments = linkRequest.getArguments();
        arguments[0] = "";
        final LinkRequest forgedLinkRequest = linkRequest.replaceArguments(desc, arguments);
        final GuardedInvocation invocation = getGuardedInvocation(beansLinker, forgedLinkRequest, linkerServices);
        // If an invocation is found we add a filter that makes it work for both Strings and ConsStrings.
        return invocation == null ? null : invocation.filterArguments(0, FILTER_CONSSTRING);
    }

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

            // drop 'thiz' passed from the script.
            return gi.replaceMethods(
                MH.dropArguments(linkerServices.filterInternalObjects(gi.getInvocation()), 1, callType.parameterType(1)),
                gi.getGuard());
        }
    }
    return getGuardedInvocation(beansLinker, linkRequest, linkerServices);
}
 
Example #22
Source File: Global.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lookup helper for JS primitive types
 *
 * @param request the link request for the dynamic call site.
 * @param self     self reference
 *
 * @return guarded invocation
 */
public static GuardedInvocation primitiveLookup(final LinkRequest request, final Object self) {
    if (isString(self)) {
        return NativeString.lookupPrimitive(request, self);
    } else if (self instanceof Number) {
        return NativeNumber.lookupPrimitive(request, self);
    } else if (self instanceof Boolean) {
        return NativeBoolean.lookupPrimitive(request, self);
    }
    throw new IllegalArgumentException("Unsupported primitive: " + self);
}
 
Example #23
Source File: ScriptObject.java    From openjdk-jdk8u-backup with 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 File: NashornPrimitiveLinker.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest origRequest, final LinkerServices linkerServices)
        throws Exception {
    final LinkRequest request = origRequest.withoutRuntimeContext(); // Nashorn has no runtime context

    final Object self = request.getReceiver();
    final NashornCallSiteDescriptor desc = (NashornCallSiteDescriptor) request.getCallSiteDescriptor();

    return Bootstrap.asTypeSafeReturn(Global.primitiveLookup(request, self), linkerServices, desc);
}
 
Example #25
Source File: StaticClassLinker.java    From jdk8u60 with 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;
}
 
Example #26
Source File: StaticClassLinker.java    From hottub with 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;
}
 
Example #27
Source File: NativeJSAdapter.java    From jdk8u_nashorn with 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 #28
Source File: CompositeTypeBasedGuardingDynamicLinker.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@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 #29
Source File: NashornStaticClassLinker.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();
    Bootstrap.checkReflectionAccess(receiverClass);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass.getName());

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null);
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #30
Source File: NashornLinker.java    From hottub with 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);
}