Search in sources :

Example 36 with PolyglotException

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());
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 37 with PolyglotException

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());
    }
}
Also used : StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 38 with PolyglotException

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));
    }
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 39 with PolyglotException

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());
    }
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 40 with PolyglotException

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);
}
Also used : ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) 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