Search in sources :

Example 6 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class LanguageSPITest method testPolyglotBindings.

@Test
public void testPolyglotBindings() {
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return Truffle.getRuntime().createCallTarget(new RootNode(languageInstance) {

                @Override
                public Object execute(VirtualFrame frame) {
                    return getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
                }
            });
        }
    });
    Context c = Context.create();
    Value languageBindings = c.eval(ProxyLanguage.ID, "");
    Value polyglotBindings = c.getPolyglotBindings();
    polyglotBindings.putMember("foo", "bar");
    assertEquals("bar", polyglotBindings.getMember("foo").asString());
    assertEquals("bar", languageBindings.getMember("foo").asString());
    languageBindings.putMember("baz", "42");
    assertEquals("42", polyglotBindings.getMember("baz").asString());
    assertEquals("42", languageBindings.getMember("baz").asString());
    ValueAssert.assertValue(c, polyglotBindings);
    ValueAssert.assertValue(c, languageBindings);
    c.close();
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Value(org.graalvm.polyglot.Value) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 7 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class MultiThreadedLanguageTest method evalAndWait.

/**
 * Method evals code on another thread and waits until the function is finished evaluation. The
 * future then waits until Future#get is invoked to complete the operation.
 */
private static Completable<Value> evalAndWait(ExecutorService executor, Context context, boolean manualComplete, CountDownLatch latch, Function<Env, Object> f) {
    Semaphore waitInEval = manualComplete ? new Semaphore(0) : null;
    Future<Value> valueResult = executor.<Value>submit(() -> {
        try {
            return eval(context, (env) -> {
                Object result = f.apply(env);
                try {
                    if (latch != null) {
                        latch.countDown();
                    }
                    if (waitInEval != null) {
                        waitInEval.acquire();
                    }
                } catch (InterruptedException e) {
                // cancelled
                }
                return result;
            });
        } catch (Throwable e) {
            if (latch != null) {
                latch.countDown();
            }
            throw e;
        }
    });
    return new Completable<>(valueResult, waitInEval);
}
Also used : Value(org.graalvm.polyglot.Value) Semaphore(java.util.concurrent.Semaphore)

Example 8 with Value

use of org.graalvm.polyglot.Value 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 9 with Value

use of org.graalvm.polyglot.Value in project graal by oracle.

the class ProxyAPITest method testProxy.

@Test
public void testProxy() {
    Proxy proxy = new Proxy() {
    };
    Value value = context.asValue(proxy);
    assertTrue(value.isProxyObject());
    assertSame(proxy, value.asProxyObject());
    assertUnsupported(value, PROXY_OBJECT);
}
Also used : Proxy(org.graalvm.polyglot.proxy.Proxy) ValueAssert.assertValue(com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 10 with Value

use of org.graalvm.polyglot.Value 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)

Aggregations

Value (org.graalvm.polyglot.Value)277 Test (org.junit.Test)203 ValueAssert.assertValue (com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue)65 Context (org.graalvm.polyglot.Context)58 BoxedTestValue (com.oracle.truffle.llvm.test.interop.values.BoxedTestValue)43 PolyglotException (org.graalvm.polyglot.PolyglotException)42 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)34 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)34 Source (org.graalvm.polyglot.Source)31 ArrayList (java.util.ArrayList)30 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)23 ProxyExecutable (org.graalvm.polyglot.proxy.ProxyExecutable)18 HashMap (java.util.HashMap)14 TruffleContext (com.oracle.truffle.api.TruffleContext)11 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 NullValue (com.oracle.truffle.llvm.test.interop.values.NullValue)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)10 DebugValue (com.oracle.truffle.api.debug.DebugValue)9 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)9 List (java.util.List)9