| 1 |
# Debugger.Source |
| 2 |
|
| 3 |
A `Debugger.Source` instance represents either a piece of JavaScript source |
| 4 |
code or the serialized text of a block of WebAssembly code. The two cases are |
| 5 |
distinguished by the latter having its `introductionType` property always |
| 6 |
being `"wasm"` and the former having its `introductionType` property never |
| 7 |
being `"wasm"`. |
| 8 |
|
| 9 |
Each [`Debugger`][debugger-object] instance has a separate collection of |
| 10 |
`Debugger.Source` instances representing the source code that has been |
| 11 |
presented to the system. |
| 12 |
|
| 13 |
A debugger may place its own properties on `Debugger.Source` instances, |
| 14 |
to store metadata about particular pieces of source code. |
| 15 |
|
| 16 |
## Debugger.Source for JavaScript |
| 17 |
|
| 18 |
For a `Debugger.Source` instance representing a piece of JavaScript source |
| 19 |
code, its properties provide the source code itself as a string, and describe |
| 20 |
where it came from. Each [`Debugger.Script`][script] instance refers to the |
| 21 |
`Debugger.Source` instance holding the source code from which it was produced. |
| 22 |
|
| 23 |
If a single piece of source code contains both top-level code and |
| 24 |
function definitions, perhaps with nested functions, then the |
| 25 |
[`Debugger.Script`][script] instances for those all refer to the same |
| 26 |
`Debugger.Source` instance. Each script indicates the substring of the |
| 27 |
overall source to which it corresponds. |
| 28 |
|
| 29 |
A `Debugger.Source` instance may represent only a portion of a larger |
| 30 |
source document. For example, an HTML document can contain JavaScript in |
| 31 |
multiple `<script>` elements and event handler content attributes. |
| 32 |
In this case, there may be either a single `Debugger.Source` instance |
| 33 |
for the entire HTML document, with each [`Debugger.Script`][script] referring to |
| 34 |
its substring of the document; or there may be a separate |
| 35 |
`Debugger.Source` instance for each `<script>` element and |
| 36 |
attribute. The choice is left up to the implementation. |
| 37 |
|
| 38 |
If a given piece of source code is presented to the JavaScript |
| 39 |
implementation more than once, with the same origin metadata, the |
| 40 |
JavaScript implementation may generate a fresh `Debugger.Source` |
| 41 |
instance to represent each presentation, or it may use a single |
| 42 |
`Debugger.Source` instance to represent them all. |
| 43 |
|
| 44 |
## Debugger.Source for WebAssembly |
| 45 |
|
| 46 |
For a `Debugger.Source` instance representing the serialized text of a block |
| 47 |
of WebAssembly code, its properties provide the serialized text as a string. |
| 48 |
|
| 49 |
Currently only entire modules evaluated via `new WebAssembly.Module` are |
| 50 |
represented. SpiderMonkey constructs exactly one `Debugger.Source` for each |
| 51 |
underlying WebAssembly module per [`Debugger`][debugger-object] instance. |
| 52 |
|
| 53 |
Please note at the time of this writing, support for WebAssembly is very |
| 54 |
preliminary. Many properties below return placeholder values. |
| 55 |
|
| 56 |
## Convention |
| 57 |
|
| 58 |
For descriptions of properties and methods below, if the behavior of the |
| 59 |
property or method differs between the instance referring to JavaScript source |
| 60 |
or to a block of WebAssembly code, the text will be split into two sections, |
| 61 |
headed by "**if the instance refers to JavaScript source**" and "**if the |
| 62 |
instance refers to WebAssembly code**", respectively. If the behavior does not |
| 63 |
differ, no such emphasized headings will appear. |
| 64 |
|
| 65 |
## Accessor Properties of the Debugger.Source Prototype Object |
| 66 |
|
| 67 |
A `Debugger.Source` instance inherits the following accessor properties |
| 68 |
from its prototype: |
| 69 |
|
| 70 |
### `text` |
| 71 |
**If the instance refers to JavaScript source**, the JavaScript source |
| 72 |
code, as a string. The value satisfies the `Program`, |
| 73 |
`FunctionDeclaration`, or `FunctionExpression` productions in the |
| 74 |
ECMAScript standard. |
| 75 |
|
| 76 |
**If the instance refers to WebAssembly code**, the `"[wasm]"` value will |
| 77 |
be returned. |
| 78 |
|
| 79 |
### `binary` |
| 80 |
**If the instance refers to WebAssembly code**, a Uint8Array that contains |
| 81 |
the WebAssembly bytecode. |
| 82 |
|
| 83 |
### `url` |
| 84 |
**If the instance refers to JavaScript source**, the filename or URL from |
| 85 |
which this script's code was loaded. For scripts created by `eval` or the |
| 86 |
`Function` constructor, this may be a synthesized filename, starting with a |
| 87 |
valid URL and followed by information tracking how the code was introduced |
| 88 |
into the system; the entire string is not a valid URL. For |
| 89 |
`Function.prototype`'s script, this is `null`. Source may be loaded from a |
| 90 |
URL in the following ways: |
| 91 |
|
| 92 |
* The URL may appear as the `src` attribute of a `<script>` element |
| 93 |
in markup text. |
| 94 |
|
| 95 |
* The URL may be passed to the `Worker` web worker constructor, or the web |
| 96 |
worker `importScripts` function. |
| 97 |
|
| 98 |
* The URL may be the name of a XPCOM JavaScript module or subscript. |
| 99 |
|
| 100 |
(Note that code passed to `eval`, the `Function` constructor, or a |
| 101 |
similar function is <i>not</i> considered to be loaded from a URL; the |
| 102 |
`url` accessor on `Debugger.Source` instances for such sources should |
| 103 |
return `undefined`.) |
| 104 |
|
| 105 |
**If the instance refers to WebAssembly code**, the URL of the script that |
| 106 |
called `new WebAssembly.Module` with the string `"> wasm"` appended. |
| 107 |
|
| 108 |
### `startLine` |
| 109 |
**If the instance refers to JavaScript source**, the 1-origin start line of the |
| 110 |
source within the file or URL it was loaded from. This is normally `1`, but |
| 111 |
may have another value if the source is part of an HTML document. |
| 112 |
|
| 113 |
### `startColumn` |
| 114 |
**If the instance refers to JavaScript source**, the 1-origin start column in |
| 115 |
UTF-16 code units of the source within the file or URL it was loaded from. This |
| 116 |
is normally `1`, but may have another value if the source is part of an HTML |
| 117 |
document. |
| 118 |
|
| 119 |
### `id` |
| 120 |
**If the instance refers to JavaScript source**, an int32 counter that identifies |
| 121 |
the source within the current process. This ID is used in other places in Gecko |
| 122 |
that weakly refer to sources, such as nsIScriptError. |
| 123 |
|
| 124 |
### `sourceMapURL` |
| 125 |
**If the instance refers to JavaScript source**, if this source was |
| 126 |
produced by a minimizer or translated from some other language, and we |
| 127 |
know the URL of a <b>source map</b> document relating the source positions |
| 128 |
in this source to the corresponding source positions in the original |
| 129 |
source, then this property's value is that URL. Otherwise, this is `null`. |
| 130 |
|
| 131 |
(On the web, the translator may provide the source map URL in a |
| 132 |
specially formatted comment in the JavaScript source code, or via a |
| 133 |
header in the HTTP reply that carried the generated JavaScript.) |
| 134 |
|
| 135 |
This property is writable, so you can change the source map URL by |
| 136 |
setting it. All Debugger.Source objects referencing the same |
| 137 |
source will see the change. Setting an empty string has no effect |
| 138 |
and will not change existing value. |
| 139 |
|
| 140 |
**If the instance refers to WebAssembly code**, `null`. Attempts to write |
| 141 |
to this property throw a `TypeError`. |
| 142 |
|
| 143 |
### `displayURL` |
| 144 |
If the script had a special `//# sourceURL` comment, as described in |
| 145 |
the source maps specification, then this property's value holds |
| 146 |
the string that was given. Otherwise, this is `null`. |
| 147 |
|
| 148 |
### `element` |
| 149 |
The [`Debugger.Object`][object] instance referring to the DOM element to which |
| 150 |
this source code belongs, if any, or `undefined` if it belongs to no DOM |
| 151 |
element. Source belongs to a DOM element in the following cases: |
| 152 |
|
| 153 |
* Source belongs to a `<script>` element if it is the element's text |
| 154 |
content (that is, it is written out as the body of the `<script>` |
| 155 |
element in the markup text), or is the source document referenced by its |
| 156 |
`src` attribute. |
| 157 |
|
| 158 |
* Source belongs to a DOM element if it is an event handler content |
| 159 |
attribute (that is, if it is written out in the markup text as an |
| 160 |
attribute value). |
| 161 |
|
| 162 |
* Source belongs to a DOM element if it was assigned to one of the |
| 163 |
element's event handler IDL attributes as a string. (Note that one may |
| 164 |
assign both strings and functions to DOM elements' event handler IDL |
| 165 |
attributes. If one assigns a function, that function's script's source |
| 166 |
does <i>not</i> belong to the DOM element; the function's definition |
| 167 |
must appear elsewhere.) |
| 168 |
|
| 169 |
(If the sources attached to a DOM element change, the `Debugger.Source` |
| 170 |
instances representing superseded code still refer to the DOM element; |
| 171 |
this accessor only reflects origins, not current relationships.) |
| 172 |
|
| 173 |
### `elementAttributeName` |
| 174 |
If this source belongs to a DOM element because it is an event handler |
| 175 |
content attribute or an event handler IDL attribute, this is the name of |
| 176 |
that attribute, a string. Otherwise, this is `undefined`. |
| 177 |
|
| 178 |
### `introductionType` |
| 179 |
**If the instance refers to JavaScript source**, a string indicating how |
| 180 |
this source code was introduced into the system. This accessor returns |
| 181 |
one of the following values: |
| 182 |
|
| 183 |
* `"eval"`, for code passed to `eval`. |
| 184 |
|
| 185 |
* `"debugger eval"`, for code evaluated by debugger. |
| 186 |
|
| 187 |
* `"Function"`, for code passed to the `Function` constructor. |
| 188 |
|
| 189 |
* `"GeneratorFunction"`, for code passed to the generator constructor. |
| 190 |
|
| 191 |
* `"AsyncFunction"`, for code passed to the async function constructor. |
| 192 |
|
| 193 |
* `"AsyncGenerator"`, for code passed to the async generator constructor. |
| 194 |
|
| 195 |
* `"Worklet"`, for code loaded by worklet. |
| 196 |
|
| 197 |
* `"importScripts"`, for code by calling `importScripts` in a web worker. |
| 198 |
|
| 199 |
* `"eventHandler"`, for code assigned to DOM elements' event handler IDL |
| 200 |
attributes as a string. |
| 201 |
|
| 202 |
* `"srcScript"`, for code belonging to `<script src="file.js">` elements. |
| 203 |
|
| 204 |
* `"inlineScript"`, for code belonging to `<script>code;</script>` elements. |
| 205 |
|
| 206 |
* `"injectedScript"`, for code belonging to scripts that _would_ be |
| 207 |
`"inlineScript"` except that they were not part of the initial file itself. |
| 208 |
|
| 209 |
For example, scripts created via: |
| 210 |
|
| 211 |
* `document.write("<script>code;</script>")` |
| 212 |
* `var s = document.createElement("script"); s.text = "code";` |
| 213 |
|
| 214 |
* `"importedModule"`, for code that was loaded indirectly by being imported |
| 215 |
by another script using ESM static or dynamic imports. |
| 216 |
|
| 217 |
* `"javascriptURL"`, for code presented in `javascript:` URLs. |
| 218 |
|
| 219 |
* `"domTimer"`, for code passed to `setTimeout`/`setInterval` as a string. |
| 220 |
|
| 221 |
* `"self-hosted"`, for internal self-hosted JS code. |
| 222 |
|
| 223 |
* `undefined`, if the implementation doesn't know how the code was |
| 224 |
introduced. |
| 225 |
|
| 226 |
**If the instance refers to WebAssembly code**, `"wasm"`. |
| 227 |
|
| 228 |
### `introductionScript` & `introductionOffset` |
| 229 |
**If the instance refers to JavaScript source**, and if this source was |
| 230 |
introduced by calling a function from debuggee code, then |
| 231 |
`introductionScript` is the [`Debugger.Script`][script] instance referring |
| 232 |
to the script containing that call, and `introductionOffset` is the call's |
| 233 |
bytecode offset within that script. Otherwise, these are both `undefined`. |
| 234 |
Taken together, these properties indicate the location of the introducing |
| 235 |
call. |
| 236 |
|
| 237 |
For the purposes of these accessors, assignments to accessor properties are |
| 238 |
treated as function calls. Thus, setting a DOM element's event handler IDL |
| 239 |
attribute by assigning to the corresponding JavaScript property creates a |
| 240 |
source whose `introductionScript` and `introductionOffset` refer to the |
| 241 |
property assignment. |
| 242 |
|
| 243 |
Since a `<script>` element parsed from a web page's original HTML was not |
| 244 |
introduced by any scripted call, its source's `introductionScript` and |
| 245 |
`introductionOffset` accessors both return `undefined`. |
| 246 |
|
| 247 |
If a `<script>` element was dynamically inserted into a document, then these |
| 248 |
accessors refer to the call that actually caused the script to run—usually |
| 249 |
the call that made the element part of the document. Thus, they do |
| 250 |
<i>not</i> refer to the call that created the element; stored the source as |
| 251 |
the element's text child; made the element a child of some uninserted parent |
| 252 |
node that was later inserted; or the like. |
| 253 |
|
| 254 |
Although the main script of a worker thread is introduced by a call to |
| 255 |
`Worker` or `SharedWorker`, these accessors always return `undefined` on |
| 256 |
such script's sources. A worker's main script source and the call that |
| 257 |
created the worker are always in separate threads, but |
| 258 |
[`Debugger`][debugger-object] is an inherently single-threaded facility: its |
| 259 |
debuggees must all run in the same thread. Since the global that created the |
| 260 |
worker is in a different thread, it is guaranteed not to be a debuggee of |
| 261 |
the [`Debugger`][debugger-object] instance that owns this source; and thus |
| 262 |
the creating call is never "in debuggee code". Relating a worker to its |
| 263 |
creator, and other multi-threaded debugging concerns, are out of scope for |
| 264 |
[`Debugger`][debugger-object]. |
| 265 |
|
| 266 |
**If the instance refers to WebAssembly code**, `introductionScript` is |
| 267 |
the [`Debugger.Script`][script] instance referring to the same underlying |
| 268 |
WebAssembly module. `introductionOffset` is `undefined`. |
| 269 |
|
| 270 |
## Function Properties of the Debugger.Source Prototype Object |
| 271 |
|
| 272 |
The functions described below may only be called with a `this` value |
| 273 |
referring to a `Debugger.Source` instance; they may not be used as |
| 274 |
methods of other kinds of objects. |
| 275 |
|
| 276 |
### `reparse()` |
| 277 |
|
| 278 |
**If the instance refers to JavaScript source**, and if the source has text |
| 279 |
available, return a `Debugger.Script` instance referring to a new script |
| 280 |
created by reparsing this source's text. Debugger `onNewScript` hooks will |
| 281 |
not be invoked for the new script. Otherwise, throw an `Error`. |
| 282 |
|
| 283 |
|
| 284 |
[debugger-object]: Debugger.md |
| 285 |
[script]: Debugger.Script.md |
| 286 |
[object]: Debugger.Object.md |