Skip to content

Commit

Permalink
[GR-49874] Support for espresso exception interop type converters for…
Browse files Browse the repository at this point in the history
… common host JDK exceptions.

PullRequest: graal/15997
  • Loading branch information
javeleon committed Nov 10, 2023
2 parents 20e401b + 57249f7 commit d24879f
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ public static void ensureInitialized() {
public static final Symbol<Type> java_lang_UnsupportedOperationException = StaticSymbols.putType("Ljava/lang/UnsupportedOperationException;");
public static final Symbol<Type> java_lang_UnsupportedClassVersionError = StaticSymbols.putType("Ljava/lang/UnsupportedClassVersionError;");
public static final Symbol<Type> java_lang_reflect_InvocationTargetException = StaticSymbols.putType("Ljava/lang/reflect/InvocationTargetException;");
public static final Symbol<Type> java_lang_NumberFormatException = StaticSymbols.putType("Ljava/lang/NumberFormatException;");

public static final Symbol<Type> java_lang_Thread = StaticSymbols.putType("Ljava/lang/Thread;");
public static final Symbol<Type> java_lang_Thread_FieldHolder = StaticSymbols.putType("Ljava/lang/Thread$FieldHolder;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,23 @@ abstract static class ReadMember {
static Object doShared(Klass receiver, String member,
@CachedLibrary("receiver") InteropLibrary lib,
@Bind("getLang(lib)") @SuppressWarnings("unused") EspressoLanguage language) throws UnknownIdentifierException {
return readMember(receiver, member, LookupFieldNodeGen.getUncached(), LookupDeclaredMethodNodeGen.getUncached(), BranchProfile.getUncached(), lib);
return readMember(receiver, member, LookupFieldNodeGen.getUncached(), LookupDeclaredMethodNodeGen.getUncached(), BranchProfile.getUncached(), lib, language);
}

@Specialization
static Object readMember(Klass receiver, String member,
@Shared("lookupField") @Cached LookupFieldNode lookupFieldNode,
@Shared("lookupMethod") @Cached LookupDeclaredMethod lookupMethod,
@Shared("error") @Cached BranchProfile error,
@CachedLibrary("receiver") InteropLibrary lib) throws UnknownIdentifierException {
@CachedLibrary("receiver") InteropLibrary lib,
@Bind("getLang(lib)") @SuppressWarnings("unused") EspressoLanguage language) throws UnknownIdentifierException {
EspressoContext ctx = EspressoContext.get(lib);
Meta meta = ctx.getMeta();
Field field = lookupFieldNode.execute(receiver, member, true);
if (field != null) {
Object result = field.get(receiver.tryInitializeAndGetStatics());
if (result instanceof StaticObject) {
result = InteropUtils.unwrap((StaticObject) result, meta, lookupFieldNode);
result = InteropUtils.unwrap(language, (StaticObject) result, meta);
}
return result;
}
Expand Down Expand Up @@ -352,9 +353,10 @@ static Object invokeMember(Klass receiver, String member,
}
throw UnknownIdentifierException.create(member);
} catch (EspressoException e) {
if (receiver.getMeta().polyglot != null && e.getGuestException().getKlass() == receiver.getMeta().polyglot.ForeignException) {
if (InteropUtils.isForeignException(e)) {
Meta meta = e.getGuestException().getKlass().getMeta();
// rethrow the original foreign exception when leaving espresso interop
throw (AbstractTruffleException) receiver.getMeta().java_lang_Throwable_backtrace.getObject(e.getGuestException()).rawForeignObject(receiver.getLanguage());
throw (AbstractTruffleException) meta.java_lang_Throwable_backtrace.getObject(e.getGuestException()).rawForeignObject(receiver.getLanguage());
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public Meta(EspressoContext context) {
java_lang_Class_forName_String_boolean_ClassLoader = java_lang_Class.requireDeclaredMethod(Name.forName, Signature.Class_String_boolean_ClassLoader);

java_lang_String = knownKlass(Type.java_lang_String);
java_lang_String_array = java_lang_String.array();
java_lang_CharSequence = knownKlass(Type.java_lang_CharSequence);

// Primitives.
Expand Down Expand Up @@ -286,6 +287,8 @@ public Meta(EspressoContext context) {
java_lang_NoSuchFieldException = knownKlass(Type.java_lang_NoSuchFieldException);
java_lang_NoSuchMethodException = knownKlass(Type.java_lang_NoSuchMethodException);
java_lang_UnsupportedOperationException = knownKlass(Type.java_lang_UnsupportedOperationException);
java_util_NoSuchElementException = knownKlass(Type.java_util_NoSuchElementException);
java_lang_NumberFormatException = knownKlass(Type.java_lang_NumberFormatException);

java_lang_StackOverflowError = knownKlass(Type.java_lang_StackOverflowError);
java_lang_OutOfMemoryError = knownKlass(Type.java_lang_OutOfMemoryError);
Expand Down Expand Up @@ -885,8 +888,6 @@ public Meta(EspressoContext context) {
java_math_MathContext = knownKlass(Type.java_math_MathContext);
java_math_MathContext_init = java_math_MathContext.requireDeclaredMethod(Name._init_, Signature._void_int);

java_util_NoSuchElementException = knownKlass(Type.java_util_NoSuchElementException);

jdk_internal_misc_UnsafeConstants = diff() //
.klass(higher(13), Type.jdk_internal_misc_UnsafeConstants) //
.notRequiredKlass();
Expand Down Expand Up @@ -1054,6 +1055,7 @@ private DiffVersionLoadHelper diff() {
public final ArrayKlass java_lang_Object_array;

public final ObjectKlass java_lang_String;
public final ArrayKlass java_lang_String_array;
public final ObjectKlass java_lang_Class;
public final ObjectKlass java_lang_CharSequence;
public final Field HIDDEN_MIRROR_KLASS;
Expand Down Expand Up @@ -1258,6 +1260,8 @@ private DiffVersionLoadHelper diff() {
public final ObjectKlass java_lang_NoSuchFieldException;
public final ObjectKlass java_lang_NoSuchMethodException;
public final ObjectKlass java_lang_UnsupportedOperationException;
public final ObjectKlass java_util_NoSuchElementException;
public final ObjectKlass java_lang_NumberFormatException;

public final ObjectKlass java_lang_Throwable;
public final Method java_lang_Throwable_getStackTrace;
Expand Down Expand Up @@ -1584,8 +1588,6 @@ private DiffVersionLoadHelper diff() {
public final ObjectKlass java_math_MathContext;
public final Method java_math_MathContext_init;

public final ObjectKlass java_util_NoSuchElementException;

public final ObjectKlass jdk_internal_misc_UnsafeConstants;
public final Field jdk_internal_misc_UnsafeConstants_ADDRESS_SIZE0;
public final Field jdk_internal_misc_UnsafeConstants_PAGE_SIZE;
Expand Down
Loading

0 comments on commit d24879f

Please sign in to comment.