Search in sources :

Example 16 with Context

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

the class LanguageSPITest method testImplicitCloseFromOtherThread.

@Test
public void testImplicitCloseFromOtherThread() throws InterruptedException {
    Engine engine = Engine.create();
    Context context = Context.newBuilder().engine(engine).build();
    langContext = null;
    context.initialize(LanguageSPITestLanguage.ID);
    Thread t = new Thread(new Runnable() {

        public void run() {
            engine.close();
        }
    });
    t.start();
    t.join(10000);
    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 17 with Context

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

the class LanguageSPITest method testLazyOptionInit.

@Test
@SuppressWarnings("all")
public void testLazyOptionInit() {
    AtomicInteger getOptionDescriptors = new AtomicInteger(0);
    AtomicInteger iterator = new AtomicInteger(0);
    AtomicInteger get = new AtomicInteger(0);
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        final OptionKey<String> option = new OptionKey<>("");

        final List<OptionDescriptor> descriptors;

        {
            descriptors = new ArrayList<>();
            descriptors.add(OptionDescriptor.newBuilder(option, ProxyLanguage.ID + ".option").build());
        }

        @Override
        protected OptionDescriptors getOptionDescriptors() {
            getOptionDescriptors.incrementAndGet();
            return new OptionDescriptors() {

                public Iterator<OptionDescriptor> iterator() {
                    iterator.incrementAndGet();
                    return descriptors.iterator();
                }

                public OptionDescriptor get(String optionName) {
                    get.incrementAndGet();
                    for (OptionDescriptor optionDescriptor : descriptors) {
                        if (optionDescriptor.getName().equals(optionName)) {
                            return optionDescriptor;
                        }
                    }
                    return null;
                }
            };
        }
    });
    Context c = Context.create();
    c.close();
    assertEquals(0, getOptionDescriptors.get());
    assertEquals(0, iterator.get());
    assertEquals(0, get.get());
    c = Context.newBuilder(ProxyLanguage.ID).option(ProxyLanguage.ID + ".option", "foobar").build();
    assertEquals(1, getOptionDescriptors.get());
    assertEquals(1, get.get());
    boolean assertionsEnabled = false;
    assert assertionsEnabled = true;
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(1, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(0, iterator.get());
    }
    c.close();
    // test lazyness when using meta-data
    getOptionDescriptors.set(0);
    iterator.set(0);
    get.set(0);
    c = Context.create();
    OptionDescriptors descriptors = c.getEngine().getLanguages().get(ProxyLanguage.ID).getOptions();
    assertEquals(1, getOptionDescriptors.get());
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(1, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(0, iterator.get());
    }
    assertEquals(0, get.get());
    for (OptionDescriptor descriptor : descriptors) {
    }
    assertEquals(1, getOptionDescriptors.get());
    if (assertionsEnabled) {
        // with assertions enabled we assert the options.
        assertEquals(2, iterator.get());
    } else {
        // for valid options we should not need the iterator
        assertEquals(1, iterator.get());
    }
    assertEquals(0, get.get());
    assertNotNull(descriptors.get(ProxyLanguage.ID + ".option"));
    assertEquals(1, getOptionDescriptors.get());
    assertEquals(1, get.get());
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) OptionDescriptor(org.graalvm.options.OptionDescriptor) ArrayList(java.util.ArrayList) OptionDescriptors(org.graalvm.options.OptionDescriptors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) OptionKey(org.graalvm.options.OptionKey) Test(org.junit.Test)

Example 18 with Context

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

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

the class LanguageSPITest method testBindingsWithDefaultScope.

@Test
public void testBindingsWithDefaultScope() {
    Context c = Context.create();
    Value bindings = c.getBindings(ProxyLanguage.ID);
    assertTrue(bindings.hasMembers());
    assertFalse(bindings.hasMember(""));
    assertTrue(bindings.getMemberKeys().isEmpty());
    assertNull(bindings.getMember(""));
    ValueAssert.assertFails(() -> bindings.putMember("", ""), UnsupportedOperationException.class);
    assertFalse(bindings.removeMember(""));
    ValueAssert.assertValue(c, bindings);
    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) Test(org.junit.Test)

Example 20 with Context

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

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