forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodify.js
More file actions
309 lines (266 loc) · 8.67 KB
/
modify.js
File metadata and controls
309 lines (266 loc) · 8.67 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import {
makeBreakpointLocation,
makeBreakpointId,
getASTLocation
} from "../../utils/breakpoint";
import {
getBreakpoint,
getBreakpointPositionsForLocation,
getFirstBreakpointPosition,
getSymbols,
getSource,
getSourceContent,
getBreakpointsList,
getPendingBreakpointList
} from "../../selectors";
import { setBreakpointPositions } from "./breakpointPositions";
import { recordEvent } from "../../utils/telemetry";
import { comparePosition } from "../../utils/location";
import { getTextAtPosition } from "../../utils/source";
import type { ThunkArgs } from "../types";
import type {
Breakpoint,
BreakpointOptions,
BreakpointPosition,
SourceLocation,
Context
} from "../../types";
// This file has the primitive operations used to modify individual breakpoints
// and keep them in sync with the breakpoints installed on server threads. These
// are collected here to make it easier to preserve the following invariant:
//
// Breakpoints are included in reducer state iff they are disabled or requests
// have been dispatched to set them in all server threads.
//
// To maintain this property, updates to the reducer and installed breakpoints
// must happen with no intervening await. Using await allows other operations to
// modify the breakpoint state in the interim and potentially cause breakpoint
// state to go out of sync.
//
// The reducer is optimistically updated when users set or remove a breakpoint,
// but it might take a little while before the breakpoints have been set or
// removed in each thread. Once all outstanding requests sent to a thread have
// been processed, the reducer and server threads will be in sync.
//
// There is another exception to the above invariant when first connecting to
// the server: breakpoints have been installed on all generated locations in the
// pending breakpoints, but no breakpoints have been added to the reducer. When
// a matching source appears, either the server breakpoint will be removed or a
// breakpoint will be added to the reducer, to restore the above invariant.
// See syncBreakpoint.js for more.
function clientSetBreakpoint(breakpoint: Breakpoint) {
return ({ getState, client }: ThunkArgs) => {
const breakpointLocation = makeBreakpointLocation(
getState(),
breakpoint.generatedLocation
);
return client.setBreakpoint(breakpointLocation, breakpoint.options);
};
}
function clientRemoveBreakpoint(generatedLocation: SourceLocation) {
return ({ getState, client }: ThunkArgs) => {
const breakpointLocation = makeBreakpointLocation(
getState(),
generatedLocation
);
return client.removeBreakpoint(breakpointLocation);
};
}
export function enableBreakpoint(cx: Context, initialBreakpoint: Breakpoint) {
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
const breakpoint = getBreakpoint(getState(), initialBreakpoint.location);
if (!breakpoint || !breakpoint.disabled) {
return;
}
dispatch({
type: "SET_BREAKPOINT",
cx,
breakpoint: { ...breakpoint, disabled: false }
});
return dispatch(clientSetBreakpoint(breakpoint));
};
}
export function addBreakpoint(
cx: Context,
initialLocation: SourceLocation,
options: BreakpointOptions = {},
disabled: boolean = false,
shouldCancel: () => boolean = () => false
) {
return async ({ dispatch, getState, sourceMaps, client }: ThunkArgs) => {
recordEvent("add_breakpoint");
const { sourceId, column, line } = initialLocation;
await dispatch(setBreakpointPositions({ cx, sourceId, line }));
const position: ?BreakpointPosition = column
? getBreakpointPositionsForLocation(getState(), initialLocation)
: getFirstBreakpointPosition(getState(), initialLocation);
if (!position) {
return;
}
const { location, generatedLocation } = position;
const source = getSource(getState(), location.sourceId);
const generatedSource = getSource(getState(), generatedLocation.sourceId);
if (!source || !generatedSource) {
return;
}
const symbols = getSymbols(getState(), source);
const astLocation = getASTLocation(source, symbols, location);
const originalContent = getSourceContent(getState(), source.id);
const originalText = getTextAtPosition(
source.id,
originalContent,
location
);
const content = getSourceContent(getState(), generatedSource.id);
const text = getTextAtPosition(
generatedSource.id,
content,
generatedLocation
);
const id = makeBreakpointId(location);
const breakpoint = {
id,
disabled,
options,
location,
astLocation,
generatedLocation,
text,
originalText
};
if (shouldCancel()) {
return;
}
dispatch({ type: "SET_BREAKPOINT", cx, breakpoint });
if (disabled) {
// If we just clobbered an enabled breakpoint with a disabled one, we need
// to remove any installed breakpoint in the server.
return dispatch(clientRemoveBreakpoint(generatedLocation));
}
return dispatch(clientSetBreakpoint(breakpoint));
};
}
/**
* Remove a single breakpoint
*
* @memberof actions/breakpoints
* @static
*/
export function removeBreakpoint(cx: Context, initialBreakpoint: Breakpoint) {
return ({ dispatch, getState, client }: ThunkArgs) => {
recordEvent("remove_breakpoint");
const breakpoint = getBreakpoint(getState(), initialBreakpoint.location);
if (!breakpoint) {
return;
}
dispatch({
type: "REMOVE_BREAKPOINT",
cx,
location: breakpoint.location
});
// If the breakpoint is disabled then it is not installed in the server.
if (breakpoint.disabled) {
return;
}
return dispatch(clientRemoveBreakpoint(breakpoint.generatedLocation));
};
}
/**
* Remove all installed, pending, and client breakpoints associated with a
* target generated location.
*
* @memberof actions/breakpoints
* @static
*/
export function removeBreakpointAtGeneratedLocation(
cx: Context,
target: SourceLocation
) {
return ({ dispatch, getState, client }: ThunkArgs) => {
// Remove any breakpoints matching the generated location.
const breakpoints = getBreakpointsList(getState());
for (const { location, generatedLocation } of breakpoints) {
if (
generatedLocation.sourceId == target.sourceId &&
comparePosition(generatedLocation, target)
) {
dispatch({
type: "REMOVE_BREAKPOINT",
cx,
location
});
}
}
// Remove any remaining pending breakpoints matching the generated location.
const pending = getPendingBreakpointList(getState());
for (const { location, generatedLocation } of pending) {
if (
generatedLocation.sourceUrl == target.sourceUrl &&
comparePosition(generatedLocation, target)
) {
dispatch({
type: "REMOVE_PENDING_BREAKPOINT",
cx,
location
});
}
}
// Remove the breakpoint from the client itself.
return dispatch(clientRemoveBreakpoint(target));
};
}
/**
* Disable a single breakpoint
*
* @memberof actions/breakpoints
* @static
*/
export function disableBreakpoint(cx: Context, initialBreakpoint: Breakpoint) {
return ({ dispatch, getState, client }: ThunkArgs) => {
const breakpoint = getBreakpoint(getState(), initialBreakpoint.location);
if (!breakpoint || breakpoint.disabled) {
return;
}
dispatch({
type: "SET_BREAKPOINT",
cx,
breakpoint: { ...breakpoint, disabled: true }
});
return dispatch(clientRemoveBreakpoint(breakpoint.generatedLocation));
};
}
/**
* Update the options of a breakpoint.
*
* @throws {Error} "not implemented"
* @memberof actions/breakpoints
* @static
* @param {SourceLocation} location
* @see DebuggerController.Breakpoints.addBreakpoint
* @param {Object} options
* Any options to set on the breakpoint
*/
export function setBreakpointOptions(
cx: Context,
location: SourceLocation,
options: BreakpointOptions = {}
) {
return ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
let breakpoint = getBreakpoint(getState(), location);
if (!breakpoint) {
return dispatch(addBreakpoint(cx, location, options));
}
// Note: setting a breakpoint's options implicitly enables it.
breakpoint = { ...breakpoint, disabled: false, options };
dispatch({
type: "SET_BREAKPOINT",
cx,
breakpoint
});
return dispatch(clientSetBreakpoint(breakpoint));
};
}