Search in sources :

Example 1 with TriState

use of com.oracle.truffle.api.utilities.TriState in project graal by oracle.

the class InspectorDebugger method createCallFrames.

private CallFrame[] createCallFrames(Iterable<DebugStackFrame> frames, SuspendAnchor topAnchor, DebugValue returnValue, CallFrame[] oldFrames) {
    List<CallFrame> cfs = new ArrayList<>();
    int depth = 0;
    int depthAll = -1;
    if (scriptsHandler == null || debuggerSession == null) {
        return new CallFrame[0];
    }
    for (DebugStackFrame frame : frames) {
        depthAll++;
        SourceSection sourceSection = frame.getSourceSection();
        if (sourceSection == null || !sourceSection.isAvailable()) {
            continue;
        }
        if (!context.isInspectInternal() && frame.isInternal()) {
            continue;
        }
        Source source = sourceSection.getSource();
        if (!context.isInspectInternal() && source.isInternal()) {
            // should not be, double-check
            continue;
        }
        Script script = scriptsHandler.assureLoaded(source);
        List<Scope> scopes = new ArrayList<>();
        DebugScope dscope;
        try {
            dscope = frame.getScope();
        } catch (DebugException 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;
        if (dscope == null) {
            functionSourceSection = sourceSection;
        }
        Scope[] oldScopes;
        if (oldFrames != null && oldFrames.length > depth) {
            oldScopes = oldFrames[depth].getScopeChain();
        } else {
            oldScopes = null;
        }
        List<DebugValue> receivers = new ArrayList<>();
        // index of "this" receiver in `receivers` array
        int thisIndex = -1;
        // index of language implementation scope
        int scopeIndex = 0;
        TriState isJS = TriState.UNDEFINED;
        while (dscope != null) {
            if (wasFunction) {
                scopeType = "closure";
            } else if (dscope.isFunctionScope()) {
                scopeType = "local";
                functionSourceSection = dscope.getSourceSection();
                wasFunction = true;
            }
            boolean scopeAdded = addScope(scopes, dscope, scopeType, scopeIndex, oldScopes);
            if (scopeAdded) {
                DebugValue receiver = dscope.getReceiver();
                receivers.add(receiver);
                if (receiver != null) {
                    if (thisIndex == -1 && "this".equals(receiver.getName())) {
                        // There is one receiver named "this".
                        thisIndex = scopes.size() - 1;
                    } else {
                        // we'll add the receiver(s) to scope variables instead.
                        if (TriState.UNDEFINED == isJS) {
                            isJS = TriState.valueOf(LanguageChecks.isJS(receiver.getOriginalLanguage()));
                        }
                        // therefore we need to keep the index for JS.
                        if (TriState.FALSE == isJS) {
                            thisIndex = -2;
                        }
                    }
                }
            }
            dscope = getParent(dscope);
            scopeIndex++;
        }
        try {
            dscope = debuggerSession.getTopScope(source.getLanguage());
        } catch (DebugException ex) {
            PrintWriter err = context.getErr();
            if (err != null) {
                err.println("getTopScope() has caused " + ex);
                ex.printStackTrace(err);
            }
        }
        while (dscope != null) {
            addScope(scopes, dscope, "global", scopeIndex, oldScopes);
            dscope = getParent(dscope);
            scopeIndex++;
        }
        RemoteObject returnObj = null;
        if (depthAll == 0 && returnValue != null) {
            returnObj = context.getRemoteObjectsHandler().getRemote(returnValue);
        }
        RemoteObject thisObj;
        if (thisIndex < -1) {
            for (int i = 0; i < receivers.size(); i++) {
                DebugValue receiver = receivers.get(i);
                if (receiver != null) {
                    scopes.get(i).getObject().setScopeReceiver(receiver);
                }
            }
            thisObj = null;
        } else if (thisIndex == -1) {
            // no added scope, no receiver
            thisObj = null;
        } else {
            thisObj = context.getRemoteObjectsHandler().getRemote(receivers.get(thisIndex));
        }
        SuspendAnchor anchor = (depthAll == 0) ? topAnchor : SuspendAnchor.BEFORE;
        CallFrame cf = new CallFrame(frame, depth++, script, sourceSection, anchor, functionSourceSection, thisObj, returnObj, 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) DebugValue(com.oracle.truffle.api.debug.DebugValue) ArrayList(java.util.ArrayList) DebugException(com.oracle.truffle.api.debug.DebugException) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) Source(com.oracle.truffle.api.source.Source) DebugScope(com.oracle.truffle.api.debug.DebugScope) SuspendAnchor(com.oracle.truffle.api.debug.SuspendAnchor) RemoteObject(com.oracle.truffle.tools.chromeinspector.types.RemoteObject) 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) TriState(com.oracle.truffle.api.utilities.TriState) PrintWriter(java.io.PrintWriter)

Aggregations

Breakpoint (com.oracle.truffle.api.debug.Breakpoint)1 DebugException (com.oracle.truffle.api.debug.DebugException)1 DebugScope (com.oracle.truffle.api.debug.DebugScope)1 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)1 DebugValue (com.oracle.truffle.api.debug.DebugValue)1 SuspendAnchor (com.oracle.truffle.api.debug.SuspendAnchor)1 Source (com.oracle.truffle.api.source.Source)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 TriState (com.oracle.truffle.api.utilities.TriState)1 CallFrame (com.oracle.truffle.tools.chromeinspector.types.CallFrame)1 RemoteObject (com.oracle.truffle.tools.chromeinspector.types.RemoteObject)1 Scope (com.oracle.truffle.tools.chromeinspector.types.Scope)1 Script (com.oracle.truffle.tools.chromeinspector.types.Script)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1