package com.oracle.truffle.tools.chromeinspector.types;
import com.oracle.truffle.tools.utils.json.JSONObject;
import com.oracle.truffle.api.debug.DebugStackFrame;
import com.oracle.truffle.api.debug.SuspendAnchor;
import com.oracle.truffle.api.source.SourceSection;
public final class CallFrame {
private final DebugStackFrame frame;
private final int depth;
private final Location location;
private final Location functionLocation;
private final String url;
private final RemoteObject thisObject;
private final RemoteObject returnObject;
private final Scope[] scopes;
public CallFrame(DebugStackFrame frame, int depth, Script script, SourceSection sourceSection, SuspendAnchor anchor,
SourceSection functionSourceSection, RemoteObject thisObject, RemoteObject returnObject, Scope... scopes) {
this.frame = frame;
this.depth = depth;
if (anchor == SuspendAnchor.BEFORE) {
this.location = new Location(script.getId(), sourceSection.getStartLine(), sourceSection.getStartColumn());
} else {
this.location = new Location(script.getId(), sourceSection.getEndLine(), sourceSection.getEndColumn());
}
if (functionSourceSection != null) {
this.functionLocation = new Location(script.getId(), functionSourceSection.getStartLine(), functionSourceSection.getStartColumn());
} else {
this.functionLocation = null;
}
this.url = script.getUrl();
this.thisObject = thisObject;
this.returnObject = returnObject;
this.scopes = scopes;
}
public DebugStackFrame getFrame() {
return frame;
}
public String getFunctionName() {
return frame.getName();
}
public Location getLocation() {
return location;
}
public Scope[] getScopeChain() {
return scopes;
}
public RemoteObject getThis() {
return thisObject;
}
public RemoteObject getReturnValue() {
return returnObject;
}
private JSONObject createJSON() {
JSONObject json = new JSONObject();
json.put("callFrameId", Integer.toString(depth));
json.put("functionName", frame.getName());
json.put("location", location.toJSON());
json.putOpt("functionLocation", (functionLocation != null) ? functionLocation.toJSON() : null);
json.put("url", url);
json.put("scopeChain", Scope.createScopesJSON(scopes));
json.put("this", thisObject.toJSON());
if (returnObject != null) {
json.put("returnValue", returnObject.toJSON());
}
return json;
}
public JSONObject toJSON() {
return createJSON();
}
}