Search in sources :

Example 21 with Source

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();
}
Also used : FileWriter(java.io.FileWriter) SourceSection(com.oracle.truffle.api.source.SourceSection) File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 22 with Source

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());
}
Also used : Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 23 with Source

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;
}
Also used : Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) SourceSection(com.oracle.truffle.api.source.SourceSection) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Source(com.oracle.truffle.api.source.Source)

Example 24 with Source

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);
}
Also used : CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) Script(com.oracle.truffle.tools.chromeinspector.types.Script) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Params(com.oracle.truffle.tools.chromeinspector.commands.Params) SourceSection(com.oracle.truffle.api.source.SourceSection) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(com.oracle.truffle.api.source.Source) Location(com.oracle.truffle.tools.chromeinspector.types.Location)

Example 25 with Source

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()]);
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Script(com.oracle.truffle.tools.chromeinspector.types.Script) ArrayList(java.util.ArrayList) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(com.oracle.truffle.api.source.Source) CommandProcessException(com.oracle.truffle.tools.chromeinspector.server.CommandProcessException) TruffleException(com.oracle.truffle.api.TruffleException) NoSuspendedThreadException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.NoSuspendedThreadException) GuestLanguageException(com.oracle.truffle.tools.chromeinspector.TruffleExecutionContext.GuestLanguageException) DebugScope(com.oracle.truffle.api.debug.DebugScope) DebugScope(com.oracle.truffle.api.debug.DebugScope) Scope(com.oracle.truffle.tools.chromeinspector.types.Scope) CallFrame(com.oracle.truffle.tools.chromeinspector.types.CallFrame) SourceSection(com.oracle.truffle.api.source.SourceSection) PrintWriter(java.io.PrintWriter)

Aggregations

Source (com.oracle.truffle.api.source.Source)113 Test (org.junit.Test)65 RootNode (com.oracle.truffle.api.nodes.RootNode)23 File (java.io.File)20 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)16 Node (com.oracle.truffle.api.nodes.Node)16 SourceSection (com.oracle.truffle.api.source.SourceSection)16 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)15 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)11 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CallTarget (com.oracle.truffle.api.CallTarget)5 FileWriter (java.io.FileWriter)5 RootCallTarget (com.oracle.truffle.api.RootCallTarget)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3