use of com.oracle.truffle.api.source.Source 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.Source in project graal by oracle.
the class SourceTextTest method nameAndShortNameNoPath.
@Test
public void nameAndShortNameNoPath() {
final String name = "/tmp/hi.txt";
Source source = Source.newBuilder("Hi").name(name).mimeType("content/unknown").build();
assertEquals(name, source.getName());
}
use of com.oracle.truffle.api.source.Source 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;
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class TruffleDebugger method getPossibleBreakpoints.
@Override
public Params getPossibleBreakpoints(Location start, Location end, boolean restrictToFunction) throws CommandProcessException {
int scriptId = start.getScriptId();
if (scriptId != end.getScriptId()) {
throw new CommandProcessException("Different location scripts: " + scriptId + ", " + end.getScriptId());
}
Script script = slh.getScript(scriptId);
if (script == null) {
throw new CommandProcessException("Unknown scriptId: " + scriptId);
}
Source source = script.getSource();
int o1 = source.getLineStartOffset(start.getLine());
if (start.getColumn() > 0) {
o1 += start.getColumn() - 1;
}
int o2;
if (end.getLine() > source.getLineCount()) {
o2 = source.getLength();
} else {
o2 = source.getLineStartOffset(end.getLine());
if (end.getColumn() > 0) {
o2 += end.getColumn() - 1;
}
}
SourceSection range = source.createSection(o1, o2 - o1);
Iterable<SourceSection> locations = SuspendableLocationFinder.findSuspendableLocations(range, restrictToFunction, context.getEnv());
JSONObject json = new JSONObject();
JSONArray arr = new JSONArray();
for (SourceSection ss : locations) {
arr.put(new Location(scriptId, ss.getStartLine(), ss.getStartColumn()).toJSON());
}
json.put("locations", arr);
return new Params(json);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class TruffleDebugger method createCallFrames.
private CallFrame[] createCallFrames(Iterable<DebugStackFrame> frames) {
List<CallFrame> cfs = new ArrayList<>();
int depth = 0;
for (DebugStackFrame frame : frames) {
SourceSection sourceSection = frame.getSourceSection();
if (sourceSection == null) {
continue;
}
if (frame.isInternal()) {
continue;
}
Source source = sourceSection.getSource();
if (source.isInternal()) {
// should not be, double-check
continue;
}
slh.assureLoaded(source);
Script script = slh.getScript(slh.getScriptId(source));
List<Scope> scopes = new ArrayList<>();
DebugScope dscope;
try {
dscope = frame.getScope();
} catch (Exception ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getScope() has caused " + ex);
ex.printStackTrace(err);
}
dscope = null;
}
String scopeType = "block";
boolean wasFunction = false;
SourceSection functionSourceSection = null;
while (dscope != null) {
if (wasFunction) {
scopeType = "closure";
} else if (dscope.isFunctionScope()) {
scopeType = "local";
functionSourceSection = dscope.getSourceSection();
wasFunction = true;
}
if (dscope.isFunctionScope() || dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
scopes.add(createScope(scopeType, dscope));
}
dscope = getParent(dscope);
}
try {
dscope = ds.getTopScope(source.getLanguage());
} catch (Exception ex) {
PrintWriter err = context.getErr();
if (err != null) {
err.println("getTopScope() has caused " + ex);
ex.printStackTrace(err);
}
}
while (dscope != null) {
if (dscope.getDeclaredValues().iterator().hasNext()) {
// provide only scopes that have some variables
scopes.add(createScope("global", dscope));
}
dscope = getParent(dscope);
}
CallFrame cf = new CallFrame(frame, depth++, script, sourceSection, functionSourceSection, null, scopes.toArray(new Scope[scopes.size()]));
cfs.add(cf);
}
return cfs.toArray(new CallFrame[cfs.size()]);
}
Aggregations