diff --git a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java index 2a9888432e1b..64ee7198d1b6 100644 --- a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java +++ b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleContext.java @@ -1274,7 +1274,7 @@ public Builder onExited(Consumer r) { /** * Specifies a {@link Runnable} that will be executed when some operation on the new context - * attenmpted while the context is already {@link TruffleContext#close()} closed}. However, + * attempted while the context is already {@link TruffleContext#close() closed}. However, * the runnable will only be executed if the outer context is not closed. * * The purpose of the runnable is to allow throwing a custom guest exception before the diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/OtherContextGuestObject.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/OtherContextGuestObject.java index 01d0e86abe25..c994c4e71b2c 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/OtherContextGuestObject.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/OtherContextGuestObject.java @@ -181,10 +181,16 @@ static Object sendImpl(Node node, PolyglotSharingLayer layer, Object receiver, M @TruffleBoundary static RuntimeException migrateException(PolyglotContextImpl receiverContext, Throwable e, PolyglotContextImpl valueContext) throws T { if (e instanceof OtherContextException) { + // Same logic as in migrateValue() OtherContextException other = (OtherContextException) e; if (other.receiverContext == receiverContext && other.delegateContext == valueContext) { + // reuse wrapper it is already wrapped throw other; + } else if (other.receiverContext == valueContext && other.delegateContext == receiverContext) { + // unpack foreign value it belongs to that context + throw (T) other.delegate; } else { + // TODO: Should the last arg be valueContext like in migrateValue? throw new OtherContextException(receiverContext, other.delegate, other.delegateContext); } } else if (InteropLibrary.getUncached().isException(e)) { @@ -338,13 +344,13 @@ static class OtherContextException extends AbstractTruffleException { } @TruffleBoundary - OtherContextException(PolyglotContextImpl thisContext, Exception delegate, PolyglotContextImpl delegateContext) { + OtherContextException(PolyglotContextImpl receiverContext, Exception delegate, PolyglotContextImpl delegateContext) { super(delegate.getMessage()); assert !(delegate instanceof OtherContextException) : "recursive host foreign value found"; - assert thisContext != null && delegateContext != null : "Must have associated contexts."; - assert thisContext != delegateContext : "no need for foreign value if contexts match"; + assert receiverContext != null && delegateContext != null : "Must have associated contexts."; + assert receiverContext != delegateContext : "no need for foreign value if contexts match"; this.delegate = delegate; - this.receiverContext = thisContext; + this.receiverContext = receiverContext; this.delegateContext = delegateContext; } diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java index 5a67da99b9f6..1ea749c71a07 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotContextImpl.java @@ -1720,6 +1720,7 @@ Object migrateValue(Object value, PolyglotContextImpl valueContext) { // guaranteed by migrateValue assert value instanceof TruffleObject; if (value instanceof OtherContextGuestObject) { + // Same logic as in migrateException() OtherContextGuestObject otherValue = (OtherContextGuestObject) value; if (otherValue.receiverContext == this && otherValue.delegateContext == valueContext) { // reuse wrapper it is already wrapped