use of com.oracle.truffle.api.source.SourceSection in project sulong by graalvm.
the class DIScopeBuilder method extend.
private static SourceSection extend(SourceSection base) {
if (base == null) {
return null;
}
SourceSection section;
try {
final Source source = base.getSource();
final int length = source.getLength() - base.getCharIndex();
section = source.createSection(base.getCharIndex(), length);
} catch (Throwable ignored) {
section = base;
}
return section;
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLDebugDirectTest method assertLocation.
private void assertLocation(final String name, final int line, final boolean isBefore, final String code, final Object... expectedFrame) {
run.addLast(() -> {
assertNotNull(suspendedEvent);
final SourceSection suspendedSourceSection = suspendedEvent.getSourceSection();
Assert.assertEquals(line, suspendedSourceSection.getStartLine());
Assert.assertEquals(code, suspendedSourceSection.getCharacters());
Assert.assertEquals(isBefore, suspendedEvent.getSuspendAnchor() == SuspendAnchor.BEFORE);
final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
assertEquals(name, frame.getName());
for (int i = 0; i < expectedFrame.length; i = i + 2) {
final String expectedIdentifier = (String) expectedFrame[i];
final Object expectedValue = expectedFrame[i + 1];
DebugScope scope = frame.getScope();
DebugValue slot = scope.getDeclaredValue(expectedIdentifier);
while (slot == null && (scope = scope.getParent()) != null) {
slot = scope.getDeclaredValue(expectedIdentifier);
}
if (expectedValue != UNASSIGNED) {
Assert.assertNotNull(expectedIdentifier, slot);
final String slotValue = slot.as(String.class);
Assert.assertEquals(expectedValue, slotValue);
} else {
Assert.assertNull(expectedIdentifier, slot);
}
}
run.removeFirst().run();
});
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLDebugTest method testSourceLocation.
@Test
public void testSourceLocation() {
final Source varsSource = slCode("function main() {\n" + " a = doNull();\n" + " c = 10;\n" + " d = \"str\";\n" + " e = new();\n" + " f = doNull;\n" + " return;\n" + "}\n" + "function doNull() {}\n");
try (DebuggerSession session = startSession()) {
session.install(Breakpoint.newBuilder(getSourceImpl(varsSource)).lineIs(7).build());
startEval(varsSource);
expectSuspended((SuspendedEvent event) -> {
DebugStackFrame frame = event.getTopStackFrame();
DebugScope scope = frame.getScope();
DebugValue v = scope.getDeclaredValue("a");
assertNull(v.getSourceLocation());
v = scope.getDeclaredValue("c");
assertNull(v.getSourceLocation());
v = scope.getDeclaredValue("d");
assertNull(v.getSourceLocation());
v = scope.getDeclaredValue("e");
assertNull(v.getSourceLocation());
v = scope.getDeclaredValue("f");
SourceSection sourceLocation = v.getSourceLocation();
Assert.assertNotNull(sourceLocation);
assertEquals(9, sourceLocation.getStartLine());
assertEquals(9, sourceLocation.getEndLine());
assertEquals("doNull() {}", sourceLocation.getCharacters());
});
expectDone();
}
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLInstrumentTest method testReplaceNodeReturnValue.
/**
* This test demonstrates that it's possible to easily replace a return value of any node using
* {@link ExecutionEventListener#onUnwind(com.oracle.truffle.api.instrumentation.EventContext, com.oracle.truffle.api.frame.VirtualFrame, java.lang.Object)}
* .
*/
@Test
public void testReplaceNodeReturnValue() throws Exception {
String code = "function main() {\n" + " a = new();\n" + " b = a.rp1;\n" + " return b;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
SourceSection ss = DebuggerTester.getSourceImpl(source).createSection(24, 5);
Context context = Context.create();
NewReplacedInstrument replaced = context.getEngine().getInstruments().get("testNewNodeReplaced").lookup(NewReplacedInstrument.class);
replaced.attachAt(ss);
Value ret = context.eval(source);
assertEquals("Replaced Value", ret.toString());
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SLStatementNode method formatSourceSection.
/**
* Formats a source section of a node in human readable form. If no source section could be
* found it looks up the parent hierarchy until it finds a source section. Nodes where this was
* required append a <code>'~'</code> at the end.
*
* @param node the node to format.
* @return a formatted source section string
*/
public static String formatSourceSection(Node node) {
if (node == null) {
return "<unknown>";
}
SourceSection section = node.getSourceSection();
boolean estimated = false;
if (section == null) {
section = node.getEncapsulatingSourceSection();
estimated = true;
}
if (section == null || section.getSource() == null) {
return "<unknown source>";
} else {
String sourceName = new File(section.getSource().getName()).getName();
int startLine = section.getStartLine();
return String.format("%s:%d%s", sourceName, startLine, estimated ? "~" : "");
}
}
Aggregations