1// Copyright 2013 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Fake Symbol if undefined, allowing test to run in non-Harmony mode as well. 29this.Symbol = typeof Symbol != 'undefined' ? Symbol : String; 30 31 32var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 33var getProto = desc.get; 34var setProto = desc.set; 35 36function TestNoPoisonPill() { 37 assertEquals("function", typeof desc.get); 38 assertEquals("function", typeof desc.set); 39 assertDoesNotThrow("desc.get.call({})"); 40 assertDoesNotThrow("desc.set.call({}, {})"); 41 42 var obj = {}; 43 var obj2 = {}; 44 desc.set.call(obj, obj2); 45 assertEquals(obj.__proto__, obj2); 46 assertEquals(desc.get.call(obj), obj2); 47} 48TestNoPoisonPill(); 49 50 51function TestRedefineObjectPrototypeProtoGetter() { 52 Object.defineProperty(Object.prototype, "__proto__", { 53 get: function() { 54 return 42; 55 } 56 }); 57 assertEquals({}.__proto__, 42); 58 assertEquals(desc.get.call({}), Object.prototype); 59 60 var desc2 = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 61 assertEquals(desc2.get.call({}), 42); 62 assertEquals(desc2.set.call({}), undefined); 63 64 Object.defineProperty(Object.prototype, "__proto__", { 65 set: function(x) {} 66 }); 67 var desc3 = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 68 assertEquals(desc3.get.call({}), 42); 69 assertEquals(desc3.set.call({}), undefined); 70} 71TestRedefineObjectPrototypeProtoGetter(); 72 73 74function TestRedefineObjectPrototypeProtoSetter() { 75 Object.defineProperty(Object.prototype, "__proto__", { set: undefined }); 76 assertThrows(function() { 77 "use strict"; 78 var o = {}; 79 var p = {}; 80 o.__proto__ = p; 81 }, TypeError); 82} 83TestRedefineObjectPrototypeProtoSetter(); 84 85 86function TestGetProtoOfValues() { 87 assertEquals(getProto.call(1), Number.prototype); 88 assertEquals(getProto.call(true), Boolean.prototype); 89 assertEquals(getProto.call(false), Boolean.prototype); 90 assertEquals(getProto.call('s'), String.prototype); 91 assertEquals(getProto.call(Symbol()), Symbol.prototype); 92 93 assertThrows(function() { getProto.call(null); }, TypeError); 94 assertThrows(function() { getProto.call(undefined); }, TypeError); 95} 96TestGetProtoOfValues(); 97 98 99var values = [1, true, false, 's', Symbol()]; 100 101 102function TestSetProtoOfValues() { 103 var proto = {}; 104 for (var i = 0; i < values.length; i++) { 105 assertEquals(setProto.call(values[i], proto), undefined); 106 } 107 108 assertThrows(function() { setProto.call(null, proto); }, TypeError); 109 assertThrows(function() { setProto.call(undefined, proto); }, TypeError); 110} 111TestSetProtoOfValues(); 112 113 114function TestSetProtoToValue() { 115 var object = {}; 116 var proto = {}; 117 setProto.call(object, proto); 118 119 var valuesWithUndefined = values.concat(undefined); 120 121 for (var i = 0; i < valuesWithUndefined.length; i++) { 122 assertEquals(setProto.call(object, valuesWithUndefined[i]), undefined); 123 assertEquals(getProto.call(object), proto); 124 } 125 126 // null is the only valid value that can be used as a [[Prototype]]. 127 assertEquals(setProto.call(object, null), undefined); 128 assertEquals(getProto.call(object), null); 129} 130TestSetProtoToValue(); 131 132 133function TestDeleteProto() { 134 assertTrue(delete Object.prototype.__proto__); 135 var o = {}; 136 var p = {}; 137 o.__proto__ = p; 138 assertEquals(Object.getPrototypeOf(o), Object.prototype); 139 var desc4 = Object.getOwnPropertyDescriptor(o, "__proto__"); 140 assertTrue(desc4.configurable); 141 assertTrue(desc4.enumerable); 142 assertTrue(desc4.writable); 143 assertEquals(desc4.value, p); 144} 145TestDeleteProto(); 146