Skip to content

Commit a165ae8

Browse files
gilles-duboscqelkorchi
authored andcommitted
Avoid using IllegalStateException in getUncachedToEspresso
This is not a control-flow exception (cherry picked from commit 9376be9)
1 parent 5be53f2 commit a165ae8

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/nodes/interop/ToEspressoNode.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.oracle.truffle.api.interop.UnsupportedTypeException;
3838
import com.oracle.truffle.api.library.CachedLibrary;
3939
import com.oracle.truffle.api.nodes.NodeInfo;
40+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
4041
import com.oracle.truffle.espresso.EspressoLanguage;
4142
import com.oracle.truffle.espresso.impl.ArrayKlass;
4243
import com.oracle.truffle.espresso.impl.Field;
@@ -262,21 +263,23 @@ public Object doInternalTypeConverter(Object value, Klass targetType,
262263
})
263264
public Object doGeneric(Object value, Klass targetType,
264265
@Bind("getMeta()") Meta meta,
265-
@CachedLibrary(limit = "LIMIT") InteropLibrary interop) throws UnsupportedTypeException {
266-
try {
267-
return getUncachedToEspresso(targetType, meta).execute(value);
268-
} catch (IllegalStateException ex) {
269-
// hit the unknown type case, so inline generic handling for that here
270-
if (targetType instanceof ObjectKlass) {
271-
try {
272-
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
273-
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
274-
} catch (ClassCastException e) {
275-
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
276-
}
266+
@CachedLibrary(limit = "LIMIT") InteropLibrary interop,
267+
@Cached InlinedBranchProfile unknownProfile) throws UnsupportedTypeException {
268+
ToEspressoNode uncachedToEspresso = getUncachedToEspresso(targetType, meta);
269+
if (uncachedToEspresso != null) {
270+
return uncachedToEspresso.execute(value);
271+
}
272+
unknownProfile.enter(this);
273+
// hit the unknown type case, so inline generic handling for that here
274+
if (targetType instanceof ObjectKlass) {
275+
try {
276+
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
277+
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
278+
} catch (ClassCastException e) {
279+
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
277280
}
278-
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
279281
}
282+
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
280283
}
281284
}
282285

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/nodes/interop/ToReference.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.oracle.truffle.api.library.CachedLibrary;
4646
import com.oracle.truffle.api.nodes.NodeInfo;
4747
import com.oracle.truffle.api.profiles.BranchProfile;
48+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
4849
import com.oracle.truffle.espresso.EspressoLanguage;
4950
import com.oracle.truffle.espresso.impl.ArrayKlass;
5051
import com.oracle.truffle.espresso.impl.Klass;
@@ -223,7 +224,8 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
223224
return ToReferenceFactory.ToByteArrayNodeGen.getUncached();
224225
}
225226
if (targetType.isArray()) {
226-
throw new IllegalStateException("Generic arrays type mappings must be handled separately!");
227+
// Generic arrays type mappings must be handled separately!
228+
return null;
227229
}
228230
if (targetType.isJavaLangObject()) {
229231
return ToReferenceFactory.ToJavaLangObjectNodeGen.getUncached();
@@ -255,11 +257,8 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
255257
return ToReferenceFactory.ToMapNodeGen.getUncached();
256258
}
257259
}
258-
if (isTypeMappingEnabled(targetType)) {
259-
throw new IllegalStateException("Interface type mappings must be handled separately!");
260-
} else {
261-
throw new IllegalStateException("unknown types must be handled separately!");
262-
}
260+
// Interface type mappings & unknown interface types must be handled separately!
261+
return null;
263262
}
264263
if (isForeignException(targetType, meta)) {
265264
return ToReferenceFactory.ToForeignExceptionNodeGen.getUncached();
@@ -300,7 +299,7 @@ static ToReference getUncachedToReference(Klass targetType, Meta meta) {
300299
if (targetType == meta.java_math_BigInteger) {
301300
return ToReferenceFactory.ToBigIntegerNodeGen.getUncached();
302301
}
303-
throw new IllegalStateException("unknown types must be handled separately!");
302+
return null;
304303
}
305304

306305
@NodeInfo(shortName = "Dynamic toEspresso node")
@@ -454,21 +453,23 @@ public StaticObject doInternalTypeConverter(Object value, @SuppressWarnings("unu
454453
})
455454
public StaticObject doGeneric(Object value, Klass targetType,
456455
@Bind("getMeta()") Meta meta,
457-
@CachedLibrary(limit = "LIMIT") InteropLibrary interop) throws UnsupportedTypeException {
458-
try {
459-
return getUncachedToReference(targetType, meta).execute(value);
460-
} catch (IllegalStateException ex) {
461-
// hit the unknown type case, so inline generic handling for that here
462-
if (targetType instanceof ObjectKlass) {
463-
try {
464-
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
465-
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
466-
} catch (ClassCastException e) {
467-
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
468-
}
456+
@CachedLibrary(limit = "LIMIT") InteropLibrary interop,
457+
@Cached InlinedBranchProfile unknownProfile) throws UnsupportedTypeException {
458+
ToReference uncachedToReference = getUncachedToReference(targetType, meta);
459+
if (uncachedToReference != null) {
460+
return uncachedToReference.execute(value);
461+
}
462+
unknownProfile.enter(this);
463+
// hit the unknown type case, so inline generic handling for that here
464+
if (targetType instanceof ObjectKlass) {
465+
try {
466+
checkHasAllFieldsOrThrow(value, (ObjectKlass) targetType, interop, getMeta());
467+
return StaticObject.createForeign(getLanguage(), targetType, value, interop);
468+
} catch (ClassCastException e) {
469+
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
469470
}
470-
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
471471
}
472+
throw UnsupportedTypeException.create(new Object[]{value}, targetType.getTypeAsString());
472473
}
473474
}
474475

0 commit comments

Comments
 (0)