forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscopes.js
More file actions
196 lines (169 loc) · 5.13 KB
/
scopes.js
File metadata and controls
196 lines (169 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// @flow
import { toPairs } from "lodash";
import { get } from "lodash";
import { simplifyDisplayName } from "./frame";
import type { Frame, Pause, Scope, BindingContents } from "debugger-html";
export type NamedValue = {
name: string,
generatedName?: string,
path: string,
contents: BindingContents | NamedValue[]
};
// VarAndBindingsPair actually is [name: string, contents: ScopeBindings]
type VarAndBindingsPair = Array<any>;
type VarAndBindingsPairs = Array<VarAndBindingsPair>;
// Create the tree nodes representing all the variables and arguments
// for the bindings from a scope.
function getBindingVariables(bindings, parentName) {
const args: VarAndBindingsPairs = bindings.arguments.map(
arg => toPairs(arg)[0]
);
const variables: VarAndBindingsPairs = toPairs(bindings.variables);
return args.concat(variables).map(binding => {
const name = (binding[0]: string);
const contents = (binding[1]: BindingContents);
return {
name,
path: `${parentName}/${name}`,
contents
};
});
}
function getSourceBindingVariables(
bindings,
sourceBindings: {
[originalName: string]: string
},
parentName: string
) {
const result = getBindingVariables(bindings, parentName);
const index: any = Object.create(null);
result.forEach(entry => {
index[entry.name] = { used: false, entry };
});
// Find and replace variables that is present in sourceBindings.
const bound = Object.keys(sourceBindings).map(name => {
const generatedName = sourceBindings[name];
const foundMap = index[generatedName];
let contents;
if (foundMap) {
foundMap.used = true;
contents = foundMap.entry.contents;
} else {
contents = { value: { type: "undefined" } };
}
return {
name,
generatedName,
path: `${parentName}/${generatedName}`,
contents
};
});
// Use rest of them (not found in the sourceBindings) as is.
const unused = result.filter(entry => !index[entry.name].used);
return bound.concat(unused);
}
export function getSpecialVariables(pauseInfo: Pause, path: string) {
const thrown = get(pauseInfo, "why.frameFinished.throw", undefined);
const returned = get(pauseInfo, "why.frameFinished.return", undefined);
const vars = [];
if (thrown !== undefined) {
vars.push({
name: "<exception>",
path: `${path}/<exception>`,
contents: { value: thrown }
});
}
if (returned !== undefined) {
// Do not display a return value of "undefined",
if (!returned || !returned.type || returned.type !== "undefined") {
vars.push({
name: "<return>",
path: `${path}/<return>`,
contents: { value: returned }
});
}
}
return vars;
}
function getThisVariable(frame: any, path: string) {
const this_ = frame.this;
if (!this_) {
return null;
}
return {
name: "<this>",
path: `${path}/<this>`,
contents: { value: this_ }
};
}
export function getScopes(
pauseInfo: Pause,
selectedFrame: Frame,
selectedScope: ?Scope
): ?(NamedValue[]) {
if (!pauseInfo || !selectedFrame) {
return null;
}
// NOTE: it's possible that we're inspecting an old server
// that does not support getting frame scopes directly
selectedScope = selectedScope || selectedFrame.scope;
if (!selectedScope) {
return null;
}
const scopes = [];
let scope = selectedScope;
const pausedScopeActor = get(pauseInfo, "frame.scope.actor");
let scopeIndex = 1;
do {
const { type, actor } = scope;
const key = `${actor}-${scopeIndex}`;
if (type === "function" || type === "block") {
const bindings = scope.bindings;
const sourceBindings = scope.sourceBindings;
let title;
if (type === "function") {
title = scope.function.displayName
? simplifyDisplayName(scope.function.displayName)
: L10N.getStr("anonymous");
} else {
title = L10N.getStr("scopes.block");
}
let vars = sourceBindings
? getSourceBindingVariables(bindings, sourceBindings, key)
: getBindingVariables(bindings, key);
// show exception, return, and this variables in innermost scope
if (scope.actor === pausedScopeActor) {
vars = vars.concat(getSpecialVariables(pauseInfo, key));
}
if (scope.actor === selectedScope.actor) {
const this_ = getThisVariable(selectedFrame, key);
if (this_) {
vars.push(this_);
}
}
if (vars && vars.length) {
vars.sort((a, b) => a.name.localeCompare(b.name));
scopes.push({
name: title,
path: key,
contents: vars
});
}
} else if (type === "object") {
let value = scope.object;
// If this is the global window scope, mark it as such so that it will
// preview Window: Global instead of Window: Window
if (value.class === "Window") {
value = Object.assign({}, scope.object, { displayClass: "Global" });
}
scopes.push({
name: scope.object.class,
path: key,
contents: { value }
});
}
scopeIndex++;
} while ((scope = scope.parent)); // eslint-disable-line no-cond-assign
return scopes;
}