Search in sources :

Example 6 with AbstractTruffleException

use of com.oracle.truffle.api.exception.AbstractTruffleException in project graal by oracle.

the class HostEntryPoint method remoteMessage.

public Object remoteMessage(long contextId, long receiverId, Message message, Object[] args) {
    Context c = unmarshall(Context.class, contextId);
    c.enter();
    try {
        Object receiver = unmarshall(Object.class, receiverId);
        Object[] localValues = unmarshallAtGuest(contextId, args);
        ReflectionLibrary lib = ReflectionLibrary.getFactory().getUncached(receiver);
        Object result;
        try {
            result = lib.send(receiver, message, localValues);
        } catch (Exception e) {
            // probably needs to support TruffleException too, but this is just a sketch
            if (e instanceof AbstractTruffleException) {
                // also send over stack traces and messages
                return new GuestExceptionPointer(guestToHost(e), e.getMessage());
            } else {
                throw new RuntimeException("Internal error thrown by remote message.", e);
            }
        }
        return marshallAtGuest(result);
    } finally {
        c.leave();
    }
}
Also used : Context(org.graalvm.polyglot.Context) ReflectionLibrary(com.oracle.truffle.api.library.ReflectionLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException)

Example 7 with AbstractTruffleException

use of com.oracle.truffle.api.exception.AbstractTruffleException in project graal by oracle.

the class AbstractExecutableTestLanguage method parse.

