Search in sources :

Example 6 with LanguageContext

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

the class LanguageSPITest method testCreateContextDuringDispose.

@Test
public void testCreateContextDuringDispose() {
    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.newContextBuilder().build();
            }
        }

        @Override
        protected void disposeContext(LanguageContext context) {
            if (contextOnDispose.get()) {
                context.env.newContextBuilder().build();
            }
        }
    });
    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)

Example 7 with LanguageContext

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

the class LanguageSPITest method testErrorInDisposeLanguage.

@Test
public void testErrorInDisposeLanguage() {
    AtomicBoolean fail = new AtomicBoolean(true);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected void disposeContext(LanguageContext context) {
            if (fail.get()) {
                throw new RuntimeException();
            }
        }
    });
    Context c = Context.create();
    c.initialize(ProxyLanguage.ID);
    testFails(() -> {
        c.close();
    });
    testFails(() -> {
        c.close();
    });
    // clean up the context
    fail.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) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Test(org.junit.Test)

Example 8 with LanguageContext

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

the class LanguageSPITestLanguage method createContext.

@Override
protected LanguageContext createContext(Env env) {
    LanguageSPITest.langContext = new LanguageContext();
    LanguageSPITest.langContext.env = env;
    LanguageSPITest.langContext.config = env.getConfig();
    return LanguageSPITest.langContext;
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)

Example 9 with LanguageContext

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

the class LanguageSPITest method testInnerContext.

@Test
public void testInnerContext() {
    Context context = Context.create();
    Function<Env, Object> f = new Function<Env, Object>() {

        public Object apply(Env env) {
            LanguageContext outerLangContext = LanguageSPITestLanguage.getContext();
            Object config = new Object();
            TruffleContext innerContext = env.newContextBuilder().config("config", config).build();
            Object p = innerContext.enter();
            LanguageContext innerLangContext = LanguageSPITestLanguage.getContext();
            try {
                try {
                    innerContext.close();
                    fail("context could be closed when entered");
                } catch (IllegalStateException e) {
                }
                assertEquals(0, innerLangContext.disposeCalled);
                assertEquals(config, innerLangContext.config.get("config"));
                assertNotSame(outerLangContext, innerLangContext);
                boolean assertions = false;
                assert (assertions = true) == true;
                if (assertions) {
                    boolean leaveFailed = false;
                    try {
                        innerContext.leave("foo");
                    } catch (AssertionError e) {
                        leaveFailed = true;
                    }
                    if (!leaveFailed) {
                        fail("no assertion error for leaving with the wrong object");
                    }
                }
            } finally {
                innerContext.leave(p);
            }
            assertSame(outerLangContext, LanguageSPITestLanguage.getContext());
            innerContext.close();
            try {
                innerContext.enter();
                fail("cannot be entered after closing");
            } catch (IllegalStateException e) {
            }
            innerContext.close();
            assertEquals(1, innerLangContext.disposeCalled);
            return null;
        }
    };
    eval(context, f);
    // ensure we are not yet closed
    eval(context, f);
    context.close();
}
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) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Test(org.junit.Test)

Example 10 with LanguageContext

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

the class LanguageSPITest method testImplicitClose.

@Test
public void testImplicitClose() {
    Engine engine = Engine.create();
    langContext = null;
    Context c = Context.newBuilder().engine(engine).build();
    c.initialize(LanguageSPITestLanguage.ID);
    LanguageContext context1 = langContext;
    Context.newBuilder().engine(engine).build().initialize(LanguageSPITestLanguage.ID);
    LanguageContext context2 = langContext;
    c.close();
    engine.close();
    assertEquals(1, context1.disposeCalled);
    assertEquals(1, context2.disposeCalled);
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) Engine(org.graalvm.polyglot.Engine) 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