|
34 | 34 | import com.oracle.truffle.api.nodes.NodeInfo; |
35 | 35 | import com.oracle.truffle.api.profiles.BranchProfile; |
36 | 36 | import com.oracle.truffle.espresso.meta.EspressoError; |
| 37 | +import com.oracle.truffle.espresso.runtime.dispatch.staticobject.EspressoInterop; |
37 | 38 | import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; |
38 | 39 |
|
39 | 40 | /** |
@@ -102,8 +103,13 @@ int doHost(Integer value) { |
102 | 103 | @Specialization |
103 | 104 | int doEspresso(StaticObject value, |
104 | 105 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
105 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Integer) { |
106 | | - return (int) getMeta().java_lang_Integer_value.get(value); |
| 106 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInInt(value)) { |
| 107 | + try { |
| 108 | + return EspressoInterop.asInt(value); |
| 109 | + } catch (UnsupportedMessageException e) { |
| 110 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 111 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); |
| 112 | + } |
107 | 113 | } |
108 | 114 | exceptionProfile.enter(); |
109 | 115 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to int")); |
@@ -148,8 +154,13 @@ byte doHost(Byte value) { |
148 | 154 | @Specialization |
149 | 155 | byte doEspresso(StaticObject value, |
150 | 156 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
151 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Byte) { |
152 | | - return (byte) getMeta().java_lang_Byte_value.get(value); |
| 157 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInByte(value)) { |
| 158 | + try { |
| 159 | + return EspressoInterop.asByte(value); |
| 160 | + } catch (UnsupportedMessageException e) { |
| 161 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 162 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInByte returns true, asByte must succeed."); |
| 163 | + } |
153 | 164 | } |
154 | 165 | exceptionProfile.enter(); |
155 | 166 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to byte")); |
@@ -194,8 +205,13 @@ short doHost(Short value) { |
194 | 205 | @Specialization |
195 | 206 | short doEspresso(StaticObject value, |
196 | 207 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
197 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Short) { |
198 | | - return (short) getMeta().java_lang_Short_value.get(value); |
| 208 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInShort(value)) { |
| 209 | + try { |
| 210 | + return EspressoInterop.asShort(value); |
| 211 | + } catch (UnsupportedMessageException e) { |
| 212 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 213 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInShort returns true, asShort must succeed."); |
| 214 | + } |
199 | 215 | } |
200 | 216 | exceptionProfile.enter(); |
201 | 217 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to short")); |
@@ -264,7 +280,7 @@ char doForeign(Object value, |
264 | 280 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", s, " to char")); |
265 | 281 | } catch (UnsupportedMessageException e) { |
266 | 282 | CompilerDirectives.transferToInterpreterAndInvalidate(); |
267 | | - throw EspressoError.shouldNotReachHere("Contract violation: if fitsInInt returns true, asInt must succeed."); |
| 283 | + throw EspressoError.shouldNotReachHere("Contract violation: if isString returns true, asString must succeed."); |
268 | 284 | } |
269 | 285 | } |
270 | 286 |
|
@@ -292,8 +308,13 @@ long doHost(Long value) { |
292 | 308 | @Specialization |
293 | 309 | long doEspresso(StaticObject value, |
294 | 310 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
295 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Long) { |
296 | | - return (long) getMeta().java_lang_Long_value.get(value); |
| 311 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInLong(value)) { |
| 312 | + try { |
| 313 | + return EspressoInterop.asLong(value); |
| 314 | + } catch (UnsupportedMessageException e) { |
| 315 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 316 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInLong returns true, asLong must succeed."); |
| 317 | + } |
297 | 318 | } |
298 | 319 | exceptionProfile.enter(); |
299 | 320 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to long")); |
@@ -338,8 +359,13 @@ float doHost(Float value) { |
338 | 359 | @Specialization |
339 | 360 | float doEspresso(StaticObject value, |
340 | 361 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
341 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Float) { |
342 | | - return (float) getMeta().java_lang_Float_value.get(value); |
| 362 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInFloat(value)) { |
| 363 | + try { |
| 364 | + return EspressoInterop.asFloat(value); |
| 365 | + } catch (UnsupportedMessageException e) { |
| 366 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 367 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInFloat returns true, asFloat must succeed."); |
| 368 | + } |
343 | 369 | } |
344 | 370 | exceptionProfile.enter(); |
345 | 371 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to float")); |
@@ -384,8 +410,13 @@ public abstract static class ToDouble extends ToPrimitive { |
384 | 410 | @Specialization |
385 | 411 | double doEspresso(StaticObject value, |
386 | 412 | @Cached BranchProfile exceptionProfile) throws UnsupportedTypeException { |
387 | | - if (value != null && !StaticObject.isNull(value) && value.getKlass() == getMeta().java_lang_Double) { |
388 | | - return (double) getMeta().java_lang_Double_value.get(value); |
| 413 | + if (value != null && !StaticObject.isNull(value) && EspressoInterop.fitsInDouble(value)) { |
| 414 | + try { |
| 415 | + return EspressoInterop.asDouble(value); |
| 416 | + } catch (UnsupportedMessageException e) { |
| 417 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 418 | + throw EspressoError.shouldNotReachHere("Contract violation: if fitsInDouble returns true, asDouble must succeed."); |
| 419 | + } |
389 | 420 | } |
390 | 421 | exceptionProfile.enter(); |
391 | 422 | throw UnsupportedTypeException.create(new Object[]{value}, EspressoError.cat("Cannot cast ", value, " to double")); |
|
0 commit comments