1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9
* SUMMARY: Functions are scoped statically, not dynamically
11
* See ECMA Section 10.1.4 Scope Chain and Identifier Resolution
12
* (This section defines the scope chain of an execution context)
14
* See ECMA Section 12.10 The with Statement
16
* See ECMA Section 13 Function Definition
17
* (This section defines the scope chain of a function object as that
18
* of the running execution context when the function was declared)
20
//-----------------------------------------------------------------------------
22
var BUGNUMBER = '(none)';
23
var summary = 'Testing that functions are scoped statically, not dynamically';
24
var self = this; // capture a reference to the global object
26
var statusitems = [ ];
28
var actualvalues = [ ];
30
var expectedvalues = [ ];
33
* In this section the expected value is 1, not 2.
35
* Why? f captures its scope chain from when it's declared, and imposes that chain
36
* when it's executed. In other words, f's scope chain is from when it was compiled.
37
* Since f is a top-level function, this is the global object only. Hence 'a' resolves to 1.
39
status = 'Section A of test';
55
* In this section the expected value is 2, not 1. That is because here
56
* f's associated scope chain now includes 'obj' before the global object.
58
status = 'Section B of test';
74
* Like Section B , except that we call f outside the with block.
75
* By the principles explained above, we still expect 2 -
77
status = 'Section C of test';
93
* Like Section C, but with one more level of indirection -
95
status = 'Section D of test';
97
var obj = {a:2, obj:{a:3}};
114
* Like Section C, but here we actually delete obj before calling f.
115
* We still expect 2 -
117
status = 'Section E of test';
134
* Like Section E. Here we redefine obj and call f under with (obj) -
135
* We still expect 2 -
137
status = 'Section F of test';
153
expect = 2; // NOT 3 !!!
158
* Explicitly verify that f exists at global level, even though
159
* it was defined under the with(obj) block -
161
status = 'Section G of test';
171
actual = String([obj.hasOwnProperty('f'), self.hasOwnProperty('f')]);
172
expect = String([false, true]);
177
* Explicitly verify that f exists at global level, even though
178
* it was defined under the with(obj) block -
180
status = 'Section H of test';
190
actual = String(['f' in obj, 'f' in self]);
191
expect = String([false, true]);
196
//-------------------------------------------------------------------------------------------------
198
//-------------------------------------------------------------------------------------------------
203
statusitems[UBound] = status;
204
actualvalues[UBound] = actual;
205
expectedvalues[UBound] = expect;
211
function resetTestVars()
222
printBugNumber(BUGNUMBER);
223
printStatus (summary);
225
for (var i = 0; i < UBound; i++)
227
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);