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