@Override
@SuppressWarnings("unused")
protected final CallTarget parse(ParsingRequest request) throws Exception {
    ExecutableContext executableContext = TestAPIAccessor.engineAccess().getCurrentContext(AbstractExecutableTestLanguage.this.getClass());
    Object[] ctxArgs = createArgumentsArray(executableContext);
    onParse(request, executableContext.env, ctxArgs);
    String rootNodeName = getRootName(request, executableContext.env, ctxArgs);
    SourceSection srcSec = request.getSource().createSection(0, request.getSource().getLength());
    return new RootNode(this, null) {

        @Child
        InteropLibrary interopLibrary = interop;

        private final SourceSection sourceSection = srcSec;

        @CompilationFinal(dimensions = 1)
        private final Object[] contextArguments = ctxArgs;

        @Override
        public Object execute(VirtualFrame frame) {
            ExecutableContext execCtx = TestAPIAccessor.engineAccess().getCurrentContext(AbstractExecutableTestLanguage.this.getClass());
            try {
                Object returnValue = AbstractExecutableTestLanguage.this.execute(this, execCtx.env, contextArguments, frame.getArguments());
                if (returnValue == null) {
                    return NullObject.SINGLETON;
                }
                return returnValue;
            } catch (AbstractTruffleException e) {
                throw e;
            } catch (Exception e) {
                throw throwAssertionError(e);
            }
        }

        @TruffleBoundary
        private AssertionError throwAssertionError(Exception e) {
            throw new AssertionError(e);
        }

        @Override
        public String getName() {
            return rootNodeName;
        }

        @Override
        public SourceSection getSourceSection() {
            return srcSec;
        }
    }.getCallTarget();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CompilationFinal(com.oracle.truffle.api.CompilerDirectives.CompilationFinal) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 8 with AbstractTruffleException

use of com.oracle.truffle.api.exception.AbstractTruffleException in project graal by oracle.

the class InsightObjectTest method wrongReturnValueCall.

@Test
public void wrongReturnValueCall() throws Exception {
    try (Context c = InsightObjectFactory.newContext()) {
        Value agent = InsightObjectFactory.readInsight(c, null);
        InsightAPI agentAPI = agent.as(InsightAPI.class);
        Assert.assertNotNull("Agent API obtained", agentAPI);
        // @formatter:off
        Source sampleScript = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(\n" + "  DEFINE(meaning,\n" + "    EXPRESSION(\n" + "      CONSTANT(6),\n" + "      CONSTANT(7)\n" + "    )\n" + "  ),\n" + "  CALL(meaning)\n" + ")", "sample.px").build();
        // @formatter:on
        final InsightAPI.OnEventHandler return42 = (ctx, frame) -> {
            try {
                ctx.returnValue(Collections.emptyMap());
            } catch (RuntimeException ex) {
                assertTrue("Expecting TruffleException: " + ex, ex instanceof AbstractTruffleException);
            }
            ctx.returnNow(42);
        };
        agentAPI.on("return", return42, createConfig(true, false, false, "meaning.*", null));
        Value fourtyTwo = c.eval(sampleScript);
        agentAPI.off("return", return42);
        assertEquals(42, fourtyTwo.asInt());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Arrays(java.util.Arrays) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Random(java.util.Random) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) InstrumentationTestLanguage(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Map(java.util.Map) Source(org.graalvm.polyglot.Source) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LinkedList(java.util.LinkedList) Before(org.junit.Before) Instrument(org.graalvm.polyglot.Instrument) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) Value(org.graalvm.polyglot.Value) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Insight(org.graalvm.tools.insight.Insight) Executors(java.util.concurrent.Executors) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Collections(java.util.Collections) InsightObjectFactory.createConfig(org.graalvm.tools.insight.test.InsightObjectFactory.createConfig) Assert.assertEquals(org.junit.Assert.assertEquals) Value(org.graalvm.polyglot.Value) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 9 with AbstractTruffleException

use of com.oracle.truffle.api.exception.AbstractTruffleException in project graal by oracle.

the class InteropDefaultsTest method testExceptionDefaults.

@Test
public void testExceptionDefaults() throws UnsupportedMessageException {
    Object empty = new TruffleObject() {
    };
    InteropLibrary emptyLib = createLibrary(InteropLibrary.class, empty);
    assertFalse(emptyLib.isException(empty));
    assertFalse(emptyLib.hasExceptionCause(empty));
    assertFalse(emptyLib.hasExceptionMessage(empty));
    assertFalse(emptyLib.hasExceptionStackTrace(empty));
    assertFails(() -> emptyLib.getExceptionCause(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.getExceptionExitStatus(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.isExceptionIncompleteSource(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.getExceptionMessage(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.getExceptionStackTrace(empty), UnsupportedMessageException.class);
    assertFails(() -> emptyLib.getExceptionType(empty), UnsupportedMessageException.class);
    AbstractTruffleException cause = new Exception("Cause Exception");
    String message = "Enclosing exception";
    AbstractTruffleException exception = new Exception(message, cause);
    InteropLibrary exceptionLib = createLibrary(InteropLibrary.class, exception);
    assertTrue(exceptionLib.isException(exception));
    assertTrue(exceptionLib.hasExceptionCause(exception));
    assertTrue(exceptionLib.hasExceptionMessage(exception));
    assertTrue(exceptionLib.hasExceptionStackTrace(exception));
    assertEquals(cause, exceptionLib.getExceptionCause(exception));
    assertEquals(message, exceptionLib.getExceptionMessage(exception));
    assertEquals(ExceptionType.RUNTIME_ERROR, exceptionLib.getExceptionType(exception));
    assertFalse(exceptionLib.isExceptionIncompleteSource(exception));
    assertFails(() -> exceptionLib.getExceptionExitStatus(exception), UnsupportedMessageException.class);
    exceptionLib.getExceptionStackTrace(exception);
    LegacyCatchableException legacyCatchableException = new LegacyCatchableException(message);
    InteropLibrary legacyCatchableExceptionLib = createLibrary(InteropLibrary.class, legacyCatchableException);
    assertTrue(legacyCatchableExceptionLib.isException(legacyCatchableException));
    assertFalse(legacyCatchableExceptionLib.hasExceptionCause(legacyCatchableException));
    assertTrue(legacyCatchableExceptionLib.hasExceptionMessage(legacyCatchableException));
    assertTrue(legacyCatchableExceptionLib.hasExceptionStackTrace(legacyCatchableException));
    assertFails(() -> legacyCatchableExceptionLib.getExceptionCause(legacyCatchableException), UnsupportedMessageException.class);
    assertEquals(message, legacyCatchableExceptionLib.getExceptionMessage(legacyCatchableException));
    assertEquals(ExceptionType.RUNTIME_ERROR, legacyCatchableExceptionLib.getExceptionType(legacyCatchableException));
    assertFails(() -> legacyCatchableExceptionLib.getExceptionExitStatus(legacyCatchableException), UnsupportedMessageException.class);
    assertFalse(legacyCatchableExceptionLib.isExceptionIncompleteSource(legacyCatchableException));
    legacyCatchableExceptionLib.getExceptionStackTrace(legacyCatchableException);
    LegacyUncatchableException legacyUncatchableException = new LegacyUncatchableException();
    InteropLibrary legacyUncatchableExceptionLib = createLibrary(InteropLibrary.class, legacyUncatchableException);
    assertFalse(legacyUncatchableExceptionLib.isException(legacyUncatchableException));
    assertFalse(legacyUncatchableExceptionLib.hasExceptionCause(legacyUncatchableException));
    assertFalse(legacyUncatchableExceptionLib.hasExceptionMessage(legacyUncatchableException));
    assertFalse(legacyUncatchableExceptionLib.hasExceptionStackTrace(legacyUncatchableException));
    assertFails(() -> legacyUncatchableExceptionLib.getExceptionCause(legacyUncatchableException), UnsupportedMessageException.class);
    assertFails(() -> legacyUncatchableExceptionLib.getExceptionMessage(legacyUncatchableException), UnsupportedMessageException.class);
    assertFails(() -> legacyUncatchableExceptionLib.getExceptionType(legacyUncatchableException), UnsupportedMessageException.class);
    assertFails(() -> legacyUncatchableExceptionLib.getExceptionExitStatus(legacyUncatchableException), UnsupportedMessageException.class);
    assertFails(() -> legacyUncatchableExceptionLib.isExceptionIncompleteSource(legacyUncatchableException), UnsupportedMessageException.class);
    assertFails(() -> legacyUncatchableExceptionLib.getExceptionStackTrace(legacyUncatchableException), UnsupportedMessageException.class);
    LegacyInternalError legacyInternalError = new LegacyInternalError(message);
    InteropLibrary legacyInternalErrorLib = createLibrary(InteropLibrary.class, legacyInternalError);
    assertFalse(legacyInternalErrorLib.isException(legacyInternalError));
    assertFalse(legacyInternalErrorLib.hasExceptionCause(legacyInternalError));
    assertFalse(legacyInternalErrorLib.hasExceptionMessage(legacyInternalError));
    assertFalse(legacyInternalErrorLib.hasExceptionStackTrace(legacyInternalError));
    assertFails(() -> legacyInternalErrorLib.getExceptionCause(legacyInternalError), UnsupportedMessageException.class);
    assertFails(() -> legacyInternalErrorLib.getExceptionMessage(legacyInternalError), UnsupportedMessageException.class);
    assertFails(() -> legacyInternalErrorLib.getExceptionType(legacyInternalError), UnsupportedMessageException.class);
    assertFails(() -> legacyInternalErrorLib.getExceptionExitStatus(legacyInternalError), UnsupportedMessageException.class);
    assertFails(() -> legacyInternalErrorLib.isExceptionIncompleteSource(legacyInternalError), UnsupportedMessageException.class);
    assertFails(() -> legacyInternalErrorLib.getExceptionStackTrace(legacyInternalError), UnsupportedMessageException.class);
}
Also used : InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) StopIterationException(com.oracle.truffle.api.interop.StopIterationException) InteropException(com.oracle.truffle.api.interop.InteropException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)9 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 Test (org.junit.Test)4 Context (org.graalvm.polyglot.Context)3 CallTarget (com.oracle.truffle.api.CallTarget)2 InstrumentationTestLanguage (com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 Source (com.oracle.truffle.api.source.Source)2 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)2 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)2 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2