]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
ba379fdc A |
26 | function bludgeonArguments() { if (0) arguments; return function g() {} } |
27 | h = bludgeonArguments(); | |
28 | gc(); | |
29 | ||
30 | var failed = false; | |
31 | function pass(msg) | |
32 | { | |
33 | print("PASS: " + msg, "green"); | |
34 | } | |
35 | ||
36 | function fail(msg) | |
37 | { | |
38 | print("FAIL: " + msg, "red"); | |
39 | failed = true; | |
40 | } | |
41 | ||
b37bf2e1 A |
42 | function shouldBe(a, b) |
43 | { | |
44 | var evalA; | |
45 | try { | |
46 | evalA = eval(a); | |
47 | } catch(e) { | |
48 | evalA = e; | |
49 | } | |
50 | ||
51 | if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number') | |
ba379fdc | 52 | pass(a + " should be " + b + " and is."); |
b37bf2e1 | 53 | else |
ba379fdc | 54 | fail(a + " should be " + b + " but instead is " + evalA + "."); |
b37bf2e1 A |
55 | } |
56 | ||
57 | function shouldThrow(a) | |
58 | { | |
b37bf2e1 A |
59 | var evalA; |
60 | try { | |
61 | eval(a); | |
62 | } catch(e) { | |
ba379fdc A |
63 | pass(a + " threw: " + e); |
64 | return; | |
b37bf2e1 | 65 | } |
ba379fdc A |
66 | |
67 | fail(a + " did not throw an exception."); | |
b37bf2e1 A |
68 | } |
69 | ||
70 | function globalStaticFunction() | |
71 | { | |
72 | return 4; | |
73 | } | |
74 | ||
75 | shouldBe("globalStaticValue", 3); | |
76 | shouldBe("globalStaticFunction()", 4); | |
77 | ||
78 | shouldBe("typeof MyObject", "function"); // our object implements 'call' | |
79 | MyObject.cantFind = 1; | |
80 | shouldBe("MyObject.cantFind", undefined); | |
81 | MyObject.regularType = 1; | |
82 | shouldBe("MyObject.regularType", 1); | |
83 | MyObject.alwaysOne = 2; | |
84 | shouldBe("MyObject.alwaysOne", 1); | |
85 | MyObject.cantDelete = 1; | |
86 | delete MyObject.cantDelete; | |
87 | shouldBe("MyObject.cantDelete", 1); | |
ba379fdc | 88 | shouldBe("delete MyObject.throwOnDelete", "an exception"); |
b37bf2e1 A |
89 | MyObject.cantSet = 1; |
90 | shouldBe("MyObject.cantSet", undefined); | |
ba379fdc A |
91 | shouldBe("MyObject.throwOnGet", "an exception"); |
92 | shouldBe("MyObject.throwOnSet = 5", "an exception"); | |
93 | shouldBe("MyObject('throwOnCall')", "an exception"); | |
94 | shouldBe("new MyObject('throwOnConstruct')", "an exception"); | |
95 | shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception"); | |
b37bf2e1 | 96 | |
14957cd0 A |
97 | MyObject.nullGetForwardSet = 1; |
98 | shouldBe("MyObject.nullGetForwardSet", 1); | |
99 | ||
b37bf2e1 A |
100 | var foundMyPropertyName = false; |
101 | var foundRegularType = false; | |
102 | for (var p in MyObject) { | |
103 | if (p == "myPropertyName") | |
104 | foundMyPropertyName = true; | |
105 | if (p == "regularType") | |
106 | foundRegularType = true; | |
107 | } | |
ba379fdc A |
108 | |
109 | if (foundMyPropertyName) | |
110 | pass("MyObject.myPropertyName was enumerated"); | |
111 | else | |
112 | fail("MyObject.myPropertyName was not enumerated"); | |
113 | ||
114 | if (foundRegularType) | |
115 | pass("MyObject.regularType was enumerated"); | |
116 | else | |
117 | fail("MyObject.regularType was not enumerated"); | |
b37bf2e1 | 118 | |
f9bf01c6 A |
119 | var alwaysOneDescriptor = Object.getOwnPropertyDescriptor(MyObject, "alwaysOne"); |
120 | shouldBe('typeof alwaysOneDescriptor', "object"); | |
121 | shouldBe('alwaysOneDescriptor.value', MyObject.alwaysOne); | |
122 | shouldBe('alwaysOneDescriptor.configurable', true); | |
123 | shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is. | |
124 | var cantFindDescriptor = Object.getOwnPropertyDescriptor(MyObject, "cantFind"); | |
125 | shouldBe('typeof cantFindDescriptor', "object"); | |
126 | shouldBe('cantFindDescriptor.value', MyObject.cantFind); | |
127 | shouldBe('cantFindDescriptor.configurable', true); | |
128 | shouldBe('cantFindDescriptor.enumerable', false); | |
129 | try { | |
130 | // If getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. | |
131 | Object.getOwnPropertyDescriptor(MyObject, "throwOnGet"); | |
132 | } catch (e) { | |
133 | pass("getting property descriptor of throwOnGet threw exception"); | |
134 | } | |
135 | var myPropertyNameDescriptor = Object.getOwnPropertyDescriptor(MyObject, "myPropertyName"); | |
136 | shouldBe('typeof myPropertyNameDescriptor', "object"); | |
137 | shouldBe('myPropertyNameDescriptor.value', MyObject.myPropertyName); | |
138 | shouldBe('myPropertyNameDescriptor.configurable', true); | |
139 | shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is. | |
140 | try { | |
141 | // if getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. | |
142 | Object.getOwnPropertyDescriptor(MyObject, "hasPropertyLie"); | |
143 | } catch (e) { | |
144 | pass("getting property descriptor of hasPropertyLie threw exception"); | |
145 | } | |
146 | shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined); | |
147 | ||
b37bf2e1 A |
148 | myObject = new MyObject(); |
149 | ||
150 | shouldBe("delete MyObject.regularType", true); | |
151 | shouldBe("MyObject.regularType", undefined); | |
152 | shouldBe("MyObject(0)", 1); | |
153 | shouldBe("MyObject()", undefined); | |
154 | shouldBe("typeof myObject", "object"); | |
155 | shouldBe("MyObject ? 1 : 0", true); // toBoolean | |
156 | shouldBe("+MyObject", 1); // toNumber | |
157 | shouldBe("(MyObject.toString())", "[object MyObject]"); // toString | |
9dae56ea | 158 | shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string |
ba379fdc | 159 | shouldBe("MyObject - 0", 1); // toNumber |
b37bf2e1 A |
160 | |
161 | shouldBe("typeof MyConstructor", "object"); | |
162 | constructedObject = new MyConstructor(1); | |
163 | shouldBe("typeof constructedObject", "object"); | |
164 | shouldBe("constructedObject.value", 1); | |
165 | shouldBe("myObject instanceof MyObject", true); | |
166 | shouldBe("(new Object()) instanceof MyObject", false); | |
167 | ||
14957cd0 A |
168 | MyObject.nullGetSet = 1; |
169 | shouldBe("MyObject.nullGetSet", 1); | |
b37bf2e1 A |
170 | shouldThrow("MyObject.nullCall()"); |
171 | shouldThrow("MyObject.hasPropertyLie"); | |
172 | ||
173 | derived = new Derived(); | |
174 | ||
f9bf01c6 A |
175 | shouldBe("derived instanceof Derived", true); |
176 | shouldBe("derived instanceof Base", true); | |
177 | ||
b37bf2e1 A |
178 | // base properties and functions return 1 when called/gotten; derived, 2 |
179 | shouldBe("derived.baseProtoDup()", 2); | |
180 | shouldBe("derived.baseProto()", 1); | |
181 | shouldBe("derived.baseDup", 2); | |
182 | shouldBe("derived.baseOnly", 1); | |
183 | shouldBe("derived.protoOnly()", 2); | |
184 | shouldBe("derived.protoDup", 2); | |
185 | shouldBe("derived.derivedOnly", 2) | |
186 | ||
187 | // base properties throw 1 when set; derived, 2 | |
188 | shouldBe("derived.baseDup = 0", 2); | |
189 | shouldBe("derived.baseOnly = 0", 1); | |
190 | shouldBe("derived.derivedOnly = 0", 2) | |
191 | shouldBe("derived.protoDup = 0", 2); | |
ba379fdc | 192 | |
f9bf01c6 A |
193 | derived2 = new Derived2(); |
194 | ||
195 | shouldBe("derived2 instanceof Derived2", true); | |
196 | shouldBe("derived2 instanceof Derived", true); | |
197 | shouldBe("derived2 instanceof Base", true); | |
198 | ||
199 | // base properties and functions return 1 when called/gotten; derived, 2 | |
200 | shouldBe("derived2.baseProtoDup()", 2); | |
201 | shouldBe("derived2.baseProto()", 1); | |
202 | shouldBe("derived2.baseDup", 2); | |
203 | shouldBe("derived2.baseOnly", 1); | |
204 | shouldBe("derived2.protoOnly()", 2); | |
205 | shouldBe("derived2.protoDup", 2); | |
206 | shouldBe("derived2.derivedOnly", 2) | |
207 | ||
208 | // base properties throw 1 when set; derived, 2 | |
209 | shouldBe("derived2.baseDup = 0", 2); | |
210 | shouldBe("derived2.baseOnly = 0", 1); | |
211 | shouldBe("derived2.derivedOnly = 0", 2) | |
212 | shouldBe("derived2.protoDup = 0", 2); | |
213 | ||
214 | shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined); | |
215 | shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined); | |
216 | var baseDupDescriptor = Object.getOwnPropertyDescriptor(derived, "baseDup"); | |
217 | shouldBe('typeof baseDupDescriptor', "object"); | |
218 | shouldBe('baseDupDescriptor.value', derived.baseDup); | |
219 | shouldBe('baseDupDescriptor.configurable', true); | |
220 | shouldBe('baseDupDescriptor.enumerable', false); | |
221 | var baseOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "baseOnly"); | |
222 | shouldBe('typeof baseOnlyDescriptor', "object"); | |
223 | shouldBe('baseOnlyDescriptor.value', derived.baseOnly); | |
224 | shouldBe('baseOnlyDescriptor.configurable', true); | |
225 | shouldBe('baseOnlyDescriptor.enumerable', false); | |
226 | shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined); | |
227 | var protoDupDescriptor = Object.getOwnPropertyDescriptor(derived, "protoDup"); | |
228 | shouldBe('typeof protoDupDescriptor', "object"); | |
229 | shouldBe('protoDupDescriptor.value', derived.protoDup); | |
230 | shouldBe('protoDupDescriptor.configurable', true); | |
231 | shouldBe('protoDupDescriptor.enumerable', false); | |
232 | var derivedOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "derivedOnly"); | |
233 | shouldBe('typeof derivedOnlyDescriptor', "object"); | |
234 | shouldBe('derivedOnlyDescriptor.value', derived.derivedOnly); | |
235 | shouldBe('derivedOnlyDescriptor.configurable', true); | |
236 | shouldBe('derivedOnlyDescriptor.enumerable', false); | |
237 | ||
ba379fdc A |
238 | shouldBe("undefined instanceof MyObject", false); |
239 | EvilExceptionObject.hasInstance = function f() { return f(); }; | |
240 | EvilExceptionObject.__proto__ = undefined; | |
241 | shouldThrow("undefined instanceof EvilExceptionObject"); | |
242 | EvilExceptionObject.hasInstance = function () { return true; }; | |
243 | shouldBe("undefined instanceof EvilExceptionObject", true); | |
244 | ||
245 | EvilExceptionObject.toNumber = function f() { return f(); } | |
246 | shouldThrow("EvilExceptionObject*5"); | |
247 | EvilExceptionObject.toStringExplicit = function f() { return f(); } | |
248 | shouldThrow("String(EvilExceptionObject)"); | |
249 | ||
250 | shouldBe("EmptyObject", "[object CallbackObject]"); | |
251 | ||
14957cd0 A |
252 | for (var i = 0; i < 6; ++i) |
253 | PropertyCatchalls.x = i; | |
254 | shouldBe("PropertyCatchalls.x", 4); | |
255 | ||
256 | for (var i = 0; i < 6; ++i) | |
257 | var x = PropertyCatchalls.x; | |
258 | shouldBe("x", null); | |
259 | ||
260 | for (var i = 0; i < 10; ++i) { | |
261 | for (var p in PropertyCatchalls) { | |
262 | if (p == "x") | |
263 | continue; | |
264 | shouldBe("p", i % 10); | |
265 | break; | |
266 | } | |
267 | } | |
268 | ||
269 | PropertyCatchalls.__proto__ = { y: 1 }; | |
270 | for (var i = 0; i < 6; ++i) | |
271 | var y = PropertyCatchalls.y; | |
272 | shouldBe("y", null); | |
273 | ||
274 | var o = { __proto__: PropertyCatchalls }; | |
275 | for (var i = 0; i < 6; ++i) | |
276 | var z = PropertyCatchalls.z; | |
277 | shouldBe("z", null); | |
278 | ||
ba379fdc A |
279 | if (failed) |
280 | throw "Some tests failed"; |