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()]);
}
Aggregations