Search in sources :

Example 41 with PolyglotException

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

the class LanguageSPITest method testExceptionGetSourceLocation.

@Test
public void testExceptionGetSourceLocation() {
    try (final Context context = Context.create(LanguageSPITestLanguage.ID)) {
        final String text = "0123456789";
        LanguageSPITestLanguage.runinside = (env) -> {
            Source src = Source.newBuilder(text).mimeType(LanguageSPITestLanguage.ID).name("test.txt").build();
            throw new ParseException(src, 1, 2);
        };
        try {
            context.eval(LanguageSPITestLanguage.ID, text);
            Assert.fail("PolyglotException expected.");
        } catch (PolyglotException pe) {
            Assert.assertTrue(pe.isSyntaxError());
            Assert.assertEquals("12", pe.getSourceLocation().getCharacters().toString());
        } finally {
            LanguageSPITestLanguage.runinside = null;
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 42 with PolyglotException

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

the class LanguageSPITest method testCancelExecutionWhileSleeping.

@Test
public void testCancelExecutionWhileSleeping() throws InterruptedException {
    ExecutorService service = Executors.newFixedThreadPool(1);
    try {
        Engine engine = Engine.create();
        Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
        CountDownLatch beforeSleep = new CountDownLatch(1);
        CountDownLatch interrupt = new CountDownLatch(1);
        AtomicInteger gotInterrupt = new AtomicInteger(0);
        Function<Env, Object> f = new Function<Env, Object>() {

            public Object apply(Env t) {
                try {
                    beforeSleep.countDown();
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    gotInterrupt.incrementAndGet();
                    interrupt.countDown();
                    throw new Interrupted();
                }
                return null;
            }
        };
        Future<Value> future = service.submit(() -> eval(context, f));
        beforeSleep.await(10000, TimeUnit.MILLISECONDS);
        context.close(true);
        interrupt.await(10000, TimeUnit.MILLISECONDS);
        assertEquals(1, gotInterrupt.get());
        try {
            future.get();
            fail();
        } catch (ExecutionException e) {
            PolyglotException polyglotException = (PolyglotException) e.getCause();
            assertTrue(polyglotException.isCancelled());
        }
        engine.close();
    } finally {
        service.shutdown();
    }
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) CountDownLatch(java.util.concurrent.CountDownLatch) Env(com.oracle.truffle.api.TruffleLanguage.Env) PolyglotException(org.graalvm.polyglot.PolyglotException) Function(java.util.function.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ExecutionException(java.util.concurrent.ExecutionException) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 43 with PolyglotException

use of org.graalvm.polyglot.PolyglotException in project sulong by graalvm.

the class LLVMLauncher method execute.

protected int execute(Context.Builder contextBuilder) {
    contextBuilder.arguments(getLanguageId(), programArgs);
    try (Context context = contextBuilder.build()) {
        runVersionAction(versionAction, context.getEngine());
        Value library = context.eval(Source.newBuilder(getLanguageId(), file).build());
        if (!library.canExecute()) {
            throw abort("no main function found");
        }
        int status = library.execute().asInt();
        if (printResult) {
            System.out.println("Result: " + status);
        }
        return status;
    } catch (PolyglotException e) {
        if (e.isExit()) {
            return e.getExitStatus();
        }
        throw e;
    } catch (IOException e) {
        throw abort(e);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) IOException(java.io.IOException) PolyglotException(org.graalvm.polyglot.PolyglotException)

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