]>
Commit | Line | Data |
---|---|---|
b37bf2e1 | 1 | /* |
81345200 | 2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
b37bf2e1 A |
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 | * | |
81345200 | 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
b37bf2e1 A |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
81345200 | 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
b37bf2e1 A |
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 | |
81345200 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
b37bf2e1 A |
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); | |
ed1e77d3 A |
77 | shouldBe("this.globalStaticFunction()", 4); |
78 | ||
79 | function globalStaticFunction2() { | |
80 | return 10; | |
81 | } | |
82 | shouldBe("globalStaticFunction2();", 10); | |
83 | this.globalStaticFunction2 = function() { return 20; } | |
84 | shouldBe("globalStaticFunction2();", 20); | |
85 | shouldBe("this.globalStaticFunction2();", 20); | |
86 | ||
87 | function iAmNotAStaticFunction() { return 10; } | |
88 | shouldBe("iAmNotAStaticFunction();", 10); | |
89 | this.iAmNotAStaticFunction = function() { return 20; } | |
90 | shouldBe("iAmNotAStaticFunction();", 20); | |
b37bf2e1 A |
91 | |
92 | shouldBe("typeof MyObject", "function"); // our object implements 'call' | |
93 | MyObject.cantFind = 1; | |
94 | shouldBe("MyObject.cantFind", undefined); | |
95 | MyObject.regularType = 1; | |
96 | shouldBe("MyObject.regularType", 1); | |
97 | MyObject.alwaysOne = 2; | |
98 | shouldBe("MyObject.alwaysOne", 1); | |
99 | MyObject.cantDelete = 1; | |
100 | delete MyObject.cantDelete; | |
101 | shouldBe("MyObject.cantDelete", 1); | |
ba379fdc | 102 | shouldBe("delete MyObject.throwOnDelete", "an exception"); |
b37bf2e1 A |
103 | MyObject.cantSet = 1; |
104 | shouldBe("MyObject.cantSet", undefined); | |
ba379fdc A |
105 | shouldBe("MyObject.throwOnGet", "an exception"); |
106 | shouldBe("MyObject.throwOnSet = 5", "an exception"); | |
107 | shouldBe("MyObject('throwOnCall')", "an exception"); | |
108 | shouldBe("new MyObject('throwOnConstruct')", "an exception"); | |
109 | shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception"); | |
b37bf2e1 | 110 | |
14957cd0 A |
111 | MyObject.nullGetForwardSet = 1; |
112 | shouldBe("MyObject.nullGetForwardSet", 1); | |
113 | ||
b37bf2e1 A |
114 | var foundMyPropertyName = false; |
115 | var foundRegularType = false; | |
116 | for (var p in MyObject) { | |
117 | if (p == "myPropertyName") | |
118 | foundMyPropertyName = true; | |
119 | if (p == "regularType") | |
120 | foundRegularType = true; | |
121 | } | |
ba379fdc A |
122 | |
123 | if (foundMyPropertyName) | |
124 | pass("MyObject.myPropertyName was enumerated"); | |
125 | else | |
126 | fail("MyObject.myPropertyName was not enumerated"); | |
127 | ||
128 | if (foundRegularType) | |
129 | pass("MyObject.regularType was enumerated"); | |
130 | else | |
131 | fail("MyObject.regularType was not enumerated"); | |
b37bf2e1 | 132 | |
f9bf01c6 A |
133 | var alwaysOneDescriptor = Object.getOwnPropertyDescriptor(MyObject, "alwaysOne"); |
134 | shouldBe('typeof alwaysOneDescriptor', "object"); | |
135 | shouldBe('alwaysOneDescriptor.value', MyObject.alwaysOne); | |
136 | shouldBe('alwaysOneDescriptor.configurable', true); | |
137 | shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is. | |
138 | var cantFindDescriptor = Object.getOwnPropertyDescriptor(MyObject, "cantFind"); | |
139 | shouldBe('typeof cantFindDescriptor', "object"); | |
140 | shouldBe('cantFindDescriptor.value', MyObject.cantFind); | |
141 | shouldBe('cantFindDescriptor.configurable', true); | |
142 | shouldBe('cantFindDescriptor.enumerable', false); | |
143 | try { | |
144 | // If getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. | |
145 | Object.getOwnPropertyDescriptor(MyObject, "throwOnGet"); | |
146 | } catch (e) { | |
147 | pass("getting property descriptor of throwOnGet threw exception"); | |
148 | } | |
149 | var myPropertyNameDescriptor = Object.getOwnPropertyDescriptor(MyObject, "myPropertyName"); | |
150 | shouldBe('typeof myPropertyNameDescriptor', "object"); | |
151 | shouldBe('myPropertyNameDescriptor.value', MyObject.myPropertyName); | |
152 | shouldBe('myPropertyNameDescriptor.configurable', true); | |
153 | shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is. | |
154 | try { | |
155 | // if getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw. | |
156 | Object.getOwnPropertyDescriptor(MyObject, "hasPropertyLie"); | |
157 | } catch (e) { | |
158 | pass("getting property descriptor of hasPropertyLie threw exception"); | |
159 | } | |
160 | shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined); | |
161 | ||
b37bf2e1 A |
162 | myObject = new MyObject(); |
163 | ||
164 | shouldBe("delete MyObject.regularType", true); | |
165 | shouldBe("MyObject.regularType", undefined); | |
166 | shouldBe("MyObject(0)", 1); | |
167 | shouldBe("MyObject()", undefined); | |
168 | shouldBe("typeof myObject", "object"); | |
169 | shouldBe("MyObject ? 1 : 0", true); // toBoolean | |
170 | shouldBe("+MyObject", 1); // toNumber | |
6fe7ccc8 | 171 | shouldBe("(Object.prototype.toString.call(MyObject))", "[object MyObject]"); // Object.prototype.toString |
b37bf2e1 | 172 | shouldBe("(MyObject.toString())", "[object MyObject]"); // toString |
6fe7ccc8 | 173 | shouldBe("String(MyObject)", "MyObjectAsString"); // toString |
ba379fdc | 174 | shouldBe("MyObject - 0", 1); // toNumber |
6fe7ccc8 | 175 | shouldBe("MyObject.valueOf()", 1); // valueOf |
b37bf2e1 A |
176 | |
177 | shouldBe("typeof MyConstructor", "object"); | |
178 | constructedObject = new MyConstructor(1); | |
179 | shouldBe("typeof constructedObject", "object"); | |
180 | shouldBe("constructedObject.value", 1); | |
181 | shouldBe("myObject instanceof MyObject", true); | |
182 | shouldBe("(new Object()) instanceof MyObject", false); | |
183 | ||
6fe7ccc8 A |
184 | shouldThrow("new MyBadConstructor()"); |
185 | ||
14957cd0 A |
186 | MyObject.nullGetSet = 1; |
187 | shouldBe("MyObject.nullGetSet", 1); | |
b37bf2e1 A |
188 | shouldThrow("MyObject.nullCall()"); |
189 | shouldThrow("MyObject.hasPropertyLie"); | |
190 | ||
191 | derived = new Derived(); | |
192 | ||
f9bf01c6 A |
193 | shouldBe("derived instanceof Derived", true); |
194 | shouldBe("derived instanceof Base", true); | |
195 | ||
b37bf2e1 A |
196 | // base properties and functions return 1 when called/gotten; derived, 2 |
197 | shouldBe("derived.baseProtoDup()", 2); | |
198 | shouldBe("derived.baseProto()", 1); | |
199 | shouldBe("derived.baseDup", 2); | |
200 | shouldBe("derived.baseOnly", 1); | |
201 | shouldBe("derived.protoOnly()", 2); | |
202 | shouldBe("derived.protoDup", 2); | |
203 | shouldBe("derived.derivedOnly", 2) | |
204 | ||
6fe7ccc8 A |
205 | shouldBe("derived.baseHardNull()", undefined) |
206 | ||
b37bf2e1 A |
207 | // base properties throw 1 when set; derived, 2 |
208 | shouldBe("derived.baseDup = 0", 2); | |
209 | shouldBe("derived.baseOnly = 0", 1); | |
210 | shouldBe("derived.derivedOnly = 0", 2) | |
211 | shouldBe("derived.protoDup = 0", 2); | |
ba379fdc | 212 | |
f9bf01c6 A |
213 | derived2 = new Derived2(); |
214 | ||
215 | shouldBe("derived2 instanceof Derived2", true); | |
216 | shouldBe("derived2 instanceof Derived", true); | |
217 | shouldBe("derived2 instanceof Base", true); | |
218 | ||
219 | // base properties and functions return 1 when called/gotten; derived, 2 | |
220 | shouldBe("derived2.baseProtoDup()", 2); | |
221 | shouldBe("derived2.baseProto()", 1); | |
222 | shouldBe("derived2.baseDup", 2); | |
223 | shouldBe("derived2.baseOnly", 1); | |
224 | shouldBe("derived2.protoOnly()", 2); | |
225 | shouldBe("derived2.protoDup", 2); | |
226 | shouldBe("derived2.derivedOnly", 2) | |
227 | ||
228 | // base properties throw 1 when set; derived, 2 | |
229 | shouldBe("derived2.baseDup = 0", 2); | |
230 | shouldBe("derived2.baseOnly = 0", 1); | |
231 | shouldBe("derived2.derivedOnly = 0", 2) | |
232 | shouldBe("derived2.protoDup = 0", 2); | |
233 | ||
234 | shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined); | |
235 | shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined); | |
236 | var baseDupDescriptor = Object.getOwnPropertyDescriptor(derived, "baseDup"); | |
237 | shouldBe('typeof baseDupDescriptor', "object"); | |
238 | shouldBe('baseDupDescriptor.value', derived.baseDup); | |
239 | shouldBe('baseDupDescriptor.configurable', true); | |
240 | shouldBe('baseDupDescriptor.enumerable', false); | |
241 | var baseOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "baseOnly"); | |
242 | shouldBe('typeof baseOnlyDescriptor', "object"); | |
243 | shouldBe('baseOnlyDescriptor.value', derived.baseOnly); | |
244 | shouldBe('baseOnlyDescriptor.configurable', true); | |
245 | shouldBe('baseOnlyDescriptor.enumerable', false); | |
246 | shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined); | |
247 | var protoDupDescriptor = Object.getOwnPropertyDescriptor(derived, "protoDup"); | |
248 | shouldBe('typeof protoDupDescriptor', "object"); | |
249 | shouldBe('protoDupDescriptor.value', derived.protoDup); | |
250 | shouldBe('protoDupDescriptor.configurable', true); | |
251 | shouldBe('protoDupDescriptor.enumerable', false); | |
252 | var derivedOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "derivedOnly"); | |
253 | shouldBe('typeof derivedOnlyDescriptor', "object"); | |
254 | shouldBe('derivedOnlyDescriptor.value', derived.derivedOnly); | |
255 | shouldBe('derivedOnlyDescriptor.configurable', true); | |
256 | shouldBe('derivedOnlyDescriptor.enumerable', false); | |
257 | ||
ba379fdc A |
258 | shouldBe("undefined instanceof MyObject", false); |
259 | EvilExceptionObject.hasInstance = function f() { return f(); }; | |
260 | EvilExceptionObject.__proto__ = undefined; | |
261 | shouldThrow("undefined instanceof EvilExceptionObject"); | |
262 | EvilExceptionObject.hasInstance = function () { return true; }; | |
263 | shouldBe("undefined instanceof EvilExceptionObject", true); | |
264 | ||
265 | EvilExceptionObject.toNumber = function f() { return f(); } | |
266 | shouldThrow("EvilExceptionObject*5"); | |
267 | EvilExceptionObject.toStringExplicit = function f() { return f(); } | |
268 | shouldThrow("String(EvilExceptionObject)"); | |
269 | ||
81345200 A |
270 | shouldBe("console", "[object Console]"); |
271 | shouldBe("typeof console.log", "function"); | |
272 | ||
ba379fdc A |
273 | shouldBe("EmptyObject", "[object CallbackObject]"); |
274 | ||
14957cd0 A |
275 | for (var i = 0; i < 6; ++i) |
276 | PropertyCatchalls.x = i; | |
277 | shouldBe("PropertyCatchalls.x", 4); | |
278 | ||
279 | for (var i = 0; i < 6; ++i) | |
280 | var x = PropertyCatchalls.x; | |
281 | shouldBe("x", null); | |
93a37866 A |
282 | var make_throw = 'make_throw'; |
283 | shouldThrow("PropertyCatchalls[make_throw]=1"); | |
284 | make_throw = 0; | |
285 | shouldThrow("PropertyCatchalls[make_throw]=1"); | |
14957cd0 A |
286 | |
287 | for (var i = 0; i < 10; ++i) { | |
288 | for (var p in PropertyCatchalls) { | |
289 | if (p == "x") | |
290 | continue; | |
291 | shouldBe("p", i % 10); | |
292 | break; | |
293 | } | |
294 | } | |
295 | ||
296 | PropertyCatchalls.__proto__ = { y: 1 }; | |
297 | for (var i = 0; i < 6; ++i) | |
298 | var y = PropertyCatchalls.y; | |
299 | shouldBe("y", null); | |
300 | ||
301 | var o = { __proto__: PropertyCatchalls }; | |
302 | for (var i = 0; i < 6; ++i) | |
303 | var z = PropertyCatchalls.z; | |
304 | shouldBe("z", null); | |
305 | ||
ba379fdc A |
306 | if (failed) |
307 | throw "Some tests failed"; |