Debian Codegraph @

rhino/testsrc/tests/ecma_3/Function/scope-001.js

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