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