use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class DefaultResultVerifier method accept.
@Override
public void accept(final SnippetRun snippetRun) throws PolyglotException {
final PolyglotException exception = snippetRun.getException();
if (exception != null) {
throw exception;
}
final TypeDescriptor resultType = TypeDescriptor.forValue(snippetRun.getResult());
final TypeDescriptor snippetReturnType = snippetRun.getSnippet().getReturnType();
if (!snippetReturnType.isAssignable(resultType)) {
throw new AssertionError(String.format("Result is out of type bounds. Expected: %s, Got: %s.", snippetReturnType, resultType));
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLInstrumentTest method testEarlyReturn.
/**
* Test that we can forcibly return early from call nodes with an arbitrary value.
*/
@Test
public void testEarlyReturn() throws Exception {
String code = "function main() {\n" + " a = 10;\n" + " b = a;\n" + " // Let fce() warm up and specialize:\n" + " while (a == b && a < 100000) {\n" + " a = fce(a);\n" + " b = b + 1;\n" + " }\n" + " c = a;\n" + " // Run fce() and alter it's return type in an instrument:\n" + " c = fce(c);\n" + " return c;\n" + "}\n" + "function fce(x) {\n" + " return x + 1;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
Engine engine = Engine.newBuilder().err(engineOut).build();
Context context = Context.newBuilder().engine(engine).build();
// No instrument:
Value ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = 200000L;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(200001L, ret.asLong());
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertFalse(ret.isNumber());
assertTrue(ret.isString());
assertEquals("Hello!1", ret.asString());
// Specialize to long again:
earlyReturn.fceCode = "<>";
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = new BigInteger("-42");
boolean interopFailure;
try {
context.eval(source);
interopFailure = false;
} catch (PolyglotException err) {
interopFailure = true;
}
assertTrue(interopFailure);
earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-41L, ret.asLong());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = Boolean.TRUE;
ret = context.eval(source);
assertTrue(ret.isBoolean());
assertEquals(Boolean.TRUE, ret.asBoolean());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = -42.42;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-42.42, ret.asDouble(), 1e-8);
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertTrue(ret.isString());
assertEquals("Hello!", ret.asString());
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLJavaInteropExceptionTest method assertNoJavaInteropStackFrames.
@SuppressWarnings("deprecation")
private static void assertNoJavaInteropStackFrames(PolyglotException ex) {
String javaInteropPackageName = com.oracle.truffle.api.interop.java.JavaInterop.class.getName().substring(0, com.oracle.truffle.api.interop.java.JavaInterop.class.getName().lastIndexOf('.') + 1);
assertFalse("expected no java interop stack trace elements", Arrays.stream(ex.getStackTrace()).anyMatch(ste -> ste.getClassName().startsWith(javaInteropPackageName)));
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLJavaInteropExceptionTest method testFunctionProxy.
@Test
public void testFunctionProxy() throws Exception {
String javaMethod = "validateFunction";
String sourceText = "" + "function supplier() {\n" + " return error();\n" + "}\n" + "function test(validator) {\n" + " return validator." + javaMethod + "(supplier);\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) {
StackTraceElement last = null;
boolean found = false;
for (StackTraceElement curr : ex.getStackTrace()) {
if (curr.getMethodName().contains(javaMethod)) {
assertNotNull(last);
assertThat("expected Proxy stack frame", last.getClassName(), containsString("Proxy"));
found = true;
break;
}
last = curr;
}
assertTrue(javaMethod + " not found in stack trace", found);
}
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class SLJavaInteropExceptionTest method testGR7284.
@Test
public void testGR7284() throws Exception {
String sourceText = "function test(validator) {\n" + " return validator.validateException();\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);
}
}
}
Aggregations