Debian Codegraph @

rhino/testsrc/tests/js1_5/Object/regress-137000.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 *
8 * Date: 03 June 2002
9 * SUMMARY: Function param or local var with same name as a function property
10 *
11 * See http://bugzilla.mozilla.org/show_bug.cgi?id=137000
12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=138708
13 * See http://bugzilla.mozilla.org/show_bug.cgi?id=150032
14 * See http://bugzilla.mozilla.org/show_bug.cgi?id=150859
15 *
16 */
17 //-----------------------------------------------------------------------------
18 var gTestfile = 'regress-137000.js';
19 var UBound = 0;
20 var BUGNUMBER = 137000;
21 var summary = 'Function param or local var with same name as a function prop';
22 var status = '';
23 var statusitems = [];
24 var actual = '';
25 var actualvalues = [];
26 var expect= '';
27 var expectedvalues = [];
28
29
30 /*
31 * Note use of 'x' both for the parameter to f,
32 * and as a property name for |f| as an object
33 */
34 function f(x)
35 {
36 }
37
38 status = inSection(1);
39 f.x = 12;
40 actual = f.x;
41 expect = 12;
42 addThis();
43
44
45
46 /*
47 * A more elaborate example, using the call() method
48 * to chain constructors from child to parent.
49 *
50 * The key point is the use of the same name 'p' for both
51 * the parameter to the constructor, and as a property name
52 */
53 function parentObject(p)
54 {
55 this.p = 1;
56 }
57
58 function childObject()
59 {
60 parentObject.call(this);
61 }
62 childObject.prototype = parentObject;
63
64 status = inSection(2);
65 var objParent = new parentObject();
66 actual = objParent.p;
67 expect = 1;
68 addThis();
69
70 status = inSection(3);
71 var objChild = new childObject();
72 actual = objChild.p;
73 expect = 1;
74 addThis();
75
76
77
78 /*
79 * A similar set-up. Here the same name is being used for
80 * the parameter to both the Base and Child constructors,
81 */
82 function Base(id)
83 {
84 }
85
86 function Child(id)
87 {
88 this.prop = id;
89 }
90 Child.prototype=Base;
91
92 status = inSection(4);
93 var c1 = new Child('child1');
94 actual = c1.prop;
95 expect = 'child1';
96 addThis();
97
98
99
100 /*
101 * Use same identifier as a property name, too -
102 */
103 function BaseX(id)
104 {
105 }
106
107 function ChildX(id)
108 {
109 this.id = id;
110 }
111 ChildX.prototype=BaseX;
112
113 status = inSection(5);
114 c1 = new ChildX('child1');
115 actual = c1.id;
116 expect = 'child1';
117 addThis();
118
119
120
121 /*
122 * From http://bugzilla.mozilla.org/show_bug.cgi?id=150032
123 *
124 * Here the same name is being used both for a local variable
125 * declared in g(), and as a property name for |g| as an object
126 */
127 function g()
128 {
129 var propA = g.propA;
130 var propB = g.propC;
131
132 this.getVarA = function() {return propA;}
133 this.getVarB = function() {return propB;}
134 }
135 g.propA = 'A';
136 g.propB = 'B';
137 g.propC = 'C';
138 var obj = new g();
139
140 status = inSection(6);
141 actual = obj.getVarA(); // this one was returning 'undefined'
142 expect = 'A';
143 addThis();
144
145 status = inSection(7);
146 actual = obj.getVarB(); // this one is easy; it never failed
147 expect = 'C';
148 addThis();
149
150
151
152 /*
153 * By [email protected]
154 * From http://bugzilla.mozilla.org/show_bug.cgi?id=150859
155 *
156 * Here the same name is being used for a local var in F
157 * and as a property name for |F| as an object
158 *
159 * Twist: the property is added via another function.
160 */
161 function setFProperty(val)
162 {
163 F.propA = val;
164 }
165
166 function F()
167 {
168 var propA = 'Local variable in F';
169 }
170
171 status = inSection(8);
172 setFProperty('Hello');
173 actual = F.propA; // this was returning 'undefined'
174 expect = 'Hello';
175 addThis();
176
177
178
179
180 //-----------------------------------------------------------------------------
181 test();
182 //-----------------------------------------------------------------------------
183
184
185
186 function addThis()
187 {
188 statusitems[UBound] = status;
189 actualvalues[UBound] = actual;
190 expectedvalues[UBound] = expect;
191 UBound++;
192 }
193
194
195 function test()
196 {
197 enterFunc('test');
198 printBugNumber(BUGNUMBER);
199 printStatus(summary);
200
201 for (var i=0; i<UBound; i++)
202 {
203 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
204 }
205
206 exitFunc ('test');
207 }