js/src/doc/Debugger/Debugger.Source.md

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 # Debugger.Source
michael@0 2
michael@0 3 A `Debugger.Source` instance represents a piece of JavaScript source
michael@0 4 code: its properties provide the source code itself as a string, and
michael@0 5 describe where it came from. Each [`Debugger.Script`][script] instance refers to
michael@0 6 the `Debugger.Source` instance holding the source code from which it was
michael@0 7 produced.
michael@0 8
michael@0 9 If a single piece of source code contains both top-level code and
michael@0 10 function definitions, perhaps with nested functions, then the
michael@0 11 [`Debugger.Script`][script] instances for those all refer to the same
michael@0 12 `Debugger.Source` instance. Each script indicates the substring of the
michael@0 13 overall source to which it corresponds.
michael@0 14
michael@0 15 A `Debugger.Source` instance may represent only a portion of a larger
michael@0 16 source document. For example, an HTML document can contain JavaScript in
michael@0 17 multiple `<script>` elements and event handler content attributes.
michael@0 18 In this case, there may be either a single `Debugger.Source` instance
michael@0 19 for the entire HTML document, with each [`Debugger.Script`][script] referring to
michael@0 20 its substring of the document; or there may be a separate
michael@0 21 `Debugger.Source` instance for each `<script>` element and
michael@0 22 attribute. The choice is left up to the implementation.
michael@0 23
michael@0 24 If a given piece of source code is presented to the JavaScript
michael@0 25 implementation more than once, with the same origin metadata, the
michael@0 26 JavaScript implementation may generate a fresh `Debugger.Source`
michael@0 27 instance to represent each presentation, or it may use a single
michael@0 28 `Debugger.Source` instance to represent them all.
michael@0 29
michael@0 30 Each [`Debugger`][debugger-object] instance has a separate collection of `Debugger.Source`
michael@0 31 instances representing the source code that has been presented to the
michael@0 32 system.
michael@0 33
michael@0 34 A debugger may place its own properties on `Debugger.Source` instances,
michael@0 35 to store metadata about particular pieces of source code.
michael@0 36
michael@0 37
michael@0 38 ## Accessor Properties of the Debugger.Source Prototype Object
michael@0 39
michael@0 40 A `Debugger.Source` instance inherits the following accessor properties
michael@0 41 from its prototype:
michael@0 42
michael@0 43 `text`
michael@0 44 : The JavaScript source code, as a string. The value satisfies the
michael@0 45 `Program`, `FunctionDeclaration`, or `FunctionExpression` productions in
michael@0 46 the ECMAScript standard.
michael@0 47
michael@0 48 `enclosingStart` <i>(future plan)</i>
michael@0 49 : The position within the enclosing document at which this source's text
michael@0 50 starts. This is a zero-based character offset. (The length of this
michael@0 51 script within the enclosing document is `source.length`.)
michael@0 52
michael@0 53 `lineCount` <i>(future plan)</i>
michael@0 54 : The number of lines in the source code. If there are characters after
michael@0 55 the last newline in the source code, those count as a final line;
michael@0 56 otherwise, `lineCount` is equal to the number of newlines in the source
michael@0 57 code.
michael@0 58
michael@0 59 `url`
michael@0 60 : The URL from which this source was loaded, if this source was loaded
michael@0 61 from a URL. Otherwise, this is `undefined`. Source may be loaded from a
michael@0 62 URL in the following ways:
michael@0 63
michael@0 64 * The URL may appear as the `src` attribute of a `<script>` element
michael@0 65 in markup text.
michael@0 66
michael@0 67 * The URL may be passed to the `Worker` web worker constructor, or the web
michael@0 68 worker `importScripts` function.
michael@0 69
michael@0 70 * The URL may be the name of a XPCOM JavaScript module or subscript.
michael@0 71
michael@0 72 (Note that code passed to `eval`, the `Function` constructor, or a
michael@0 73 similar function is <i>not</i> considered to be loaded from a URL; the
michael@0 74 `url` accessor on `Debugger.Source` instances for such sources should
michael@0 75 return `undefined`.)
michael@0 76
michael@0 77 `element`
michael@0 78 : The [`Debugger.Object`][object] instance referring to the DOM element to which
michael@0 79 this source code belongs, if any, or `undefined` if it belongs to no DOM
michael@0 80 element. Source belongs to a DOM element in the following cases:
michael@0 81
michael@0 82 * Source belongs to a `<script>` element if it is the element's text
michael@0 83 content (that is, it is written out as the body of the `<script>`
michael@0 84 element in the markup text), or is the source document referenced by its
michael@0 85 `src` attribute.
michael@0 86
michael@0 87 * Source belongs to a DOM element if it is an event handler content
michael@0 88 attribute (that is, if it is written out in the markup text as an
michael@0 89 attribute value).
michael@0 90
michael@0 91 * Source belongs to a DOM element if it was assigned to one of the
michael@0 92 element's event handler IDL attributes as a string. (Note that one may
michael@0 93 assign both strings and functions to DOM elements' event handler IDL
michael@0 94 attributes. If one assigns a function, that function's script's source
michael@0 95 does <i>not</i> belong to the DOM element; the function's definition
michael@0 96 must appear elsewhere.)
michael@0 97
michael@0 98 (If the sources attached to a DOM element change, the `Debugger.Source`
michael@0 99 instances representing superceded code still refer to the DOM element;
michael@0 100 this accessor only reflects origins, not current relationships.)
michael@0 101
michael@0 102 `elementAttributeName`
michael@0 103 : If this source belongs to a DOM element because it is an event handler
michael@0 104 content attribute or an event handler IDL attribute, this is the name of
michael@0 105 that attribute, a string. Otherwise, this is `undefined`.
michael@0 106
michael@0 107 `introductionType`
michael@0 108 : A string indicating how this source code was introduced into the system.
michael@0 109 This accessor returns one of the following values:
michael@0 110
michael@0 111 * `"eval"`, for code passed to `eval`.
michael@0 112
michael@0 113 * `"Function"`, for code passed to the `Function` constructor.
michael@0 114
michael@0 115 * `"Worker"`, for code loaded by calling the Web worker constructor—the
michael@0 116 worker's main script.
michael@0 117
michael@0 118 * `"importScripts"`, for code by calling `importScripts` in a web worker.
michael@0 119
michael@0 120 * `"eventHandler"`, for code assigned to DOM elements' event handler IDL
michael@0 121 attributes as a string.
michael@0 122
michael@0 123 * `"scriptElement"`, for code belonging to `<script>` elements.
michael@0 124
michael@0 125 * `"javascriptURL"`, for code presented in `javascript:` URLs.
michael@0 126
michael@0 127 * `"setTimeout"`, for code passed to `setTimeout` as a string.
michael@0 128
michael@0 129 * `"setInterval"`, for code passed to `setInterval` as a string.
michael@0 130
michael@0 131 * `undefined`, if the implementation doesn't know how the code was
michael@0 132 introduced.
michael@0 133
michael@0 134 `introductionScript`, `introductionScriptOffset` <i>(future plan)</i>
michael@0 135 : If this source was introduced by calling a function from debuggee code,
michael@0 136 then `introductionScript` is the [`Debugger.Script`][script] instance referring to
michael@0 137 the script containing that call, and `introductionScriptOffset` is the
michael@0 138 call's bytecode offset within that script. Otherwise, these are both
michael@0 139 `undefined`. Taken together, these properties indicate the location of
michael@0 140 the introducing call.
michael@0 141
michael@0 142 For the purposes of these accessors, assignments to accessor properties
michael@0 143 are treated as function calls. Thus, setting a DOM element's event
michael@0 144 handler IDL attribute by assigning to the corresponding JavaScript
michael@0 145 property creates a source whose `introductionScript` and
michael@0 146 `introductionScriptOffset` refer to the property assignment.
michael@0 147
michael@0 148 Since a `<script>` element parsed from a web page's original HTML
michael@0 149 was not introduced by any scripted call, its source's
michael@0 150 `introductionScript` and `introductionScriptOffset` accessors both
michael@0 151 return `undefined`.
michael@0 152
michael@0 153 If a `<script>` element was dynamically inserted into a document,
michael@0 154 then these accessors refer to the call that actually caused the script
michael@0 155 to run—usually the call that made the element part of the document.
michael@0 156 Thus, they do <i>not</i> refer to the call that created the element;
michael@0 157 stored the source as the element's text child; made the element a child
michael@0 158 of some uninserted parent node that was later inserted; or the like.
michael@0 159
michael@0 160 Although the main script of a worker thread is introduced by a call to
michael@0 161 `Worker` or `SharedWorker`, these accessors always return `undefined` on
michael@0 162 such script's sources. A worker's main script source and the call that
michael@0 163 created the worker are always in separate threads, but [`Debugger`][debugger-object] is an
michael@0 164 inherently single-threaded facility: its debuggees must all run in the
michael@0 165 same thread. Since the global that created the worker is in a different
michael@0 166 thread, it is guaranteed not to be a debuggee of the [`Debugger`][debugger-object] instance
michael@0 167 that owns this source; and thus the creating call is never "in debuggee
michael@0 168 code". Relating a worker to its creator, and other multi-threaded
michael@0 169 debugging concerns, are out of scope for [`Debugger`][debugger-object].
michael@0 170
michael@0 171
michael@0 172
michael@0 173 ## Function Properties of the Debugger.Source Prototype Object
michael@0 174
michael@0 175 <code>substring(<i>start</i>, [<i>end</i>])</code> <i>(future plan)</i>
michael@0 176 : Return a substring of this instance's `source` property, starting at
michael@0 177 <i>start</i> and extending to, but not including, <i>end</i>. If
michael@0 178 <i>end</i> is `undefined`, the substring returned extends to the end of
michael@0 179 the source.
michael@0 180
michael@0 181 Both indices are zero-based. If either is `NaN` or negative, it is
michael@0 182 replaced with zero. If either is greater than the length of the source,
michael@0 183 it is replaced with the length of the source. If <i>start</i> is larger
michael@0 184 than <i>end</i>, they are swapped. (This is meant to be consistent with
michael@0 185 the way `String.prototype.substring` interprets its arguments.)
michael@0 186
michael@0 187 <code>lineToPosition(<i>line</i>)</code> <i>(future plan)</i>
michael@0 188 : Return an object of the form
michael@0 189 <code>{ line:<i>line</i>, start:<i>start</i>, length:<i>length</i> }</code>, where
michael@0 190 <i>start</i> is the character index within `source` of the first
michael@0 191 character of line number <i>line</i>, and <i>length</i> is the length of
michael@0 192 that line in characters, including the final newline, if any. The first
michael@0 193 line is numbered one. If <i>line</i> is negative or greater than the
michael@0 194 number of lines in this `Debugger.Source` instance, then return `null`.
michael@0 195
michael@0 196 <code>positionToLine(<i>start</i>)</code> <i>(future plan)</i>
michael@0 197 : Return an object of the form
michael@0 198 <code>{ line:<i>line</i>, start:<i>start</i>, length:<i>length</i> }</code>, where
michael@0 199 <i>line</i> is the line number containing the character position
michael@0 200 <i>start</i>, and <i>length</i> is the length of that line in
michael@0 201 characters, including the final newline, if any. The first line is
michael@0 202 numbered one. If <i>start</i> is negative or greater than the length of
michael@0 203 the source code, then return `null`.
michael@0 204
michael@0 205 <code>findScripts(<i>query</i>)</code> <i>(future plan)</i>
michael@0 206 : Return an array of [`Debugger.Script`][script] instances for all debuggee scripts
michael@0 207 matching <i>query</i> that are produced from this `Debugger.Source`
michael@0 208 instance. Aside from the restriction to scripts produced from this
michael@0 209 source, <i>query</i> is interpreted as for
michael@0 210 `Debugger.prototype.findScripts`.
michael@0 211
michael@0 212

mercurial