use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ValueHostConversionTest method testExceptionFrames1.
@Test
public void testExceptionFrames1() {
Value innerInner = context.asValue(new Function<Object, Object>() {
public Object apply(Object t) {
throw new RuntimeException("foobar");
}
});
Value inner = context.asValue(new Function<Object, Object>() {
public Object apply(Object t) {
return innerInner.execute(t);
}
});
Value outer = context.asValue(new Function<Object, Object>() {
public Object apply(Object t) {
return inner.execute(t);
}
});
try {
outer.execute(1);
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foobar", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
for (int i = 0; i < 3; i++) {
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("apply", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("execute", frame.toHostFrame().getMethodName());
}
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames1", frame.toHostFrame().getMethodName());
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ValueHostConversionTest method testExceptionFrames3.
@Test
public void testExceptionFrames3() {
Value value = context.asValue(new TestExceptionFrames2());
TestExceptionFrames3 f = value.as(TestExceptionFrames3.class);
try {
f.foo();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foo", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("foo", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("invoke", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("foo", frame.toHostFrame().getMethodName());
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames3", frame.toHostFrame().getMethodName());
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ValueHostInteropTest method testException.
@Test
public void testException() {
Value iterator = context.asValue(Collections.emptyList().iterator());
try {
iterator.getMember("next").execute();
fail("expected an exception but none was thrown");
} catch (PolyglotException ex) {
assertTrue("expected HostException but was: " + ex.getClass(), ex.isHostException());
assertThat(ex.asHostException(), CoreMatchers.instanceOf(NoSuchElementException.class));
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ProxyAPITest method testProxyPrimitive.
@SuppressWarnings("deprecation")
@Test
public void testProxyPrimitive() {
ProxyPrimitiveTest proxy = new ProxyPrimitiveTest();
Value value = context.asValue(proxy);
assertTrue(value.isProxyObject());
assertSame(proxy, value.asProxyObject());
// no need to invoke asPrimitive yet.
assertEquals(0, proxy.invocationCounter);
assertProxyPrimitive(proxy, value, false, Boolean.class, BOOLEAN, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, "a", String.class, STRING, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, 'a', Character.class, STRING, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, (byte) 42, Byte.class, NUMBER, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, (short) 42, Short.class, NUMBER, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, 42, Integer.class, NUMBER, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, 42L, Long.class, NUMBER, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, 42.0f, Float.class, NUMBER, PROXY_OBJECT);
assertProxyPrimitive(proxy, value, 42.0d, Double.class, NUMBER, PROXY_OBJECT);
// test errors
proxy.primitive = null;
try {
// force to unbox the primitive
value.isNumber();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof IllegalStateException);
}
RuntimeException e = new RuntimeException();
try {
value = context.asValue(new org.graalvm.polyglot.proxy.ProxyPrimitive() {
public Object asPrimitive() {
throw e;
}
});
// force to unbox the primitive
value.isNumber();
} catch (PolyglotException ex) {
assertTrue(ex.isHostException());
assertSame(e, ex.asHostException());
assertFalse(ex.isInternalError());
}
}
use of org.graalvm.polyglot.PolyglotException in project graal by oracle.
the class ProxyAPITest method testProxyInstantiable.
@Test
public void testProxyInstantiable() {
ProxyInstantiableTest proxy = new ProxyInstantiableTest();
Value value = context.asValue(proxy);
assertTrue(value.canInstantiate());
assertSame(proxy, value.asProxyObject());
assertEquals(0, proxy.newInstanceCounter);
proxy.newInstance = (args) -> {
assertEquals(2, args.length);
assertEquals("a", args[0].asString());
assertEquals('a', args[0].as(Object.class));
ValueAssert.assertValue(context, args[0], Trait.STRING);
assertTrue(args[1].isNumber());
assertEquals((byte) 42, args[1].asByte());
assertEquals((short) 42, args[1].asShort());
assertEquals(42, args[1].asInt());
assertEquals(42L, args[1].asLong());
assertEquals(42, args[1].as(Object.class));
ValueAssert.assertValue(context, args[1], Trait.NUMBER);
return 42;
};
assertEquals(42, value.newInstance('a', 42).asInt());
assertEquals(1, proxy.newInstanceCounter);
final RuntimeException ex = new RuntimeException();
proxy.newInstance = (args) -> {
throw ex;
};
try {
value.newInstance();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertSame(ex, e.asHostException());
assertEquals(2, proxy.newInstanceCounter);
}
assertValue(context, value, Trait.PROXY_OBJECT, Trait.INSTANTIABLE);
}
Aggregations