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