Skip to content

Commit

Permalink
[GR-54863] [GR-54866] Backport to 24.0: Support converting foreign ob…
Browse files Browse the repository at this point in the history
…ject to a non-mapped interface.

PullRequest: graal/18125
  • Loading branch information
OracleLabsAutomation authored and elkorchi committed Jun 27, 2024
2 parents 710725a + 3e65683 commit 5616789
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
import com.oracle.truffle.espresso.EspressoLanguage;
import com.oracle.truffle.espresso.impl.ArrayKlass;
import com.oracle.truffle.espresso.impl.Field;
Expand Down Expand Up @@ -262,21 +263,30 @@ public Object doInternalTypeConverter(Object value, Klass targetType,
})
public Object doGeneric(Object value, Klass targetType,
@Bind("getMeta()") Meta meta,
@CachedLibrary(limit = "LIMIT") InteropLibrary interop) throws UnsupportedTypeException {
try {
return getUncachedToEspresso(targetType, meta).execute(value);
} catch (IllegalStateException ex) {
// hit the unknown type case, so inline generic handling for that here
if (targetType instanceof ObjectKlass) {
try {
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
} catch (ClassCastException e) {
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
@CachedLibrary(limit = "LIMIT") InteropLibrary interop,
@Cached LookupTypeConverterNode lookupTypeConverterNode,
@Cached LookupInternalTypeConverterNode lookupInternalTypeConverterNode,
@Cached ToReference.DynamicToReference converterToEspresso,
@Cached InlinedBranchProfile unknownProfile) throws UnsupportedTypeException {
ToEspressoNode uncachedToEspresso = getUncachedToEspresso(targetType, meta);
if (uncachedToEspresso != null) {
return uncachedToEspresso.execute(value);
}
unknownProfile.enter(this);
// hit the unknown type case, so inline generic handling for that here
if (targetType instanceof ObjectKlass) {
StaticObject result = ToReference.tryConverterForUnknownTarget(value, interop, lookupTypeConverterNode, lookupInternalTypeConverterNode, converterToEspresso, meta);
if (result != null) {
return result;
}
try {
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
} catch (ClassCastException e) {
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
}

Expand Down
Loading

0 comments on commit 5616789

Please sign in to comment.