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();
}
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;
}
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;
}
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);
}
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;
}
Aggregations