use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class DebugStackFrameTest method testSourceSections.
@Test
public void testSourceSections() {
final Source source = testSource("ROOT(DEFINE(a,ROOT(\n" + " STATEMENT())\n" + "),\n" + "DEFINE(b,ROOT(\n" + " CALL(a))\n" + "), \n" + "CALL(b))\n");
try (DebuggerSession session = startSession()) {
session.suspendNextExecution();
startEval(source);
expectSuspended((SuspendedEvent event) -> {
DebugStackFrame frame = event.getTopStackFrame();
SourceSection ss = frame.getSourceSection();
assertSection(ss, "STATEMENT()", 2, 3, 2, 13);
SourceSection fss = getFunctionSourceSection(frame);
assertSection(fss, "ROOT(\n STATEMENT())\n", 1, 15, 2, 15);
Iterator<DebugStackFrame> stackFrames = event.getStackFrames().iterator();
// The top one
assertEquals(frame, stackFrames.next());
// b
frame = stackFrames.next();
ss = frame.getSourceSection();
assertSection(ss, "CALL(a)", 5, 3, 5, 9);
fss = getFunctionSourceSection(frame);
assertSection(fss, "ROOT(\n CALL(a))\n", 4, 10, 5, 11);
// root
frame = stackFrames.next();
ss = frame.getSourceSection();
assertSection(ss, "CALL(b)", 7, 1, 7, 7);
fss = getFunctionSourceSection(frame);
assertSection(fss, source.getCharacters().toString(), 1, 1, 7, 9);
assertFalse(stackFrames.hasNext());
event.prepareContinue();
});
expectDone();
}
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SourceSectionTest method emptySourceSectionOnLongSource.
@Test
public void emptySourceSectionOnLongSource() {
SourceSection section = longSource.createSection(longSource.getCharacters().length() - 1, 0);
assertNotNull(section);
assertEquals(longSource.getCharacters().length() - 1, section.getCharIndex());
assertEquals(0, section.getCharLength(), 0);
assertEquals(3, section.getStartLine());
assertEquals(3, section.getEndLine());
assertEquals(2, section.getStartColumn());
assertEquals(2, section.getEndColumn());
SourceSection other = longSource.createSection(longSource.getCharacters().length() - 1, 0);
assertTrue(section.equals(other));
assertEquals(other.hashCode(), section.hashCode());
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SourceSectionTest method emptySourceTest0.
@Test
public void emptySourceTest0() {
SourceSection section = emptySource.createSection(0, 0);
assertNotNull(section);
assertEquals(section.getCharacters(), "");
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SourceSectionTest method onceObtainedAlwaysTheSame.
@Test
public void onceObtainedAlwaysTheSame() throws Exception {
File sample = File.createTempFile("hello", ".txt");
sample.deleteOnExit();
try (FileWriter w = new FileWriter(sample)) {
w.write("Hello world!");
}
Source complexHello = Source.newBuilder(sample).build();
SourceSection helloTo = complexHello.createSection(6, 5);
assertEquals("world", helloTo.getCharacters());
try (FileWriter w = new FileWriter(sample)) {
w.write("Hi world!");
}
Source simpleHi = Source.newBuilder(sample).build();
SourceSection hiTo = simpleHi.createSection(3, 5);
assertEquals("world", hiTo.getCharacters());
assertEquals("Previously allocated sections remain the same", "world", helloTo.getCharacters());
sample.delete();
}
use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.
the class SuspendableLocationFinder method findSuspendableLocations.
static Iterable<SourceSection> findSuspendableLocations(SourceSection range, boolean restrictToSingleFunction, TruffleInstrument.Env env) {
Source source = range.getSource();
int startIndex = range.getCharIndex();
int endIndex = range.getCharEndIndex();
SectionsCollector sectionsCollector = collectSuspendableLocations(source, startIndex, endIndex, restrictToSingleFunction, env);
List<SourceSection> sections = sectionsCollector.getSections();
if (sections.isEmpty()) {
SourceSectionFilter filter = SourceSectionFilter.newBuilder().sourceIs(source).indexIn(IndexRange.between(startIndex, endIndex)).build();
ContainerNodeCollector nodeCollector = new ContainerNodeCollector(startIndex);
EventBinding<ContainerNodeCollector> binding = env.getInstrumenter().attachLoadSourceSectionListener(filter, nodeCollector, true);
binding.dispose();
if (nodeCollector.getContainerNode() != null) {
Node suspendableNode = nodeCollector.getContainerNode().findNearestNodeAt(startIndex, SUSPENDABLE_TAGS_SET);
if (suspendableNode != null) {
startIndex = Math.min(startIndex, suspendableNode.getSourceSection().getCharIndex());
endIndex = Math.max(endIndex, suspendableNode.getSourceSection().getCharEndIndex());
sectionsCollector = collectSuspendableLocations(source, startIndex, endIndex, restrictToSingleFunction, env);
sections = sectionsCollector.getSections();
}
}
}
return sections;
}
Aggregations