Search in sources :

Example 26 with Context

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

the class LanguageSPITest method testImplicitEngineClose.

@Test
public void testImplicitEngineClose() {
    Context context = Context.create();
    context.close();
    try {
        context.getEngine().getOptions();
        fail();
    } catch (IllegalStateException e) {
    // expect closed
    }
    try {
        context.getEngine().getLanguages();
        fail();
    } catch (IllegalStateException e) {
    // expect closed
    }
    try {
        context.getEngine().getInstruments();
        fail();
    } catch (IllegalStateException e) {
    // expect closed
    }
    // does not fail
    context.getEngine().close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Test(org.junit.Test)

Example 27 with Context

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

the class LanguageSPITest method testLookupHostDisabled.

@Test
public void testLookupHostDisabled() {
    Context context = Context.newBuilder().allowHostAccess(false).build();
    try {
        eval(context, new Function<Env, Object>() {

            public Object apply(Env t) {
                return t.lookupHostSymbol("java.util.HashMap");
            }
        });
        fail();
    } catch (PolyglotException e) {
        assertTrue(!e.isInternalError());
    }
    context.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) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 28 with Context

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

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

the class LanguageSPITest method testLazyInit.

@Test
public void testLazyInit() {
    LanguageSPITestLanguage.instanceCount.set(0);
    Context context = Context.create();
    assertEquals(0, LanguageSPITestLanguage.instanceCount.get());
    context.initialize(LanguageSPITestLanguage.ID);
    assertEquals(1, LanguageSPITestLanguage.instanceCount.get());
    context.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) Test(org.junit.Test)

Example 30 with Context

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

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