Debian Codegraph @

js/src/doc/Debugger/Debugger.Script.md

raw
1 # Debugger.Script
2
3 A `Debugger.Script` instance may refer to a sequence of bytecode in the
4 debuggee or to a block of WebAssembly code. For the former, it is the
5 [`Debugger`][debugger-object] API's presentation of a JSAPI `JSScript`
6 object. The two cases are distinguished by their `format` property being
7 `"js"` or `"wasm"`.
8
9 ## Debugger.Script for JSScripts
10
11 For `Debugger.Script` instances referring to a `JSScript`, they are
12 distinguished by their `format` property being `"js"`.
13
14 Each of the following is represented by a single `JSScript` object:
15
16 * The body of a function—that is, all the code in the function that is not
17 contained within some nested function.
18
19 * The code passed to a single call to `eval`, excluding the bodies of any
20 functions that code defines.
21
22 * The contents of a `<script>` element.
23
24 * A DOM event handler, whether embedded in HTML or attached to the element
25 by other JavaScript code.
26
27 * Code appearing in a `javascript:` URL.
28
29 The [`Debugger`][debugger-object] interface constructs `Debugger.Script` objects as scripts
30 of debuggee code are uncovered by the debugger: via the `onNewScript`
31 handler method; via [`Debugger.Frame`][frame]'s `script` properties; via the
32 `functionScript` method of [`Debugger.Object`][object] instances; and so on. For a
33 given [`Debugger`][debugger-object] instance, SpiderMonkey constructs exactly one
34 `Debugger.Script` instance for each underlying script object; debugger
35 code can add its own properties to a script object and expect to find
36 them later, use `==` to decide whether two expressions refer to the same
37 script, and so on.
38
39 (If more than one [`Debugger`][debugger-object] instance is debugging the same code, each
40 [`Debugger`][debugger-object] gets a separate `Debugger.Script` instance for a given
41 script. This allows the code using each [`Debugger`][debugger-object] instance to place
42 whatever properties it likes on its `Debugger.Script` instances, without
43 worrying about interfering with other debuggers.)
44
45 A `Debugger.Script` instance is a strong reference to a JSScript object;
46 it protects the script it refers to from being garbage collected.
47
48 Note that SpiderMonkey may use the same `Debugger.Script` instances for
49 equivalent functions or evaluated code—that is, scripts representing the
50 same source code, at the same position in the same source file,
51 evaluated in the same lexical environment.
52
53 ## Debugger.Script for WebAssembly
54
55 For `Debugger.Script` instances referring to a block of WebAssembly code, they
56 are distinguished by their `format` property being `"wasm"`.
57
58 Currently only entire modules evaluated via `new WebAssembly.Module` are
59 represented.
60
61 `Debugger.Script` objects for WebAssembly are uncovered via `onNewScript` when
62 a new WebAssembly module is instantiated and via the `findScripts` method on
63 [`Debugger`][debugger-object] instances. SpiderMonkey constructs exactly one
64 `Debugger.Script` for each underlying WebAssembly module per
65 [`Debugger`][debugger-object] instance.
66
67 A `Debugger.Script` instance is a strong reference to the underlying
68 WebAssembly module; it protects the module it refers to from being garbage
69 collected.
70
71 Please note at the time of this writing, support for WebAssembly is
72 very preliminary. Many properties and methods below throw.
73
74 ## Convention
75
76 For descriptions of properties and methods below, if the behavior of the
77 property or method differs between the instance referring to a `JSScript` or
78 to a block of WebAssembly code, the text will be split into two sections,
79 headed by "**if the instance refers to a `JSScript`**" and "**if the instance
80 refers to WebAssembly code**", respectively. If the behavior does not differ,
81 no such emphasized headings will appear.
82
83 ## Accessor Properties of the Debugger.Script Prototype Object
84
85 A `Debugger.Script` instance inherits the following accessor properties
86 from its prototype:
87
88 ### `isGeneratorFunction`
89 True if this instance refers to a `JSScript` for a function defined with a
90 `function*` expression or statement. False otherwise.
91
92 ### `isAsyncFunction`
93 True if this instance refers to a `JSScript` for an async function, defined
94 with an `async function` expression or statement. False otherwise.
95
96 ### `isFunction`
97 True if this instance refers to a `JSScript` for a function. False
98 otherwise.
99
100 ### `isModule`
101 True if this instance refers to a `JSScript` that was parsed and loaded
102 as an ECMAScript module. False otherwise.
103
104 ### `displayName`
105 **If the instance refers to a `JSScript`**, this is the script's display
106 name, if it has one. If the script has no display name &mdash; for example,
107 if it is a top-level `eval` script &mdash; this is `undefined`.
108
109 If the script's function has a given name, its display name is the same as
110 its function's given name.
111
112 If the script's function has no name, SpiderMonkey attempts to infer an
113 appropriate name for it given its context. For example:
114
115 ```js
116 function f() {} // display name: f (the given name)
117 var g = function () {}; // display name: g
118 o.p = function () {}; // display name: o.p
119 var q = {
120 r: function () {} // display name: q.r
121 };
122 ```
123
124 Note that the display name may not be a proper JavaScript identifier,
125 or even a proper expression: we attempt to find helpful names even when
126 the function is not immediately assigned as the value of some variable
127 or property. Thus, we use <code><i>a</i>/<i>b</i></code> to refer to
128 the <i>b</i> defined within <i>a</i>, and <code><i>a</i>&lt;</code> to
129 refer to a function that occurs somewhere within an expression that is
130 assigned to <i>a</i>. For example:
131
132 ```js
133 function h() {
134 var i = function() {}; // display name: h/i
135 f(function () {}); // display name: h/<
136 }
137 var s = f(function () {}); // display name: s<
138 ```
139
140 **If the instance refers to WebAssembly code**, throw a `TypeError`.
141
142 ### `parameterNames`
143 **If the instance refers to a `JSScript`**, the names of its parameters,
144 as an array of strings. If the script is not a function script this is
145 `undefined`.
146
147 If the function uses destructuring parameters, the corresponding array elements
148 are `undefined`. For example, if the referent is a function script declared in this
149 way:
150
151 ```js
152 function f(a, [b, c], {d, e:f}) { ... }
153 ```
154
155 then this `Debugger.Script` instance's `parameterNames` property would
156 have the value:
157
158 ```js
159 ["a", undefined, undefined]
160 ```
161
162 **If the instance refers to WebAssembly code**, throw a `TypeError`.
163
164 ### `url`
165 **If the instance refers to a `JSScript`**, the filename or URL from which
166 this script's code was loaded. For scripts created by `eval` or the
167 `Function` constructor, this may be a synthesized filename, starting with a
168 valid URL and followed by information tracking how the code was introduced
169 into the system; the entire string is not a valid URL. For
170 `Function.prototype`'s script, this is `null`. If this `Debugger.Script`'s
171 `source` property is non-`null`, then this is equal to `source.url`.
172
173 **If the instance refers to WebAssembly code**, throw a `TypeError`.
174
175 ### `startLine`
176 **If the instance refers to a `JSScript`**, the 1-origin number of the line at
177 which this script's code starts, within the file or document named by
178 `url`.
179
180 ### `startColumn`
181 **If the instance refers to a `JSScript`**, the 1-origin column number at
182 which this script's code starts, within the file or document named by `url`.
183 For functions, this is the start of the function's arguments:
184 ```js
185 function f() { ... }
186 // ^ start (column 11)
187 let g = x => x*x;
188 // ^ start (column 9)
189 let h = (x) => x*x;
190 // ^ start (column 9)
191 ```
192 For default class constructors, it is the start of the `class` keyword:
193 ```js
194 let MyClass = class { };
195 // ^ start (column 15)
196 ```
197 For scripts from other sources, such as `eval` or the `Function`
198 constructor, it is typically 0:
199 ```js
200 let f = new Function(" console.log('hello world');");
201 // ^ start (column 1, from the string's perspective)
202 ```
203
204 ### `lineCount`
205 **If the instance refers to a `JSScript`**, the 1-origin number of lines this
206 script's code occupies, within the file or document named by `url`.
207
208 ### `source`
209 **If the instance refers to a `JSScript`**, the
210 [`Debugger.Source`][source] instance representing the source code from
211 which this script was produced. This is `null` if the source code was not
212 retained.
213
214 **If the instance refers to WebAssembly code**, the
215 [`Debugger.Source`][source] instance representing the serialized text
216 format of the WebAssembly code.
217
218 ### `sourceStart`
219 **If the instance refers to a `JSScript`**, the character within the
220 [`Debugger.Source`][source] instance given by `source` at which this
221 script's code starts; zero-based. If this is a function's script, this is
222 the index of the start of the `function` token in the source code.
223
224 **If the instance refers to WebAssembly code**, throw a `TypeError`.
225
226 ### `sourceLength`
227 **If the instance refers to a `JSScript`**, the length, in characters, of
228 this script's code within the [`Debugger.Source`][source] instance given
229 by `source`.
230
231 **If the instance refers to WebAssembly code**, throw a `TypeError`.
232
233 ### `mainOffset`
234 **If the instance refers to a `JSScript`**, the 0-origin offset of the main
235 entry point of the script, excluding any prologue.
236
237 **If the instance refers to WebAssembly code**, throw a `TypeError`.
238
239 ### `global`
240 **If the instance refers to a `JSScript`**, a [`Debugger.Object`][object]
241 instance referring to the global object in whose scope this script
242 runs. The result refers to the global directly, not via a wrapper or a
243 `WindowProxy` ("outer window", in Firefox).
244
245 **If the instance refers to WebAssembly code**, throw a `TypeError`.
246
247 ### `format`
248 **If the instance refers to a `JSScript`**, `"js"`.
249
250 **If the instance refers to WebAssembly code**, `"wasm"`.
251
252 ## Function Properties of the Debugger.Script Prototype Object
253
254 The functions described below may only be called with a `this` value
255 referring to a `Debugger.Script` instance; they may not be used as
256 methods of other kinds of objects.
257
258 ### `getChildScripts()`
259 **If the instance refers to a `JSScript`**, return a new array whose
260 elements are Debugger.Script objects for each function
261 in this script. Only direct children are included; nested
262 children can be reached by walking the tree.
263
264 **If the instance refers to WebAssembly code**, throw a `TypeError`.
265
266 ### `getPossibleBreakpoints(query)`
267 Query for the recommended breakpoint locations available in SpiderMonkey.
268 Returns a result array of objects with the following properties:
269 * `offset: number` - The 0-origin offset the breakpoint.
270 * `lineNumber: number` - The 1-origin line number of the breakpoint.
271 * `columnNumber: number` - The 1-origin column number of the breakpoint.
272 * `isStepStart: boolean` - True if SpiderMonkey recommends that the
273 breakpoint be treated as a step location when users of debuggers
274 step to the next item. This _roughly_ translates to the start of
275 each statement, though not entirely.
276
277 The `query` argument can be used to filter the set of breakpoints.
278 The `query` object can contain the following properties:
279
280 * `minOffset: number` - The inclusive lower bound of `offset` values to include (0-origin).
281 * `maxOffset: number` - The exclusive upper bound of `offset` values to include (0-origin).
282 * `line: number` - Limit to breakpoints on the given line (1-origin).
283 * `minLine: number` - The inclusive lower bound of lines to include (1-origin).
284 * `minColumn: number` - The inclusive lower bound of the line/minLine column to include (1-origin).
285 * `maxLine: number` - The exclusive upper bound of lines to include (1-origin).
286 * `maxColumn: number` - The exclusive upper bound of the line/maxLine column to include (1-origin).
287
288 ### `getPossibleBreakpointOffsets(query)`
289 Query for the recommended breakpoint locations available in SpiderMonkey.
290 Identical to getPossibleBreakpoints except this returns an array of `offset`
291 values instead of offset metadata objects.
292
293 ### `getOffsetMetadata(offset)`
294 Get metadata about a given bytecode offset (0-origin).
295 Returns an object with the following properties:
296 * `lineNumber: number` - The 1-origin line number of the breakpoint.
297 * `columnNumber: number` - The 1-origin column number of the breakpoint.
298 * `isBreakpoint: boolean` - True if this offset qualifies as a breakpoint,
299 defined using the same semantics used for `getPossibleBreakpoints()`.
300 * `isStepStart: boolean` - True if SpiderMonkey recommends that the
301 breakpoint be treated as a step location when users of debuggers
302 step to the next item. This _roughly_ translates to the start of
303 each statement, though not entirely.
304
305 ### `setBreakpoint(offset, handler)`
306 **If the instance refers to a `JSScript`**, set a breakpoint at the
307 bytecode instruction at <i>offset</i> (0-origin) in this script, reporting hits
308 to the `hit` method of <i>handler</i>. If <i>offset</i> is not a valid offset
309 in this script, throw an error. Also, even if <i>offset</i> is valid offset
310 in this script, some instructions for engine-internal operation (e.g.
311 SetAliasedVar in the generator function initialization) don't allow setting
312 breakpoints, and in that case, this also throws an error.
313
314 When execution reaches the given instruction, SpiderMonkey calls the
315 `hit` method of <i>handler</i>, passing a [`Debugger.Frame`][frame]
316 instance representing the currently executing stack frame. The `hit`
317 method's return value should be a [resumption value][rv], determining
318 how execution should continue.
319
320 Any number of breakpoints may be set at a single location; when control
321 reaches that point, SpiderMonkey calls their handlers in an unspecified
322 order.
323
324 Any number of breakpoints may use the same <i>handler</i> object.
325
326 Breakpoint handler method calls are cross-compartment, intra-thread
327 calls: the call takes place in the same thread that hit the breakpoint,
328 and in the compartment containing the handler function (typically the
329 debugger's compartment).
330
331 The new breakpoint belongs to the [`Debugger`][debugger-object] instance to
332 which this script belongs. Removing a global from the
333 [`Debugger`][debugger-object] instance's set of debuggees clears all the
334 breakpoints belonging to that [`Debugger`][debugger-object] instance in that
335 global's scripts.
336
337 ### `getBreakpoints([offset])`
338 **If the instance refers to a `JSScript`**, return an array containing the
339 handler objects for all the breakpoints set at <i>offset</i> (0-origin) in this
340 script. If <i>offset</i> is omitted, return the handlers of all
341 breakpoints set anywhere in this script. If <i>offset</i> is present, but
342 not a valid offset in this script, throw an error.
343
344 **If the instance refers to WebAssembly code**, throw a `TypeError`.
345
346 ### `clearBreakpoint(handler, [offset])`
347 **If the instance refers to a `JSScript`**, remove all breakpoints set in
348 this [`Debugger`][debugger-object] instance that use <i>handler</i> as
349 their handler. If <i>offset</i> (0-origin) is given, remove only those
350 breakpoints set at <i>offset</i> that use <i>handler</i>; if <i>offset</i> is
351 not a valid offset in this script, throw an error.
352
353 Note that, if breakpoints using other handler objects are set at the
354 same location(s) as <i>handler</i>, they remain in place.
355
356 ### `clearAllBreakpoints([offset])`
357 **If the instance refers to a `JSScript`**, remove all breakpoints set in
358 this script. If <i>offset</i> (0-origin) is present, remove all breakpoints set
359 at that offset in this script; if <i>offset</i> is not a valid bytecode
360 offset in this script, throw an error.
361
362 ### `getEffectfulOffsets()`
363 **If the instance refers to a `JSScript`**, return an array
364 containing the 0-origin offsets of all bytecodes in the script which can have
365 direct side effects that are visible outside the currently executing frame.
366 This includes, for example, operations that set properties or elements on
367 objects, or that may set names in environments created outside the frame.
368
369 This doesn't include some instructions for engine-internal operation (e.g.
370 SetAliasedVar in the generator function initialization). Those instructions
371 can be effectful in term of engine-internal, but that's not user-visible and
372 can be treated as not-effectful here.
373
374 ### `getOffsetsCoverage()`:
375 **If the instance refers to a `JSScript`**, return `null` or an array which
376 contains information about the coverage of all opcodes. The elements of
377 the array are objects, each of which describes a single opcode, and
378 contains the following properties:
379
380 * `lineNumber`: the 1-origin line number of the current opcode.
381
382 * `columnNumber`: the 1-origin column number of the current opcode.
383
384 * `offset`: the 0-origin bytecode instruction offset of the current opcode.
385
386 * `count`: the number of times the current opcode got executed.
387
388 If this script has no coverage, or if it is not instrumented, then this
389 function will return `null`. To ensure that the debuggee is instrumented,
390 the flag `Debugger.collectCoverageInfo` should be set to `true`.
391
392 **If the instance refers to WebAssembly code**, throw a `TypeError`.
393
394 ### `isInCatchScope([offset])`
395 **If the instance refers to a `JSScript`**, this is `true` if this 0-origin
396 offset falls within the scope of a try block, and `false` otherwise.
397
398 **If the instance refers to WebAssembly code**, throw a `TypeError`.
399
400 ### Deprecated Debugger.Script Prototype Functions
401
402 The following functions have all been deprecated in favor of `getOffsetMetadata`,
403 `getPossibleBreakpoints`, and `getPossibleBreakpointOffsets`. These functions
404 all have an under-defined concept of what offsets are and are not included
405 in their results.
406
407 #### `getAllOffsets()`
408 **If the instance refers to a `JSScript`**, return an array <i>L</i>
409 describing the relationship between bytecode instruction offsets (0-origin) and
410 source code positions in this script. <i>L</i> is sparse, and indexed by
411 source line number. If a source line number <i>line</i> has no code, then
412 <i>L</i> has no <i>line</i> property. If there is code for <i>line</i>,
413 then <code><i>L</i>[<i>line</i>]</code> is an array of offsets of byte
414 code instructions that are entry points to that line.
415
416 For example, suppose we have a script for the following source code:
417
418 ```js
419 a=[]
420 for (i=1; i < 10; i++)
421 // It's hip to be square.
422 a[i] = i*i;
423 ```
424
425 Calling `getAllOffsets()` on that code might yield an array like this:
426
427 ```js
428 [, [0], [16, 75], , [52]]
429 ```
430
431 This array indicates that:
432
433 * the first line's code starts at offset 0 in the script;
434
435 * the `for` statement head has two entry points at offsets 16 and 75 (for
436 the initialization, which is performed only once, and the loop test,
437 which is performed at the start of each iteration);
438
439 * the third line has no code;
440
441 * and the fourth line begins at offset 52.
442
443 **If the instance refers to WebAssembly code**, throw a `TypeError`.
444
445 #### `getAllColumnOffsets()`
446 **If the instance refers to a `JSScript`**, return an array describing the
447 relationship between bytecode instruction offsets and source code
448 positions in this script. Unlike getAllOffsets(), which returns all
449 offsets that are entry points for each line, getAllColumnOffsets() returns
450 all offsets that are entry points for each (line, column) pair.
451
452 The elements of the array are objects, each of which describes a single
453 entry point, and contains the following properties:
454
455 * lineNumber: the 1-origin line number for which offset is an entry point
456
457 * columnNumber: the 1-origin column number for which offset is an entry point
458
459 * offset: the 0-origin bytecode instruction offset of the entry point
460
461 For example, suppose we have a script for the following source code:
462
463 ```js
464 a=[]
465 for (i=1; i < 10; i++)
466 // It's hip to be square.
467 a[i] = i*i;
468 ```
469
470 Calling `getAllColumnOffsets()` on that code might yield an array like this:
471
472 ```js
473 [{ lineNumber: 1, columnNumber: 1, offset: 0 },
474 { lineNumber: 2, columnNumber: 6, offset: 16 },
475 { lineNumber: 2, columnNumber: 11, offset: 28 },
476 { lineNumber: 4, columnNumber: 5, offset: 52 },
477 { lineNumber: 4, columnNumber: 14, offset: 67 },
478 { lineNumber: 2, columnNumber: 19, offset: 75 }]
479 ```
480
481 **If the instance refers to WebAssembly code**, throw a `TypeError`.
482
483 #### `getLineOffsets(line)`
484 **If the instance refers to a `JSScript`**, return an array of bytecode
485 instruction offsets representing the entry points to source line
486 <i>line</i> (1-origin). If the script contains no executable code at that line,
487 the array returned is empty.
488
489 #### `getOffsetLocation(offset)`
490 **If the instance refers to a `JSScript`**, return an object describing the
491 source code location responsible for the bytecode at <i>offset</i> in this
492 script. The object has the following properties:
493
494 * `lineNumber`: the 1-origin line number for which offset is an entry point
495
496 * `columnNumber`: the 1-origin column number for which offset is an entry point
497
498 * `isEntryPoint`: true if the offset is a column entry point, as
499 would be reported by getAllColumnOffsets(); otherwise false.
500
501
502 [debugger-object]: Debugger.md
503 [source]: Debugger.Source.md
504 [object]: Debugger.Object.md
505 [frame]: Debugger.Frame.md
506 [rv]: Conventions.md#resumption-values