Debugger.Script - Firefox Developer Tools 编辑

/wiki/en-US/docs/Tools/Debugger-API/Debugger.Script

A /wiki/en-US/docs/Tools/Debugger-API/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 /wiki/en-US/docs/Tools/Debugger-API/Debugger API’s presentation of a JSAPI JSScript object. The two cases are distinguished by their format property being "js" or "wasm".

/wiki/en-US/docs/Tools/Debugger-API/Debugger.Script for JSScripts

For /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instances referring to a JSScript, they are distinguished by their format property being "js".

Each of the following is represented by a single JSScript object:

  • The body of a function—that is, all the code in the function that is not contained within some nested function.

  • The code passed to a single call to eval, excluding the bodies of any functions that code defines.

  • The contents of a <script> element.

  • A DOM event handler, whether embedded in HTML or attached to the element by other JavaScript code.

  • Code appearing in a javascript: URL.

The /wiki/en-US/docs/Tools/Debugger-API/Debugger interface constructs /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script objects as scripts of debuggee code are uncovered by the debugger: via the onNewScript handler method; via /wiki/en-US/docs/Tools/Debugger-API/Debugger.Frame’s script properties; via the functionScript method of /wiki/en-US/docs/Tools/Debugger-API/Debugger.Object instances; and so on. For a given /wiki/en-US/docs/Tools/Debugger-API/Debugger instance, SpiderMonkey constructs exactly one /wiki/en-US/docs/Tools/Debugger-API/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.

(If more than one /wiki/en-US/docs/Tools/Debugger-API/Debugger instance is debugging the same code, each /wiki/en-US/docs/Tools/Debugger-API/Debugger gets a separate /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instance for a given script. This allows the code using each /wiki/en-US/docs/Tools/Debugger-API/Debugger instance to place whatever properties it likes on its /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instances, without worrying about interfering with other debuggers.)

A /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instance is a strong reference to a JSScript object; it protects the script it refers to from being garbage collected.

Note that SpiderMonkey may use the same /wiki/en-US/docs/Tools/Debugger-API/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.

/wiki/en-US/docs/Tools/Debugger-API/Debugger.Script for WebAssembly

For /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instances referring to a block of WebAssembly code, they are distinguished by their format property being "wasm".

Currently only entire modules evaluated via new WebAssembly.Module are represented.

/wiki/en-US/docs/Tools/Debugger-API/Debugger.Script objects for WebAssembly are uncovered via onNewScript when a new WebAssembly module is instantiated and via the findScripts method on /wiki/en-US/docs/Tools/Debugger-API/Debugger instances. SpiderMonkey constructs exactly one /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script for each underlying WebAssembly module per /wiki/en-US/docs/Tools/Debugger-API/Debugger instance.

A /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instance is a strong reference to the underlying WebAssembly module; it protects the module it refers to from being garbage collected.

Please note at the time of this writing, support for WebAssembly is very preliminary. Many properties and methods below throw.

Convention

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.

Accessor Properties of the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script Prototype Object

A /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instance inherits the following accessor properties from its prototype:

isGeneratorFunction
True if this instance refers to a JSScript for a function defined with a function* expression or statement. False otherwise.
isAsyncFunction
True if this instance refers to a JSScript for an async function, defined with an async function expression or statement. False otherwise.
displayName

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.

If the script’s function has a given name, its display name is the same as its function’s given name.

If the script’s function has no name, SpiderMonkey attempts to infer an appropriate name for it given its context. For example:

function f() {}          // display name: f (the given name)
var g = function () {};  // display name: g
o.p = function () {};    // display name: o.p
var q = {
  r: function () {}      // display name: q.r
};

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 theb defined withina, and a< to refer to a function that occurs somewhere within an expression that is assigned toa. For example:

function h() {
  var i = function() {};    // display name: h/i
  f(function () {});        // display name: h/<
}
var s = f(function () {});  // display name: s<

If the instance refers to WebAssembly code, throw a TypeError.

url

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 /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script’s source property is non-null, then this is equal to source.url.

If the instance refers to WebAssembly code, throw a TypeError.

startLine
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.
lineCount
If the instance refers to a JSScript, the number of lines this script’s code occupies, within the file or document named by url.
source

If the instance refers to a JSScript, the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Source instance representing the source code from which this script was produced. This is null if the source code was not retained.

If the instance refers to WebAssembly code, the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Source instance representing the serialized text format of the WebAssembly code.

sourceStart

If the instance refers to a JSScript, the character within the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Source 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.

If the instance refers to WebAssembly code, throw a TypeError.

sourceLength

If the instance refers to a JSScript, the length, in characters, of this script’s code within the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Source instance given by source.

If the instance refers to WebAssembly code, throw a TypeError.

global

If the instance refers to a JSScript, a /wiki/en-US/docs/Tools/Debugger-API/Debugger.Object 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).

If the instance refers to WebAssembly code, throw a TypeError.

format

If the instance refers to a JSScript, "js".

If the instance refers to WebAssembly code, "wasm".

Function Properties of the /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script Prototype Object

The functions described below may only be called with a this value referring to a /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script instance; they may not be used as methods of other kinds of objects.

getAllOffsets()

If the instance refers to a JSScript, return an arrayL 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 numberline has no code, thenL has noline property. If there is code forline, then L[line] is an array of offsets of byte code instructions that are entry points to that line.

