Search in sources :

Example 21 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method JVM_GetClassInterfaces.

@VmImpl(isJni = true)
@JavaType(Class[].class)
public StaticObject JVM_GetClassInterfaces(@JavaType(Class.class) StaticObject self) {
    final Klass[] superInterfaces = self.getMirrorKlass().getImplementedInterfaces();
    StaticObject instance = getMeta().java_lang_Class.allocateReferenceArray(superInterfaces.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            return superInterfaces[i].mirror();
        }
    });
    return instance;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 22 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method JVM_GetArrayElement.

/**
 * Returns the value of the indexed component in the specified array object. The value is
 * automatically wrapped in an object if it has a primitive type.
 *
 * @param array the array
 * @param index the index
 * @throws NullPointerException If the specified object is null
 * @throws IllegalArgumentException If the specified object is not an array
 * @throws ArrayIndexOutOfBoundsException If the specified {@code index} argument is negative,
 *             or if it is greater than or equal to the length of the specified array
 * @return the (possibly wrapped) value of the indexed component in the specified array
 */
@VmImpl(isJni = true)
@JavaType(Object.class)
public StaticObject JVM_GetArrayElement(@JavaType(Object.class) StaticObject array, int index, @Inject SubstitutionProfiler profiler) {
    Meta meta = getMeta();
    if (StaticObject.isNull(array)) {
        profiler.profile(7);
        throw meta.throwNullPointerException();
    }
    if (array.isArray()) {
        profiler.profile(6);
        return getInterpreterToVM().getArrayObject(index, array);
    }
    if (!array.getClass().isArray()) {
        profiler.profile(5);
        throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, "Argument is not an array");
    }
    assert array.getClass().isArray() && array.getClass().getComponentType().isPrimitive();
    if (index < 0 || index >= JVM_GetArrayLength(array, profiler)) {
        profiler.profile(4);
        throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, "index");
    }
    Object elem = Array.get(array, index);
    return getMeta().boxPrimitive(elem);
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 23 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method JVM_GetCallerClass.

@VmImpl(isJni = true)
@JavaType(Class.class)
public StaticObject JVM_GetCallerClass(int depth, @Inject SubstitutionProfiler profiler) {
    // temporarily for existing code to use until a replacement API is defined.
    if (depth != JVM_CALLER_DEPTH) {
        FrameInstance callerFrame = getCallerFrame(depth, true, getMeta());
        if (callerFrame != null) {
            Method callerMethod = getMethodFromFrame(callerFrame);
            if (callerMethod != null) {
                return callerMethod.getDeclaringKlass().mirror();
            }
        }
        // Not found.
        return StaticObject.NULL;
    }
    // Getting the class of the caller frame.
    // 
    // The call stack at this point looks something like this:
    // 
    // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
    // [1] [ @CallerSensitive API.method ]
    // [.] [ (skipped intermediate frames) ]
    // [n] [ caller ]
    Meta meta = getMeta();
    StaticObject[] exception = new StaticObject[] { null };
    Method callerMethod = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Method>() {

        private int depth = 0;

        @SuppressWarnings("fallthrough")
        @Override
        public Method visitFrame(FrameInstance frameInstance) {
            Method method = getMethodFromFrame(frameInstance);
            if (method != null) {
                switch(depth) {
                    case 0:
                        // Reflection.getCallerClass.
                        if (method != meta.sun_reflect_Reflection_getCallerClass) {
                            exception[0] = Meta.initExceptionWithMessage(meta.java_lang_InternalError, "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
                            return /* ignore */
                            method;
                        }
                    // fall-through
                    case 1:
                        // Frame 0 and 1 must be caller sensitive.
                        if (!method.isCallerSensitive()) {
                            exception[0] = Meta.initExceptionWithMessage(meta.java_lang_InternalError, "CallerSensitive annotation expected at frame " + depth);
                            return /* ignore */
                            method;
                        }
                        break;
                    default:
                        if (!isIgnoredBySecurityStackWalk(method, meta)) {
                            return method;
                        }
                }
                ++depth;
            }
            return null;
        }
    });
    // InternalError was recorded.
    StaticObject internalError = exception[0];
    if (internalError != null) {
        profiler.profile(0);
        assert InterpreterToVM.instanceOf(internalError, meta.java_lang_InternalError);
        throw meta.throwException(internalError);
    }
    if (callerMethod == null) {
        return StaticObject.NULL;
    }
    return callerMethod.getDeclaringKlass().mirror();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 24 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class Management method GetMemoryUsage.

@ManagementImpl
@JavaType(Object.class)
public StaticObject GetMemoryUsage(@SuppressWarnings("unused") boolean heap) {
    Method init = getMeta().java_lang_management_MemoryUsage.lookupDeclaredMethod(Symbol.Name._init_, getSignatures().makeRaw(Symbol.Type._void, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long, Symbol.Type._long));
    StaticObject instance = getMeta().java_lang_management_MemoryUsage.allocateInstance();
    init.invokeDirect(instance, 0L, 0L, 0L, 0L);
    return instance;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 25 with JavaType

use of com.oracle.truffle.espresso.substitutions.JavaType in project graal by oracle.

the class VM method getGuestReflectiveMethodRoot.

// endregion stack inspection
// region annotations
@JavaType(java.lang.reflect.Method.class)
private static StaticObject getGuestReflectiveMethodRoot(@JavaType(java.lang.reflect.Method.class) StaticObject seed, Meta meta) {
    assert InterpreterToVM.instanceOf(seed, meta.java_lang_reflect_Method);
    StaticObject curMethod = seed;
    Method target = null;
    while (target == null) {
        target = (Method) meta.HIDDEN_METHOD_KEY.getHiddenObject(curMethod);
        if (target == null) {
            curMethod = meta.java_lang_reflect_Method_root.getObject(curMethod);
        }
    }
    return curMethod;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Aggregations

JavaType (com.oracle.truffle.espresso.substitutions.JavaType)58 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)44 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)28 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)24 Klass (com.oracle.truffle.espresso.impl.Klass)24 Meta (com.oracle.truffle.espresso.meta.Meta)20 Method (com.oracle.truffle.espresso.impl.Method)19 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)15 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)13 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)8 NativeType (com.oracle.truffle.espresso.ffi.NativeType)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)5 Field (com.oracle.truffle.espresso.impl.Field)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)4 ArrayList (java.util.ArrayList)4 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3