Search in sources :

Example 11 with ProxyExecutable

use of org.graalvm.polyglot.proxy.ProxyExecutable in project graal by oracle.

the class PolyglotExceptionTest method testLanguageExceptionUnwrapping.

@Test
public void testLanguageExceptionUnwrapping() {
    Context context = Context.create();
    Value throwError = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            throw new RuntimeException();
        }
    });
    VerifyErrorTruffleObject checkErrorObj = new VerifyErrorTruffleObject();
    Value checkError = context.asValue(checkErrorObj);
    checkErrorObj.verifyError = (e) -> {
        Assert.assertTrue(e instanceof TruffleException);
        Assert.assertEquals("HostException", e.getClass().getSimpleName());
    };
    Assert.assertTrue(checkError.execute(throwError).asBoolean());
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleException(com.oracle.truffle.api.TruffleException) Test(org.junit.Test)

Example 12 with ProxyExecutable

use of org.graalvm.polyglot.proxy.ProxyExecutable in project graal by oracle.

the class ProxySPITest method testProxyExecutable.

@Test
public void testProxyExecutable() throws Throwable {
    Context context = Context.create();
    ProxyExecutable proxyOuter = new ProxyExecutable() {

        public Object execute(Value... t) {
            return t[0].asInt();
        }
    };
    eval(context, proxyOuter, (proxyInner) -> {
        assertEquals(true, Message.IS_EXECUTABLE, proxyInner);
        assertEquals(42, Message.createExecute(0), proxyInner, 42);
        assertUnsupported(Message.createNew(0), proxyInner, 42);
        assertUnsupported(Message.AS_POINTER, proxyInner);
        assertUnsupported(Message.GET_SIZE, proxyInner);
        assertEmpty(Message.KEYS, proxyInner);
        assertUnsupported(Message.READ, proxyInner);
        assertUnsupported(Message.WRITE, proxyInner);
        assertUnsupported(Message.TO_NATIVE, proxyInner);
        assertUnsupported(Message.UNBOX, proxyInner);
        assertUnsupported(Message.createInvoke(0), proxyInner);
        assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
        assertEquals(false, Message.IS_BOXED, proxyInner);
        assertEquals(false, Message.IS_NULL, proxyInner);
        assertEquals(false, Message.HAS_KEYS, proxyInner);
        assertEquals(false, Message.HAS_SIZE, proxyInner);
        assertEquals(false, Message.IS_POINTER, proxyInner);
        assertEquals(0, Message.KEY_INFO, proxyInner);
        return null;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 13 with ProxyExecutable

use of org.graalvm.polyglot.proxy.ProxyExecutable in project sulong by graalvm.

the class LLVMInteropTest method test009.

@Test
public void test009() {
    try (Runner runner = new Runner("interop009")) {
        runner.export(new ProxyExecutable() {

            @Override
            public Object execute(Value... t) {
                return t[0].asInt() + t[1].asInt();
            }
        }, "foreign");
        Assert.assertEquals(42, runner.run());
    }
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) BoxedTestValue(com.oracle.truffle.llvm.test.interop.values.BoxedTestValue) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 14 with ProxyExecutable

use of org.graalvm.polyglot.proxy.ProxyExecutable in project sulong by graalvm.

the class LLVMInteropTest method test011.

@Test
public void test011() {
    try (Runner runner = new Runner("interop011")) {
        runner.export(new ProxyExecutable() {

            @Override
            public Object execute(Value... t) {
                return t[0].asFloat() + t[1].asFloat();
            }
        }, "foreign");
        Assert.assertEquals(42.0, runner.run(), 0.1);
    }
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) BoxedTestValue(com.oracle.truffle.llvm.test.interop.values.BoxedTestValue) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Example 15 with ProxyExecutable

use of org.graalvm.polyglot.proxy.ProxyExecutable in project sulong by graalvm.

the class LLVMInteropTest method testConstruct001.

@Test
public void testConstruct001() {
    final StringBuilder buf;
    try (Runner runner = new Runner("construct001")) {
        buf = new StringBuilder();
        runner.export(new ProxyExecutable() {

            @Override
            public Object execute(Value... t) {
                Assert.assertEquals("argument count", 1, t.length);
                if (t[0].isString()) {
                    buf.append(t[0].asString());
                } else {
                    Assert.fail("unexpected value type");
                }
                return 0;
            }
        }, "callback");
        runner.load();
        Assert.assertEquals("construct\n", buf.toString());
    }
    Assert.assertEquals("construct\ndestruct\n", buf.toString());
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) BoxedTestValue(com.oracle.truffle.llvm.test.interop.values.BoxedTestValue) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) Test(org.junit.Test)

Aggregations

Value (org.graalvm.polyglot.Value)17 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)17 Test (org.junit.Test)14 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)10 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)10 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)9 Context (org.graalvm.polyglot.Context)6 PolyglotException (org.graalvm.polyglot.PolyglotException)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 StackFrame (org.graalvm.polyglot.PolyglotException.StackFrame)3 List (java.util.List)2 Source (org.graalvm.polyglot.Source)2 After (org.junit.After)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertFalse (org.junit.Assert.assertFalse)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertNotSame (org.junit.Assert.assertNotSame)2