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 // Flags: --harmony-symbols 29 30 // Fake Symbol if undefined, allowing test to run in non-Harmony mode as well. 31 this.Symbol = typeof Symbol != 'undefined' ? Symbol : String; 32 33 34 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 35 var getProto = desc.get; 36 var setProto = desc.set; 37 38 function TestNoPoisonPill() { 39 assertEquals("function", typeof desc.get); 40 assertEquals("function", typeof desc.set); 41 assertDoesNotThrow("desc.get.call({})"); 42 assertDoesNotThrow("desc.set.call({}, {})"); 43 44 var obj = {}; 45 var obj2 = {}; 46 desc.set.call(obj, obj2); 47 assertEquals(obj.__proto__, obj2); 48 assertEquals(desc.get.call(obj), obj2); 49 } 50 TestNoPoisonPill(); 51 52 53 function TestRedefineObjectPrototypeProtoGetter() { 54 Object.defineProperty(Object.prototype, "__proto__", { 55 get: function() { 56 return 42; 57 } 58 }); 59 assertEquals({}.__proto__, 42); 60 assertEquals(desc.get.call({}), Object.prototype); 61 62 var desc2 = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 63 assertEquals(desc2.get.call({}), 42); 64 assertEquals(desc2.set.call({}), undefined); 65 66 Object.defineProperty(Object.prototype, "__proto__", { 67 set: function(x) {} 68 }); 69 var desc3 = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 70 assertEquals(desc3.get.call({}), 42); 71 assertEquals(desc3.set.call({}), undefined); 72 } 73 TestRedefineObjectPrototypeProtoGetter(); 74 75 76 function TestRedefineObjectPrototypeProtoSetter() { 77 Object.defineProperty(Object.prototype, "__proto__", { set: undefined }); 78 assertThrows(function() { 79 "use strict"; 80 var o = {}; 81 var p = {}; 82 o.__proto__ = p; 83 }, TypeError); 84 } 85 TestRedefineObjectPrototypeProtoSetter(); 86 87 88 function TestGetProtoOfValues() { 89 assertEquals(getProto.call(1), Number.prototype); 90 assertEquals(getProto.call(true), Boolean.prototype); 91 assertEquals(getProto.call(false), Boolean.prototype); 92 assertEquals(getProto.call('s'), String.prototype); 93 assertEquals(getProto.call(Symbol()), Symbol.prototype); 94 95 assertThrows(function() { getProto.call(null); }, TypeError); 96 assertThrows(function() { getProto.call(undefined); }, TypeError); 97 } 98 TestGetProtoOfValues(); 99 100 101 var values = [1, true, false, 's', Symbol()]; 102 103 104 function TestSetProtoOfValues() { 105 var proto = {}; 106 for (var i = 0; i < values.length; i++) { 107 assertEquals(setProto.call(values[i], proto), undefined); 108 } 109 110 assertThrows(function() { setProto.call(null, proto); }, TypeError); 111 assertThrows(function() { setProto.call(undefined, proto); }, TypeError); 112 } 113 TestSetProtoOfValues(); 114 115 116 function TestSetProtoToValue() { 117 var object = {}; 118 var proto = {}; 119 setProto.call(object, proto); 120 121 var valuesWithUndefined = values.concat(undefined); 122 123 for (var i = 0; i < valuesWithUndefined.length; i++) { 124 assertEquals(setProto.call(object, valuesWithUndefined[i]), undefined); 125 assertEquals(getProto.call(object), proto); 126 } 127 128 // null is the only valid value that can be used as a [[Prototype]]. 129 assertEquals(setProto.call(object, null), undefined); 130 assertEquals(getProto.call(object), null); 131 } 132 TestSetProtoToValue(); 133 134 135 function TestDeleteProto() { 136 assertTrue(delete Object.prototype.__proto__); 137 var o = {}; 138 var p = {}; 139 o.__proto__ = p; 140 assertEquals(Object.getPrototypeOf(o), Object.prototype); 141 var desc4 = Object.getOwnPropertyDescriptor(o, "__proto__"); 142 assertTrue(desc4.configurable); 143 assertTrue(desc4.enumerable); 144 assertTrue(desc4.writable); 145 assertEquals(desc4.value, p); 146 } 147 TestDeleteProto(); 148