]> git.saurik.com Git - apple/javascriptcore.git/blame - API/tests/testapi.js
JavaScriptCore-1097.3.tar.gz
[apple/javascriptcore.git] / API / tests / testapi.js
CommitLineData
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
26function bludgeonArguments() { if (0) arguments; return function g() {} }
27h = bludgeonArguments();
28gc();
29
30var failed = false;
31function pass(msg)
32{
33 print("PASS: " + msg, "green");
34}
35
36function fail(msg)
37{
38 print("FAIL: " + msg, "red");
39 failed = true;
40}
41
b37bf2e1
A
42function 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
57function 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
70function globalStaticFunction()
71{
72 return 4;
73}
74
75shouldBe("globalStaticValue", 3);
76shouldBe("globalStaticFunction()", 4);
77
78shouldBe("typeof MyObject", "function"); // our object implements 'call'
79MyObject.cantFind = 1;
80shouldBe("MyObject.cantFind", undefined);
81MyObject.regularType = 1;
82shouldBe("MyObject.regularType", 1);
83MyObject.alwaysOne = 2;
84shouldBe("MyObject.alwaysOne", 1);
85MyObject.cantDelete = 1;
86delete MyObject.cantDelete;
87shouldBe("MyObject.cantDelete", 1);
ba379fdc 88shouldBe("delete MyObject.throwOnDelete", "an exception");
b37bf2e1
A
89MyObject.cantSet = 1;
90shouldBe("MyObject.cantSet", undefined);
ba379fdc
A
91shouldBe("MyObject.throwOnGet", "an exception");
92shouldBe("MyObject.throwOnSet = 5", "an exception");
93shouldBe("MyObject('throwOnCall')", "an exception");
94shouldBe("new MyObject('throwOnConstruct')", "an exception");
95shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception");
b37bf2e1 96
14957cd0
A
97MyObject.nullGetForwardSet = 1;
98shouldBe("MyObject.nullGetForwardSet", 1);
99
b37bf2e1
A
100var foundMyPropertyName = false;
101var foundRegularType = false;
102for (var p in MyObject) {
103 if (p == "myPropertyName")
104 foundMyPropertyName = true;
105 if (p == "regularType")
106 foundRegularType = true;
107}
ba379fdc
A
108
109if (foundMyPropertyName)
110 pass("MyObject.myPropertyName was enumerated");
111else
112 fail("MyObject.myPropertyName was not enumerated");
113
114if (foundRegularType)
115 pass("MyObject.regularType was enumerated");
116else
117 fail("MyObject.regularType was not enumerated");
b37bf2e1 118
f9bf01c6
A
119var alwaysOneDescriptor = Object.getOwnPropertyDescriptor(MyObject, "alwaysOne");
120shouldBe('typeof alwaysOneDescriptor', "object");
121shouldBe('alwaysOneDescriptor.value', MyObject.alwaysOne);
122shouldBe('alwaysOneDescriptor.configurable', true);
123shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is.
124var cantFindDescriptor = Object.getOwnPropertyDescriptor(MyObject, "cantFind");
125shouldBe('typeof cantFindDescriptor', "object");
126shouldBe('cantFindDescriptor.value', MyObject.cantFind);
127shouldBe('cantFindDescriptor.configurable', true);
128shouldBe('cantFindDescriptor.enumerable', false);
129try {
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}
135var myPropertyNameDescriptor = Object.getOwnPropertyDescriptor(MyObject, "myPropertyName");
136shouldBe('typeof myPropertyNameDescriptor', "object");
137shouldBe('myPropertyNameDescriptor.value', MyObject.myPropertyName);
138shouldBe('myPropertyNameDescriptor.configurable', true);
139shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is.
140try {
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}
146shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined);
147
b37bf2e1
A
148myObject = new MyObject();
149
150shouldBe("delete MyObject.regularType", true);
151shouldBe("MyObject.regularType", undefined);
152shouldBe("MyObject(0)", 1);
153shouldBe("MyObject()", undefined);
154shouldBe("typeof myObject", "object");
155shouldBe("MyObject ? 1 : 0", true); // toBoolean
156shouldBe("+MyObject", 1); // toNumber
6fe7ccc8 157shouldBe("(Object.prototype.toString.call(MyObject))", "[object MyObject]"); // Object.prototype.toString
b37bf2e1 158shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
6fe7ccc8 159shouldBe("String(MyObject)", "MyObjectAsString"); // toString
ba379fdc 160shouldBe("MyObject - 0", 1); // toNumber
6fe7ccc8 161shouldBe("MyObject.valueOf()", 1); // valueOf
b37bf2e1
A
162
163shouldBe("typeof MyConstructor", "object");
164constructedObject = new MyConstructor(1);
165shouldBe("typeof constructedObject", "object");
166shouldBe("constructedObject.value", 1);
167shouldBe("myObject instanceof MyObject", true);
168shouldBe("(new Object()) instanceof MyObject", false);
169
6fe7ccc8
A
170shouldThrow("new MyBadConstructor()");
171
14957cd0
A
172MyObject.nullGetSet = 1;
173shouldBe("MyObject.nullGetSet", 1);
b37bf2e1
A
174shouldThrow("MyObject.nullCall()");
175shouldThrow("MyObject.hasPropertyLie");
176
177derived = new Derived();
178
f9bf01c6
A
179shouldBe("derived instanceof Derived", true);
180shouldBe("derived instanceof Base", true);
181
b37bf2e1
A
182// base properties and functions return 1 when called/gotten; derived, 2
183shouldBe("derived.baseProtoDup()", 2);
184shouldBe("derived.baseProto()", 1);
185shouldBe("derived.baseDup", 2);
186shouldBe("derived.baseOnly", 1);
187shouldBe("derived.protoOnly()", 2);
188shouldBe("derived.protoDup", 2);
189shouldBe("derived.derivedOnly", 2)
190
6fe7ccc8
A
191shouldBe("derived.baseHardNull()", undefined)
192
b37bf2e1
A
193// base properties throw 1 when set; derived, 2
194shouldBe("derived.baseDup = 0", 2);
195shouldBe("derived.baseOnly = 0", 1);
196shouldBe("derived.derivedOnly = 0", 2)
197shouldBe("derived.protoDup = 0", 2);
ba379fdc 198
f9bf01c6
A
199derived2 = new Derived2();
200
201shouldBe("derived2 instanceof Derived2", true);
202shouldBe("derived2 instanceof Derived", true);
203shouldBe("derived2 instanceof Base", true);
204
205// base properties and functions return 1 when called/gotten; derived, 2
206shouldBe("derived2.baseProtoDup()", 2);
207shouldBe("derived2.baseProto()", 1);
208shouldBe("derived2.baseDup", 2);
209shouldBe("derived2.baseOnly", 1);
210shouldBe("derived2.protoOnly()", 2);
211shouldBe("derived2.protoDup", 2);
212shouldBe("derived2.derivedOnly", 2)
213
214// base properties throw 1 when set; derived, 2
215shouldBe("derived2.baseDup = 0", 2);
216shouldBe("derived2.baseOnly = 0", 1);
217shouldBe("derived2.derivedOnly = 0", 2)
218shouldBe("derived2.protoDup = 0", 2);
219
220shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined);
221shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined);
222var baseDupDescriptor = Object.getOwnPropertyDescriptor(derived, "baseDup");
223shouldBe('typeof baseDupDescriptor', "object");
224shouldBe('baseDupDescriptor.value', derived.baseDup);
225shouldBe('baseDupDescriptor.configurable', true);
226shouldBe('baseDupDescriptor.enumerable', false);
227var baseOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "baseOnly");
228shouldBe('typeof baseOnlyDescriptor', "object");
229shouldBe('baseOnlyDescriptor.value', derived.baseOnly);
230shouldBe('baseOnlyDescriptor.configurable', true);
231shouldBe('baseOnlyDescriptor.enumerable', false);
232shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined);
233var protoDupDescriptor = Object.getOwnPropertyDescriptor(derived, "protoDup");
234shouldBe('typeof protoDupDescriptor', "object");
235shouldBe('protoDupDescriptor.value', derived.protoDup);
236shouldBe('protoDupDescriptor.configurable', true);
237shouldBe('protoDupDescriptor.enumerable', false);
238var derivedOnlyDescriptor = Object.getOwnPropertyDescriptor(derived, "derivedOnly");
239shouldBe('typeof derivedOnlyDescriptor', "object");
240shouldBe('derivedOnlyDescriptor.value', derived.derivedOnly);
241shouldBe('derivedOnlyDescriptor.configurable', true);
242shouldBe('derivedOnlyDescriptor.enumerable', false);
243
ba379fdc
A
244shouldBe("undefined instanceof MyObject", false);
245EvilExceptionObject.hasInstance = function f() { return f(); };
246EvilExceptionObject.__proto__ = undefined;
247shouldThrow("undefined instanceof EvilExceptionObject");
248EvilExceptionObject.hasInstance = function () { return true; };
249shouldBe("undefined instanceof EvilExceptionObject", true);
250
251EvilExceptionObject.toNumber = function f() { return f(); }
252shouldThrow("EvilExceptionObject*5");
253EvilExceptionObject.toStringExplicit = function f() { return f(); }
254shouldThrow("String(EvilExceptionObject)");
255
256shouldBe("EmptyObject", "[object CallbackObject]");
257
14957cd0
A
258for (var i = 0; i < 6; ++i)
259 PropertyCatchalls.x = i;
260shouldBe("PropertyCatchalls.x", 4);
261
262for (var i = 0; i < 6; ++i)
263 var x = PropertyCatchalls.x;
264shouldBe("x", null);
265
266for (var i = 0; i < 10; ++i) {
267 for (var p in PropertyCatchalls) {
268 if (p == "x")
269 continue;
270 shouldBe("p", i % 10);
271 break;
272 }
273}
274
275PropertyCatchalls.__proto__ = { y: 1 };
276for (var i = 0; i < 6; ++i)
277 var y = PropertyCatchalls.y;
278shouldBe("y", null);
279
280var o = { __proto__: PropertyCatchalls };
281for (var i = 0; i < 6; ++i)
282 var z = PropertyCatchalls.z;
283shouldBe("z", null);
284
ba379fdc
A
285if (failed)
286 throw "Some tests failed";