2
|
1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
|
|
2 |
// Use of this source code is governed by a BSD-style license that can be
|
|
3 |
// found in the LICENSE file.
|
|
4 |
|
|
5 |
package org.chromium.debug.core.model;
|
|
6 |
|
|
7 |
import java.util.ArrayList;
|
|
8 |
import java.util.Collection;
|
|
9 |
import java.util.Collections;
|
|
10 |
import java.util.List;
|
|
11 |
|
|
12 |
import org.chromium.debug.core.ChromiumDebugPlugin;
|
|
13 |
import org.chromium.sdk.CallFrame;
|
|
14 |
import org.chromium.sdk.JsArray;
|
|
15 |
import org.chromium.sdk.JsFunction;
|
|
16 |
import org.chromium.sdk.JsObject;
|
|
17 |
import org.chromium.sdk.JsScope;
|
|
18 |
import org.chromium.sdk.JsValue;
|
|
19 |
import org.chromium.sdk.JsVariable;
|
|
20 |
import org.chromium.sdk.Script;
|
|
21 |
import org.chromium.sdk.internal.tools.v8.MethodIsBlockingException;
|
|
22 |
import org.eclipse.core.runtime.IStatus;
|
|
23 |
import org.eclipse.core.runtime.Status;
|
|
24 |
import org.eclipse.debug.core.DebugException;
|
|
25 |
import org.eclipse.debug.core.model.IRegisterGroup;
|
|
26 |
import org.eclipse.debug.core.model.IStackFrame;
|
|
27 |
import org.eclipse.debug.core.model.IThread;
|
|
28 |
import org.eclipse.debug.core.model.IVariable;
|
|
29 |
import org.eclipse.osgi.util.NLS;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* An IStackFrame implementation over a JsStackFrame instance.
|
|
33 |
*/
|
|
34 |
public class StackFrame extends DebugElementImpl implements IStackFrame {
|
|
35 |
|
|
36 |
private final JavascriptThread thread;
|
|
37 |
|
|
38 |
private final CallFrame stackFrame;
|
|
39 |
|
|
40 |
private IVariable[] variables;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Constructs a stack frame for the given handler using the FrameMirror data
|
|
44 |
* from the remote V8 VM.
|
|
45 |
*
|
|
46 |
* @param debugTarget the global parent
|
|
47 |
* @param thread for which the stack frame is created
|
|
48 |
* @param stackFrame an underlying SDK stack frame
|
|
49 |
*/
|
|
50 |
public StackFrame(IChromiumDebugTarget debugTarget, JavascriptThread thread, CallFrame stackFrame) {
|
|
51 |
super(debugTarget);
|
|
52 |
this.thread = thread;
|
|
53 |
this.stackFrame = stackFrame;
|
|
54 |
}
|
|
55 |
|
|
56 |
public CallFrame getCallFrame() {
|
|
57 |
return stackFrame;
|
|
58 |
}
|
|
59 |
|
|
60 |
public IThread getThread() {
|
|
61 |
return thread;
|
|
62 |
}
|
|
63 |
|
|
64 |
public IVariable[] getVariables() throws DebugException {
|
|
65 |
if (variables == null) {
|
|
66 |
try {
|
|
67 |
variables = wrapScopes(getDebugTarget(), stackFrame.getVariableScopes(),
|
|
68 |
stackFrame.getReceiverVariable());
|
|
69 |
} catch (RuntimeException e) {
|
|
70 |
// We shouldn't throw RuntimeException from here, because calling
|
|
71 |
// ElementContentProvider#update will forget to call update.done().
|
|
72 |
throw new DebugException(new Status(IStatus.ERROR, ChromiumDebugPlugin.PLUGIN_ID,
|
|
73 |
"Failed to read variables", e)); //$NON-NLS-1$
|
|
74 |
}
|
|
75 |
}
|
|
76 |
return variables;
|
|
77 |
}
|
|
78 |
|
|
79 |
static IVariable[] wrapVariables(
|
|
80 |
IChromiumDebugTarget debugTarget, Collection<? extends JsVariable> jsVars,
|
|
81 |
Collection <? extends JsVariable> jsInternalProperties) {
|
|
82 |
List<Variable> vars = new ArrayList<Variable>(jsVars.size());
|
|
83 |
for (JsVariable jsVar : jsVars) {
|
|
84 |
vars.add(new Variable(debugTarget, jsVar, false));
|
|
85 |
}
|
|
86 |
for (JsVariable jsMetaVar : jsInternalProperties) {
|
|
87 |
vars.add(new Variable(debugTarget, jsMetaVar, true));
|
|
88 |
}
|
|
89 |
return vars.toArray(new IVariable[vars.size()]);
|
|
90 |
}
|
|
91 |
|
|
92 |
static IVariable[] wrapScopes(IChromiumDebugTarget debugTarget, List<? extends JsScope> jsScopes,
|
|
93 |
JsVariable receiverVariable) {
|
|
94 |
List<Variable> vars = new ArrayList<Variable>();
|
|
95 |
|
|
96 |
for (JsScope scope : jsScopes) {
|
|
97 |
if (scope.getType() == JsScope.Type.GLOBAL) {
|
|
98 |
if (receiverVariable != null) {
|
|
99 |
vars.add(new Variable(debugTarget, receiverVariable, false));
|
|
100 |
receiverVariable = null;
|
|
101 |
}
|
|
102 |
vars.add(new Variable(debugTarget, wrapScopeAsVariable(scope), false));
|
|
103 |
} else {
|
|
104 |
for (JsVariable var : scope.getVariables()) {
|
|
105 |
vars.add(new Variable(debugTarget, var, false));
|
|
106 |
}
|
|
107 |
}
|
|
108 |
}
|
|
109 |
if (receiverVariable != null) {
|
|
110 |
vars.add(new Variable(debugTarget, receiverVariable, false));
|
|
111 |
}
|
|
112 |
|
|
113 |
IVariable[] result = new IVariable[vars.size()];
|
|
114 |
// Return in reverse order.
|
|
115 |
for (int i = 0; i < result.length; i++) {
|
|
116 |
result[result.length - i - 1] = vars.get(i);
|
|
117 |
}
|
|
118 |
return result;
|
|
119 |
}
|
|
120 |
|
|
121 |
private static JsVariable wrapScopeAsVariable(final JsScope jsScope) {
|
|
122 |
class ScopeObjectVariable implements JsVariable, JsObject {
|
|
123 |
public String getFullyQualifiedName() {
|
|
124 |
return getName();
|
|
125 |
}
|
|
126 |
public String getName() {
|
|
127 |
// TODO(peter.rybin): should we localize it?
|
|
128 |
return "<" + jsScope.getType() + ">";
|
|
129 |
}
|
|
130 |
public JsValue getValue() throws UnsupportedOperationException {
|
|
131 |
return this;
|
|
132 |
}
|
|
133 |
public boolean isMutable() {
|
|
134 |
return false;
|
|
135 |
}
|
|
136 |
public boolean isReadable() {
|
|
137 |
return true;
|
|
138 |
}
|
|
139 |
public void setValue(String newValue, SetValueCallback callback)
|
|
140 |
throws UnsupportedOperationException {
|
|
141 |
throw new UnsupportedOperationException();
|
|
142 |
}
|
|
143 |
public JsArray asArray() {
|
|
144 |
return null;
|
|
145 |
}
|
|
146 |
public JsFunction asFunction() {
|
|
147 |
return null;
|
|
148 |
}
|
|
149 |
public String getClassName() {
|
|
150 |
// TODO(peter.rybin): should we localize it?
|
|
151 |
return "#Scope";
|
|
152 |
}
|
|
153 |
public Collection<? extends JsVariable> getProperties() throws MethodIsBlockingException {
|
|
154 |
return jsScope.getVariables();
|
|
155 |
}
|
|
156 |
public Collection<? extends JsVariable> getInternalProperties()
|
|
157 |
throws MethodIsBlockingException {
|
|
158 |
return Collections.emptyList();
|
|
159 |
}
|
|
160 |
public JsVariable getProperty(String name) {
|
|
161 |
for (JsVariable var : getProperties()) {
|
|
162 |
if (var.getName().equals(name)) {
|
|
163 |
return var;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
return null;
|
|
167 |
}
|
|
168 |
public JsObject asObject() {
|
|
169 |
return this;
|
|
170 |
}
|
|
171 |
public Type getType() {
|
|
172 |
return Type.TYPE_OBJECT;
|
|
173 |
}
|
|
174 |
public String getValueString() {
|
|
175 |
return getClassName();
|
|
176 |
}
|
|
177 |
public String getRefId() {
|
|
178 |
return null;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
return new ScopeObjectVariable();
|
|
182 |
}
|
|
183 |
|
|
184 |
public boolean hasVariables() throws DebugException {
|
|
185 |
return stackFrame.getReceiverVariable() != null || stackFrame.getVariableScopes().size() > 0;
|
|
186 |
}
|
|
187 |
|
|
188 |
public int getLineNumber() throws DebugException {
|
|
189 |
// convert 0-based to 1-based
|
|
190 |
return stackFrame.getLineNumber() + 1;
|
|
191 |
}
|
|
192 |
|
|
193 |
public int getCharStart() throws DebugException {
|
|
194 |
return stackFrame.getCharStart();
|
|
195 |
}
|
|
196 |
|
|
197 |
public int getCharEnd() throws DebugException {
|
|
198 |
return -1;
|
|
199 |
}
|
|
200 |
|
|
201 |
public String getName() throws DebugException {
|
|
202 |
String name = stackFrame.getFunctionName();
|
|
203 |
Script script = stackFrame.getScript();
|
|
204 |
if (script == null) {
|
|
205 |
return Messages.StackFrame_UnknownScriptName;
|
|
206 |
}
|
|
207 |
int line = script.getStartLine() + getLineNumber();
|
|
208 |
if (line != -1) {
|
|
209 |
name = NLS.bind(Messages.StackFrame_NameFormat, new Object[] {name, script.getName(), line});
|
|
210 |
}
|
|
211 |
return name;
|
|
212 |
}
|
|
213 |
|
|
214 |
public IRegisterGroup[] getRegisterGroups() throws DebugException {
|
|
215 |
return null;
|
|
216 |
}
|
|
217 |
|
|
218 |
public boolean hasRegisterGroups() throws DebugException {
|
|
219 |
return false;
|
|
220 |
}
|
|
221 |
|
|
222 |
public boolean canStepInto() {
|
|
223 |
return getThread().canStepInto();
|
|
224 |
}
|
|
225 |
|
|
226 |
public boolean canStepOver() {
|
|
227 |
return getThread().canStepOver();
|
|
228 |
}
|
|
229 |
|
|
230 |
public boolean canStepReturn() {
|
|
231 |
return getThread().canStepReturn();
|
|
232 |
}
|
|
233 |
|
|
234 |
public boolean isStepping() {
|
|
235 |
return getThread().isStepping();
|
|
236 |
}
|
|
237 |
|
|
238 |
public void stepInto() throws DebugException {
|
|
239 |
getThread().stepInto();
|
|
240 |
}
|
|
241 |
|
|
242 |
public void stepOver() throws DebugException {
|
|
243 |
getThread().stepOver();
|
|
244 |
}
|
|
245 |
|
|
246 |
public void stepReturn() throws DebugException {
|
|
247 |
getThread().stepReturn();
|
|
248 |
}
|
|
249 |
|
|
250 |
public boolean canResume() {
|
|
251 |
return getThread().canResume();
|
|
252 |
}
|
|
253 |
|
|
254 |
public boolean canSuspend() {
|
|
255 |
return getThread().canSuspend();
|
|
256 |
}
|
|
257 |
|
|
258 |
public boolean isSuspended() {
|
|
259 |
return getThread().isSuspended();
|
|
260 |
}
|
|
261 |
|
|
262 |
public void resume() throws DebugException {
|
|
263 |
getThread().resume();
|
|
264 |
}
|
|
265 |
|
|
266 |
public void suspend() throws DebugException {
|
|
267 |
getThread().suspend();
|
|
268 |
}
|
|
269 |
|
|
270 |
public boolean canTerminate() {
|
|
271 |
return getThread().canTerminate();
|
|
272 |
}
|
|
273 |
|
|
274 |
public boolean isTerminated() {
|
|
275 |
return getThread().isTerminated();
|
|
276 |
}
|
|
277 |
|
|
278 |
public void terminate() throws DebugException {
|
|
279 |
getThread().terminate();
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Returns the name of the source file this stack frame is associated with.
|
|
284 |
*
|
|
285 |
* @return the name of the source file this stack frame is associated with
|
|
286 |
*/
|
|
287 |
String getSourceName() {
|
|
288 |
Script script = stackFrame.getScript();
|
|
289 |
if (script == null) {
|
|
290 |
return Messages.StackFrame_UnknownScriptName;
|
|
291 |
}
|
|
292 |
return script.getName();
|
|
293 |
}
|
|
294 |
|
|
295 |
@Override
|
|
296 |
public boolean equals(Object obj) {
|
|
297 |
if (obj instanceof StackFrame) {
|
|
298 |
StackFrame other = (StackFrame) obj;
|
|
299 |
return other.stackFrame.equals(this.stackFrame);
|
|
300 |
}
|
|
301 |
return false;
|
|
302 |
}
|
|
303 |
|
|
304 |
@Override
|
|
305 |
public int hashCode() {
|
|
306 |
return stackFrame.hashCode();
|
|
307 |
}
|
|
308 |
|
|
309 |
}
|