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());
}
}
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());
}
}
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());
}
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());
}
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);
}
Aggregations