Search in sources :

Example 86 with Context

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

the class ContextsEventsTest method testGetActiveContexts.

@Test
public void testGetActiveContexts() {
    try {
        TestContextsInstrument.includeActiveContexts = true;
        final List<ContextEvent> events;
        try (Context context = Context.create()) {
            Instrument testContexsInstrument = context.getEngine().getInstruments().get("testContexsInstrument");
            TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
            events = test.events;
            context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
            assertTrue(Integer.toString(events.size()), events.size() > 1);
            // Have creation of polyglot context and a language
            assertEquals(3, events.size());
            assertTrue(events.get(0).created);
            assertNotNull(events.get(0).context);
            assertNull(events.get(0).language);
            assertTrue(events.get(1).created);
            assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
            assertTrue(events.get(2).languageInitialized);
        }
        assertEquals(6, events.size());
        Engine engine = Engine.create();
        // Contexts created and closed:
        try (Context context = Context.newBuilder().engine(engine).build()) {
            context.eval(Source.create(InstrumentationTestLanguage.ID, "STATEMENT()"));
            context.eval(Source.create(InstrumentationTestLanguage.ID, "CONTEXT(STATEMENT())"));
        }
        Instrument testBlockOnStatementsInstrument = engine.getInstruments().get("testBlockOnStatementsInstrument");
        ThreadsEventsTest.TestBlockOnStatementsInstrument testBlock = testBlockOnStatementsInstrument.lookup(ThreadsEventsTest.TestBlockOnStatementsInstrument.class);
        testBlock.runOnBlock = new Runnable() {

            @Override
            // We want to hide 'events' from the outer context not to use it by a mistake
            @SuppressWarnings("hiding")
            public void run() {
                Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
                TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
                final List<ContextEvent> events = test.events;
                assertEquals(3, events.size());
                assertTrue(events.get(0).created);
                assertNull(events.get(0).language);
                assertTrue(events.get(1).created);
                assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
                assertTrue(events.get(2).languageInitialized);
                testBlock.blockOnStatements.set(false);
            }
        };
        Context context = Context.newBuilder().engine(engine).build();
        context.eval(Source.create(InstrumentationTestLanguage.ID, "ROOT(CONTEXT(EXPRESSION()), CONTEXT(STATEMENT))"));
    } finally {
        TestContextsInstrument.includeActiveContexts = false;
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) ArrayList(java.util.ArrayList) List(java.util.List) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 87 with Context

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

the class ContextsEventsTest method testMultipleContexts.

@Test
public void testMultipleContexts() {
    final int numContexts = 5;
    final List<ContextEvent> events;
    try (Engine engine = Engine.create()) {
        Instrument testContexsInstrument = engine.getInstruments().get("testContexsInstrument");
        TestContextsInstrument test = testContexsInstrument.lookup(TestContextsInstrument.class);
        events = test.events;
        Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
        for (int i = 0; i < numContexts; i++) {
            try (Context context = Context.newBuilder().engine(engine).build()) {
                assertEquals(6 * i + 1, events.size());
                context.eval(source);
            }
            assertEquals(6 * i + 6, events.size());
        }
        assertEquals(6 * numContexts, events.size());
        TruffleContext lastContext = null;
        for (int i = 0; i < numContexts; i++) {
            int ci = 6 * i;
            assertTrue(events.get(ci).created);
            assertNull(events.get(ci).language);
            assertNotEquals(lastContext, events.get(ci).context);
            lastContext = events.get(ci).context;
            assertTrue(events.get(ci + 1).created);
            assertNotNull(events.get(ci + 1).language);
            assertEquals(lastContext, events.get(ci + 1).context);
            assertTrue(events.get(ci + 2).languageInitialized);
            assertTrue(events.get(ci + 3).languageFinalized);
            assertNotNull(events.get(ci + 4).language);
            assertNull(events.get(ci + 5).language);
        }
    }
    // No more events
    assertEquals(6 * numContexts, events.size());
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TruffleContext(com.oracle.truffle.api.TruffleContext) Engine(org.graalvm.polyglot.Engine) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 88 with Context

use of org.graalvm.polyglot.Context in project sieve by jtulach.

the class Main method execute.

private static void execute(Integer repeat) throws Exception {
    Context vm = Context.newBuilder().build();
    if (repeat != null) {
        vm.eval("js", "count=" + repeat);
    }
    File scriptDir = findScriptDir();
    Source ruby = Source.newBuilder("ruby", new File(scriptDir, "sieve.rb")).build();
    Source js = Source.newBuilder("js", new File(scriptDir, "sieve.js")).build();
    vm.eval(ruby);
    vm.eval(js);
}
Also used : Context(org.graalvm.polyglot.Context) File(java.io.File) Source(org.graalvm.polyglot.Source)

Example 89 with Context

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

the class SplittingStrategyTest method testHardSplitLimitInContext.

@Test
@SuppressWarnings("try")
public void testHardSplitLimitInContext() {
    final int expectedSplittingIncrease = 20;
    try (TruffleCompilerOptions.TruffleOptionsOverrideScope s = TruffleCompilerOptions.overrideOptions(TruffleCompilerOptions.TruffleSplittingMaxNumberOfSplitNodes, expectedSplittingIncrease)) {
        Context c = Context.create();
        for (int i = 0; i < 100; i++) {
            eval(c, "exec");
        }
        Assert.assertEquals("Wrong number of splits: ", expectedSplittingIncrease, listener.splitCount * DUMMYROOTNODECOUNT);
    }
}
Also used : Context(org.graalvm.polyglot.Context) TruffleCompilerOptions(org.graalvm.compiler.truffle.common.TruffleCompilerOptions) Test(org.junit.Test)

Example 90 with Context

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

the class SplittingStrategyTest method testGrowingSplitLimitInContext.

@Test
public void testGrowingSplitLimitInContext() {
    Context c = Context.create();
    // Eval a lot to fill out budget
    useUpTheBudget(c);
    final int baseSplitCount = listener.splitCount;
    for (int i = 0; i < 10; i++) {
        eval(c, "exec");
    }
    Assert.assertEquals("Split count growing without new call targets", baseSplitCount, listener.splitCount);
    eval(c, "new");
    for (int i = 0; i < 10; i++) {
        eval(c, "exec");
    }
    Assert.assertEquals("Split count not correct after one new target", (int) (baseSplitCount + TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleSplittingGrowthLimit)), listener.splitCount);
    eval(c, "new2");
    for (int i = 0; i < 10; i++) {
        eval(c, "exec");
    }
    Assert.assertEquals("Split count not correct after one new target", (int) (baseSplitCount + 2 * TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleSplittingGrowthLimit)), listener.splitCount);
}
Also used : Context(org.graalvm.polyglot.Context) 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