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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/* 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/>. */
import { createBreakpoint } from "../../client/firefox/create";
import {
makeBreakpointServerLocation,
makeBreakpointId,
} from "../../utils/breakpoint/index";
import {
getBreakpoint,
getBreakpointPositionsForLocation,
getFirstBreakpointPosition,
getSettledSourceTextContent,
getBreakpointsList,
getPendingBreakpointList,
isMapScopesEnabled,
getBlackBoxRanges,
isSourceMapIgnoreListEnabled,
isSourceOnSourceMapIgnoreList,
} from "../../selectors/index";
import { setBreakpointPositions } from "./breakpointPositions";
import { setSkipPausing } from "../pause/skipPausing";
import { PROMISE } from "../utils/middleware/promise";
import { recordEvent } from "../../utils/telemetry";
import { comparePosition } from "../../utils/location";
import { getTextAtPosition, isLineBlackboxed } from "../../utils/source";
import { getMappedScopesForLocation } from "../pause/mapScopes";
import { validateBreakpoint } from "../../utils/context";
// 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 if 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.
async function clientSetBreakpoint(client, { getState, dispatch }, breakpoint) {
const breakpointServerLocation = makeBreakpointServerLocation(
getState(),
breakpoint.generatedLocation
);
const shouldMapBreakpointExpressions =
isMapScopesEnabled(getState()) &&
breakpoint.location.source.isOriginal &&
(breakpoint.options.logValue || breakpoint.options.condition);
if (shouldMapBreakpointExpressions) {
breakpoint = await dispatch(updateBreakpointSourceMapping(breakpoint));
}
return client.setBreakpoint(breakpointServerLocation, breakpoint.options);
}
function clientRemoveBreakpoint(client, state, generatedLocation) {
const breakpointServerLocation = makeBreakpointServerLocation(
state,
generatedLocation
);
return client.removeBreakpoint(breakpointServerLocation);
}
export function enableBreakpoint(initialBreakpoint) {
return thunkArgs => {
const { dispatch, getState, client } = thunkArgs;
const state = getState();
const breakpoint = getBreakpoint(state, initialBreakpoint.location);
const blackboxedRanges = getBlackBoxRanges(state);
const isSourceOnIgnoreList =
isSourceMapIgnoreListEnabled(state) &&
isSourceOnSourceMapIgnoreList(state, breakpoint.location.source);
if (
!breakpoint ||
!breakpoint.disabled ||
isLineBlackboxed(
blackboxedRanges[breakpoint.location.source.url],
breakpoint.location.line,
isSourceOnIgnoreList
)
) {
return null;
}
dispatch(setSkipPausing(false));
return dispatch({
type: "SET_BREAKPOINT",
breakpoint: createBreakpoint({ ...breakpoint, disabled: false }),
[PROMISE]: clientSetBreakpoint(client, thunkArgs, breakpoint),
});
};
}
export function addBreakpoint(
initialLocation,
options = {},
disabled,
shouldCancel = () => false
) {
return async thunkArgs => {
const { dispatch, getState, client } = thunkArgs;
recordEvent("add_breakpoint");
await dispatch(setBreakpointPositions(initialLocation));
const position = initialLocation.column
? getBreakpointPositionsForLocation(getState(), initialLocation)
: getFirstBreakpointPosition(getState(), initialLocation);
// No position is found if the `initialLocation` is on a non-breakable line or
// the line no longer exists.
if (!position) {
return null;
}
const { location, generatedLocation } = position;
if (!location.source || !generatedLocation.source) {
return null;
}
const originalContent = getSettledSourceTextContent(getState(), location);
const originalText = getTextAtPosition(
location.source.id,
originalContent,
location
);
const content = getSettledSourceTextContent(getState(), generatedLocation);
const text = getTextAtPosition(
generatedLocation.source.id,
content,
generatedLocation
);
const id = makeBreakpointId(location);
const breakpoint = createBreakpoint({
id,
disabled,
options,
location,
generatedLocation,
text,
originalText,
});
if (shouldCancel()) {
return null;
}
dispatch(setSkipPausing(false));
return dispatch({
type: "SET_BREAKPOINT",
breakpoint,
// If we just clobbered an enabled breakpoint with a disabled one, we need
// to remove any installed breakpoint in the server.
[PROMISE]: disabled
? clientRemoveBreakpoint(client, getState(), generatedLocation)
: clientSetBreakpoint(client, thunkArgs, breakpoint),
});
};
}
/**
* Remove a single breakpoint
*
* @memberof actions/breakpoints
* @static
*/
export function removeBreakpoint(initialBreakpoint) {
return ({ dispatch, getState, client }) => {
recordEvent("remove_breakpoint");
const breakpoint = getBreakpoint(getState(), initialBreakpoint.location);
if (!breakpoint) {
return null;
}
dispatch(setSkipPausing(false));
return dispatch({
type: "REMOVE_BREAKPOINT",
breakpoint,
// If the breakpoint is disabled then it is not installed in the server.
[PROMISE]: breakpoint.disabled
? Promise.resolve()
: clientRemoveBreakpoint(
client,
getState(),
breakpoint.generatedLocation
),
});
};
}
/**
* Remove all installed, pending, and client breakpoints associated with a
* target generated location.
*
* @param {Object} target
* Location object where to remove breakpoints.
*/
export function removeBreakpointAtGeneratedLocation(target) {
return ({ dispatch, getState, client }) => {
// remove breakpoint from the server
const onBreakpointRemoved = clientRemoveBreakpoint(
client,
getState(),
target
);
// Remove any breakpoints matching the generated location.
const breakpoints = getBreakpointsList(getState());
for (const breakpoint of breakpoints) {
const { generatedLocation } = breakpoint;
if (
generatedLocation.source.id == target.source.id &&
comparePosition(generatedLocation, target)
) {
dispatch({
type: "REMOVE_BREAKPOINT",
breakpoint,
[PROMISE]: onBreakpointRemoved,
});
}
}
// Remove any remaining pending breakpoints matching the generated location.
const pending = getPendingBreakpointList(getState());
for (const pendingBreakpoint of pending) {
const { generatedLocation } = pendingBreakpoint;
if (
generatedLocation.sourceUrl == target.source.url &&
comparePosition(generatedLocation, target)
) {
dispatch({
type: "REMOVE_PENDING_BREAKPOINT",
pendingBreakpoint,
});
}
}
return onBreakpointRemoved;
};
}
/**
* Disable a single breakpoint
*
* @memberof actions/breakpoints
* @static
*/
export function disableBreakpoint(initialBreakpoint) {
return ({ dispatch, getState, client }) => {
const breakpoint = getBreakpoint(getState(), initialBreakpoint.location);
if (!breakpoint || breakpoint.disabled) {
return null;
}
dispatch(setSkipPausing(false));
return dispatch({
type: "SET_BREAKPOINT",
breakpoint: createBreakpoint({ ...breakpoint, disabled: true }),
[PROMISE]: clientRemoveBreakpoint(
client,
getState(),
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(location, options = {}) {
return thunkArgs => {
const { dispatch, getState, client } = thunkArgs;
let breakpoint = getBreakpoint(getState(), location);
if (!breakpoint) {
return dispatch(addBreakpoint(location, options));
}
// Note: setting a breakpoint's options implicitly enables it.
breakpoint = createBreakpoint({ ...breakpoint, disabled: false, options });
return dispatch({
type: "SET_BREAKPOINT",
breakpoint,
[PROMISE]: clientSetBreakpoint(client, thunkArgs, breakpoint),
});
};
}
async function updateExpression(parserWorker, mappings, originalExpression) {
const mapped = await parserWorker.mapExpression(
originalExpression,
mappings,
[],
false,
false
);
if (!mapped) {
return originalExpression;
}
if (!originalExpression.trimEnd().endsWith(";")) {
return mapped.expression.replace(/;$/, "");
}
return mapped.expression;
}
function updateBreakpointSourceMapping(breakpoint) {
return async ({ getState, dispatch, parserWorker }) => {
const options = { ...breakpoint.options };
const mappedScopes = await dispatch(
getMappedScopesForLocation(breakpoint.location)
);
if (!mappedScopes) {
return breakpoint;
}
const { mappings } = mappedScopes;
if (options.condition) {
options.condition = await updateExpression(
parserWorker,
mappings,
options.condition
);
}
if (options.logValue) {
options.logValue = await updateExpression(
parserWorker,
mappings,
options.logValue
);
}
// As we waited for lots of asynchronous operations,
// verify that the breakpoint is still valid before
// trying to set/update it on the server.
validateBreakpoint(getState(), breakpoint);
return { ...breakpoint, options };
};
}
|