|
1 # Debugger.Script |
|
2 |
|
3 A `Debugger.Script` instance refers to a sequence of bytecode in the |
|
4 debuggee; it is the [`Debugger`][debugger-object] API's presentation of a JSAPI `JSScript` |
|
5 object. Each of the following is represented by single JSScript object: |
|
6 |
|
7 * The body of a function—that is, all the code in the function that is not |
|
8 contained within some nested function. |
|
9 |
|
10 * The code passed to a single call to `eval`, excluding the bodies of any |
|
11 functions that code defines. |
|
12 |
|
13 * The contents of a `<script>` element. |
|
14 |
|
15 * A DOM event handler, whether embedded in HTML or attached to the element |
|
16 by other JavaScript code. |
|
17 |
|
18 * Code appearing in a `javascript:` URL. |
|
19 |
|
20 The [`Debugger`][debugger-object] interface constructs `Debugger.Script` objects as scripts |
|
21 of debuggee code are uncovered by the debugger: via the `onNewScript` |
|
22 handler method; via [`Debugger.Frame`][frame]'s `script` properties; via the |
|
23 `functionScript` method of [`Debugger.Object`][object] instances; and so on. For a |
|
24 given [`Debugger`][debugger-object] instance, SpiderMonkey constructs exactly one |
|
25 `Debugger.Script` instance for each underlying script object; debugger |
|
26 code can add its own properties to a script object and expect to find |
|
27 them later, use `==` to decide whether two expressions refer to the same |
|
28 script, and so on. |
|
29 |
|
30 (If more than one [`Debugger`][debugger-object] instance is debugging the same code, each |
|
31 [`Debugger`][debugger-object] gets a separate `Debugger.Script` instance for a given |
|
32 script. This allows the code using each [`Debugger`][debugger-object] instance to place |
|
33 whatever properties it likes on its `Debugger.Script` instances, without |
|
34 worrying about interfering with other debuggers.) |
|
35 |
|
36 A `Debugger.Script` instance is a strong reference to a JSScript object; |
|
37 it protects the script it refers to from being garbage collected. |
|
38 |
|
39 Note that SpiderMonkey may use the same `Debugger.Script` instances for |
|
40 equivalent functions or evaluated code—that is, scripts representing the |
|
41 same source code, at the same position in the same source file, |
|
42 evaluated in the same lexical environment. |
|
43 |
|
44 ## Accessor Properties of the Debugger.Script Prototype Object |
|
45 |
|
46 A `Debugger.Script` instance inherits the following accessor properties |
|
47 from its prototype: |
|
48 |
|
49 `url` |
|
50 : The filename or URL from which this script's code was loaded. If the |
|
51 `source` property is non-`null`, then this is equal to `source.url`. |
|
52 |
|
53 `startLine` |
|
54 : The number of the line at which this script's code starts, within the |
|
55 file or document named by `url`. |
|
56 |
|
57 `lineCount` |
|
58 : The number of lines this script's code occupies, within the file or |
|
59 document named by `url`. |
|
60 |
|
61 `source` |
|
62 : The [`Debugger.Source`][source] instance representing the source code from which |
|
63 this script was produced. This is `null` if the source code was not |
|
64 retained. |
|
65 |
|
66 `sourceStart` |
|
67 : The character within the [`Debugger.Source`][source] instance given by `source` at |
|
68 which this script's code starts; zero-based. If this is a function's |
|
69 script, this is the index of the start of the `function` token in the |
|
70 source code. |
|
71 |
|
72 `sourceLength` |
|
73 : The length, in characters, of this script's code within the |
|
74 [`Debugger.Source`][source] instance given by `source`. |
|
75 |
|
76 `global` |
|
77 : A [`Debugger.Object`][object] instance referring to the global object in whose |
|
78 scope this script runs. The result refers to the global directly, not |
|
79 via a wrapper or a `WindowProxy` ("outer window", in Firefox). |
|
80 |
|
81 `staticLevel` |
|
82 : The number of function bodies enclosing this script's code. |
|
83 |
|
84 Global code is at level zero; bodies of functions defined at the top |
|
85 level in global code are at level one; bodies of functions nested within |
|
86 those are at level two; and so on. |
|
87 |
|
88 A script for code passed to direct `eval` is at a static level one |
|
89 greater than that of the script containing the call to `eval`, because |
|
90 direct eval code runs within the caller's scope. However, a script for |
|
91 code passed to an indirect `eval` call is at static level zero, since it |
|
92 is evaluated in the global scope. |
|
93 |
|
94 Note that a generator's expressions are considered to be part of the |
|
95 body of a synthetic function, produced by the compiler. |
|
96 |
|
97 Scripts' static level be useful in deciding where to set breakpoints. |
|
98 For example, a breakpoint set on line 3 in this code: |
|
99 |
|
100 function f() { |
|
101 x = function g() { // line 2 |
|
102 // line 3; no code here |
|
103 ...; |
|
104 } |
|
105 } |
|
106 |
|
107 should be set in `g`'s script, not in `f`'s, even though neither script |
|
108 contains code at that line. In such a case, the most deeply nested |
|
109 script—the one with the highest static level—should receive the |
|
110 breakpoint. |
|
111 |
|
112 `strictMode` |
|
113 : This is `true` if this script's code is ECMAScript strict mode code, and |
|
114 `false` otherwise. |
|
115 |
|
116 `sourceMapURL` |
|
117 : If this script was produced by a minimizer or translated from some other |
|
118 language, and we know the URL of a <b>source map</b> document relating |
|
119 the source positions in this script to the corresponding source |
|
120 positions in the original source, then this property's value is that |
|
121 URL. Otherwise, this is `null`. |
|
122 |
|
123 (On the web, the translator may provide the source map URL in a |
|
124 specially formatted comment in the JavaScript source code, or via a |
|
125 header in the HTTP reply that carried the generated JavaScript.) |
|
126 |
|
127 ## Function Properties of the Debugger.Script Prototype Object |
|
128 |
|
129 The functions described below may only be called with a `this` value |
|
130 referring to a `Debugger.Script` instance; they may not be used as |
|
131 methods of other kinds of objects. |
|
132 |
|
133 <code>decompile([<i>pretty</i>])</code> |
|
134 : Return a string containing JavaScript source code equivalent to this |
|
135 script in its effect and result. If <i>pretty</i> is present and true, |
|
136 produce indented code with line breaks. |
|
137 |
|
138 (Note that [`Debugger.Object`][object] instances referring to functions also have |
|
139 a `decompile` method, whose result includes the function header and |
|
140 parameter names, so it is probably better to write |
|
141 <code><i>f</i>.decompile()</code> than to write |
|
142 <code><i>f</i>.getFunctionScript().decompile()</code>.) |
|
143 |
|
144 `getAllOffsets()` |
|
145 : Return an array <i>L</i> describing the relationship between bytecode |
|
146 instruction offsets and source code positions in this script. <i>L</i> |
|
147 is sparse, and indexed by source line number. If a source line number |
|
148 <i>line</i> has no code, then <i>L</i> has no <i>line</i> property. If |
|
149 there is code for <i>line</i>, then <code><i>L</i>[<i>line</i>]</code> is an array |
|
150 of offsets of byte code instructions that are entry points to that line. |
|
151 |
|
152 For example, suppose we have a script for the following source code: |
|
153 |
|
154 a=[] |
|
155 for (i=1; i < 10; i++) |
|
156 // It's hip to be square. |
|
157 a[i] = i*i; |
|
158 |
|
159 Calling `getAllOffsets()` on that code might yield an array like this: |
|
160 |
|
161 [[0], [5, 20], , [10]] |
|
162 |
|
163 This array indicates that: |
|
164 |
|
165 * the first line's code starts at offset 0 in the script; |
|
166 |
|
167 * the `for` statement head has two entry points at offsets 5 and 20 (for |
|
168 the initialization, which is performed only once, and the loop test, |
|
169 which is performed at the start of each iteration); |
|
170 |
|
171 * the third line has no code; |
|
172 |
|
173 * and the fourth line begins at offset 10. |
|
174 |
|
175 <code>getLineOffsets(<i>line</i>)</code> |
|
176 : Return an array of bytecode instruction offsets representing the entry |
|
177 points to source line <i>line</i>. If the script contains no executable |
|
178 code at that line, the array returned is empty. |
|
179 |
|
180 <code>getOffsetLine(<i>offset</i>)</code> |
|
181 : Return the source code line responsible for the bytecode at |
|
182 <i>offset</i> in this script. |
|
183 |
|
184 `getChildScripts()` |
|
185 : Return a new array whose elements are Debugger.Script objects for each |
|
186 function and each generator expression in this script. Only direct |
|
187 children are included; nested children can be reached by walking the |
|
188 tree. |
|
189 |
|
190 <code>setBreakpoint(<i>offset</i>, <i>handler</i>)</code> |
|
191 : Set a breakpoint at the bytecode instruction at <i>offset</i> in this |
|
192 script, reporting hits to the `hit` method of <i>handler</i>. If |
|
193 <i>offset</i> is not a valid offset in this script, throw an error. |
|
194 |
|
195 When execution reaches the given instruction, SpiderMonkey calls the |
|
196 `hit<code> method of <i>handler</i>, passing a [</code>Debugger.Frame`][frame] instance |
|
197 representing the currently executing stack frame. The `hit` method's |
|
198 return value should be a [resumption value][rv], determining how execution should |
|
199 continue. |
|
200 |
|
201 Any number of breakpoints may be set at a single location; when control |
|
202 reaches that point, SpiderMonkey calls their handlers in an unspecified |
|
203 order. |
|
204 |
|
205 Any number of breakpoints may use the same <i>handler</i> object. |
|
206 |
|
207 Breakpoint handler method calls are cross-compartment, intra-thread |
|
208 calls: the call takes place in the same thread that hit the breakpoint, |
|
209 and in the compartment containing the handler function (typically the |
|
210 debugger's compartment). |
|
211 |
|
212 The new breakpoint belongs to the [`Debugger`][debugger-object] instance to which this |
|
213 script belongs; disabling the [`Debugger`][debugger-object] instance disables this |
|
214 breakpoint. |
|
215 |
|
216 <code>getBreakpoints([<i>offset</i>])</code> |
|
217 : Return an array containing the handler objects for all the breakpoints |
|
218 set at <i>offset</i> in this script. If <i>offset</i> is omitted, return |
|
219 the handlers of all breakpoints set anywhere in this script. If |
|
220 <i>offset</i> is present, but not a valid offset in this script, throw |
|
221 an error. |
|
222 |
|
223 <code>clearBreakpoints(handler, [<i>offset</i>])</code> |
|
224 : Remove all breakpoints set in this [`Debugger`][debugger-object] instance that use |
|
225 <i>handler</i> as their handler. If <i>offset</i> is given, remove only |
|
226 those breakpoints set at <i>offset</i> that use <i>handler</i>; if |
|
227 <i>offset</i> is not a valid offset in this script, throw an error. |
|
228 |
|
229 Note that, if breakpoints using other handler objects are set at the |
|
230 same location(s) as <i>handler</i>, they remain in place. |
|
231 |
|
232 <code>clearAllBreakpoints([<i>offset</i>])</code> |
|
233 : Remove all breakpoints set in this script. If <i>offset</i> is present, |
|
234 remove all breakpoints set at that offset in this script; if |
|
235 <i>offset</i> is not a valid bytecode offset in this script, throw an |
|
236 error. |
|
237 |
|
238 |