Search in sources :

Example 96 with Source

use of org.graalvm.polyglot.Source 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 97 with Source

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

the class DebuggerTesterSnippets method assertColumnBreakpointsResolution.

/**
 * Utility method that checks proper resolution of column breakpoints. Breakpoints are submitted
 * to marked positions and their resolution location is checked.
 * <p>
 * The source need to contain both breakpoint submission marks in the form of "B&lt;number&gt;_"
 * and breakpoint resolution marks in the form of "R&lt;number&gt;_" where &lt;number&gt; is an
 * identification of the breakpoint. These marks need to be placed at the proper breakpoint
 * submission/resolution line/column position. When several submitted breakpoints resolve to the
 * same position, an interval can be specified in the form of "R&lt;start number&gt;-&lt;end
 * number&gt;_", where both start and end line numbers are inclusive. The marks are stripped off
 * before execution.
 * <p>
 * The guest language code with marks may look like:
 *
 * <pre>
 * // B1_test
 * function B2_test(n) {B3_
 *   if (R1-4_n &lt;= B4_1) {B5_
 *     return R5_2 * n;
 *   }
 *   return R6_n - 1;
 * B6_}
 * </pre>
 *
 * @param sourceWithMarks a source text, which contains the resolution marks
 * @param breakpointMarkName the breakpoint submission mark name. It is used in a regular
 *            expression, therefore the mark must not have a special meaning in a regular
 *            expression.
 * @param resolvedMarkName the resolved mark name. It is used in a regular expression, therefore
 *            the mark must not have a special meaning in a regular expression.
 * @param language the source language
 * @since 0.33
 */
public void assertColumnBreakpointsResolution(String sourceWithMarks, String breakpointMarkName, String resolvedMarkName, String language) throws IOException {
    Pattern br = Pattern.compile("([" + breakpointMarkName + resolvedMarkName + "]\\d+_|" + resolvedMarkName + "\\d+-\\d+_)");
    Map<Integer, int[]> bps = new HashMap<>();
    String sourceString = sourceWithMarks;
    Matcher bm = br.matcher(sourceString);
    while (bm.find()) {
        String bg = bm.group();
        int index = bm.start();
        int state = (bg.charAt(0) == 'B') ? 0 : 1;
        String bpNums = bg.substring(1, bg.length() - 1);
        int bn1;
        int bn2;
        int rangeIndex = bpNums.indexOf('-');
        if (rangeIndex > 0) {
            bn1 = Integer.parseInt(bpNums.substring(0, rangeIndex));
            bn2 = Integer.parseInt(bpNums.substring(rangeIndex + 1));
        } else {
            bn1 = bn2 = Integer.parseInt(bpNums);
        }
        for (int bn = bn1; bn <= bn2; bn++) {
            int[] bp = bps.get(bn);
            if (bp == null) {
                bp = new int[2];
                bps.put(bn, bp);
            }
            if (bp[state] > 0) {
                Assert.fail(bg + " specified more than once.");
            }
            bp[state] = index + 1;
        }
        sourceString = bm.replaceFirst("");
        bm.reset(sourceString);
    }
    if (TRACE) {
        trace("sourceString = '" + sourceString + "'");
    }
    final Source source = Source.newBuilder(language, sourceString, "testMisplacedColumnBreakpoint." + language).build();
    for (Map.Entry<Integer, int[]> bentry : bps.entrySet()) {
        int bpId = bentry.getKey();
        int[] bp = bentry.getValue();
        Assert.assertTrue(Integer.toString(bpId), bp[0] > 0);
        Assert.assertTrue(Integer.toString(bpId), bp[1] > 0);
        int line = source.getLineNumber(bp[0] - 1);
        int column = source.getColumnNumber(bp[0] - 1);
        if (TRACE) {
            trace("TESTING BP_" + bpId + ": " + bp[0] + " (" + line + ":" + column + ") => " + bp[1] + ":");
        }
        try (DebuggerSession session = startSession()) {
            startEval(source);
            int[] resolvedIndexPtr = new int[] { 0 };
            Breakpoint breakpoint = session.install(Breakpoint.newBuilder(DebuggerTester.getSourceImpl(source)).lineIs(line).columnIs(column).oneShot().resolveListener(new Breakpoint.ResolveListener() {

                @Override
                public void breakpointResolved(Breakpoint brkp, SourceSection section) {
                    resolvedIndexPtr[0] = section.getCharIndex() + 1;
                    if (TRACE) {
                        trace("  resolved: " + (resolvedIndexPtr[0]));
                    }
                }
            }).build());
            expectSuspended((SuspendedEvent event) -> {
                Assert.assertEquals("Expected " + bp[0] + " => " + bp[1] + ", resolved at " + resolvedIndexPtr[0], bp[1], event.getSourceSection().getCharIndex() + 1);
                Assert.assertSame(breakpoint, event.getBreakpoints().iterator().next());
                event.prepareContinue();
            });
            expectDone();
            Assert.assertEquals("Expected resolved " + bp[0] + " => " + bp[1], bp[1], resolvedIndexPtr[0]);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(org.graalvm.polyglot.Source) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SourceSection(com.oracle.truffle.api.source.SourceSection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 98 with Source

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

the class DebuggerTesterSnippets method testDebugging.

// BEGIN: DebuggerTesterSnippets.testDebugging
public void testDebugging() {
    try (DebuggerTester tester = new DebuggerTester()) {
        // use your guest language source here
        Source source = null;
        try (DebuggerSession session = tester.startSession()) {
            session.suspendNextExecution();
            tester.startEval(source);
            tester.expectSuspended(new SuspendedCallback() {

                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert suspended event is proper here
                    event.prepareStepInto(1);
                }
            });
            tester.expectSuspended(new SuspendedCallback() {

                @Override
                public void onSuspend(SuspendedEvent event) {
                    // assert another suspended event is proper here
                    event.prepareContinue();
                }
            });
            // expect no more suspended events
            tester.expectDone();
        }
    }
}
Also used : DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) SuspendedCallback(com.oracle.truffle.api.debug.SuspendedCallback)

Example 99 with Source

use of org.graalvm.polyglot.Source 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 100 with Source

use of org.graalvm.polyglot.Source in project sulong by graalvm.

the class LLVMDebugTest method test.

@Test
public void test() throws Throwable {
    final Source source = loadOriginalSource(testName);
    final Trace trace = readTrace(testName);
    final Source bitcode = loadBitcodeSource(testName, configuration);
    runTest(source, bitcode, trace);
}
Also used : Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

Source (org.graalvm.polyglot.Source)196 Test (org.junit.Test)165 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)103 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)95 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)51 Value (org.graalvm.polyglot.Value)31 Context (org.graalvm.polyglot.Context)25 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)21 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)14 Instrument (org.graalvm.polyglot.Instrument)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 SourceSection (com.oracle.truffle.api.source.SourceSection)12 EventContext (com.oracle.truffle.api.instrumentation.EventContext)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Engine (org.graalvm.polyglot.Engine)9 ArrayList (java.util.ArrayList)8 PolyglotException (org.graalvm.polyglot.PolyglotException)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)7