Search in sources :

Example 1 with Context

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

the class NestedContextTest method testRecursiveEval.

@Test
public void testRecursiveEval() throws Exception {
    final Source testSource = testSource("ROOT(\n" + "  STATEMENT,\n" + "  STATEMENT\n" + ")\n");
    final Context context = Context.create();
    final AtomicInteger suspensionCount = new AtomicInteger(0);
    Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(new SuspendedCallback() {

        public void onSuspend(SuspendedEvent event) {
            checkState(event, 3, true, "STATEMENT");
            // recursive evaluation should not trigger a suspended event
            context.eval(testSource);
            suspensionCount.incrementAndGet();
        }
    })) {
        session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        context.eval(testSource);
    }
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 2 with Context

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

the class TimeBoxingTest method testTimeBoxing.

@Test
public void testTimeBoxing() throws Exception {
    final Context context = Context.create();
    Source source = Source.newBuilder(InstrumentationTestLanguage.ID, "ROOT(LOOP(infinity,STATEMENT))", "NotEnoughTime").buildLiteral();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
            debugger.startSession(new SuspendedCallback() {

                public void onSuspend(SuspendedEvent event) {
                    event.prepareKill();
                }
            }).suspendNextExecution();
        }
    }, 1000);
    try {
        // throws KillException, wrapped by PolyglotException
        context.eval(source);
        Assert.fail();
    } catch (PolyglotException pex) {
        Assert.assertEquals("com.oracle.truffle.api.debug.KillException", pex.getMessage());
    }
}
Also used : Context(org.graalvm.polyglot.Context) Debugger(com.oracle.truffle.api.debug.Debugger) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback) Test(org.junit.Test)

Example 3 with Context

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

the class DebuggerContextsTest method testMultipleContexts.

@Test
public void testMultipleContexts() {
    Source source = Source.create(InstrumentationTestLanguage.ID, "STATEMENT()");
    Engine engine = Engine.create();
    TestContextsListener contextsListener = new TestContextsListener();
    List<ContextEvent> events = contextsListener.events;
    int numContexts = 5;
    Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
    try (DebuggerSession session = debugger.startSession(null)) {
        session.setContextsListener(contextsListener, false);
        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());
        DebugContext 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);
        }
        engine.close();
    }
    // No more events
    assertEquals(6 * numContexts, events.size());
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugContext(com.oracle.truffle.api.debug.DebugContext) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 4 with Context

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

the class DebuggerContextsTest method testInContextEval.

@Test
public void testInContextEval() {
    try (Engine engine = Engine.create()) {
        TestContextsListener contextsListener = new TestContextsListener();
        List<ContextEvent> events = contextsListener.events;
        Debugger debugger = engine.getInstruments().get("debugger").lookup(Debugger.class);
        try (DebuggerSession session = debugger.startSession(null)) {
            session.setContextsListener(contextsListener, false);
            Context context = Context.newBuilder().engine(engine).build();
            assert context != null;
            assertEquals(1, events.size());
            assertTrue(events.get(0).created);
            DebugContext dc = events.get(0).context;
            assertNotNull(dc);
            assertNull(events.get(0).language);
            // "Global" evaluation in the brand new context
            DebugValue result = dc.evaluate("VARIABLE(a, 10)", InstrumentationTestLanguage.ID);
            assertEquals(3, events.size());
            assertTrue(events.get(1).created);
            assertEquals(InstrumentationTestLanguage.ID, events.get(1).language.getId());
            assertTrue(events.get(2).languageInitialized);
            String type = dc.runInContext(() -> {
                assertEquals("10", result.as(String.class));
                DebugValue metaObj = result.getMetaObject();
                return metaObj.as(String.class);
            });
            assertEquals("Integer", type);
            assertEquals(3, events.size());
        }
    }
}
Also used : Debugger(com.oracle.truffle.api.debug.Debugger) DebugContext(com.oracle.truffle.api.debug.DebugContext) Context(org.graalvm.polyglot.Context) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) DebugContext(com.oracle.truffle.api.debug.DebugContext) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 5 with Context

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

the class LanguageSPIOrderTest method testLanguageAccessRights.

@Test
public void testLanguageAccessRights() {
    Context context = Context.create(DEPENDENT);
    try {
        context.initialize(PUBLIC);
        fail();
    } catch (IllegalStateException e) {
    }
    try {
        context.initialize(TRANSITIVE);
        fail();
    } catch (IllegalStateException e) {
    }
    try {
        context.initialize(INTERNAL);
        fail();
    } catch (IllegalStateException e) {
    }
    context.initialize(DEPENDENT);
    try {
        context.initialize(PUBLIC);
        fail();
    } catch (IllegalStateException e) {
    }
    try {
        context.initialize(TRANSITIVE);
        fail();
    } catch (IllegalStateException e) {
    }
    try {
        context.initialize(INTERNAL);
        fail();
    } catch (IllegalStateException e) {
    }
    context.close();
}
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