Search in sources :

Example 6 with EspressoContext

use of com.oracle.truffle.espresso.runtime.EspressoContext in project graal by oracle.

the class DestroyVMNode method execute.

@Override
public Object execute(VirtualFrame frame) {
    assert frame.getArguments().length == 0;
    EspressoContext context = EspressoContext.get(this);
    // Throws an exit exception.
    context.destroyVM(true);
    throw EspressoError.shouldNotReachHere();
}
Also used : EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext)

Example 7 with EspressoContext

use of com.oracle.truffle.espresso.runtime.EspressoContext in project graal by oracle.

the class VM method JVM_GetClassDeclaredFields.

@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Field[].class)
public StaticObject JVM_GetClassDeclaredFields(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
    // TODO(peterssen): From Hostpot: 4496456 We need to filter out
    // java.lang.Throwable.backtrace.
    Meta meta = getMeta();
    ArrayList<Field> collectedMethods = new ArrayList<>();
    Klass klass = self.getMirrorKlass();
    klass.ensureLinked();
    for (Field f : klass.getDeclaredFields()) {
        if (!publicOnly || f.isPublic()) {
            collectedMethods.add(f);
        }
    }
    final Field[] fields = collectedMethods.toArray(Field.EMPTY_ARRAY);
    EspressoContext context = meta.getContext();
    // TODO(peterssen): Cache guest j.l.reflect.Field constructor.
    // Calling the constructor is just for validation, manually setting the fields would be
    // faster.
    Method fieldInit;
    if (meta.getJavaVersion().java15OrLater()) {
        fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
        Type.java_lang_Class, /* name */
        Type.java_lang_String, /* type */
        Type.java_lang_Class, /* modifiers */
        Type._int, /* trustedFinal */
        Type._boolean, /* slot */
        Type._int, /* signature */
        Type.java_lang_String, /* annotations */
        Type._byte_array));
    } else {
        fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
        Type.java_lang_Class, /* name */
        Type.java_lang_String, /* type */
        Type.java_lang_Class, /* modifiers */
        Type._int, /* slot */
        Type._int, /* signature */
        Type.java_lang_String, /* annotations */
        Type._byte_array));
    }
    StaticObject fieldsArray = meta.java_lang_reflect_Field.allocateReferenceArray(fields.length, new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int i) {
            final Field f = fields[i];
            StaticObject instance = meta.java_lang_reflect_Field.allocateInstance();
            Attribute rawRuntimeVisibleAnnotations = f.getAttribute(Name.RuntimeVisibleAnnotations);
            StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
            Attribute rawRuntimeVisibleTypeAnnotations = f.getAttribute(Name.RuntimeVisibleTypeAnnotations);
            StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
            if (meta.getJavaVersion().java15OrLater()) {
                fieldInit.invokeDirect(/* this */
                instance, /* declaringKlass */
                f.getDeclaringKlass().mirror(), /* name */
                context.getStrings().intern(f.getName()), /* type */
                f.resolveTypeKlass().mirror(), /* modifiers */
                f.getModifiers(), /* trustedFinal */
                f.isTrustedFinal(), /* slot */
                f.getSlot(), /* signature */
                meta.toGuestString(f.getGenericSignature()), /* annotations */
                runtimeVisibleAnnotations);
            } else {
                fieldInit.invokeDirect(/* this */
                instance, /* declaringKlass */
                f.getDeclaringKlass().mirror(), /* name */
                context.getStrings().intern(f.getName()), /* type */
                f.resolveTypeKlass().mirror(), /* modifiers */
                f.getModifiers(), /* slot */
                f.getSlot(), /* signature */
                meta.toGuestString(f.getGenericSignature()), /* annotations */
                runtimeVisibleAnnotations);
            }
            meta.HIDDEN_FIELD_KEY.setHiddenObject(instance, f);
            meta.HIDDEN_FIELD_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
            return instance;
        }
    });
    return fieldsArray;
}
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) Field(com.oracle.truffle.espresso.impl.Field) 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)

