Search in sources :

Example 51 with Source

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

the class BreakpointTest method testNotStepIntoBreakpointCondition.

@Test
public void testNotStepIntoBreakpointCondition() {
    Source defineSource = testSource("ROOT(DEFINE(test, ROOT(\n" + "STATEMENT(EXPRESSION),\n" + "CONSTANT(true))))");
    Source testSource = testSource("ROOT(\n" + "STATEMENT,\n" + "STATEMENT,\n" + "STATEMENT)");
    try (DebuggerSession session = startSession()) {
        startEval(defineSource);
        expectDone();
        Breakpoint breakpoint = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        breakpoint.setCondition("CALL(test)");
        session.suspendNextExecution();
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            assertEquals("STATEMENT", event.getSourceSection().getCharacters());
            assertEquals(2, event.getSourceSection().getStartLine());
            assertEquals(0, event.getBreakpoints().size());
            event.prepareStepInto(1);
        });
        assertEquals(0, breakpoint.getHitCount());
        expectSuspended((SuspendedEvent event) -> {
            assertEquals("STATEMENT", event.getSourceSection().getCharacters());
            assertEquals(3, event.getSourceSection().getStartLine());
            assertEquals(1, event.getBreakpoints().size());
            event.prepareStepInto(1);
        });
        assertEquals(1, breakpoint.getHitCount());
        expectSuspended((SuspendedEvent event) -> {
            assertEquals("STATEMENT", event.getSourceSection().getCharacters());
            assertEquals(4, event.getSourceSection().getStartLine());
            assertEquals(0, event.getBreakpoints().size());
            event.prepareStepInto(1);
        });
        expectDone();
        assertEquals(1, breakpoint.getHitCount());
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 52 with Source

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

the class BreakpointTest method testBreakpointConditionExecutedOnce.

@Test
public void testBreakpointConditionExecutedOnce() {
    Source testSource = testSource("ROOT(\n" + "STATEMENT,\n" + "STATEMENT,\n" + "STATEMENT)");
    try (DebuggerSession session = startSession()) {
        Breakpoint breakpoint = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        breakpoint.setCondition("ROOT(PRINT(OUT, Hi), CONSTANT(true))");
        // Breakpoint only:
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            assertSame(breakpoint, event.getBreakpoints().iterator().next());
            assertNull(event.getBreakpointConditionException(breakpoint));
        });
        assertEquals(1, breakpoint.getHitCount());
        expectDone();
        assertEquals("Hi", getOutput());
        // Breakpoint with step and an other breakpoint:
        Breakpoint breakpoint2 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
        session.suspendNextExecution();
        startEval(testSource);
        expectSuspended((SuspendedEvent event) -> {
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            assertEquals(2, event.getBreakpoints().size());
            assertTrue(event.getBreakpoints().contains(breakpoint));
            assertTrue(event.getBreakpoints().contains(breakpoint2));
            assertNull(event.getBreakpointConditionException(breakpoint));
            assertNull(event.getBreakpointConditionException(breakpoint2));
            event.prepareStepOver(1);
        });
        expectSuspended((SuspendedEvent event) -> {
            assertTrue(event.getBreakpoints().isEmpty());
            event.prepareStepOver(1);
        });
        expectDone();
        assertEquals("HiHi", getOutput());
        assertEquals(2, breakpoint.getHitCount());
        assertEquals(1, breakpoint2.getHitCount());
    }
}
Also used : Breakpoint(com.oracle.truffle.api.debug.Breakpoint) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 53 with Source

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

the class SLInstrumentTest method testChangeArgumentsOnReenter.

/**
 * Test that we can alter function arguments on reenter.
 */
@Test
public void testChangeArgumentsOnReenter() throws Exception {
    String code = "function main() {\n" + "  y = fce(0, 10000);\n" + "  return y;\n" + "}\n" + "function fce(x, z) {\n" + "  y = 2 * x;\n" + "  if (y < z) {\n" + "    print(\"A bad error.\");\n" + "    return 0 - 1;\n" + "  } else {\n" + "    return y;\n" + "  }\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    Context context = Context.create();
    IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
    incOnError.attachOn("A bad error");
    Value ret = context.eval(source);
    assertEquals(10000, ret.asInt());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 54 with Source

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

the class SLInstrumentTest method testEarlyReturn.

/**
 * Test that we can forcibly return early from call nodes with an arbitrary value.
 */
@Test
public void testEarlyReturn() throws Exception {
    String code = "function main() {\n" + "  a = 10;\n" + "  b = a;\n" + "  // Let fce() warm up and specialize:\n" + "  while (a == b && a < 100000) {\n" + "    a = fce(a);\n" + "    b = b + 1;\n" + "  }\n" + "  c = a;\n" + "  // Run fce() and alter it's return type in an instrument:\n" + "  c = fce(c);\n" + "  return c;\n" + "}\n" + "function fce(x) {\n" + "  return x + 1;\n" + "}\n";
    final Source source = Source.newBuilder("sl", code, "testing").build();
    ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
    Engine engine = Engine.newBuilder().err(engineOut).build();
    Context context = Context.newBuilder().engine(engine).build();
    // No instrument:
    Value ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(100001L, ret.asLong());
    EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
    earlyReturn.fceCode = "fce(a)";
    earlyReturn.returnValue = 200000L;
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(200001L, ret.asLong());
    earlyReturn.returnValue = "Hello!";
    ret = context.eval(source);
    assertFalse(ret.isNumber());
    assertTrue(ret.isString());
    assertEquals("Hello!1", ret.asString());
    // Specialize to long again:
    earlyReturn.fceCode = "<>";
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(100001L, ret.asLong());
    earlyReturn.fceCode = "fce(a)";
    earlyReturn.returnValue = new BigInteger("-42");
    boolean interopFailure;
    try {
        context.eval(source);
        interopFailure = false;
    } catch (PolyglotException err) {
        interopFailure = true;
    }
    assertTrue(interopFailure);
    earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(-41L, ret.asLong());
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = Boolean.TRUE;
    ret = context.eval(source);
    assertTrue(ret.isBoolean());
    assertEquals(Boolean.TRUE, ret.asBoolean());
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = -42.42;
    ret = context.eval(source);
    assertTrue(ret.isNumber());
    assertEquals(-42.42, ret.asDouble(), 1e-8);
    earlyReturn.fceCode = "fce(c)";
    earlyReturn.returnValue = "Hello!";
    ret = context.eval(source);
    assertTrue(ret.isString());
    assertEquals("Hello!", ret.asString());
}
Also used : Context(org.graalvm.polyglot.Context) EventContext(com.oracle.truffle.api.instrumentation.EventContext) SLBigNumber(com.oracle.truffle.sl.runtime.SLBigNumber) Value(org.graalvm.polyglot.Value) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PolyglotException(org.graalvm.polyglot.PolyglotException) Source(org.graalvm.polyglot.Source) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 55 with Source

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

the class SLInteropControlFlowTest method testIf.

@Test
public void testIf() throws IOException {
    final Source src = Source.newBuilder("sl", "function testIf(a) {if(a) {return 1;} else {return 0;}} function main() {return testIf;}", "testIf.sl").build();
    final Value fnc = context.eval(src);
    fnc.execute(false);
}
Also used : Value(org.graalvm.polyglot.Value) 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