]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/Function/scope-001.js
2 * The contents of this file are subject to the Netscape Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/NPL/
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is mozilla.org code.
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
19 * Contributor(s): pschwartau@netscape.com, rogerl@netscape.com
22 * SUMMARY: Functions are scoped statically, not dynamically
24 * See ECMA Section 10.1.4 Scope Chain and Identifier Resolution
25 * (This section defines the scope chain of an execution context)
27 * See ECMA Section 12.10 The with Statement
29 * See ECMA Section 13 Function Definition
30 * (This section defines the scope chain of a function object as that
31 * of the running execution context when the function was declared)
33 //-------------------------------------------------------------------------------------------------
36 var summary
= 'Testing that functions are scoped statically, not dynamically';
37 var self
= this; // capture a reference to the global object
39 var statusitems
= [ ];
41 var actualvalues
= [ ];
43 var expectedvalues
= [ ];
46 * In this section the expected value is 1, not 2.
48 * Why? f captures its scope chain from when it's declared, and imposes that chain
49 * when it's executed. In other words, f's scope chain is from when it was compiled.
50 * Since f is a top-level function, this is the global object only. Hence 'a' resolves to 1.
52 status
= 'Section A of test';
68 * In this section the expected value is 2, not 1. That is because here
69 * f's associated scope chain now includes 'obj' before the global object.
71 status
= 'Section B of test';
82 // Mozilla result, which contradicts IE and the ECMA spec: expect = 2;
88 * Like Section B , except that we call f outside the with block.
89 * By the principles explained above, we still expect 2 -
91 status
= 'Section C of test';
102 // Mozilla result, which contradicts IE and the ECMA spec: expect = 2;
108 * Like Section C, but with one more level of indirection -
110 status
= 'Section D of test';
112 var obj
= {a:2, obj:{a:3}};
124 // Mozilla result, which contradicts IE and the ECMA spec: expect = 3;
130 * Like Section C, but here we actually delete obj before calling f.
131 * We still expect 2 -
133 status
= 'Section E of test';
145 // Mozilla result, which contradicts IE and the ECMA spec: expect = 2;
151 * Like Section E. Here we redefine obj and call f under with (obj) -
152 * We still expect 2 -
154 status
= 'Section F of test';
170 // Mozilla result, which contradicts IE and the ECMA spec: expect = 2; // NOT 3 !!!
176 * Explicitly verify that f exists at global level, even though
177 * it was defined under the with(obj) block -
179 status
= 'Section G of test';
189 actual
= String([obj
.hasOwnProperty('f'), self
.hasOwnProperty('f')]);
190 expect
= String([false, true]);
195 * Explicitly verify that f exists at global level, even though
196 * it was defined under the with(obj) block -
198 status
= 'Section H of test';
208 actual
= String(['f' in obj
, 'f' in self
]);
209 expect
= String([false, true]);
214 //-------------------------------------------------------------------------------------------------
216 //-------------------------------------------------------------------------------------------------
221 statusitems
[UBound
] = status
;
222 actualvalues
[UBound
] = actual
;
223 expectedvalues
[UBound
] = expect
;
229 function resetTestVars()
240 printBugNumber (bug
);
241 printStatus (summary
);
243 for (var i
= 0; i
< UBound
; i
++)
245 reportCompare(expectedvalues
[i
], actualvalues
[i
], statusitems
[i
]);