private <T> T convertToPrimitive(Class<T> clazz) { Object val = get(); if (clazz.isInstance(val)) { return clazz.cast(val); } if (val instanceof TruffleObject) { TruffleObject receiver = (TruffleObject) val; if (ForeignAccess.sendIsBoxed(debugger.msgNodes.isBoxed, receiver)) { try { Object unboxed = ForeignAccess.sendUnbox(debugger.msgNodes.unbox, receiver); if (clazz.isInstance(unboxed)) { return clazz.cast(unboxed); } } catch (UnsupportedMessageException e) { throw new AssertionError("isBoxed returned true but unbox threw unsupported error."); } } } return null; }
@Override protected Object executeImpl(Object receiver, Object[] args) { if (ForeignAccess.sendIsBoxed(isBoxedNode, (TruffleObject) receiver)) { try { return ForeignAccess.sendUnbox(unboxNode, (TruffleObject) receiver); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreter(); throw new AssertionError("isBoxed returned true but unbox threw unsupported error."); } } else { return null; } } }
@Override protected Object executeImpl(PolyglotLanguageContext context, Object receiver, Object[] args) { if (ForeignAccess.sendIsBoxed(isBoxedNode, (TruffleObject) receiver)) { try { return ForeignAccess.sendUnbox(unboxNode, (TruffleObject) receiver); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreter(); throw new AssertionError("isBoxed returned true but unbox threw unsupported error."); } } else { return null; } } }
Object unbox(TruffleObject value) { if (!ForeignAccess.sendIsBoxed(isBoxedNode, value)) { return null; } Object result; try { result = ForeignAccess.sendUnbox(unboxNode, value); } catch (UnsupportedMessageException e) { return null; } if (result instanceof TruffleObject && isNull((TruffleObject) result)) { return null; } else { return result; } }
Object unbox(TruffleObject value) { if (!ForeignAccess.sendIsBoxed(isBoxedNode, value)) { return null; } Object result; try { result = ForeignAccess.sendUnbox(unboxNode, value); } catch (UnsupportedMessageException e) { return null; } if (result instanceof TruffleObject && isNull((TruffleObject) result)) { return null; } else { return result; } }
private <T> T convertToPrimitive(Class<T> clazz) { Object val = get(); if (clazz.isInstance(val)) { return clazz.cast(val); } if (val instanceof TruffleObject) { TruffleObject receiver = (TruffleObject) val; if (ForeignAccess.sendIsBoxed(getDebugger().msgNodes.isBoxed, receiver)) { try { Object unboxed = ForeignAccess.sendUnbox(getDebugger().msgNodes.unbox, receiver); if (clazz.isInstance(unboxed)) { return clazz.cast(unboxed); } } catch (UnsupportedMessageException e) { throw new AssertionError("isBoxed returned true but unbox threw unsupported error."); } } } return null; }
private Object convert(TruffleObject obj) { boolean isBoxedResult = ForeignAccess.sendIsBoxed(isBoxed, obj); if (isBoxedProfile.profile(isBoxedResult)) { try { Object newValue = ForeignAccess.sendUnbox(unbox, obj); return new ConvertedObject(obj, newValue); } catch (UnsupportedMessageException e) { return new ConvertedObject(obj, null); } } else { boolean isNullResult = ForeignAccess.sendIsNull(isNull, obj); if (isNullResult) { return new ConvertedObject(obj, null); } } return obj; } }
@Specialization Object doTruffleObject(TruffleObject arg, @Cached("createIsBoxedNode()") Node isBoxed, @Cached("createUnboxNode()") Node unbox) { try { if (ForeignAccess.sendIsBoxed(isBoxed, arg)) { Object unboxedObject = ForeignAccess.sendUnbox(unbox, arg); if (unboxedObject instanceof String) { return unboxedObject; } CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } return arg; } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } }
@Specialization String doTruffleObject(TruffleObject arg, @Cached("createIsBoxedNode()") Node isBoxed, @Cached("createUnboxNode()") Node unbox) { try { if (ForeignAccess.sendIsBoxed(isBoxed, arg)) { Object unboxedObject = ForeignAccess.sendUnbox(unbox, arg); if (unboxedObject instanceof String) { return (String) unboxedObject; } else { CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } } else { return getFromArrayNode().execute(arg); } } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } }
@Specialization long expectNumberTruffleObject(TruffleObject arg, @Cached("createIsBoxedNode()") Node isBoxed, @Cached("createUnboxNode()") Node unbox, @Cached("create()") RegexParamToLongNode convertUnboxed) { try { if (ForeignAccess.sendIsBoxed(isBoxed, arg)) { Object unboxedObject = ForeignAccess.sendUnbox(unbox, arg); assert !(unboxedObject instanceof TruffleObject) : "unboxed Object must be a primitive!"; return convertUnboxed.execute(unboxedObject); } CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } catch (UnsupportedMessageException e) { CompilerDirectives.transferToInterpreter(); throw UnsupportedTypeException.raise(new Object[]{arg}); } }
private static Object handleMessage(Message message, Node messageNode, TruffleObject obj, String name, Object[] args) throws InteropException { if (message == Message.WRITE) { ForeignAccess.sendWrite(messageNode, obj, name, args[0]); return null; } if (message == Message.HAS_SIZE || message == Message.IS_BOXED || message == Message.IS_EXECUTABLE || message == Message.IS_NULL || message == Message.GET_SIZE) { return ForeignAccess.send(messageNode, obj); } if (message == Message.KEY_INFO) { return ForeignAccess.sendKeyInfo(messageNode, obj, name); } if (message == Message.READ) { return ForeignAccess.sendRead(messageNode, obj, name); } if (message == Message.UNBOX) { return ForeignAccess.sendUnbox(messageNode, obj); } if (Message.createExecute(0).equals(message)) { return ForeignAccess.sendExecute(messageNode, obj, args); } if (Message.createInvoke(0).equals(message)) { return ForeignAccess.sendInvoke(messageNode, obj, name, args); } if (Message.createNew(0).equals(message)) { return ForeignAccess.sendNew(messageNode, obj, args); } CompilerDirectives.transferToInterpreter(); throw UnsupportedMessageException.raise(message); }