For example, suppose we have a script for the following source code:

a=[]
for (i=1; i < 10; i++)
    // It's hip to be square.
    a[i] = i*i;

Calling getAllOffsets() on that code might yield an array like this:

[[0], [5, 20], , [10]]

This array indicates that:

  • the first line’s code starts at offset 0 in the script;

  • 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);

  • the third line has no code;

  • and the fourth line begins at offset 10.

If the instance refers to WebAssembly code, throw a TypeError.

getAllColumnOffsets():

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.

The elements of the array are objects, each of which describes a single entry point, and contains the following properties:

  • lineNumber: the line number for which offset is an entry point

  • columnNumber: the column number for which offset is an entry point

  • offset: the bytecode instruction offset of the entry point

For example, suppose we have a script for the following source code:

a=[]
for (i=1; i < 10; i++)
    // It's hip to be square.
    a[i] = i*i;

Calling getAllColumnOffsets() on that code might yield an array like this:

[{ lineNumber: 0, columnNumber: 0, offset: 0 },
 { lineNumber: 1, columnNumber: 5, offset: 5 },
 { lineNumber: 1, columnNumber: 10, offset: 20 },
 { lineNumber: 3, columnNumber: 4, offset: 10 }]

If the instance refers to WebAssembly code, throw a TypeError.

getLineOffsets(line)
If the instance refers to a JSScript, return an array of bytecode instruction offsets representing the entry points to source lineline. If the script contains no executable code at that line, the array returned is empty.
getOffsetLocation(offset)

If the instance refers to a JSScript, return an object describing the source code location responsible for the bytecode atoffset in this script. The object has the following properties:

  • lineNumber: the line number for which offset is an entry point

  • columnNumber: the column number for which offset is an entry point

  • isEntryPoint: true if the offset is a column entry point, as would be reported by getAllColumnOffsets(); otherwise false.

getOffsetsCoverage():

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:

  • lineNumber: the line number of the current opcode.

  • columnNumber: the column number of the current opcode.

  • offset: the bytecode instruction offset of the current opcode.

  • count: the number of times the current opcode got executed.

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 /wiki/en-US/docs/Tools/Debugger-API/Debugger.collectCoverageInfo should be set to true.

If the instance refers to WebAssembly code, throw a TypeError.

getChildScripts()

If the instance refers to a JSScript, return a new array whose elements are /wiki/en-US/docs/Tools/Debugger-API/Debugger.Script objects for each function in this script. Only direct children are included; nested children can be reached by walking the tree.

If the instance refers to WebAssembly code, throw a TypeError.

setBreakpoint(offset,handler)

If the instance refers to a JSScript, set a breakpoint at the bytecode instruction atoffset in this script, reporting hits to the hit method ofhandler. Ifoffset is not a valid offset in this script, throw an error.

When execution reaches the given instruction, SpiderMonkey calls the hit method ofhandler, passing a /wiki/en-US/docs/Tools/Debugger-API/Debugger.Frame instance representing the currently executing stack frame. The hit method’s return value should be a resumption value, determining how execution should continue.

Any number of breakpoints may be set at a single location; when control reaches that point, SpiderMonkey calls their handlers in an unspecified order.

Any number of breakpoints may use the samehandler object.

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).

The new breakpoint belongs to the /wiki/en-US/docs/Tools/Debugger-API/Debugger instance to which this script belongs. Disabling the /wiki/en-US/docs/Tools/Debugger-API/Debugger instance disables this breakpoint; and removing a global from the /wiki/en-US/docs/Tools/Debugger-API/Debugger instance’s set of debuggees clears all the breakpoints belonging to that /wiki/en-US/docs/Tools/Debugger-API/Debugger instance in that global’s scripts.

getBreakpoints([offset])

If the instance refers to a JSScript, return an array containing the handler objects for all the breakpoints set atoffset in this script. Ifoffset is omitted, return the handlers of all breakpoints set anywhere in this script. Ifoffset is present, but not a valid offset in this script, throw an error.

If the instance refers to WebAssembly code, throw a TypeError.

clearBreakpoint(handler, [offset])

If the instance refers to a JSScript, remove all breakpoints set in this /wiki/en-US/docs/Tools/Debugger-API/Debugger instance that usehandler as their handler. Ifoffset is given, remove only those breakpoints set atoffset that usehandler; ifoffset is not a valid offset in this script, throw an error.

Note that, if breakpoints using other handler objects are set at the same location(s) ashandler, they remain in place.

clearAllBreakpoints([offset])
If the instance refers to a JSScript, remove all breakpoints set in this script. Ifoffset is present, remove all breakpoints set at that offset in this script; ifoffset is not a valid bytecode offset in this script, throw an error.
isInCatchScope([offset])

If the instance refers to a JSScript, this is true if this offset falls within the scope of a try block, and false otherwise.

If the instance refers to WebAssembly code, throw a TypeError.

Source Metadata

Generated from file:
 
js/src/doc//wiki/en-US/docs/Tools/Debugger-API/Debugger//wiki/en-US/docs/Tools/Debugger-API/Debugger.Script.md
Watermark:
sha256:8816a4e8617be32c4ce7f3ae54970fe9c8a7d248175d215a8990ccff23e6efa9
Changeset:
5572465c08a9+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:21 次

字数:24502

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文