|
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/. */ |
|
5 |
|
6 /* |
|
7 * Date: 28 May 2001 |
|
8 * |
|
9 * SUMMARY: Functions are scoped statically, not dynamically |
|
10 * |
|
11 * See ECMA Section 10.1.4 Scope Chain and Identifier Resolution |
|
12 * (This section defines the scope chain of an execution context) |
|
13 * |
|
14 * See ECMA Section 12.10 The with Statement |
|
15 * |
|
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) |
|
19 */ |
|
20 //----------------------------------------------------------------------------- |
|
21 var UBound = 0; |
|
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 |
|
25 var status = ''; |
|
26 var statusitems = [ ]; |
|
27 var actual = ''; |
|
28 var actualvalues = [ ]; |
|
29 var expect= ''; |
|
30 var expectedvalues = [ ]; |
|
31 |
|
32 /* |
|
33 * In this section the expected value is 1, not 2. |
|
34 * |
|
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. |
|
38 */ |
|
39 status = 'Section A of test'; |
|
40 var a = 1; |
|
41 function f() |
|
42 { |
|
43 return a; |
|
44 } |
|
45 var obj = {a:2}; |
|
46 with (obj) |
|
47 { |
|
48 actual = f(); |
|
49 } |
|
50 expect = 1; |
|
51 addThis(); |
|
52 |
|
53 |
|
54 /* |
|
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. |
|
57 */ |
|
58 status = 'Section B of test'; |
|
59 var a = 1; |
|
60 var obj = {a:2}; |
|
61 with (obj) |
|
62 { |
|
63 function f() |
|
64 { |
|
65 return a; |
|
66 } |
|
67 actual = f(); |
|
68 } |
|
69 expect = 2; |
|
70 addThis(); |
|
71 |
|
72 |
|
73 /* |
|
74 * Like Section B , except that we call f outside the with block. |
|
75 * By the principles explained above, we still expect 2 - |
|
76 */ |
|
77 status = 'Section C of test'; |
|
78 var a = 1; |
|
79 var obj = {a:2}; |
|
80 with (obj) |
|
81 { |
|
82 function f() |
|
83 { |
|
84 return a; |
|
85 } |
|
86 } |
|
87 actual = f(); |
|
88 expect = 2; |
|
89 addThis(); |
|
90 |
|
91 |
|
92 /* |
|
93 * Like Section C, but with one more level of indirection - |
|
94 */ |
|
95 status = 'Section D of test'; |
|
96 var a = 1; |
|
97 var obj = {a:2, obj:{a:3}}; |
|
98 with (obj) |
|
99 { |
|
100 with (obj) |
|
101 { |
|
102 function f() |
|
103 { |
|
104 return a; |
|
105 } |
|
106 } |
|
107 } |
|
108 actual = f(); |
|
109 expect = 3; |
|
110 addThis(); |
|
111 |
|
112 |
|
113 /* |
|
114 * Like Section C, but here we actually delete obj before calling f. |
|
115 * We still expect 2 - |
|
116 */ |
|
117 status = 'Section E of test'; |
|
118 var a = 1; |
|
119 var obj = {a:2}; |
|
120 with (obj) |
|
121 { |
|
122 function f() |
|
123 { |
|
124 return a; |
|
125 } |
|
126 } |
|
127 delete obj; |
|
128 actual = f(); |
|
129 expect = 2; |
|
130 addThis(); |
|
131 |
|
132 |
|
133 /* |
|
134 * Like Section E. Here we redefine obj and call f under with (obj) - |
|
135 * We still expect 2 - |
|
136 */ |
|
137 status = 'Section F of test'; |
|
138 var a = 1; |
|
139 var obj = {a:2}; |
|
140 with (obj) |
|
141 { |
|
142 function f() |
|
143 { |
|
144 return a; |
|
145 } |
|
146 } |
|
147 delete obj; |
|
148 var obj = {a:3}; |
|
149 with (obj) |
|
150 { |
|
151 actual = f(); |
|
152 } |
|
153 expect = 2; // NOT 3 !!! |
|
154 addThis(); |
|
155 |
|
156 |
|
157 /* |
|
158 * Explicitly verify that f exists at global level, even though |
|
159 * it was defined under the with(obj) block - |
|
160 */ |
|
161 status = 'Section G of test'; |
|
162 var a = 1; |
|
163 var obj = {a:2}; |
|
164 with (obj) |
|
165 { |
|
166 function f() |
|
167 { |
|
168 return a; |
|
169 } |
|
170 } |
|
171 actual = String([obj.hasOwnProperty('f'), self.hasOwnProperty('f')]); |
|
172 expect = String([false, true]); |
|
173 addThis(); |
|
174 |
|
175 |
|
176 /* |
|
177 * Explicitly verify that f exists at global level, even though |
|
178 * it was defined under the with(obj) block - |
|
179 */ |
|
180 status = 'Section H of test'; |
|
181 var a = 1; |
|
182 var obj = {a:2}; |
|
183 with (obj) |
|
184 { |
|
185 function f() |
|
186 { |
|
187 return a; |
|
188 } |
|
189 } |
|
190 actual = String(['f' in obj, 'f' in self]); |
|
191 expect = String([false, true]); |
|
192 addThis(); |
|
193 |
|
194 |
|
195 |
|
196 //------------------------------------------------------------------------------------------------- |
|
197 test(); |
|
198 //------------------------------------------------------------------------------------------------- |
|
199 |
|
200 |
|
201 function addThis() |
|
202 { |
|
203 statusitems[UBound] = status; |
|
204 actualvalues[UBound] = actual; |
|
205 expectedvalues[UBound] = expect; |
|
206 UBound++; |
|
207 resetTestVars(); |
|
208 } |
|
209 |
|
210 |
|
211 function resetTestVars() |
|
212 { |
|
213 delete a; |
|
214 delete obj; |
|
215 delete f; |
|
216 } |
|
217 |
|
218 |
|
219 function test() |
|
220 { |
|
221 enterFunc ('test'); |
|
222 printBugNumber(BUGNUMBER); |
|
223 printStatus (summary); |
|
224 |
|
225 for (var i = 0; i < UBound; i++) |
|
226 { |
|
227 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
228 } |
|
229 |
|
230 exitFunc ('test'); |
|
231 } |