Search in sources :

Example 26 with PolyglotException

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

the class SLMain method executeSource.

private static int executeSource(Source source, InputStream in, PrintStream out, Map<String, String> options) {
    Context context;
    try {
        context = Context.newBuilder(SL).in(in).out(out).options(options).build();
    } catch (IllegalArgumentException e) {
        System.err.println(e.getMessage());
        return 1;
    }
    out.println("== running on " + context.getEngine());
    try {
        Value result = context.eval(source);
        if (context.getBindings(SL).getMember("main") == null) {
            System.err.println("No function main() defined in SL source file.");
            return 1;
        }
        if (!result.isNull()) {
            out.println(result.toString());
        }
        return 0;
    } catch (PolyglotException ex) {
        if (ex.isInternalError()) {
            // for internal errors we print the full stack trace
            ex.printStackTrace();
        } else {
            System.err.println(ex.getMessage());
        }
        return 1;
    } finally {
        context.close();
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 27 with PolyglotException

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

the class SLExceptionTest method assertException.

private void assertException(boolean failImmediately, String source, String... expectedFrames) {
    boolean initialExecute = true;
    try {
        Value value = ctx.eval("sl", source);
        initialExecute = false;
        if (failImmediately) {
            Assert.fail("Should not reach here.");
        }
        ProxyExecutable proxy = (args) -> args[0].execute();
        value.execute(proxy);
        Assert.fail("Should not reach here.");
    } catch (PolyglotException e) {
        Assert.assertEquals(failImmediately, initialExecute);
        assertFrames(failImmediately, e, expectedFrames);
    }
}
Also used : Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Value(org.graalvm.polyglot.Value) Assert.assertNotSame(org.junit.Assert.assertNotSame) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Source(org.graalvm.polyglot.Source) After(org.junit.After) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Assert.fail(org.junit.Assert.fail) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 28 with PolyglotException

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

the class SLExceptionTest method assertHostException.

private void assertHostException(String source, String... expectedFrames) {
    boolean initialExecute = true;
    RuntimeException[] exception = new RuntimeException[1];
    try {
        Value value = ctx.eval("sl", source);
        initialExecute = false;
        ProxyExecutable proxy = (args) -> {
            throw exception[0] = new RuntimeException();
        };
        value.execute(proxy);
        Assert.fail("Should not reach here.");
    } catch (PolyglotException e) {
        Assert.assertFalse(initialExecute);
        Assert.assertTrue(e.asHostException() == exception[0]);
        assertFrames(false, e, expectedFrames);
    }
}
Also used : Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Value(org.graalvm.polyglot.Value) Assert.assertNotSame(org.junit.Assert.assertNotSame) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Source(org.graalvm.polyglot.Source) After(org.junit.After) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Assert.fail(org.junit.Assert.fail) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 29 with PolyglotException

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

the class SLJavaInteropExceptionTest method testGR7284GuestHostGuestHost.

@Test
public void testGR7284GuestHostGuestHost() throws Exception {
    String sourceText = "function test(validator) {\n" + "  return validator.validateNested();\n" + "}";
    try (Context context = Context.newBuilder(SLLanguage.ID).build()) {
        context.eval(Source.newBuilder(SLLanguage.ID, sourceText, "Test").build());
        Value test = context.getBindings(SLLanguage.ID).getMember("test");
        try {
            test.execute(new Validator());
            fail("expected a PolyglotException but did not throw");
        } catch (PolyglotException ex) {
            assertTrue("expected HostException", ex.isHostException());
            assertThat(ex.asHostException(), instanceOf(NoSuchElementException.class));
            assertNoJavaInteropStackFrames(ex);
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 30 with PolyglotException

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

the class ExpressionTest method testExpression.

@Test
public void testExpression() {
    Assume.assumeThat(testRun, TEST_RESULT_MATCHER);
    boolean success = false;
    try {
        try {
            final Value result = testRun.getSnippet().getExecutableValue().execute(testRun.getActualParameters().toArray());
            TestUtil.validateResult(testRun, result, null);
            success = true;
        } catch (PolyglotException pe) {
            TestUtil.validateResult(testRun, null, pe);
            success = true;
        }
    } finally {
        TEST_RESULT_MATCHER.accept(new AbstractMap.SimpleImmutableEntry<>(testRun, success));
    }
}
Also used : AbstractMap(java.util.AbstractMap) Value(org.graalvm.polyglot.Value) 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