Search in sources :

Example 6 with ProxyExecutable

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

the class LLVMInteropTest method test008.

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

            @Override
            public Object execute(Value... t) {
                return t[0].asByte() + t[1].asByte();
            }
        }, "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 7 with ProxyExecutable

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

the class LLVMInteropTest method testConstruct002.

@Test
public void testConstruct002() {
    final StringBuilder buf;
    try (Runner runner = new Runner("construct002")) {
        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\natexit\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)

Example 8 with ProxyExecutable

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

the class PolyglotEngineOptionsTest method installSLBuiltin.

private static void installSLBuiltin(Context context, NodeFactory<? extends SLBuiltinNode> builtin) {
    context.eval("sl", "function installBuiltin(e) { return e(); }");
    context.getBindings("sl").getMember("installBuiltin").execute(new ProxyExecutable() {

        @Override
        public Object execute(Value... t) {
            SLContext.getCurrent().installBuiltin(builtin);
            return true;
        }
    });
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value)

Example 9 with ProxyExecutable

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

the class SLExceptionTest method assertException.

private void assertException(boolean failImmediately, String source, String... expectedFrames) {
    boolean initialExecute = true;
    try {
        Value value = ctx.eval("sl", source);
        initialExecute = false;
        if (failImmediately) {
            Assert.fail("Should not reach here.");
        }
        ProxyExecutable proxy = (args) -> args[0].execute();
        value.execute(proxy);
        Assert.fail("Should not reach here.");
    } catch (PolyglotException e) {
        Assert.assertEquals(failImmediately, initialExecute);
        assertFrames(failImmediately, e, expectedFrames);
    }
}
Also used : Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Value(org.graalvm.polyglot.Value) Assert.assertNotSame(org.junit.Assert.assertNotSame) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Source(org.graalvm.polyglot.Source) After(org.junit.After) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Assert.fail(org.junit.Assert.fail) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

Example 10 with ProxyExecutable

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

the class SLExceptionTest method assertHostException.

private void assertHostException(String source, String... expectedFrames) {
    boolean initialExecute = true;
    RuntimeException[] exception = new RuntimeException[1];
    try {
        Value value = ctx.eval("sl", source);
        initialExecute = false;
        ProxyExecutable proxy = (args) -> {
            throw exception[0] = new RuntimeException();
        };
        value.execute(proxy);
        Assert.fail("Should not reach here.");
    } catch (PolyglotException e) {
        Assert.assertFalse(initialExecute);
        Assert.assertTrue(e.asHostException() == exception[0]);
        assertFrames(false, e, expectedFrames);
    }
}
Also used : Iterator(java.util.Iterator) PolyglotException(org.graalvm.polyglot.PolyglotException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Value(org.graalvm.polyglot.Value) Assert.assertNotSame(org.junit.Assert.assertNotSame) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StackFrame(org.graalvm.polyglot.PolyglotException.StackFrame) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Source(org.graalvm.polyglot.Source) After(org.junit.After) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Assert.fail(org.junit.Assert.fail) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) PolyglotException(org.graalvm.polyglot.PolyglotException)

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