Example 8 with EspressoContext

use of com.oracle.truffle.espresso.runtime.EspressoContext 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 9 with EspressoContext

use of com.oracle.truffle.espresso.runtime.EspressoContext in project graal by oracle.

the class Target_sun_misc_Unsafe method park.

/**
 * Block current thread, returning when a balancing <tt>unpark</tt> occurs, or a balancing
 * <tt>unpark</tt> has already occurred, or the thread is interrupted, or, if not absolute and
 * time is not zero, the given time nanoseconds have elapsed, or if absolute, the given deadline
 * in milliseconds since Epoch has passed, or spuriously (i.e., returning for no "reason").
 * Note: This operation is in the Unsafe class only because <tt>unpark</tt> is, so it would be
 * strange to place it elsewhere.
 */
@TruffleBoundary
@Substitution(hasReceiver = true)
@SuppressWarnings("try")
public static void park(@JavaType(Unsafe.class) StaticObject self, boolean isAbsolute, long time, @Inject Meta meta) {
    if (time < 0 || (isAbsolute && time == 0)) {
        // don't wait at all
        return;
    }
    EspressoContext context = meta.getContext();
    StaticObject thread = context.getCurrentThread();
    if (meta.getThreadAccess().isInterrupted(thread, false)) {
        return;
    }
    Unsafe unsafe = UnsafeAccess.getIfAllowed(meta);
    Thread hostThread = Thread.currentThread();
    Object blocker = LockSupport.getBlocker(hostThread);
    State state = time > 0 ? State.TIMED_WAITING : State.WAITING;
    try (Transition transition = Transition.transition(context, state)) {
        Field parkBlocker = meta.java_lang_Thread.lookupDeclaredField(Symbol.Name.parkBlocker, Type.java_lang_Object);
        StaticObject guestBlocker = parkBlocker.getObject(thread);
        // LockSupport.park(/* guest blocker */);
        if (!StaticObject.isNull(guestBlocker)) {
            unsafe.putObject(hostThread, PARK_BLOCKER_OFFSET, guestBlocker);
        }
        parkBoundary(self, isAbsolute, time, meta);
    }
    unsafe.putObject(hostThread, PARK_BLOCKER_OFFSET, blocker);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) State(com.oracle.truffle.espresso.threads.State) Transition(com.oracle.truffle.espresso.threads.Transition) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Unsafe(sun.misc.Unsafe) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 10 with EspressoContext

use of com.oracle.truffle.espresso.runtime.EspressoContext in project graal by oracle.

the class EspressoLanguage method createContext.

@Override
protected EspressoContext createContext(final TruffleLanguage.Env env) {
    // TODO(peterssen): Redirect in/out to env.in()/out()
    EspressoContext context = new EspressoContext(env, this);
    context.setMainArguments(env.getApplicationArguments());
    return context;
}
Also used : EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext)

Aggregations

EspressoContext (com.oracle.truffle.espresso.runtime.EspressoContext)10 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)6 Klass (com.oracle.truffle.espresso.impl.Klass)3 Method (com.oracle.truffle.espresso.impl.Method)3 Meta (com.oracle.truffle.espresso.meta.Meta)3 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)2 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)2 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)2 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)2 PermittedSubclassesAttribute (com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute)2 RecordAttribute (com.oracle.truffle.espresso.classfile.attributes.RecordAttribute)2 SignatureAttribute (com.oracle.truffle.espresso.classfile.attributes.SignatureAttribute)2 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)2 Field (com.oracle.truffle.espresso.impl.Field)2 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)2 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)2 Attribute (com.oracle.truffle.espresso.runtime.Attribute)2 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)2 State (com.oracle.truffle.espresso.threads.State)2 Transition (com.oracle.truffle.espresso.threads.Transition)2