Search in sources :

Example 51 with JavaType

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

the class VM method JVM_FindClassFromCaller.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class.class)
public StaticObject JVM_FindClassFromCaller(@Pointer TruffleObject namePtr, boolean init, @JavaType(ClassLoader.class) StaticObject loader, @JavaType(Class.class) StaticObject caller) {
    Meta meta = getMeta();
    Symbol<Type> type = namePtrToInternal(namePtr);
    Klass result;
    if (Types.isPrimitive(type)) {
        result = null;
    } else {
        StaticObject protectionDomain;
        // manager to avoid the performance cost of getting the calling class.
        if (!StaticObject.isNull(caller) && !StaticObject.isNull(loader)) {
            protectionDomain = JVM_GetProtectionDomain(caller);
        } else {
            protectionDomain = StaticObject.NULL;
        }
        result = meta.resolveSymbolOrNull(type, loader, protectionDomain);
    }
    if (result == null) {
        throw meta.throwExceptionWithMessage(meta.java_lang_ClassNotFoundException, NativeUtils.interopPointerToString(namePtr));
    }
    if (init) {
        result.safeInitialize();
    }
    return result.mirror();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) 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) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 52 with JavaType

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

the class VM method JVM_FindClassFromBootLoader.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class.class)
public StaticObject JVM_FindClassFromBootLoader(@Pointer TruffleObject namePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    if (name == null) {
        return StaticObject.NULL;
    }
    String internalName = name;
    if (!name.startsWith("[")) {
        // Force 'L' type.
        internalName = "L" + name + ";";
    }
    if (!Validation.validTypeDescriptor(ByteSequence.create(internalName), false)) {
        return StaticObject.NULL;
    }
    Symbol<Type> type = getTypes().fromClassGetName(internalName);
    if (Types.isPrimitive(type)) {
        return StaticObject.NULL;
    }
    Klass klass = getMeta().resolveSymbolOrNull(type, StaticObject.NULL, StaticObject.NULL);
    if (klass == null) {
        return StaticObject.NULL;
    }
    return klass.mirror();
}
Also used : NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 53 with JavaType

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

the class JniEnv method ToReflectedMethod.

// endregion Register/Unregister natives
// region Reflection
/**
 * <h3>jobject ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID methodID, jboolean
 * isStatic);</h3>
 * <p>
 * Converts a method ID derived from cls to a java.lang.reflect.Method or
 * java.lang.reflect.Constructor object. isStatic must be set to JNI_TRUE if the method ID
 * refers to a static field, and JNI_FALSE otherwise.
 * <p>
 * Throws OutOfMemoryError and returns 0 if fails.
 */
