Debian Codegraph @

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