Search in sources :

Example 11 with Context

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

the class LanguageSPITest method testErrorInParse.

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

        @Override
        protected CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest request) throws Exception {
            throw new RuntimeException();
        }
    });
    Context c = Context.create();
    c.initialize(ProxyLanguage.ID);
    testFails(() -> c.eval(ProxyLanguage.ID, "t0"));
    testFails(() -> c.eval(ProxyLanguage.ID, "t1"));
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) 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 12 with Context

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

the class LanguageSPITest method testBindingsWithMultipleScopes.

@Test
public void testBindingsWithMultipleScopes() {
    // innermost to outermost
    TestScope[] scopes = new TestScope[5];
    for (int i = 0; i < 5; i++) {
        scopes[i] = new TestScope();
    }
    setupTopScopes((Object[]) scopes);
    Context c = Context.create();
    assertEquals(0, findScopeInvokes);
    Value bindings = c.getBindings(ProxyLanguage.ID);
    assertEquals(1, findScopeInvokes);
    assertTrue(bindings.hasMembers());
    assertFalse(bindings.hasMember(""));
    assertNull(bindings.getMember(""));
    ValueAssert.assertFails(() -> bindings.putMember("foo", "bar"), UnsupportedOperationException.class);
    // test insertion into first insertable scope
    scopes[1].insertable = true;
    scopes[2].insertable = true;
    // should end up in scope 1
    bindings.putMember("foo", "bar");
    assertEquals("bar", bindings.getMember("foo").asString());
    assertEquals("bar", scopes[1].values.get("foo"));
    assertNull(scopes[0].values.get("foo"));
    assertNull(scopes[2].values.get("foo"));
    ValueAssert.assertValue(c, bindings, ValueAssert.Trait.MEMBERS);
    // test check for existing keys for remove
    scopes[2].removable = true;
    scopes[2].values.put("foo", "baz");
    scopes[2].values.put("bar", "baz");
    scopes[3].values.put("bar", "42");
    assertEquals("bar", bindings.getMember("foo").asString());
    assertEquals("baz", bindings.getMember("bar").asString());
    ValueAssert.assertFails(() -> bindings.removeMember("foo"), UnsupportedOperationException.class);
    assertTrue(bindings.removeMember("bar"));
    assertNotNull(scopes[2].values.get("foo"));
    assertNull(scopes[2].values.get("bar"));
    assertEquals("42", bindings.getMember("bar").asString());
    ValueAssert.assertValue(c, bindings, ValueAssert.Trait.MEMBERS);
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 13 with Context

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

the class LanguageSPITest method testPolyglotBindingsMultiThreaded.

@Test
public void testPolyglotBindingsMultiThreaded() throws InterruptedException, ExecutionException, TimeoutException {
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }

        @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();
    ExecutorService service = Executors.newFixedThreadPool(20);
    Value languageBindings = c.eval(ProxyLanguage.ID, "");
    Value polyglotBindings = c.getPolyglotBindings();
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < 2000; i++) {
        futures.add(service.submit(() -> {
            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());
        }));
    }
    for (Future<?> future : futures) {
        future.get(100000, TimeUnit.MILLISECONDS);
    }
    service.shutdown();
    service.awaitTermination(100000, TimeUnit.MILLISECONDS);
    c.close();
}
Also used : 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) ArrayList(java.util.ArrayList) 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) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) ExecutorService(java.util.concurrent.ExecutorService) Value(org.graalvm.polyglot.Value) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 14 with Context

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

the class LanguageSPITest method testPolyglotClose.

@Test
public void testPolyglotClose() {
    langContext = null;
    Engine engine = Engine.create();
    Context context = Context.newBuilder().engine(engine).build();
    context.initialize(LanguageSPITestLanguage.ID);
    assertNotNull(langContext);
    assertEquals(0, langContext.disposeCalled);
    context.close();
    engine.close();
    assertEquals(1, langContext.disposeCalled);
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 15 with Context

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

the class LanguageSPITest method testPolyglotBindingsPreserveLanguage.

@Test
public void testPolyglotBindingsPreserveLanguage() {
    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) {
                    Object bindings = getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
                    try {
                        ForeignAccess.sendWrite(Message.WRITE.createNode(), (TruffleObject) bindings, "exportedValue", "convertOnToString");
                    } catch (UnknownIdentifierException | UnsupportedTypeException | UnsupportedMessageException e) {
                        throw new AssertionError(e);
                    }
                    return bindings;
                }
            });
        }

        @Override
        protected String toString(LanguageContext context, Object value) {
            if (value.equals("convertOnToString")) {
                return "myStringToString";
            }
            return super.toString(context, value);
        }
    });
    Context c = Context.create();
    c.eval(ProxyLanguage.ID, "");
    assertEquals("Make sure language specific toString was invoked.", "myStringToString", c.getPolyglotBindings().getMember("exportedValue").toString());
}
Also used : 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) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) 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) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12