Search in sources :

Example 1 with ProxyExecutable

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

the class PolyglotExceptionTest method testExceptionWrapping.

@Test
public void testExceptionWrapping() {
    Context context = Context.create();
    Context otherContext = Context.create();
    CauseErrorTruffleObject causeError = new CauseErrorTruffleObject();
    causeError.thrownError = new TestGuestError();
    Value throwError = context.asValue(causeError);
    Value throwErrorOtherContext = otherContext.asValue(causeError);
    try {
        throwError.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        Assert.assertEquals(e.getMessage(), "MyError");
        Assert.assertTrue(e.isGuestException());
    }
    Value verifyError = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            try {
                throwError.execute();
                Assert.fail();
            } catch (PolyglotException e) {
                Assert.assertEquals(e.getMessage(), "MyError");
                Assert.assertTrue(e.isGuestException());
                throw e;
            }
            return null;
        }
    });
    try {
        verifyError.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        Assert.assertEquals(e.getMessage(), "MyError");
        Assert.assertTrue(e.isGuestException());
    }
    // if the exception was thrown by a different context it will be treated
    // as a host exception.
    Value verifyErrorOtherContext = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            try {
                throwErrorOtherContext.execute();
            } catch (PolyglotException e) {
                Assert.assertEquals(e.getMessage(), "MyError");
                Assert.assertTrue(e.isGuestException());
                throw e;
            }
            return null;
        }
    });
    try {
        verifyErrorOtherContext.execute();
        Assert.fail();
    } catch (PolyglotException e) {
        // assert that polyglot exception was not unboxed if from other context
        Assert.assertTrue(e.asHostException() instanceof PolyglotException);
        PolyglotException polyglot = (PolyglotException) e.asHostException();
        Assert.assertEquals(polyglot.getMessage(), "MyError");
        Assert.assertTrue(polyglot.isGuestException());
    }
    context.close();
    otherContext.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) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 2 with ProxyExecutable

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

the class ProxyAPITest method testExceptionFrames.

@Test
public void testExceptionFrames() {
    Value innerInner = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            throw new RuntimeException("foobar");
        }
    });
    Value inner = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            return innerInner.execute();
        }
    });
    Value outer = context.asValue(new ProxyExecutable() {

        public Object execute(Value... arguments) {
            return inner.execute();
        }
    });
    try {
        outer.execute();
        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 < 6; i++) {
            frame = frameIterator.next();
            assertTrue(frame.isHostFrame());
            assertEquals("execute", frame.toHostFrame().getMethodName());
        }
        frame = frameIterator.next();
        assertTrue(frame.isHostFrame());
        assertEquals("testExceptionFrames", frame.toHostFrame().getMethodName());
    }
}
Also used : ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) 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) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 3 with ProxyExecutable

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

the class PolyglotNativeAPI method polyglot_create_function.

@CEntryPoint(name = "polyglot_create_function")
public static PolyglotStatus polyglot_create_function(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, PolyglotCallbackPointer callback, VoidPointer data, PolyglotValuePointerPointer value) {
    return withHandledErrors(() -> {
        Context c = ObjectHandles.getGlobal().get(polyglot_context);
        ProxyExecutable executable = (Value... arguments) -> {
            ObjectHandle[] handleArgs = new ObjectHandle[arguments.length];
            for (int i = 0; i < arguments.length; i++) {
                handleArgs[i] = createHandle(arguments[i]);
            }
            PolyglotCallbackInfo cbInfo = (PolyglotCallbackInfo) createHandle(new PolyglotCallbackInfoInternal(handleArgs, data));
            try {
                PolyglotValuePointer result = callback.invoke(CEntryPointContext.getCurrentIsolateThread(), cbInfo);
                CallbackException ce = exceptionsTL.get();
                if (ce != null) {
                    exceptionsTL.remove();
                    throw ce;
                } else {
                    return PolyglotNativeAPI.fetchHandle(result);
                }
            } finally {
                PolyglotCallbackInfoInternal info = fetchHandle(cbInfo);
                for (ObjectHandle arg : info.arguments) {
                    freeHandle(arg);
                }
                freeHandle(cbInfo);
            }
        };
        value.write(createHandle(c.asValue(executable)));
    });
}
Also used : CEntryPointContext(org.graalvm.nativeimage.c.function.CEntryPointContext) Context(org.graalvm.polyglot.Context) PolyglotCallbackInfo(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotCallbackInfo) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) ObjectHandle(org.graalvm.nativeimage.ObjectHandle) Value(org.graalvm.polyglot.Value) PolyglotValuePointer(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotValuePointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 4 with ProxyExecutable

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

the class LLVMInteropTest method test010.

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

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

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

the class LLVMInteropTest method test012.

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

            @Override
            public Object execute(Value... t) {
                Assert.assertEquals("argument count", 2, t.length);
                return t[0].asDouble() + t[1].asDouble();
            }
        }, "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)

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