Skip to content

Commit

Permalink
Avoid using IllegalStateException in getUncachedToEspresso
Browse files Browse the repository at this point in the history
This is not a control-flow exception

(cherry picked from commit 9376be9)
  • Loading branch information
gilles-duboscq authored and elkorchi committed Jun 26, 2024
1 parent 5be53f2 commit a165ae8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 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,23 @@ 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 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) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.profiles.BranchProfile;
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.Klass;
Expand Down Expand Up @@ -223,7 +224,8 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
return ToReferenceFactory.ToByteArrayNodeGen.getUncached();
}
if (targetType.isArray()) {
throw new IllegalStateException("Generic arrays type mappings must be handled separately!");
// Generic arrays type mappings must be handled separately!
return null;
}
if (targetType.isJavaLangObject()) {
return ToReferenceFactory.ToJavaLangObjectNodeGen.getUncached();
Expand Down Expand Up @@ -255,11 +257,8 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
return ToReferenceFactory.ToMapNodeGen.getUncached();
}
}
if (isTypeMappingEnabled(targetType)) {
throw new IllegalStateException("Interface type mappings must be handled separately!");
} else {
throw new IllegalStateException("unknown types must be handled separately!");
}
// Interface type mappings & unknown interface types must be handled separately!
return null;
}
if (isForeignException(targetType, meta)) {
return ToReferenceFactory.ToForeignExceptionNodeGen.getUncached();
Expand Down Expand Up @@ -300,7 +299,7 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
if (targetType == meta.java_math_BigInteger) {
return ToReferenceFactory.ToBigIntegerNodeGen.getUncached();
}
throw new IllegalStateException("unknown types must be handled separately!");
return null;
}

@NodeInfo(shortName = "Dynamic toEspresso node")
Expand Down Expand Up @@ -454,21 +453,23 @@ public StaticObject doInternalTypeConverter(Object value, @SuppressWarnings("unu
})
public StaticObject doGeneric(Object value, Klass targetType,
@Bind("getMeta()") Meta meta,
@CachedLibrary(limit = "LIMIT") InteropLibrary interop) throws UnsupportedTypeException {
try {
return getUncachedToReference(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 InlinedBranchProfile unknownProfile) throws UnsupportedTypeException {
ToReference uncachedToReference = getUncachedToReference(targetType, meta);
if (uncachedToReference != null) {
return uncachedToReference.execute(value);
}
unknownProfile.enter(this);
// 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());
}
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
}
}

Expand Down

0 comments on commit a165ae8

Please sign in to comment.