| 1 |
# Debugger.Environment |
| 2 |
|
| 3 |
A `Debugger.Environment` instance represents a lexical environment, |
| 4 |
associating names with variables. Each [`Debugger.Frame`][frame] instance |
| 5 |
representing a debuggee frame has an associated environment object |
| 6 |
describing the variables in scope in that frame; and each |
| 7 |
[`Debugger.Object`][object] instance representing a debuggee function has an |
| 8 |
environment object representing the environment the function has closed |
| 9 |
over. |
| 10 |
|
| 11 |
ECMAScript environments form a tree, in which each local environment is |
| 12 |
parented by its enclosing environment (in ECMAScript terms, its 'outer' |
| 13 |
environment). We say an environment <i>binds</i> an identifier if that |
| 14 |
environment itself associates the identifier with a variable, independently |
| 15 |
of its outer environments. We say an identifier is <i>in scope</i> in an |
| 16 |
environment if the identifier is bound in that environment or any enclosing |
| 17 |
environment. |
| 18 |
|
| 19 |
SpiderMonkey creates `Debugger.Environment` instances as needed as the |
| 20 |
debugger inspects stack frames and function objects; calling |
| 21 |
`Debugger.Environment` as a function or constructor raises a `TypeError` |
| 22 |
exception. |
| 23 |
|
| 24 |
SpiderMonkey creates exactly one `Debugger.Environment` instance for each |
| 25 |
environment it presents via a given [`Debugger`][debugger-object] instance: |
| 26 |
if the debugger encounters the same environment through two different |
| 27 |
routes (perhaps two functions have closed over the same environment), |
| 28 |
SpiderMonkey presents the same `Debugger.Environment` instance to the |
| 29 |
debugger each time. This means that the debugger can use the `==` operator |
| 30 |
to recognize when two `Debugger.Environment` instances refer to the same |
| 31 |
environment in the debuggee, and place its own properties on a |
| 32 |
`Debugger.Environment` instance to store metadata about particular |
| 33 |
environments. |
| 34 |
|
| 35 |
(If more than one [`Debugger`][debugger-object] instance is debugging the |
| 36 |
same code, each [`Debugger`][debugger-object] gets a separate |
| 37 |
`Debugger.Environment` instance for a given environment. This allows the |
| 38 |
code using each [`Debugger`][debugger-object] instance to place whatever |
| 39 |
properties it likes on its own [`Debugger.Object`][object] instances, |
| 40 |
without worrying about interfering with other debuggers.) |
| 41 |
|
| 42 |
If a `Debugger.Environment` instance's referent is not a debuggee |
| 43 |
environment, then attempting to access its properties (other than |
| 44 |
`inspectable`) or call any its methods throws an instance of `Error`. |
| 45 |
|
| 46 |
`Debugger.Environment` instances protect their referents from the |
| 47 |
garbage collector; as long as the `Debugger.Environment` instance is |
| 48 |
live, the referent remains live. Garbage collection has no visible |
| 49 |
effect on `Debugger.Environment` instances. |
| 50 |
|
| 51 |
|
| 52 |
## Accessor Properties of the Debugger.Environment Prototype Object |
| 53 |
|
| 54 |
A `Debugger.Environment` instance inherits the following accessor |
| 55 |
properties from its prototype: |
| 56 |
|
| 57 |
### `inspectable` |
| 58 |
True if this environment is a debuggee environment, and can therefore |
| 59 |
be inspected. False otherwise. All other properties and methods of |
| 60 |
`Debugger.Environment` instances throw if applied to a non-inspectable |
| 61 |
environment. |
| 62 |
|
| 63 |
### `type` |
| 64 |
The type of this environment object, one of the following values: |
| 65 |
|
| 66 |
* "declarative", indicating that the environment is a declarative |
| 67 |
environment record. Function calls, calls to `eval`, `let` blocks, |
| 68 |
`catch` blocks, and the like create declarative environment records. |
| 69 |
|
| 70 |
* "object", indicating that the environment's bindings are the |
| 71 |
properties of an object. The global object and DOM elements appear in |
| 72 |
the chain of environments via object environments. (Note that `with` |
| 73 |
statements have their own environment type.) |
| 74 |
|
| 75 |
* "with", indicating that the environment was introduced by a `with` |
| 76 |
statement. |
| 77 |
|
| 78 |
### `scopeKind` |
| 79 |
If this is a declarative environment, a string describing the kind of scope |
| 80 |
which this environment is associated with, or `null` for other types of |
| 81 |
environments. There is an assortment of possible scope kinds which can be |
| 82 |
generated, with a selection of possible values below. Unlike the type |
| 83 |
accessor, the categorization this performs is specific to SpiderMonkey's |
| 84 |
implementation, and not derived from distinctions made in the ECMAScript |
| 85 |
language specification. |
| 86 |
|
| 87 |
* "function", indicating the top level body scope of a function for |
| 88 |
arguments and 'var' variables. |
| 89 |
|
| 90 |
* "function lexical", indicating the top level lexical scope in a function. |
| 91 |
|
| 92 |
### `parent` |
| 93 |
The environment that encloses this one (the "outer" environment, in |
| 94 |
ECMAScript terminology), or `null` if this is the outermost environment. |
| 95 |
|
| 96 |
### `object` |
| 97 |
A [`Debugger.Object`][object] instance referring to the object whose |
| 98 |
properties this environment reflects. If this is a declarative |
| 99 |
environment record, this accessor throws a `TypeError` (since |
| 100 |
declarative environment records have no such object). Both `"object"` |
| 101 |
and `"with"` environments have `object` properties that provide the |
| 102 |
object whose properties they reflect as variable bindings. |
| 103 |
|
| 104 |
### `calleeScript` |
| 105 |
If this environment represents the variable environment (the top-level |
| 106 |
environment within the function, which receives `var` definitions) for |
| 107 |
a call to a function <i>f</i>, then this property's value is a |
| 108 |
[`Debugger.Script`][script] instance referring to <i>f</i>'s script. Otherwise, |
| 109 |
this property's value is `null`. |
| 110 |
|
| 111 |
### `optimizedOut` |
| 112 |
True if this environment is optimized out. False otherwise. For example, |
| 113 |
functions whose locals are never aliased may present optimized-out |
| 114 |
environments. When true, `getVariable` returns an ordinary JavaScript |
| 115 |
object whose `optimizedOut` property is true on all bindings, and |
| 116 |
`setVariable` throws a `ReferenceError`. |
| 117 |
|
| 118 |
|
| 119 |
## Function Properties of the Debugger.Environment Prototype Object |
| 120 |
|
| 121 |
The methods described below may only be called with a `this` value |
| 122 |
referring to a `Debugger.Environment` instance; they may not be used as |
| 123 |
methods of other kinds of objects. |
| 124 |
|
| 125 |
### `names()` |
| 126 |
Return an array of strings giving the names of the identifiers bound by |
| 127 |
this environment. The result does not include the names of identifiers |
| 128 |
bound by enclosing environments. |
| 129 |
|
| 130 |
### `getVariable(name)` |
| 131 |
|
| 132 |
Return the value of the variable bound to <i>name</i> in this |
| 133 |
environment, or `undefined` if this environment does not bind |
| 134 |
<i>name</i>. <i>Name</i> must be a string that is a valid ECMAScript |
| 135 |
identifier name. The result is a debuggee value, in most cases. |
| 136 |
|
| 137 |
JavaScript engines often omit variables from environments, to save space |
| 138 |
and reduce execution time. If the given variable should be in scope, but |
| 139 |
`getVariable` is unable to produce its value, it returns an ordinary |
| 140 |
JavaScript object (not a [`Debugger.Object`][object] instance) whose |
| 141 |
`optimizedOut` property is `true`. |
| 142 |
|
| 143 |
Aside from the above case, this method can return something that is not a |
| 144 |
debuggee value in two other cases. If a function argument is missing, then it |
| 145 |
returns an ordinary JavaScript object whose `missingArgument` property is |
| 146 |
`true`. Finally, if a variable name is bound in the environment but not yet |
| 147 |
initialized (for example, if the debuggee is paused in the middle of an |
| 148 |
initializer expression) then it returns an ordinary JavaScript object whose |
| 149 |
`uninitialized` property is `true`. |
| 150 |
|
| 151 |
This is not an [invocation function][inv fr]; |
| 152 |
if this call would cause debuggee code to run (say, because the |
| 153 |
environment is a `"with"` environment, and <i>name</i> refers to an |
| 154 |
accessor property of the `with` statement's operand), this call throws a |
| 155 |
[`Debugger.DebuggeeWouldRun`][wouldrun] |
| 156 |
exception. |
| 157 |
|
| 158 |
### `setVariable(name, value)` |
| 159 |
Store <i>value</i> as the value of the variable bound to <i>name</i> in |
| 160 |
this environment. <i>Name</i> must be a string that is a valid |
| 161 |
ECMAScript identifier name; <i>value</i> must be a debuggee value. |
| 162 |
|
| 163 |
If this environment binds no variable named <i>name</i>, throw a |
| 164 |
`ReferenceError`. |
| 165 |
|
| 166 |
This is not an [invocation function][inv fr]; |
| 167 |
if this call would cause debuggee code to run, this call throws a |
| 168 |
[`Debugger.DebuggeeWouldRun`][wouldrun] |
| 169 |
exception. |
| 170 |
|
| 171 |
### `find(name)` |
| 172 |
Return a reference to the innermost environment, starting with this |
| 173 |
environment, that binds <i>name</i>. If <i>name</i> is not in scope in |
| 174 |
this environment, return `null`. <i>Name</i> must be a string whose |
| 175 |
value is a valid ECMAScript identifier name. |
| 176 |
|
| 177 |
|
| 178 |
[frame]: Debugger.Frame.md |
| 179 |
[object]: Debugger.Object.md |
| 180 |
[debugger-object]: Debugger.md |
| 181 |
[inv fr]: Debugger.Frame.html#invocation-functions-and-debugger-frames |
| 182 |
[wouldrun]: Conventions.html#the-debugger-debuggeewouldrun-exception |