1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple 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 INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25'Test prototypes of various objects and the various means to access them.'
26);
27
28shouldBe("('').__proto__", "String.prototype");
29shouldBe("(0).__proto__", "Number.prototype");
30shouldBe("([]).__proto__", "Array.prototype");
31shouldBe("({}).__proto__", "Object.prototype");
32shouldBe("(new Date).__proto__", "Date.prototype");
33shouldBe("(new Number).__proto__", "Number.prototype");
34shouldBe("(new Object).__proto__", "Object.prototype");
35shouldBe("(new String).__proto__", "String.prototype");
36shouldBe("Array.prototype.__proto__", "Object.prototype");
37shouldBe("Date.prototype.__proto__", "Object.prototype");
38shouldBe("Number.prototype.__proto__", "Object.prototype");
39shouldBe("Object.prototype.__proto__", "null");
40shouldBe("String.prototype.__proto__", "Object.prototype");
41shouldBe("Array.__proto__", "Object.__proto__");
42shouldBe("Date.__proto__", "Object.__proto__");
43shouldBe("Number.__proto__", "Object.__proto__");
44shouldBe("String.__proto__", "Object.__proto__");
45
46shouldThrow("Object.getPrototypeOf('')");
47shouldThrow("Object.getPrototypeOf(0)");
48shouldBe("Object.getPrototypeOf([])", "Array.prototype");
49shouldBe("Object.getPrototypeOf({})", "Object.prototype");
50shouldBe("Object.getPrototypeOf(new Date)", "Date.prototype");
51shouldBe("Object.getPrototypeOf(new Number)", "Number.prototype");
52shouldBe("Object.getPrototypeOf(new Object)", "Object.prototype");
53shouldBe("Object.getPrototypeOf(new String)", "String.prototype");
54shouldBe("Object.getPrototypeOf(Array.prototype)", "Object.prototype");
55shouldBe("Object.getPrototypeOf(Date.prototype)", "Object.prototype");
56shouldBe("Object.getPrototypeOf(Number.prototype)", "Object.prototype");
57shouldBe("Object.getPrototypeOf(Object.prototype)", "null");
58shouldBe("Object.getPrototypeOf(String.prototype)", "Object.prototype");
59shouldBe("Object.getPrototypeOf(Array)", "Object.__proto__");
60shouldBe("Object.getPrototypeOf(Date)", "Object.__proto__");
61shouldBe("Object.getPrototypeOf(Number)", "Object.__proto__");
62shouldBe("Object.getPrototypeOf(String)", "Object.__proto__");
63
64shouldBeFalse("String.prototype.isPrototypeOf('')");
65shouldBeFalse("Number.prototype.isPrototypeOf(0)");
66shouldBeTrue("Array.prototype.isPrototypeOf([])");
67shouldBeTrue("Object.prototype.isPrototypeOf({})");
68shouldBeTrue("Date.prototype.isPrototypeOf(new Date)");
69shouldBeTrue("Number.prototype.isPrototypeOf(new Number)");
70shouldBeTrue("Object.prototype.isPrototypeOf(new Object)");
71shouldBeTrue("String.prototype.isPrototypeOf(new String)");
72shouldBeTrue("Object.prototype.isPrototypeOf(Array.prototype)");
73shouldBeTrue("Object.prototype.isPrototypeOf(Date.prototype)");
74shouldBeTrue("Object.prototype.isPrototypeOf(Number.prototype)");
75shouldBeTrue("Object.prototype.isPrototypeOf(String.prototype)");
76shouldBeTrue("Object.__proto__.isPrototypeOf(Array)");
77shouldBeTrue("Object.__proto__.isPrototypeOf(Date)");
78shouldBeTrue("Object.__proto__.isPrototypeOf(Number)");
79shouldBeTrue("Object.__proto__.isPrototypeOf(String)");
80
81shouldBeTrue("var wasSet = false; var o = { }; o.__defineGetter__(\"__proto__\", function() { wasSet = true }); o.__proto__; wasSet;");
82shouldBeTrue("var wasSet = false; var o = { }; o.__defineSetter__(\"__proto__\", function() { wasSet = true }); o.__proto__ = {}; wasSet;");
83shouldBeTrue("var wasSet = false; var o = { }; Object.defineProperty(o, \"__proto__\", { \"get\": function() { wasSet = true } }); o.__proto__; wasSet;");
84shouldBeFalse("var wasSet = false; var o = { }; Object.defineProperty(o, \"__proto__\", { \"__proto__\": function(x) { wasSet = true } }); o.__proto__ = {}; wasSet;");
85
86// Deleting Object.prototype.__proto__ removes the ability to set the object's prototype.
87shouldBeTrue("var o = {}; o.__proto__ = { x:true }; o.x");
88shouldBeFalse("var o = {}; o.__proto__ = { x:true }; o.hasOwnProperty('__proto__')");
89delete Object.prototype.__proto__;
90shouldBeUndefined("var o = {}; o.__proto__ = { x:true }; o.x");
91shouldBeTrue("var o = {}; o.__proto__ = { x:true }; o.hasOwnProperty('__proto__')");
92