Search in sources :

Example 21 with Context

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

the class LanguageSPITest method testEngineCloseInsideFromSameThreadCancelExecution.

@Test
public void testEngineCloseInsideFromSameThreadCancelExecution() {
    Engine engine = Engine.create();
    langContext = null;
    Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
    eval(context, new Function<Env, Object>() {

        public Object apply(Env t) {
            engine.close(true);
            return null;
        }
    });
    assertEquals(1, langContext.disposeCalled);
    engine.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Env(com.oracle.truffle.api.TruffleLanguage.Env) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 22 with Context

use of org.graalvm.polyglot.Context 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 23 with Context

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

Example 24 with Context

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

the class LanguageSPITest method testContextCloseInsideFromSameThreadCancelExecution.

@Test
public void testContextCloseInsideFromSameThreadCancelExecution() {
    Engine engine = Engine.create();
    langContext = null;
    Context context = Context.newBuilder(LanguageSPITestLanguage.ID).engine(engine).build();
    eval(context, new Function<Env, Object>() {

        public Object apply(Env t) {
            context.close(true);
            return null;
        }
    });
    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) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Env(com.oracle.truffle.api.TruffleLanguage.Env) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 25 with Context

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

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