Search in sources :

Example 41 with JavaType

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

the class VM method JVM_GetRecordComponents.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(internalName = "[Ljava/lang/reflect/RecordComponent;")
public StaticObject JVM_GetRecordComponents(@JavaType(Class.class) StaticObject self) {
    Klass k = self.getMirrorKlass();
    if (!(k instanceof ObjectKlass)) {
        return StaticObject.NULL;
    }
    ObjectKlass klass = (ObjectKlass) k;
    RecordAttribute record = (RecordAttribute) klass.getAttribute(RecordAttribute.NAME);
    if (record == null) {
        return StaticObject.NULL;
    }
    RecordAttribute.RecordComponentInfo[] components = record.getComponents();
    return getMeta().java_lang_reflect_RecordComponent.allocateReferenceArray(components.length, (i) -> components[i].toGuestComponent(getMeta(), klass));
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 42 with JavaType

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

the class VM method JVM_GetVmArguments.

/**
 * Returns all values declared to the {@link EspressoOptions#VMArguments} option during context
 * creation.
 * <p>
 * In practice, this should be the list of arguments passed to the context, but depending on who
 * built it, it may be any arbitrary list of Strings.
 * <p>
 * Note that even if that's the case, it may differs slightly from the expected list of
 * arguments. The Java world expects this to be the arguments passed to the VM creation, which
 * is expected to have passed through the regular java launcher, and have been de-sugarified
 * (/ex: '-m [module]' -> '-Djdk.module.main=[module]').
 * <p>
 * In the Java-on-Truffle case, the VM arguments and the context builder options are equivalent,
 * but that is not true in the regular Espresso launcher case, or in an embedding scenario.
 */
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(String[].class)
public StaticObject JVM_GetVmArguments() {
    String[] vmArgs = getContext().getVmArguments();
    StaticObject array = getMeta().java_lang_String.allocateReferenceArray(vmArgs.length);
    for (int i = 0; i < vmArgs.length; i++) {
        getInterpreterToVM().setArrayObject(getMeta().toGuestString(vmArgs[i]), i, array);
    }
    return array;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 43 with JavaType

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

the class VM method JVM_GetClassDeclaredConstructors.

// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(Constructor[].class)
public StaticObject JVM_GetClassDeclaredConstructors(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    Meta meta = getMeta();
    ArrayList<Method> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Method m : klass.getDeclaredConstructors()) {
        if (Name._init_.equals(m.getName()) && (!publicOnly || m.isPublic())) {
            collectedMethods.add(m);
        }
    }
    final Method[] constructors = collectedMethods.toArray(Method.EMPTY_ARRAY);
    EspressoContext context = meta.getContext();
    // TODO(peterssen): Cache guest j.l.reflect.Constructor constructor.
    // Calling the constructor is just for validation, manually setting the fields would be
    // faster.
    Method constructorInit = meta.java_lang_reflect_Constructor.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
    Type.java_lang_Class, /* parameterTypes */
    Type.java_lang_Class_array, /* checkedExceptions */
    Type.java_lang_Class_array, /* modifiers */
    Type._int, /* slot */
    Type._int, /* signature */
    Type.java_lang_String, /* annotations */
    Type._byte_array, /* parameterAnnotations */
    Type._byte_array));
    StaticObject arr = meta.java_lang_reflect_Constructor.allocateReferenceArray(constructors.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            final Method m = constructors[i];
            Attribute rawRuntimeVisibleAnnotations = m.getAttribute(Name.RuntimeVisibleAnnotations);
            StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleParameterAnnotations = m.getAttribute(Name.RuntimeVisibleParameterAnnotations);
            StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleTypeAnnotations = m.getAttribute(Name.RuntimeVisibleTypeAnnotations);
            StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
            final Klass[] rawParameterKlasses = m.resolveParameterKlasses();
            StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(m.getParameterCount(), new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawParameterKlasses[j].mirror();
                }
            });
            final Klass[] rawCheckedExceptions = m.getCheckedExceptions();
            StaticObject checkedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {

                @Override
                public StaticObject apply(int j) {
                    return rawCheckedExceptions[j].mirror();
                }
            });
            SignatureAttribute signatureAttribute = (SignatureAttribute) m.getAttribute(Name.Signature);
            StaticObject genericSignature = StaticObject.NULL;
            if (signatureAttribute != null) {
                String sig = m.getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
                genericSignature = meta.toGuestString(sig);
            }
            StaticObject instance = meta.java_lang_reflect_Constructor.allocateInstance();
            constructorInit.invokeDirect(/* this */
            instance, /* declaringKlass */
            m.getDeclaringKlass().mirror(), /* parameterTypes */
            parameterTypes, /* checkedExceptions */
            checkedExceptions, /* modifiers */
            m.getMethodModifiers(), // TODO(peterssen): Fill method slot.
            i, /* signature */
            genericSignature, /* annotations */
            runtimeVisibleAnnotations, /* parameterAnnotations */
            runtimeVisibleParameterAnnotations);
            meta.HIDDEN_CONSTRUCTOR_KEY.setHiddenObject(instance, m);
            meta.HIDDEN_CONSTRUCTOR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
            return instance;
        }
    });
    return arr;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) MethodParametersAttribute(com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute) EnclosingMethodAttribute(com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute) Attribute(com.oracle.truffle.espresso.runtime.Attribute) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) RecordAttribute(com.oracle.truffle.espresso.classfile.attributes.RecordAttribute) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) ArrayList(java.util.ArrayList) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Method(com.oracle.truffle.espresso.impl.Method) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) SignatureAttribute(com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute) 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) IntFunction(java.util.function.IntFunction) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 44 with JavaType

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

