Search in sources :

Example 1 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project graal by oracle.

the class TimeBoxingTest method testTimeBoxing.

@Test
public void testTimeBoxing() throws Exception {
    final Context context = Context.create();
    Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(LOOP(infinity,STATEMENT))", "NotEnoughTime").buildLiteral();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
            debugger.startSession(new SuspendedCallback() {

                public void onSuspend(SuspendedEvent event) {
                    event.prepareKill();
                }
            }).suspendNextExecution();
        }
    }, 1000);
    try {
        // throws KillException, wrapped by PolyglotException
        context.eval(source);
        Assert.fail();
    } catch (PolyglotException pex) {
        Assert.assertEquals("com.oracle.truffle.api.debug.KillException", pex.getMessage());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 2 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project graal by oracle.

the class LanguageSPITest method testLookupHostDisabled.

@Test
public void testLookupHostDisabled() {
    Context context = Context.newBuilder().allowHostAccess(false).build();
    try {
        eval(context, new Function<Env, Object>() {

            public Object apply(Env t) {
                return t.lookupHostSymbol("java.util.HashMap");
            }
        });
        fail();
    } catch (PolyglotException e) {
        assertTrue(!e.isInternalError());
    }
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Env(com.oracle.truffle.api.TruffleLanguage.Env) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 3 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project graal by oracle.

the class MultiThreadedLanguageTest method testAsssertionIfThreadStillActive.

@Test
public void testAsssertionIfThreadStillActive() throws InterruptedException {
    MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
        return true;
    };
    Engine engine = Engine.create();
    Context context = Context.newBuilder().allowCreateThread(true).engine(engine).build();
    Semaphore wait = new Semaphore(0);
    Thread returnThread = eval(context, new Function<Env, Object>() {

        public Object apply(Env env) {
            Semaphore waitForEnter = new Semaphore(0);
            Thread t = env.createThread(() -> {
                try {
                    waitForEnter.release();
                    wait.acquire();
                } catch (InterruptedException e) {
                }
            });
            t.start();
            try {
                waitForEnter.acquire();
            } catch (InterruptedException e) {
            }
            return t;
        }
    }).asHostObject();
    try {
        engine.close();
        Assert.fail();
    } catch (PolyglotException e) {
        assertTrue(e.isInternalError());
        assertTrue(e.getMessage().contains("The language did not complete all polyglot threads but should have"));
    }
    wait.release(1);
    returnThread.join();
    engine.close();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) Function(java.util.function.Function) Semaphore(java.util.concurrent.Semaphore) Env(com.oracle.truffle.api.TruffleLanguage.Env) PolyglotException(org.graalvm.polyglot.PolyglotException) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 4 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project graal by oracle.

the class PolyglotExceptionTest method testExceptionWrapping.

@Test
public void testExceptionWrapping() {
    Context context = Context.create();
    Context otherContext = Context.create();
    CauseErrorTruffleObject causeError = new CauseErrorTruffleObject();
    causeError.thrownError = new TestGuestError();
    Value throwError = context.asValue(causeError);
    Value throwErrorOtherContext = otherContext.asValue(causeError);
    try {
        throwError.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        Assert.assertEquals(e.getMessage(), "MyError");
        Assert.assertTrue(e.isGuestException());
    }
    Value verifyError = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            try {
                throwError.execute();
                Assert.fail();
            } catch (PolyglotException e) {
                Assert.assertEquals(e.getMessage(), "MyError");
                Assert.assertTrue(e.isGuestException());
                throw e;
            }
            return null;
        }
    });
    try {
        verifyError.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        Assert.assertEquals(e.getMessage(), "MyError");
        Assert.assertTrue(e.isGuestException());
    }
    // if the exception was thrown by a different context it will be treated
    // as a host exception.
    Value verifyErrorOtherContext = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            try {
                throwErrorOtherContext.execute();
            } catch (PolyglotException e) {
                Assert.assertEquals(e.getMessage(), "MyError");
                Assert.assertTrue(e.isGuestException());
                throw e;
            }
            return null;
        }
    });
    try {
        verifyErrorOtherContext.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        // assert that polyglot exception was not unboxed if from other context
        Assert.assertTrue(e.asHostException() instanceof PolyglotException);
        PolyglotException polyglot = (PolyglotException) e.asHostException();
        Assert.assertEquals(polyglot.getMessage(), "MyError");
        Assert.assertTrue(polyglot.isGuestException());
    }
    context.close();
    otherContext.close();
}
Also used : Context(org.graalvm.polyglot.Context) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 5 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project graal by oracle.

the class ProxyAPITest method testExceptionFrames.

@Test
public void testExceptionFrames() {
    Value innerInner = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            throw new RuntimeException("foobar");
        }
    });
    Value inner = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            return innerInner.execute();
        }
    });
    Value outer = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            return inner.execute();
        }
    });
    try {
        outer.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        assertTrue(e.isHostException());
        assertTrue(e.asHostException() instanceof RuntimeException);
        assertEquals("foobar", e.getMessage());
        Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
        StackFrame frame;
        for (int i = 0; i < 6; i++) {
            frame = frameIterator.next();
            assertTrue(frame.isHostFrame());
            assertEquals("execute", frame.toHostFrame().getMethodName());
        }
        frame = frameIterator.next();
        assertTrue(frame.isHostFrame());
        assertEquals("testExceptionFrames", frame.toHostFrame().getMethodName());
    }
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Aggregations

PolyglotException (org.graalvm.polyglot.PolyglotException)43 Value (org.graalvm.polyglot.Value)32 Test (org.junit.Test)32 Context (org.graalvm.polyglot.Context)19 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)9 Source (org.graalvm.polyglot.Source)9 ArrayList (java.util.ArrayList)7 Iterator (java.util.Iterator)6 StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)6 AbstractMap (java.util.AbstractMap)5 TruffleContext (com.oracle.truffle.api.TruffleContext)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 Engine (org.graalvm.polyglot.Engine)4 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)4 Env (com.oracle.truffle.api.TruffleLanguage.Env)3 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)3 IOException (java.io.IOException)3 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 CodeAction (com.oracle.truffle.api.instrumentation.test.UnwindReenterReturnTest.TestControlFlow.CodeAction)2