use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLParseErrorTest method testParseError.
@Test
public void testParseError() throws IOException {
try {
final Source src = Source.newBuilder("sl", "function testSyntaxError(a) {break;} function main() {return testSyntaxError;}", "testSyntaxError.sl").build();
context.eval(src);
Assert.assertTrue("Should not reach here.", false);
} catch (PolyglotException e) {
Assert.assertTrue("Should be a syntax error.", e.isSyntaxError());
Assert.assertNotNull("Should have source section.", e.getSourceLocation());
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLTCKLanguageProvider method createExpressions.
@Override
public Collection<? extends Snippet> createExpressions(Context context) {
final Collection<Snippet> res = new ArrayList<>();
final Value fnc = eval(context, String.format(PATTERN_BIN_OP_FNC, "add", "+"), "add");
Snippet.Builder opb = Snippet.newBuilder("+", fnc, TypeDescriptor.NUMBER).parameterTypes(TypeDescriptor.NUMBER, TypeDescriptor.NUMBER);
res.add(opb.build());
opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.STRING, TypeDescriptor.ANY);
res.add(opb.build());
opb = Snippet.newBuilder("+", fnc, TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, TypeDescriptor.STRING);
res.add(opb.build());
res.add(createBinaryOperator(context, "-", "sub", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, "*", "mul", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, "/", "div", TypeDescriptor.NUMBER, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).resultVerifier((snippetRun) -> {
final Value dividend = snippetRun.getParameters().get(0);
final Value divider = snippetRun.getParameters().get(1);
final PolyglotException exception = snippetRun.getException();
if (dividend.isNumber() && divider.fitsInDouble() && divider.asDouble() == 0) {
Assert.assertNotNull(exception);
} else if (exception != null) {
throw exception;
} else {
Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
}
}).build());
res.add(createBinaryOperator(context, "==", "eq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
res.add(createBinaryOperator(context, "!=", "neq", TypeDescriptor.BOOLEAN, TypeDescriptor.ANY, TypeDescriptor.ANY).build());
res.add(createBinaryOperator(context, "<=", "le", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, ">=", "ge", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, "<", "l", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, ">", "g", TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.NUMBER).build());
res.add(createBinaryOperator(context, "||", "or", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
final Value firstParam = snippetRun.getParameters().get(0);
final Value secondParam = snippetRun.getParameters().get(1);
final PolyglotException exception = snippetRun.getException();
if (firstParam.isBoolean() && !firstParam.asBoolean() && !secondParam.isBoolean()) {
Assert.assertNotNull(exception);
} else if (exception != null) {
throw exception;
} else {
Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
}
}).build());
res.add(createBinaryOperator(context, "&&", "land", TypeDescriptor.BOOLEAN, TypeDescriptor.BOOLEAN, TypeDescriptor.ANY).resultVerifier((snippetRun) -> {
final Value firstParam = snippetRun.getParameters().get(0);
final Value secondParam = snippetRun.getParameters().get(1);
final PolyglotException exception = snippetRun.getException();
if (firstParam.isBoolean() && firstParam.asBoolean() && !secondParam.isBoolean()) {
Assert.assertNotNull(exception);
} else if (exception != null) {
throw exception;
} else {
Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(snippetRun.getResult())));
}
}).build());
res.add(createPostfixOperator(context, "()", "callNoArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY)).build());
res.add(createPostfixOperator(context, "(1)", "callOneArg", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER)).build());
res.add(createPostfixOperator(context, "(1, \"\")", "callTwoArgs", TypeDescriptor.NULL, TypeDescriptor.executable(TypeDescriptor.ANY, TypeDescriptor.NUMBER, TypeDescriptor.STRING)).build());
return Collections.unmodifiableCollection(res);
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLExceptionTest method testProxyGuestLanguageStack.
@Test
public void testProxyGuestLanguageStack() {
Value bar = ctx.eval("sl", "function foo(f) { f(); } function bar(f) { return foo(f); } function main() { return bar; }");
TestProxy proxy = new TestProxy(3, bar);
try {
bar.execute(proxy);
fail();
} catch (PolyglotException e) {
assertProxyException(proxy, e);
for (PolyglotException seenException : proxy.seenExceptions) {
// exceptions are unwrapped and wrapped again
assertNotSame(e, seenException);
assertSame(e.asHostException(), seenException.asHostException());
}
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLTestRunner method run.
private static void run(Context context, Path path, PrintWriter out) throws IOException {
try {
/* Parse the SL source file. */
Source source = Source.newBuilder(SLLanguage.ID, path.toFile()).interactive(true).build();
/* Call the main entry point, without any arguments. */
context.eval(source);
} catch (PolyglotException ex) {
if (!ex.isInternalError()) {
out.println(ex.getMessage());
} else {
throw ex;
}
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class UnwindReenterReturnTest method testReenterSimpleOnException.
@Test
public void testReenterSimpleOnException() throws Exception {
List<CodeAction> actionsExc = new ArrayList<>();
List<CodeAction> actionsUnwind = new ArrayList<>();
// Throw unwind from return exception and do reenter:
actionsExc.add(new CodeAction("CALL(a)", TestControlFlow.ACTION.UNWIND));
testControlFlow.actions.put(TestControlFlow.WHERE.RETURN_EXCEPTIONAL, actionsExc);
actionsUnwind.add(new CodeAction("CALL(a)", TestControlFlow.ACTION.REENTER));
testControlFlow.actions.put(TestControlFlow.WHERE.UNWIND, actionsUnwind);
String message = null;
try {
run(CODE1_EXC);
fail();
} catch (PolyglotException pe) {
message = pe.getMessage();
assertTrue(message, message.contains("Identifier redefinition not supported"));
}
assertEquals(0, actionsExc.size());
assertEquals(0, actionsUnwind.size());
assertEquals("[CALL(a), CALL(a)]", testControlFlow.nodesEntered.toString());
assertEquals("[CALL(a), CALL(a)]", testControlFlow.nodesReturned.toString());
assertEquals("[" + message + ", " + message + "]", testControlFlow.returnValuesExceptions.toString());
assertEquals("[CALL(a)]", testControlFlow.nodesUnwound.toString());
assertEquals("[null]", testControlFlow.unwoundInfos.toString());
}
Aggregations