1 // Copyright 2012 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: --allow-natives-syntax 29 30 // Handy abbreviations. 31 var dp = Object.defineProperty; 32 var gop = Object.getOwnPropertyDescriptor; 33 34 function getter() { return 111; } 35 function setter(x) { print(222); } 36 function anotherGetter() { return 333; } 37 function anotherSetter(x) { print(444); } 38 var obj1, obj2, obj3, obj4; 39 40 // Two objects with the same getter. 41 obj1 = {}; 42 dp(obj1, "alpha", { get: getter }); 43 obj2 = {}; 44 dp(obj2, "alpha", { get: getter }); 45 assertTrue(%HaveSameMap(obj1, obj2)); 46 47 // Two objects with the same getter, oldskool. 48 obj1 = {}; 49 obj1.__defineGetter__("bravo", getter); 50 assertEquals(getter, obj1.__lookupGetter__("bravo")); 51 obj2 = {}; 52 obj2.__defineGetter__("bravo", getter); 53 assertEquals(getter, obj2.__lookupGetter__("bravo")); 54 assertTrue(%HaveSameMap(obj1, obj2)); 55 56 // Two objects with the same setter. 57 obj1 = {}; 58 dp(obj1, "charlie", { set: setter }); 59 obj2 = {}; 60 dp(obj2, "charlie", { set: setter }); 61 assertTrue(%HaveSameMap(obj1, obj2)); 62 63 // Two objects with the same setter, oldskool. 64 obj1 = {}; 65 obj1.__defineSetter__("delta", setter); 66 assertEquals(setter, obj1.__lookupSetter__("delta")); 67 obj2 = {}; 68 obj2.__defineSetter__("delta", setter); 69 assertEquals(setter, obj2.__lookupSetter__("delta")); 70 assertTrue(%HaveSameMap(obj1, obj2)); 71 72 // Two objects with the same getter and setter. 73 obj1 = {}; 74 dp(obj1, "foxtrot", { get: getter, set: setter }); 75 obj2 = {}; 76 dp(obj2, "foxtrot", { get: getter, set: setter }); 77 assertTrue(%HaveSameMap(obj1, obj2)); 78 79 // Two objects with the same getter and setter, set separately. 80 obj1 = {}; 81 dp(obj1, "golf", { get: getter, configurable: true }); 82 dp(obj1, "golf", { set: setter, configurable: true }); 83 obj2 = {}; 84 dp(obj2, "golf", { get: getter, configurable: true }); 85 dp(obj2, "golf", { set: setter, configurable: true }); 86 assertTrue(%HaveSameMap(obj1, obj2)); 87 88 // Two objects with the same getter and setter, set separately, oldskool. 89 obj1 = {}; 90 obj1.__defineGetter__("hotel", getter); 91 obj1.__defineSetter__("hotel", setter); 92 obj2 = {}; 93 obj2.__defineGetter__("hotel", getter); 94 obj2.__defineSetter__("hotel", setter); 95 assertTrue(%HaveSameMap(obj1, obj2)); 96 97 // Attribute-only change, shouldn't affect previous descriptor properties. 98 obj1 = {}; 99 dp(obj1, "india", { get: getter, configurable: true, enumerable: true }); 100 assertEquals(getter, gop(obj1, "india").get); 101 assertTrue(gop(obj1, "india").configurable); 102 assertTrue(gop(obj1, "india").enumerable); 103 dp(obj1, "india", { enumerable: false }); 104 assertEquals(getter, gop(obj1, "india").get); 105 assertTrue(gop(obj1, "india").configurable); 106 assertFalse(gop(obj1, "india").enumerable); 107 108 // Attribute-only change, shouldn't affect objects with previously shared maps. 109 obj1 = {}; 110 dp(obj1, "juliet", { set: setter, configurable: true, enumerable: false }); 111 assertEquals(setter, gop(obj1, "juliet").set); 112 assertTrue(gop(obj1, "juliet").configurable); 113 assertFalse(gop(obj1, "juliet").enumerable); 114 obj2 = {}; 115 dp(obj2, "juliet", { set: setter, configurable: true, enumerable: false }); 116 assertEquals(setter, gop(obj2, "juliet").set); 117 assertTrue(gop(obj2, "juliet").configurable); 118 assertFalse(gop(obj2, "juliet").enumerable); 119 dp(obj1, "juliet", { set: setter, configurable: false, enumerable: true }); 120 assertEquals(setter, gop(obj1, "juliet").set); 121 assertFalse(gop(obj1, "juliet").configurable); 122 assertTrue(gop(obj1, "juliet").enumerable); 123 assertEquals(setter, gop(obj2, "juliet").set); 124 assertTrue(gop(obj2, "juliet").configurable); 125 assertFalse(gop(obj2, "juliet").enumerable); 126 127 // Two objects with the different getters. 128 obj1 = {}; 129 dp(obj1, "kilo", { get: getter }); 130 obj2 = {}; 131 dp(obj2, "kilo", { get: anotherGetter }); 132 assertEquals(getter, gop(obj1, "kilo").get); 133 assertEquals(anotherGetter, gop(obj2, "kilo").get); 134 assertFalse(%HaveSameMap(obj1, obj2)); 135 136 // Two objects with the same getters and different setters. 137 obj1 = {}; 138 dp(obj1, "lima", { get: getter, set: setter }); 139 obj2 = {}; 140 dp(obj2, "lima", { get: getter, set: anotherSetter }); 141 assertEquals(setter, gop(obj1, "lima").set); 142 assertEquals(anotherSetter, gop(obj2, "lima").set); 143 assertFalse(%HaveSameMap(obj1, obj2)); 144 145 // Even 'undefined' is a kind of getter. 146 obj1 = {}; 147 dp(obj1, "mike", { get: undefined }); 148 assertTrue("mike" in obj1); 149 assertEquals(undefined, gop(obj1, "mike").get); 150 assertEquals(undefined, obj1.__lookupGetter__("mike")); 151 assertEquals(undefined, gop(obj1, "mike").set); 152 assertEquals(undefined, obj1.__lookupSetter__("mike")); 153 154 // Even 'undefined' is a kind of setter. 155 obj1 = {}; 156 dp(obj1, "november", { set: undefined }); 157 assertTrue("november" in obj1); 158 assertEquals(undefined, gop(obj1, "november").get); 159 assertEquals(undefined, obj1.__lookupGetter__("november")); 160 assertEquals(undefined, gop(obj1, "november").set); 161 assertEquals(undefined, obj1.__lookupSetter__("november")); 162 163 // Redefining a data property. 164 obj1 = {}; 165 obj1.oscar = 12345; 166 dp(obj1, "oscar", { set: setter }); 167 assertEquals(setter, gop(obj1, "oscar").set); 168 169 // Re-adding the same getter/attributes pair. 170 obj1 = {}; 171 dp(obj1, "papa", { get: getter, configurable: true }); 172 dp(obj1, "papa", { get: getter, set: setter, configurable: true }); 173 assertEquals(getter, gop(obj1, "papa").get); 174 assertEquals(setter, gop(obj1, "papa").set); 175 assertTrue(gop(obj1, "papa").configurable); 176 assertFalse(gop(obj1, "papa").enumerable); 177 178 // Two objects with the same getter on the prototype chain. 179 obj1 = {}; 180 dp(obj1, "quebec", { get: getter }); 181 obj2 = Object.create(obj1); 182 obj3 = Object.create(obj2); 183 obj4 = Object.create(obj2); 184 assertTrue(%HaveSameMap(obj3, obj4)); 185 186 // Two objects with the same setter on the prototype chain. 187 obj1 = {}; 188 dp(obj1, "romeo", { set: setter }); 189 obj2 = Object.create(obj1); 190 obj3 = Object.create(obj2); 191 obj4 = Object.create(obj2); 192 assertTrue(%HaveSameMap(obj3, obj4)); 193