OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 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 // Tests the Object.seal and Object.isSealed methods - ES 15.2.3.9 and |
| 29 // ES 15.2.3.12 |
| 30 |
| 31 |
| 32 // Test that we throw an error if an object is not passed as argument. |
| 33 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); |
| 34 for (var key in non_objects) { |
| 35 try { |
| 36 Object.seal(non_objects[key]); |
| 37 assertUnreachable(); |
| 38 } catch(e) { |
| 39 assertTrue(/Object.seal called on non-object/.test(e)); |
| 40 } |
| 41 } |
| 42 |
| 43 for (var key in non_objects) { |
| 44 try { |
| 45 Object.isSealed(non_objects[key]); |
| 46 assertUnreachable(); |
| 47 } catch(e) { |
| 48 assertTrue(/Object.isSealed called on non-object/.test(e)); |
| 49 } |
| 50 } |
| 51 |
| 52 // Test normal data properties. |
| 53 var obj = { x: 42, z: 'foobar' }; |
| 54 var desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 55 assertTrue(desc.writable); |
| 56 assertTrue(desc.configurable); |
| 57 assertEquals(42, desc.value); |
| 58 |
| 59 desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
| 60 assertTrue(desc.writable); |
| 61 assertTrue(desc.configurable); |
| 62 assertEquals('foobar', desc.value); |
| 63 |
| 64 assertTrue(Object.isExtensible(obj)); |
| 65 assertFalse(Object.isSealed(obj)); |
| 66 |
| 67 Object.seal(obj); |
| 68 |
| 69 // Make sure we are no longer extensible. |
| 70 assertFalse(Object.isExtensible(obj)); |
| 71 assertTrue(Object.isSealed(obj)); |
| 72 |
| 73 // We should not be frozen, since we are still able to |
| 74 // update values. |
| 75 assertFalse(Object.isFrozen(obj)); |
| 76 |
| 77 // We should not allow new properties to be added. |
| 78 try { |
| 79 obj.foo = 42; |
| 80 assertUnreachable(); |
| 81 } catch(e) { |
| 82 assertTrue(/object is not extensible/.test(e)); |
| 83 } |
| 84 |
| 85 desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 86 assertTrue(desc.writable); |
| 87 assertFalse(desc.configurable); |
| 88 assertEquals(42, desc.value); |
| 89 |
| 90 desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
| 91 assertTrue(desc.writable); |
| 92 assertFalse(desc.configurable); |
| 93 assertEquals("foobar", desc.value); |
| 94 |
| 95 // Since writable is not affected by seal we should still be able to |
| 96 // update the values. |
| 97 obj.x = "43"; |
| 98 assertEquals(43, obj.x); |
| 99 |
| 100 // Test on accessors. |
| 101 var obj2 = {}; |
| 102 function get() { return 43; }; |
| 103 function set() {}; |
| 104 Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true }); |
| 105 |
| 106 desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 107 assertTrue(desc.configurable); |
| 108 assertEquals(undefined, desc.value); |
| 109 assertEquals(set, desc.set); |
| 110 assertEquals(get, desc.get); |
| 111 |
| 112 assertTrue(Object.isExtensible(obj2)); |
| 113 assertFalse(Object.isSealed(obj2)); |
| 114 Object.seal(obj2); |
| 115 |
| 116 // Since this is an accessor property the object is now effectively both |
| 117 // sealed and frozen (accessors has no writable attribute). |
| 118 assertTrue(Object.isFrozen(obj2)); |
| 119 assertFalse(Object.isExtensible(obj2)); |
| 120 assertTrue(Object.isSealed(obj2)); |
| 121 |
| 122 desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 123 assertFalse(desc.configurable); |
| 124 assertEquals(undefined, desc.value); |
| 125 assertEquals(set, desc.set); |
| 126 assertEquals(get, desc.get); |
| 127 |
| 128 try { |
| 129 obj2.foo = 42; |
| 130 assertUnreachable(); |
| 131 } catch(e) { |
| 132 assertTrue(/object is not extensible/.test(e)); |
| 133 } |
| 134 |
| 135 |
| 136 // Test seal on arrays. |
| 137 var arr = new Array(42,43); |
| 138 |
| 139 desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 140 assertTrue(desc.configurable); |
| 141 assertTrue(desc.writable); |
| 142 assertEquals(42, desc.value); |
| 143 |
| 144 desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 145 assertTrue(desc.configurable); |
| 146 assertTrue(desc.writable); |
| 147 assertEquals(43, desc.value); |
| 148 |
| 149 assertTrue(Object.isExtensible(arr)); |
| 150 assertFalse(Object.isSealed(arr)); |
| 151 Object.seal(arr); |
| 152 assertTrue(Object.isSealed(arr)); |
| 153 assertFalse(Object.isExtensible(arr)); |
| 154 // Since the values in the array is still writable this object |
| 155 // is not frozen. |
| 156 assertFalse(Object.isFrozen(arr)); |
| 157 |
| 158 desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 159 assertFalse(desc.configurable); |
| 160 assertTrue(desc.writable); |
| 161 assertEquals(42, desc.value); |
| 162 |
| 163 desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 164 assertFalse(desc.configurable); |
| 165 assertTrue(desc.writable); |
| 166 assertEquals(43, desc.value); |
| 167 |
| 168 arr[0] = 'foo'; |
| 169 |
| 170 // We should be able to overwrite the existing value. |
| 171 assertEquals('foo', arr[0]); |
| 172 |
| 173 |
| 174 // Test that isSealed returns the correct value even if configurable |
| 175 // has been set to false on all properties manually and the extensible |
| 176 // flag has also been set to false manually. |
| 177 var obj3 = { x: 42, y: 'foo' }; |
| 178 |
| 179 assertFalse(Object.isFrozen(obj3)); |
| 180 |
| 181 Object.defineProperty(obj3, 'x', {configurable: false, writable: true}); |
| 182 Object.defineProperty(obj3, 'y', {configurable: false, writable: false}); |
| 183 Object.preventExtensions(obj3); |
| 184 |
| 185 assertTrue(Object.isSealed(obj3)); |
| 186 |
| 187 |
| 188 // Make sure that an object that has a configurable property |
| 189 // is not classified as sealed. |
| 190 var obj4 = {}; |
| 191 Object.defineProperty(obj4, 'x', {configurable: true, writable: false}); |
| 192 Object.defineProperty(obj4, 'y', {configurable: false, writable: false}); |
| 193 Object.preventExtensions(obj4); |
| 194 |
| 195 assertFalse(Object.isSealed(obj4)); |
OLD | NEW |