]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - tests/mozilla/js1_5/Scope/regress-185485.js
JavaScriptCore-461.tar.gz
[apple/javascriptcore.git] / tests / mozilla / js1_5 / Scope / regress-185485.js
... / ...
CommitLineData
1/* ***** BEGIN LICENSE BLOCK *****
2* Version: NPL 1.1/GPL 2.0/LGPL 2.1
3*
4* The contents of this file are subject to the Netscape Public License
5* Version 1.1 (the "License"); you may not use this file except in
6* compliance with the License. You may obtain a copy of the License at
7* http://www.mozilla.org/NPL/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is JavaScript Engine testing utilities.
15*
16* The Initial Developer of the Original Code is Netscape Communications Corp.
17* Portions created by the Initial Developer are Copyright (C) 2002
18* the Initial Developer. All Rights Reserved.
19*
20* Contributor(s): igor@icesoft.no, pschwartau@netscape.com
21*
22* Alternatively, the contents of this file may be used under the terms of
23* either the GNU General Public License Version 2 or later (the "GPL"), or
24* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25* in which case the provisions of the GPL or the LGPL are applicable instead
26* of those above. If you wish to allow use of your version of this file only
27* under the terms of either the GPL or the LGPL, and not to allow others to
28* use your version of this file under the terms of the NPL, indicate your
29* decision by deleting the provisions above and replace them with the notice
30* and other provisions required by the GPL or the LGPL. If you do not delete
31* the provisions above, a recipient may use your version of this file under
32* the terms of any one of the NPL, the GPL or the LGPL.
33*
34* ***** END LICENSE BLOCK *****
35*
36*
37* Date: 16 Dec 2002
38* SUMMARY: Testing |with (x) {function f() {}}| when |x.f| already exists
39* See http://bugzilla.mozilla.org/show_bug.cgi?id=185485
40*
41* The idea is this: if |x| does not already have a property named |f|,
42* a |with| statement cannot be used to define one. See, for example,
43*
44* http://bugzilla.mozilla.org/show_bug.cgi?id=159849#c11
45* http://bugzilla.mozilla.org/show_bug.cgi?id=184107
46*
47*
48* However, if |x| already has a property |f|, a |with| statement can be
49* used to modify the value it contains:
50*
51* with (x) {f = 1;}
52*
53* This should work even if we use a |var| statement, like this:
54*
55* with (x) {var f = 1;}
56*
57* However, it should NOT work if we use a |function| statement, like this:
58*
59* with (x) {function f() {}}
60*
61* Instead, this should newly define a function f in global scope.
62* See http://bugzilla.mozilla.org/show_bug.cgi?id=185485
63*
64*/
65//-----------------------------------------------------------------------------
66var UBound = 0;
67var bug = 185485;
68var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists';
69var status = '';
70var statusitems = [];
71var actual = '';
72var actualvalues = [];
73var expect= '';
74var expectedvalues = [];
75
76var x = { f:0, g:0 };
77
78with (x)
79{
80 f = 1;
81}
82status = inSection(1);
83actual = x.f;
84expect = 1;
85addThis();
86
87with (x)
88{
89 var f = 2;
90}
91status = inSection(2);
92actual = x.f;
93expect = 2;
94addThis();
95
96/*
97 * Use of a function statement under the with-block should not affect
98 * the local property |f|, but define a function |f| in global scope -
99 */
100with (x)
101{
102 function f() {}
103}
104status = inSection(3);
105actual = x.f;
106expect = 2;
107addThis();
108
109status = inSection(4);
110actual = typeof this.f;
111expect = 'function';
112addThis();
113
114
115/*
116 * Compare use of function expression instead of function statement.
117 * Note it is important that |x.g| already exists. Otherwise, this
118 * would newly define |g| in global scope -
119 */
120with (x)
121{
122 var g = function() {}
123}
124status = inSection(5);
125actual = x.g.toString();
126expect = (function () {}).toString();
127addThis();
128
129
130
131//-----------------------------------------------------------------------------
132test();
133//-----------------------------------------------------------------------------
134
135
136
137function addThis()
138{
139 statusitems[UBound] = status;
140 actualvalues[UBound] = actual;
141 expectedvalues[UBound] = expect;
142 UBound++;
143}
144
145
146function test()
147{
148 enterFunc('test');
149 printBugNumber(bug);
150 printStatus(summary);
151
152 for (var i=0; i<UBound; i++)
153 {
154 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
155 }
156
157 exitFunc ('test');
158}