the class VM method JVM_GetNestMembers.

@VmImpl(isJni = true)
@JavaType(Class[].class)
public StaticObject JVM_GetNestMembers(@JavaType(Class.class) StaticObject current) {
    Klass k = current.getMirrorKlass();
    Klass[] nestMembers = k.getNestMembers();
    StaticObject[] array = new StaticObject[nestMembers.length];
    for (int i = 0; i < nestMembers.length; i++) {
        array[i] = nestMembers[i].mirror();
    }
    return StaticObject.createArray(getMeta().java_lang_Class_array, array);
}
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 45 with JavaType

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

the class VM method JVM_Clone.

@VmImpl(isJni = true)
@JavaType(Object.class)
public static StaticObject JVM_Clone(@JavaType(Object.class) StaticObject self, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    assert StaticObject.notNull(self);
    char exceptionBranch = 3;
    if (self.isArray()) {
        // Arrays are always cloneable.
        if (self.isForeignObject()) {
            return cloneForeignArray(self, meta, InteropLibrary.getUncached(self.rawForeignObject()), ToEspressoNodeGen.getUncached(), profiler, exceptionBranch);
        }
        return self.copy();
    }
    if (self.isForeignObject()) {
        profiler.profile(exceptionBranch);
        throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, "Clone not supported for non-array foreign objects");
    }
    if (!meta.java_lang_Cloneable.isAssignableFrom(self.getKlass())) {
        profiler.profile(0);
        throw meta.throwException(meta.java_lang_CloneNotSupportedException);
    }
    if (InterpreterToVM.instanceOf(self, meta.java_lang_ref_Reference)) {
        // Non-strong references are not cloneable.
        if (// 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_WeakReference) || // 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_SoftReference) || // 
        InterpreterToVM.instanceOf(self, meta.java_lang_ref_FinalReference) || InterpreterToVM.instanceOf(self, meta.java_lang_ref_PhantomReference)) {
            profiler.profile(1);
            throw meta.throwExceptionWithMessage(meta.java_lang_CloneNotSupportedException, self.getKlass().getName().toString());
        }
    }
    final StaticObject clone = self.copy();
    // If the original object is finalizable, so is the copy.
    assert self.getKlass() instanceof ObjectKlass;
    if (((ObjectKlass) self.getKlass()).hasFinalizer()) {
        profiler.profile(2);
        meta.java_lang_ref_Finalizer_register.invokeDirect(null, clone);
    }
    return clone;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) 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