@@ -184,18 +184,17 @@ public Object doMappedInterface(Object value, Klass targetType,
184
184
@ SuppressWarnings ("unused" ) @ CachedLibrary (limit = "LIMIT" ) InteropLibrary interop ,
185
185
@ Cached InlinedBranchProfile error ) throws UnsupportedTypeException {
186
186
try {
187
- Object metaObject = getMetaObjectOrThrow (value , interop );
187
+ Object metaObject = interop . getMetaObject (value );
188
188
WrappedProxyKlass proxyKlass = lookupProxyKlassNode .execute (metaObject , getMetaName (metaObject , interop ), targetType );
189
189
if (proxyKlass != null ) {
190
190
targetType .safeInitialize ();
191
191
return proxyKlass .createProxyInstance (value , getLanguage (), interop );
192
192
}
193
- error .enter (this );
194
- throw new ClassCastException ();
195
- } catch (ClassCastException e ) {
196
- error .enter (this );
197
- throw UnsupportedTypeException .create (new Object []{value }, EspressoError .format ("Could not cast foreign object to %s: " , targetType .getTypeAsString ()));
193
+ } catch (UnsupportedMessageException e ) {
194
+ // no meta object, fall through to throw unsupported type
198
195
}
196
+ error .enter (this );
197
+ throw unsupportedType (value , targetType );
199
198
}
200
199
201
200
@ Specialization (guards = {
@@ -227,20 +226,19 @@ public Object doTypeConverter(Object value, Klass targetType,
227
226
@ SuppressWarnings ("unused" ) @ CachedLibrary (limit = "LIMIT" ) InteropLibrary interop ,
228
227
@ Cached InlinedBranchProfile error ) throws UnsupportedTypeException {
229
228
try {
230
- Object metaObject = getMetaObjectOrThrow (value , interop );
229
+ Object metaObject = interop . getMetaObject (value );
231
230
String metaName = getMetaName (metaObject , interop );
232
231
233
232
// check if there's a specific type mapping available
234
233
PolyglotTypeMappings .TypeConverter converter = lookupTypeConverter .execute (metaName );
235
234
if (converter != null ) {
236
235
return converter .convert (StaticObject .createForeign (getLanguage (), targetType , value , interop ));
237
236
}
238
- error .enter (this );
239
- throw new ClassCastException ();
240
- } catch (ClassCastException e ) {
241
- error .enter (this );
242
- throw UnsupportedTypeException .create (new Object []{value }, EspressoError .format ("Could not cast foreign object to %s: " , targetType .getNameAsString (), e .getMessage ()));
237
+ } catch (UnsupportedMessageException e ) {
238
+ // no meta object, fall through to throw unsupported type
243
239
}
240
+ error .enter (this );
241
+ throw unsupportedType (value , targetType );
244
242
}
245
243
246
244
@ Specialization (guards = {
@@ -252,22 +250,21 @@ public Object doInternalTypeConverter(Object value, Klass targetType,
252
250
@ Cached ToReference .DynamicToReference converterToEspresso ,
253
251
@ Cached LookupInternalTypeConverterNode lookupInternalTypeConverter ,
254
252
@ SuppressWarnings ("unused" ) @ CachedLibrary (limit = "LIMIT" ) InteropLibrary interop ,
255
- @ Cached InlinedBranchProfile error ) throws UnsupportedTypeException {
253
+ @ Cached InlinedBranchProfile errorProfile ) throws UnsupportedTypeException {
256
254
try {
257
- Object metaObject = getMetaObjectOrThrow (value , interop );
255
+ Object metaObject = interop . getMetaObject (value );
258
256
String metaName = getMetaName (metaObject , interop );
259
257
260
258
// check if there's a specific type mapping available
261
259
PolyglotTypeMappings .InternalTypeConverter converter = lookupInternalTypeConverter .execute (metaName );
262
260
if (converter != null ) {
263
261
return converter .convertInternal (interop , value , getMeta (), converterToEspresso );
264
262
}
265
- error .enter (this );
266
- throw new ClassCastException ();
267
- } catch (ClassCastException e ) {
268
- error .enter (this );
269
- throw UnsupportedTypeException .create (new Object []{value }, EspressoError .format ("Could not cast foreign object to %s: " , targetType .getNameAsString (), e .getMessage ()));
263
+ } catch (UnsupportedMessageException e ) {
264
+ // no meta object, fall through to throw unsupported type
270
265
}
266
+ errorProfile .enter (this );
267
+ throw unsupportedType (value , targetType );
271
268
}
272
269
273
270
@ Specialization (guards = {
@@ -295,12 +292,8 @@ public Object doGeneric(Object value, Klass targetType,
295
292
if (result != null ) {
296
293
return result ;
297
294
}
298
- try {
299
- checkHasAllFieldsOrThrow (value , (ObjectKlass ) targetType , interop , getMeta ());
300
- return StaticObject .createForeign (getLanguage (), targetType , value , interop );
301
- } catch (ClassCastException e ) {
302
- throw UnsupportedTypeException .create (new Object []{value }, targetType .getTypeAsString ());
303
- }
295
+ checkHasAllFieldsOrThrow (value , (ObjectKlass ) targetType , interop , getMeta ());
296
+ return StaticObject .createForeign (getLanguage (), targetType , value , interop );
304
297
}
305
298
throw UnsupportedTypeException .create (new Object []{value }, targetType .getTypeAsString ());
306
299
}
@@ -336,16 +329,8 @@ public static String getMetaName(Object metaObject, InteropLibrary interop) {
336
329
}
337
330
}
338
331
339
- public static Object getMetaObjectOrThrow (Object value , InteropLibrary interop ) throws ClassCastException {
340
- try {
341
- return interop .getMetaObject (value );
342
- } catch (UnsupportedMessageException e ) {
343
- throw new ClassCastException ("Could not lookup meta object" );
344
- }
345
- }
346
-
347
332
@ TruffleBoundary
348
- public static void checkHasAllFieldsOrThrow (Object value , ObjectKlass klass , InteropLibrary interopLibrary , Meta meta ) {
333
+ public static void checkHasAllFieldsOrThrow (Object value , ObjectKlass klass , InteropLibrary interopLibrary , Meta meta ) throws UnsupportedTypeException {
349
334
CompilerAsserts .partialEvaluationConstant (klass );
350
335
/*
351
336
* For boxed types a .value member is not required if there's a direct conversion via
@@ -371,11 +356,16 @@ public static void checkHasAllFieldsOrThrow(Object value, ObjectKlass klass, Int
371
356
372
357
for (Field f : klass .getDeclaredFields ()) {
373
358
if (!f .isStatic () && !interopLibrary .isMemberExisting (value , f .getNameAsString ())) {
374
- throw new ClassCastException ( "Missing field: " + f .getNameAsString ());
359
+ throw UnsupportedTypeException . create ( new Object []{ value }, klass . getTypeAsString () + " due to missing field: " + f .getNameAsString ());
375
360
}
376
361
}
377
362
if (klass .getSuperClass () != null ) {
378
363
checkHasAllFieldsOrThrow (value , klass .getSuperKlass (), interopLibrary , meta );
379
364
}
380
365
}
366
+
367
+ @ TruffleBoundary
368
+ static UnsupportedTypeException unsupportedType (Object value , Klass targetType ) {
369
+ return UnsupportedTypeException .create (new Object []{value }, EspressoError .format ("Could not cast foreign object to %s: " , targetType .getNameAsString ()));
370
+ }
381
371
}
0 commit comments