]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/Function/scope-002.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 * Like scope-001.js, but using assignment var f = function expression
34 * instead of a function declaration: function f() {} etc.
36 //-------------------------------------------------------------------------------------------------
39 var summary
= 'Testing that functions are scoped statically, not dynamically';
40 var self
= this; // capture a reference to the global object
42 var statusitems
= [ ];
44 var actualvalues
= [ ];
46 var expectedvalues
= [ ];
50 * In this section the expected value is 1, not 2.
52 * Why? f captures its scope chain from when it's declared, and imposes that chain
53 * when it's executed. In other words, f's scope chain is from when it was compiled.
54 * Since f is a top-level function, this is the global object only. Hence 'a' resolves to 1.
56 status
= 'Section A of test';
58 var f = function () {return a
;};
69 * In this section the expected value is 2, not 1. That is because here
70 * f's associated scope chain now includes 'obj' before the global object.
72 status
= 'Section B of test';
77 var f = function () {return a
;};
85 * Like Section B , except that we call f outside the with block.
86 * By the principles explained above, we still expect 2 -
88 status
= 'Section C of test';
93 var f = function () {return a
;};
101 * Like Section C, but with one more level of indirection -
103 status
= 'Section D of test';
105 var obj
= {a:2, obj:{a:3}};
110 var f = function () {return a
;};
119 * Like Section C, but here we actually delete obj before calling f.
120 * We still expect 2 -
122 status
= 'Section E of test';
127 var f = function () {return a
;};
136 * Like Section E. Here we redefine obj and call f under with (obj) -
137 * We still expect 2 -
139 status
= 'Section F of test';
144 var f = function () {return a
;};
152 expect
= 2; // NOT 3 !!!
157 * Explicitly verify that f exists at global level, even though
158 * it was defined under the with(obj) block -
160 status
= 'Section G of test';
165 var f = function () {return a
;};
167 actual
= String([obj
.hasOwnProperty('f'), self
.hasOwnProperty('f')]);
168 expect
= String([false, true]);
173 * Explicitly verify that f exists at global level, even though
174 * it was defined under the with(obj) block -
176 status
= 'Section H of test';
181 var f = function () {return a
;};
183 actual
= String(['f' in obj
, 'f' in self
]);
184 expect
= String([false, true]);
189 //-------------------------------------------------------------------------------------------------
191 //-------------------------------------------------------------------------------------------------
196 statusitems
[UBound
] = status
;
197 actualvalues
[UBound
] = actual
;
198 expectedvalues
[UBound
] = expect
;
204 function resetTestVars()
215 printBugNumber (bug
);
216 printStatus (summary
);
218 for (var i
= 0; i
< UBound
; i
++)
220 reportCompare(expectedvalues
[i
], actualvalues
[i
], statusitems
[i
]);