Search in sources :

Example 1 with LanguageContext

use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext 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)

Example 2 with LanguageContext

use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.

the class LanguageSPITest method testExportSymbolInCreate.

@SuppressWarnings("deprecation")
@Test
public void testExportSymbolInCreate() {
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected LanguageContext createContext(com.oracle.truffle.api.TruffleLanguage.Env env) {
            env.exportSymbol("symbol", env.asGuestValue(env));
            return super.createContext(env);
        }
    });
    Context c = Context.create();
    c.initialize(ProxyLanguage.ID);
    assertTrue(c.importSymbol("symbol").isHostObject());
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Test(org.junit.Test)

Example 3 with LanguageContext

use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.

the class LanguageSPITest method testErrorInFindMetaObject.

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

        @Override
        protected Object findMetaObject(LanguageContext context, Object value) {
            throw new RuntimeException();
        }

        @Override
        protected CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest request) throws Exception {
            return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(42));
        }
    });
    Context c = Context.create();
    Value v = c.eval(ProxyLanguage.ID, "");
    testFails(() -> v.getMetaObject());
    testFails(() -> v.getMetaObject());
    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) 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) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 4 with LanguageContext

use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.

the class LanguageSPITest method testCloseInnerContextWithParent.

@Test
public void testCloseInnerContextWithParent() {
    Context context = Context.create();
    LanguageContext returnedInnerContext = eval(context, new Function<Env, Object>() {

        public Object apply(Env env) {
            TruffleContext innerContext = env.newContextBuilder().build();
            Object p = innerContext.enter();
            LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
            innerContext.leave(p);
            return innerLangContext;
        }
    }).asHostObject();
    context.close();
    // inner context automatically closed
    assertEquals(1, returnedInnerContext.disposeCalled);
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Function(java.util.function.Function) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Env(com.oracle.truffle.api.TruffleLanguage.Env) Test(org.junit.Test)

Example 5 with LanguageContext

use of com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext in project graal by oracle.

the class LanguageSPITest method testCreateThreadDuringDispose.

@Test
public void testCreateThreadDuringDispose() {
    AtomicBoolean contextOnFinalize = new AtomicBoolean(true);
    AtomicBoolean contextOnDispose = new AtomicBoolean(false);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected LanguageContext createContext(Env env) {
            return new LanguageContext(env);
        }

        @Override
        protected void finalizeContext(LanguageContext context) {
            if (contextOnFinalize.get()) {
                context.env.createThread(() -> {
                }).start();
            }
        }

        @Override
        protected void disposeContext(LanguageContext context) {
            if (contextOnDispose.get()) {
                context.env.createThread(() -> {
                }).start();
            }
        }
    });
    Context c = Context.create();
    c.initialize(ProxyLanguage.ID);
    // Fails on finalize
    testFails(() -> {
        c.close();
    });
    contextOnFinalize.set(false);
    contextOnDispose.set(true);
    // Fails on dispose
    testFails(() -> {
        c.close();
    });
    // clean up the context
    contextOnDispose.set(false);
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Test(org.junit.Test)

Aggregations

LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)10 TruffleContext (com.oracle.truffle.api.TruffleContext)9 Context (org.graalvm.polyglot.Context)9 Test (org.junit.Test)9 Env (com.oracle.truffle.api.TruffleLanguage.Env)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CallTarget (com.oracle.truffle.api.CallTarget)2 RootCallTarget (com.oracle.truffle.api.RootCallTarget)2 TruffleException (com.oracle.truffle.api.TruffleException)2 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Function (java.util.function.Function)2 PolyglotException (org.graalvm.polyglot.PolyglotException)2 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 RootNode (com.oracle.truffle.api.nodes.RootNode)1 Engine (org.graalvm.polyglot.Engine)1