Debian Codegraph @

js/src/doc/Debugger/Conventions.md

raw
1 # General Conventions
2
3 This page describes general conventions used in the [`Debugger`][debugger] API,
4 and defines some terminology used throughout the specification.
5
6
7 ## Properties
8
9 Properties of objects that comprise the `Debugger` interface, and those
10 that the interface creates, follow some general conventions:
11
12 - Instances and prototypes are extensible; you can add your own properties
13 and methods to them.
14
15 - Properties are configurable. This applies to both "own" and prototype
16 properties, and to both methods and data properties. (Leaving these
17 properties open to redefinition will hopefully make it easier for
18 JavaScript debugger code to cope with bugs, bug fixes, and changes in the
19 interface over time.)
20
21 - Method properties are writable.
22
23 - We prefer inherited accessor properties to own data properties. Both are
24 read using the same syntax, but inherited accessors seem like a more
25 accurate reflection of what's going on. Unless otherwise noted, these
26 properties have getters but no setters, as they cannot meaningfully be
27 assigned to.
28
29
30 ## Debuggee Values
31
32 The `Debugger` interface follows some conventions to help debuggers safely
33 inspect and modify the debuggee's objects and values. Primitive values are
34 passed freely between debugger and debuggee; copying or wrapping is handled
35 transparently. Objects received from the debuggee (including host objects
36 like DOM elements) are fronted in the debugger by `Debugger.Object`
37 instances, which provide reflection-oriented methods for inspecting their
38 referents; see `Debugger.Object`, below.
39
40 Of the debugger's objects, only `Debugger.Object` instances may be passed
41 to the debuggee: when this occurs, the debuggee receives the
42 `Debugger.Object`'s referent, not the `Debugger.Object` instance itself.
43
44 In the descriptions below, the term "debuggee value" means either a
45 primitive value or a `Debugger.Object` instance; it is a value that might
46 be received from the debuggee, or that could be passed to the debuggee.
47
48
49 ## Debuggee Code
50
51 Each `Debugger` instance maintains a set of global objects that, taken
52 together, comprise the debuggee. Code evaluated in the scope of a debuggee
53 global object, directly or indirectly, is considered *debuggee code*.
54 Similarly:
55
56 - a *debuggee frame* is a frame running debuggee code;
57
58 - a *debuggee function* is a function that closes over a debuggee
59 global object (and thus the function's code is debuggee code);
60
61 - a *debuggee environment* is an environment whose outermost
62 enclosing environment is a debuggee global object; and
63
64 - a *debuggee script* is a script containing debuggee code.
65
66
67 ## Completion Values
68
69 The `Debugger` API often needs to convey the result of running some JS code. For example, suppose you get a `frame.onPop` callback telling you that a method in the debuggee just finished. Did it return successfully? Did it throw? What did it return? The debugger passes the `onPop` handler a *completion value* that tells what happened.
70
71 A completion value is one of these:
72
73 * `{ return: value }`
74
75 The code completed normally, returning <i>value</i>. <i>Value</i> is a
76 debuggee value.
77
78 * `{ throw: value, stack: stack }`
79
80 The code threw <i>value</i> as an exception. <i>Value</i> is a debuggee
81 value. <i>stack</i> is a `SavedFrame` representing the location from which
82 the value was thrown, and may be missing.
83
84 * `null`
85
86 The code was terminated, as if by the "slow script" ribbon.
87
88 Generators and async functions add a wrinkle: they can suspend themselves (with `yield` or `await`), which removes their frame from the stack. Later, the generator or async frame might be returned to the stack and continue running where it left off. Does it count as "completion" when a generator suspends itself?
89
90 The `Debugger` API says yes. `yield` and `await` do trigger the `frame.onPop` handler, passing a completion value that explains why the frame is being suspended. The completion value gets an extra `.yield` or `.await` property, to distinguish this kind of completion from a normal `return`.
91
92 ```js
93 { return: value, yield: true }
94 ```
95
96 where *value* is a debuggee value for the iterator result object, like `{ value: 1, done: false }`, for the yield.
97
98 When a generator function is called, it first evaluates any default argument
99 expressions and destructures its arguments. Then its frame is suspended, and the
100 new generator object is returned to the caller. This initial suspension is reported
101 to any `onPop` handlers as a completion value of the form:
102
103 ```js
104 { return: generatorObject, yield: true, initial: true }
105 ```
106
107 where *generatorObject* is a debuggee value for the generator object being
108 returned to the caller.
109
110 When an async function awaits a promise, its suspension is reported to any
111 `onPop` handlers as a completion value of the form:
112
113 ```js
114 { return: promise, await: true }
115 ```
116
117 where *promise* is a debuggee value for the promise being returned to the
118 caller.
119
120 The first time a call to an async function awaits, returns, or throws, a promise
121 of its result is returned to the caller. Subsequent resumptions of the async
122 call, if any, are initiated directly from the job queue's event loop, with no
123 calling frame on the stack. Thus, if needed, an `onPop` handler can distinguish
124 an async call's initial suspension, which returns the promise, from any
125 subsequent suspensions by checking the `Debugger.Frame`'s `older` property: if
126 that is `null`, the call was resumed directly from the event loop.
127
128 Async generators are a combination of async functions and generators that can
129 use both `yield` and `await` expressions. Suspensions of async generator frames
130 are reported using any combination of the completion values above.
131
132
133 ## Resumption Values
134
135 As the debuggee runs, the `Debugger` interface calls various
136 debugger-provided handler functions to report the debuggee's behavior.
137 Some of these calls can return a value indicating how the debuggee's
138 execution should continue; these are called *resumption values*. A
139 resumption value has one of the following forms:
140
141 * `undefined`
142
143 The debuggee should continue execution normally.
144
145 * `{ return: value }`
146
147 Force the top frame of the debuggee to return <i>value</i> immediately,
148 as if by executing a `return` statement. <i>Value</i> must be a debuggee
149 value. (Most handler functions support this, except those whose
150 descriptions say otherwise.) See the list of special cases below.
151
152 * `{ throw: value }`
153
154 Throw <i>value</i> as an exception from the current bytecode
155 instruction. <i>Value</i> must be a debuggee value. Note that unlike
156 completion values, resumption values do not specify a stack. When
157 initiating an exceptional return from a handler, the current debuggee stack
158 will be used. If a handler wants to avoid modifying the stack of an
159 already-thrown exception, it should return `undefined`.
160
161 * `null`
162
163 Terminate the debuggee, as if it had been cancelled by the "slow script"
164 dialog box.
165
166 In some places, the JS language treats `return` statements specially or
167 doesn't allow them at all. So there are a few special cases.
168
169 * An arrow function without curly braces can't contain a return
170 statement, but `{ return: value }` works anyway,
171 returning the specified value.
172
173 Likewise, if the top frame of the debuggee is not in a function at
174 all—that is, it's running toplevel code in a `script` tag, or `eval`
175 code—then <i>value</i> is returned even though `return` statements
176 aren't legal in that kind of code. (In the case of a `script` tag,
177 the browser discards the return value.)
178
179 * If the debuggee is in a function that was called as a constructor (that
180 is, via a `new` expression), then <i>value</i> serves as the value
181 returned by the function's body, not that produced by the `new`
182 expression: if the value is not an object, the `new` expression returns
183 the frame's `this` value.
184
185 Similarly, if the function is the constructor for a subclass, then a
186 non-object value may result in a `TypeError`.
187
188 * Returning from a generator simulates a `return`, not a `yield`;
189 there is no way to force a debuggee generator to `yield`.
190
191 The way generators execute is rather odd. When a generator-function
192 is first called, it is put onto the stack and runs just a few
193 bytecode instructions (or more, if the generator-function has any
194 default argument values to compute), then performs the "initial
195 suspend". At that point, a new generator object is created and
196 returned to the caller. Thereafter, the caller may cause execution
197 of the generator to resume at any time, by calling `genObj.next()`,
198 and the generator may pause itself again using `yield`.
199
200 JS generators normally can't return before the "initial
201 suspend"—there’s no place to put a `return` statement—but
202 `{ return: value }` there works anyway, replacing
203 the generator object that the initial suspend would normally create
204 and return.
205
206 Returning from a generator that's been resumed via `genobj.next()`
207 (or one of the other methods) closes the generator, and the
208 `genobj.next()` or other method returns a new object of the form
209 `{ done: true, value: value }`.
210
211 If a debugger hook function throws an exception, rather than returning a
212 resumption value, we never propagate such an exception to the debuggee;
213 instead, we call the associated `Debugger` instance's
214 `uncaughtExceptionHook` property, as described below.
215
216
217 ## Timestamps
218
219 Timestamps are expressed in units of milliseconds since an arbitrary,
220 but fixed, epoch. The resolution of timestamps is generally greater
221 than milliseconds, though no specific resolution is guaranteed.
222
223
224 ## The `Debugger.DebuggeeWouldRun` Exception
225
226 Some debugger operations that appear to simply inspect the debuggee's state
227 may actually cause debuggee code to run. For example, reading a variable
228 might run a getter function on the global or on a `with` expression's
229 operand; and getting an object's property descriptor will run a handler
230 trap if the object is a proxy. To protect the debugger's integrity, only
231 methods whose stated purpose is to run debuggee code can do so. These
232 methods are called [invocation functions][inv fr], and they follow certain
233 common conventions to report the debuggee's behavior safely. For other
234 methods, if their normal operation would cause debuggee code to run, they
235 throw an instance of the `Debugger.DebuggeeWouldRun` exception.
236
237 If there are debugger frames on stack from multiple Debugger instances, the
238 thrown exception is an instance of the topmost locking debugger's global's
239 `Debugger.DebuggeeWouldRun`.
240
241 A `Debugger.DebuggeeWouldRun` exception may have a `cause` property,
242 providing more detailed information on why the debuggee would have run. The
243 `cause` property's value is one of the following strings:
244
245 * `"proxy"`: Carrying out the operation would have caused a proxy handler to run. |
246 * `"getter"`: Carrying out the operation would have caused an object property getter to run. |
247 * `"setter"`: Carrying out the operation would have caused an object property setter to run. |
248
249 If the system can't determine why control attempted to enter the debuggee,
250 it will leave the exception's `cause` property undefined.
251
252
253 [debugger]: Debugger-API.md
254 [inv fr]: Debugger.Frame.md#invocation-functions-and-debugger-frames