tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

index.rst (16359B)


      1 ===============
      2 Debugger.Script
      3 ===============
      4 
      5 A ``Debugger.Script`` instance may refer to a sequence of bytecode in the debuggee or to a block of WebAssembly code. For the former, it is the :doc:`Debugger <../debugger/index>` API’s presentation of a JSAPI ``JSScript`` object. The two cases are distinguished by their ``format`` property being ``"js"`` or ``"wasm"``.
      6 
      7 
      8 Debugger.Script for JSScripts
      9 *****************************
     10 
     11 For ``Debugger.Script`` instances referring to a ``JSScript``, they are distinguished by their ``format`` property being ``"js"``.
     12 
     13 Each of the following is represented by a single ``JSScript`` object:
     14 
     15 
     16 - The body of a function—that is, all the code in the function that is not contained within some nested function.
     17 
     18 - The code passed to a single call to ``eval``, excluding the bodies of any functions that code defines.
     19 
     20 - The contents of a ``<script>`` element.
     21 
     22 - A DOM event handler, whether embedded in HTML or attached to the element by other JavaScript code.
     23 
     24 - Code appearing in a ``javascript:`` URL.
     25 
     26 
     27 The :doc:`Debugger <../debugger/index>` interface constructs ``Debugger.Script`` objects as scripts of debuggee code are uncovered by the debugger: via the ``onNewScript`` handler method; via :doc:`Debugger.Frame <../debugger.frame/index>`’s ``script`` properties; via the ``functionScript`` method of :doc:`Debugger.Object <../debugger.object/index>` instances; and so on. For a given :doc:`Debugger <../debugger/index>` instance, SpiderMonkey constructs exactly one ``Debugger.Script`` instance for each underlying script object; debugger code can add its own properties to a script object and expect to find them later, use ``==`` to decide whether two expressions refer to the same script, and so on.
     28 
     29 (If more than one :doc:`Debugger <../debugger/index>` instance is debugging the same code, each :doc:`Debugger <../debugger/index>` gets a separate ``Debugger.Script`` instance for a given script. This allows the code using each :doc:`Debugger <../debugger/index>` instance to place whatever properties it likes on its ``Debugger.Script`` instances, without worrying about interfering with other debuggers.)
     30 
     31 A ``Debugger.Script`` instance is a strong reference to a JSScript object; it protects the script it refers to from being garbage collected.
     32 
     33 Note that SpiderMonkey may use the same ``Debugger.Script`` instances for equivalent functions or evaluated code—that is, scripts representing the same source code, at the same position in the same source file, evaluated in the same lexical environment.
     34 
     35 
     36 Debugger.Script for WebAssembly
     37 *******************************
     38 
     39 For ``Debugger.Script`` instances referring to a block of WebAssembly code, they are distinguished by their ``format`` property being ``"wasm"``.
     40 
     41 Currently only entire modules evaluated via ``new WebAssembly.Module`` are represented.
     42 
     43 ``Debugger.Script`` objects for WebAssembly are uncovered via ``onNewScript`` when a new WebAssembly module is instantiated and via the ``findScripts`` method on :doc:`Debugger <../debugger/index>` instances. SpiderMonkey constructs exactly one ``Debugger.Script`` for each underlying WebAssembly module per :doc:`Debugger <../debugger/index>` instance.
     44 
     45 A ``Debugger.Script`` instance is a strong reference to the underlying WebAssembly module; it protects the module it refers to from being garbage collected.
     46 
     47 Please note at the time of this writing, support for WebAssembly is very preliminary. Many properties and methods below throw.
     48 
     49 
     50 Convention
     51 **********
     52 
     53 For descriptions of properties and methods below, if the behavior of the property or method differs between the instance referring to a ``JSScript`` or to a block of WebAssembly code, the text will be split into two sections, headed by “**if the instance refers to a JSScript**” and “**if the instance refers to WebAssembly code**”, respectively. If the behavior does not differ, no such emphasized headings will appear.
     54 
     55 
     56 Accessor Properties of the Debugger.Script Prototype Object
     57 ***********************************************************
     58 
     59 A ``Debugger.Script`` instance inherits the following accessor properties from its prototype:
     60 
     61 
     62 ``isGeneratorFunction``
     63  True if this instance refers to a ``JSScript`` for a function defined with a ``function*`` expression or statement. False otherwise.
     64 
     65 ``isAsyncFunction``
     66  True if this instance refers to a ``JSScript`` for an async function, defined with an ``async function`` expression or statement. False otherwise.
     67 
     68 ``displayName``
     69  **If the instance refers to a JSScript**, this is the script’s display name, if it has one. If the script has no display name — for example, if it is a top-level ``eval`` script — this is ``undefined``.
     70 
     71  If the script’s function has a given name, its display name is the same as its function’s given name.
     72 
     73  If the script’s function has no name, SpiderMonkey attempts to infer an appropriate name for it given its context. For example:
     74 
     75  .. code-block:: javascript
     76 
     77    function f() {}          // display name: f (the given name)
     78    var g = function () {};  // display name: g
     79    o.p = function () {};    // display name: o.p
     80    var q = {
     81      r: function () {}      // display name: q.r
     82    };
     83 
     84 
     85  Note that the display name may not be a proper JavaScript identifier, or even a proper expression: we attempt to find helpful names even when the function is not immediately assigned as the value of some variable or property. Thus, we use ``a/b`` to refer to the *b* defined within *a*, and ``a<`` to refer to a function that occurs somewhere within an expression that is assigned to *a*. For example:
     86 
     87  .. code-block:: javascript
     88 
     89    function h() {
     90      var i = function() {};    // display name: h/i
     91      f(function () {});        // display name: h/<
     92    }
     93    var s = f(function () {});  // display name: s<``</pre>
     94 
     95  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
     96 
     97 ``url``
     98 
     99  **If the instance refers to a JSScript**, the filename or URL from which this script’s code was loaded. For scripts created by ``eval`` or the ``Function`` constructor, this may be a synthesized filename, starting with a valid URL and followed by information tracking how the code was introduced into the system; the entire string is not a valid URL. For ``Function.prototype``’s script, this is ``null``. If this ``Debugger.Script``’s ``source`` property is non-``null``, then this is equal to ``source.url``.
    100 
    101  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    102 
    103 ``startLine``
    104  **If the instance refers to a JSScript**, the number of the line at which this script’s code starts, within the file or document named by ``url``.
    105 
    106 ``lineCount``
    107  **If the instance refers to a JSScript**, the number of lines this script’s code occupies, within the file or document named by ``url``.
    108 
    109 ``source``
    110  **If the instance refers to a JSScript**, the :doc:`Debugger.Source <../debugger.source/index>` instance representing the source code from which this script was produced. This is ``null`` if the source code was not retained.
    111 
    112  **If the instance refers to WebAssembly code**, the :doc:`Debugger.Source <../debugger.source/index>` instance representing the serialized text format of the WebAssembly code.
    113 
    114 ``sourceStart``
    115  **If the instance refers to a JSScript**, the character within the :doc:`Debugger.Source <../debugger.source/index>` instance given by ``source`` at which this script’s code starts; zero-based. If this is a function’s script, this is the index of the start of the ``function`` token in the source code.
    116 
    117  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    118 
    119 ``sourceLength``
    120  **If the instance refers to a JSScript**, the length, in characters, of this script’s code within the :doc:`Debugger.Source <../debugger.source/index>` instance given by ``source``.
    121 
    122  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    123 
    124 ``global``
    125 
    126  **If the instance refers to a JSScript**, a :doc:`Debugger.Object <../debugger.object/index>` instance referring to the global object in whose scope this script runs. The result refers to the global directly, not via a wrapper or a ``WindowProxy`` (“outer window”, in Firefox).
    127 
    128  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    129 
    130 ``format``
    131  **If the instance refers to a JSScript**, ``"js"``.
    132 
    133  **If the instance refers to WebAssembly code**, ``"wasm"``.
    134 
    135 
    136 
    137 Function Properties of the Debugger.Script Prototype Object
    138 ***********************************************************
    139 
    140 The functions described below may only be called with a ``this`` value referring to a ``Debugger.Script`` instance; they may not be used as methods of other kinds of objects.
    141 
    142 
    143 ``getAllOffsets()``
    144  **If the instance refers to a JSScript**, return an array *L* describing the relationship between bytecode instruction offsets and source code positions in this script. *L* is sparse, and indexed by source line number. If a source line number *line* has no code, then *L* has no *line* property. If there is code for *line*, then ``L[line]`` is an array of offsets of byte code instructions that are entry points to that line.
    145 
    146  For example, suppose we have a script for the following source code:
    147 
    148  .. code-block:: javascript
    149 
    150    a=[]
    151    for (i=1; i < 10; i++)
    152      // It's hip to be square.
    153      a[i] = i*i;
    154 
    155  Calling ``getAllOffsets()`` on that code might yield an array like this:
    156 
    157  .. code-block:: javascript
    158 
    159    [[0], [5, 20], , [10]]
    160 
    161  This array indicates that:
    162 
    163  - the first line’s code starts at offset 0 in the script;
    164  - the ``for`` statement head has two entry points at offsets 5 and 20 (for the initialization, which is performed only once, and the loop test, which is performed at the start of each iteration);
    165  - the third line has no code;
    166  - and the fourth line begins at offset 10.
    167 
    168  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    169 
    170 
    171 ``getAllColumnOffsets()``:
    172  **If the instance refers to a JSScript**, return an array describing the relationship between bytecode instruction offsets and source code positions in this script. Unlike getAllOffsets(), which returns all offsets that are entry points for each line, getAllColumnOffsets() returns all offsets that are entry points for each (line, column) pair.
    173 
    174  The elements of the array are objects, each of which describes a single entry point, and contains the following properties:
    175 
    176  - lineNumber: the line number for which offset is an entry point
    177  - columnNumber: the 1-based column number for which offset is an entry point
    178  - offset: the bytecode instruction offset of the entry point
    179 
    180 
    181  For example, suppose we have a script for the following source code:
    182 
    183  .. code-block:: javascript
    184 
    185    a=[]
    186    for (i=1; i < 10; i++)
    187      // It's hip to be square.
    188    a[i] = i*i;
    189 
    190  Calling ``getAllColumnOffsets()`` on that code might yield an array like this:
    191 
    192  .. code-block:: javascript
    193 
    194    [{ lineNumber: 0, columnNumber: 1, offset: 0 },
    195     { lineNumber: 1, columnNumber: 6, offset: 5 },
    196     { lineNumber: 1, columnNumber: 11, offset: 20 },
    197     { lineNumber: 3, columnNumber: 5, offset: 10 }]
    198 
    199  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    200 
    201 ``getLineOffsets(line)``
    202  **If the instance refers to a JSScript**, return an array of bytecode instruction offsets representing the entry points to source line *line*. If the script contains no executable code at that line, the array returned is empty.
    203 
    204 ``getOffsetLocation(offset)``
    205  **If the instance refers to a JSScript**, return an object describing the source code location responsible for the bytecode at *offset* in this script. The object has the following properties:
    206 
    207  - lineNumber: the line number for which offset is an entry point
    208  - columnNumber: the 1-based column number for which offset is an entry point
    209  - isEntryPoint: true if the offset is a column entry point, as would be reported by getAllColumnOffsets(); otherwise false.
    210 
    211 
    212 ``getOffsetsCoverage()``:
    213  **If the instance refers to a JSScript**, return ``null`` or an array which contains information about the coverage of all opcodes. The elements of the array are objects, each of which describes a single opcode, and contains the following properties:
    214 
    215  - lineNumber: the line number of the current opcode.
    216  - columnNumber: the 1-based column number of the current opcode.
    217  - offset: the bytecode instruction offset of the current opcode.
    218  - count: the number of times the current opcode got executed.
    219 
    220 
    221  If this script has no coverage, or if it is not instrumented, then this function will return ``null``. To ensure that the debuggee is instrumented, the flag ``Debugger.collectCoverageInfo`` should be set to ``true``.
    222 
    223  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    224 
    225 ``getChildScripts()``
    226  **If the instance refers to a JSScript**, return a new array whose elements are Debugger.Script objects for each function in this script. Only direct children are included; nested children can be reached by walking the tree.
    227 
    228  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    229 
    230 ``setBreakpoint(offset, handler)``
    231  **If the instance refers to a JSScript**, set a breakpoint at the bytecode instruction at *offset* in this script, reporting hits to the ``hit`` method of *handler*. If *offset* is not a valid offset in this script, throw an error.
    232 
    233  When execution reaches the given instruction, SpiderMonkey calls the ``hit`` method of *handler*, passing a :doc:`Debugger.Frame <../debugger.frame/index>` instance representing the currently executing stack frame. The ``hit`` method’s return value should be a resumption value, determining how execution should continue.
    234 
    235  Any number of breakpoints may be set at a single location; when control reaches that point, SpiderMonkey calls their handlers in an unspecified order.
    236 
    237  Any number of breakpoints may use the same *handler* object.
    238 
    239  Breakpoint handler method calls are cross-compartment, intra-thread calls: the call takes place in the same thread that hit the breakpoint, and in the compartment containing the handler function (typically the debugger’s compartment).
    240 
    241  The new breakpoint belongs to the :doc:`Debugger <../debugger/index>` instance to which this script belongs. Disabling the :doc:`Debugger <../debugger/index>` instance disables this breakpoint; and removing a global from the :doc:`Debugger <../debugger/index>` instance’s set of debuggees clears all the breakpoints belonging to that :doc:`Debugger <../debugger/index>` instance in that global’s scripts.
    242 
    243 ``getBreakpoints([offset])``
    244  **If the instance refers to a JSScript**, return an array containing the handler objects for all the breakpoints set at *offset* in this script. If *offset* is omitted, return the handlers of all breakpoints set anywhere in this script. If *offset* is present, but not a valid offset in this script, throw an error.
    245 
    246  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    247 
    248 ``clearBreakpoint(handler, [offset])``
    249  **If the instance refers to a JSScript**, remove all breakpoints set in this :doc:`Debugger <../debugger/index>` instance that use *handler* as their handler. If *offset* is given, remove only those breakpoints set at *offset* that use *handler*; if *offset* is not a valid offset in this script, throw an error.
    250 
    251  Note that, if breakpoints using other handler objects are set at the same location(s) as *handler*, they remain in place.
    252 
    253 ``clearAllBreakpoints([offset])``
    254  **If the instance refers to a JSScript**, remove all breakpoints set in this script. If *offset* is present, remove all breakpoints set at that offset in this script; if *offset* is not a valid bytecode offset in this script, throw an error.
    255 
    256 ``isInCatchScope([offset])``
    257  **If the instance refers to a JSScript**, this is ``true`` if this offset falls within the scope of a try block, and ``false`` otherwise.
    258 
    259  **If the instance refers to WebAssembly code**, throw a ``TypeError``.
    260 
    261 
    262 Source Metadata
    263 ***************
    264 
    265 Generated from file:
    266  js/src/doc/Debugger/Debugger.Script.md
    267 
    268 Watermark:
    269  sha256:8816a4e8617be32c4ce7f3ae54970fe9c8a7d248175d215a8990ccff23e6efa9
    270 
    271 Changeset:
    272  `5572465c08a9+ <https://hg.mozilla.org/mozilla-central/rev/5572465c08a9>`_