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();
}
}
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);
}
}
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);
}
}
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);
}
}
}
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));
}
}
Aggregations