@JniImpl
@JavaType(java.lang.reflect.Executable.class)
public StaticObject ToReflectedMethod(@JavaType(Class.class) StaticObject unused, @Handle(Method.class) long methodId, @SuppressWarnings("unused") boolean isStatic) {
    Method method = methodIds.getObject(methodId);
    assert method.getDeclaringKlass().isAssignableFrom(unused.getMirrorKlass());
    StaticObject methods = null;
    if (method.isConstructor()) {
        methods = getVM().JVM_GetClassDeclaredConstructors(method.getDeclaringKlass().mirror(), false);
    } else {
        methods = getVM().JVM_GetClassDeclaredMethods(method.getDeclaringKlass().mirror(), false);
    }
    for (StaticObject declMethod : methods.<StaticObject[]>unwrap()) {
        assert InterpreterToVM.instanceOf(declMethod, getMeta().java_lang_reflect_Executable);
        Method m = null;
        if (method.isConstructor()) {
            assert InterpreterToVM.instanceOf(declMethod, getMeta().java_lang_reflect_Constructor);
            m = (Method) getMeta().HIDDEN_CONSTRUCTOR_KEY.getHiddenObject(declMethod);
        } else {
            assert InterpreterToVM.instanceOf(declMethod, getMeta().java_lang_reflect_Method);
            m = (Method) getMeta().HIDDEN_METHOD_KEY.getHiddenObject(declMethod);
        }
        if (method == m) {
            return declMethod;
        }
    }
    throw EspressoError.shouldNotReachHere("Method/constructor not found ", method);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 54 with JavaType

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

the class JniEnv method GetStaticFieldID.

/**
 * <h3>jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig);
 * </h3>
 * <p>
 * Returns the field ID for a static field of a class. The field is specified by its name and
 * signature. The GetStatic<type>Field and SetStatic<type>Field families of accessor functions
 * use field IDs to retrieve static fields.
 * <p>
 * GetStaticFieldID() causes an uninitialized class to be initialized.
 *
 * @param clazz a Java class object.
 * @param namePtr the static field name in a 0-terminated modified UTF-8 string.
 * @param typePtr the field signature in a 0-terminated modified UTF-8 string.
 * @return a field ID, or NULL if the specified static field cannot be found.
 * @throws NoSuchFieldError if the specified static field cannot be found.
 * @throws ExceptionInInitializerError if the class initializer fails due to an exception.
 * @throws OutOfMemoryError if the system runs out of memory.
 */
@JniImpl
@Handle(Field.class)
public long GetStaticFieldID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject typePtr) {
    String name = NativeUtils.interopPointerToString(namePtr);
    String type = NativeUtils.interopPointerToString(typePtr);
    assert name != null && type != null;
    Field field = null;
    Symbol<Name> fieldName = getNames().lookup(name);
    if (fieldName != null) {
        Symbol<Type> fieldType = getTypes().lookup(type);
        if (fieldType != null) {
            Klass klass = clazz.getMirrorKlass();
            klass.safeInitialize();
            // Lookup only if name and type are known symbols.
            field = klass.lookupField(fieldName, fieldType, Klass.LookupMode.STATIC_ONLY);
            assert field == null || field.getType().equals(fieldType);
        }
    }
    if (field == null || !field.isStatic()) {
        Meta meta = getMeta();
        throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchFieldError, name);
    }
    return fieldIds.handlify(field);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) Meta(com.oracle.truffle.espresso.meta.Meta) NativeType(com.oracle.truffle.espresso.ffi.NativeType) Type(com.oracle.truffle.espresso.descriptors.Symbol.Type) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 55 with JavaType

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

the class JniEnv method NewDirectByteBuffer.

// endregion Release*ArrayElements
// region DirectBuffers
/**
 * <h3>jobject NewDirectByteBuffer(JNIEnv* env, void* address, jlong capacity);</h3>
 * <p>
 * Allocates and returns a direct java.nio.ByteBuffer referring to the block of memory starting
 * at the memory address address and extending capacity bytes.
 * <p>
 * Native code that calls this function and returns the resulting byte-buffer object to
 * Java-level code should ensure that the buffer refers to a valid region of memory that is
 * accessible for reading and, if appropriate, writing. An attempt to access an invalid memory
 * location from Java code will either return an arbitrary value, have no visible effect, or
 * cause an unspecified exception to be thrown.
 *
 * @param addressPtr the starting address of the memory region (must not be NULL)
 * @param capacity the size in bytes of the memory region (must be positive)
 * @return a local reference to the newly-instantiated java.nio.ByteBuffer object. Returns NULL
 *         if an exception occurs, or if JNI access to direct buffers is not supported by this
 *         virtual machine.
 * @throws OutOfMemoryError if allocation of the ByteBuffer object fails
 */
@JniImpl
@JavaType(internalName = "Ljava/nio/DirectByteBuffer;")
public StaticObject NewDirectByteBuffer(@Pointer TruffleObject addressPtr, long capacity) {
    Meta meta = getMeta();
    StaticObject instance = meta.java_nio_DirectByteBuffer.allocateInstance();
    long address = NativeUtils.interopAsPointer(addressPtr);
    meta.java_nio_DirectByteBuffer_init_long_int.invokeDirect(instance, address, (int) capacity);
    return instance;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) 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