Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 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.freeze and Object.isFrozen 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.freeze(non_objects[key]); |
| 37 | assertUnreachable(); |
| 38 | } catch(e) { |
| 39 | assertTrue(/Object.freeze called on non-object/.test(e)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | for (var key in non_objects) { |
| 44 | try { |
| 45 | Object.isFrozen(non_objects[key]); |
| 46 | assertUnreachable(); |
| 47 | } catch(e) { |
| 48 | assertTrue(/Object.isFrozen 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.isFrozen(obj)); |
| 66 | |
| 67 | Object.freeze(obj); |
| 68 | |
| 69 | // Make sure we are no longer extensible. |
| 70 | assertFalse(Object.isExtensible(obj)); |
| 71 | assertTrue(Object.isFrozen(obj)); |
| 72 | |
| 73 | try { |
| 74 | obj.foo = 42; |
| 75 | assertUnreachable(); |
| 76 | } catch(e) { |
| 77 | assertTrue(/object is not extensible/.test(e)); |
| 78 | } |
| 79 | |
| 80 | desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
| 81 | assertFalse(desc.writable); |
| 82 | assertFalse(desc.configurable); |
| 83 | assertEquals(42, desc.value); |
| 84 | |
| 85 | desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
| 86 | assertFalse(desc.writable); |
| 87 | assertFalse(desc.configurable); |
| 88 | assertEquals("foobar", desc.value); |
| 89 | |
| 90 | // Make sure that even if we try overwrite a value that is not writable, it is |
| 91 | // not changed. |
| 92 | obj.x = "tete"; |
| 93 | assertEquals(42, obj.x); |
| 94 | obj.x = { get: function() {return 43}, set: function() {} }; |
| 95 | assertEquals(42, obj.x); |
| 96 | |
| 97 | // Test on accessors. |
| 98 | var obj2 = {}; |
| 99 | function get() { return 43; }; |
| 100 | function set() {}; |
| 101 | Object.defineProperty(obj2, 'x', { get: get, set: set, configurable: true }); |
| 102 | |
| 103 | desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 104 | assertTrue(desc.configurable); |
| 105 | assertEquals(undefined, desc.value); |
| 106 | assertEquals(set, desc.set); |
| 107 | assertEquals(get, desc.get); |
| 108 | |
| 109 | assertTrue(Object.isExtensible(obj2)); |
| 110 | assertFalse(Object.isFrozen(obj2)); |
| 111 | Object.freeze(obj2); |
| 112 | assertTrue(Object.isFrozen(obj2)); |
| 113 | assertFalse(Object.isExtensible(obj2)); |
| 114 | |
| 115 | desc = Object.getOwnPropertyDescriptor(obj2, 'x'); |
| 116 | assertFalse(desc.configurable); |
| 117 | assertEquals(undefined, desc.value); |
| 118 | assertEquals(set, desc.set); |
| 119 | assertEquals(get, desc.get); |
| 120 | |
| 121 | try { |
| 122 | obj2.foo = 42; |
| 123 | assertUnreachable(); |
| 124 | } catch(e) { |
| 125 | assertTrue(/object is not extensible/.test(e)); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | // Test freeze on arrays. |
| 130 | var arr = new Array(42,43); |
| 131 | |
| 132 | desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 133 | assertTrue(desc.configurable); |
| 134 | assertTrue(desc.writable); |
| 135 | assertEquals(42, desc.value); |
| 136 | |
| 137 | desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 138 | assertTrue(desc.configurable); |
| 139 | assertTrue(desc.writable); |
| 140 | assertEquals(43, desc.value); |
| 141 | |
| 142 | assertTrue(Object.isExtensible(arr)); |
| 143 | assertFalse(Object.isFrozen(arr)); |
| 144 | Object.freeze(arr); |
| 145 | assertTrue(Object.isFrozen(arr)); |
| 146 | assertFalse(Object.isExtensible(arr)); |
| 147 | |
| 148 | desc = Object.getOwnPropertyDescriptor(arr, '0'); |
| 149 | assertFalse(desc.configurable); |
| 150 | assertFalse(desc.writable); |
| 151 | assertEquals(42, desc.value); |
| 152 | |
| 153 | desc = Object.getOwnPropertyDescriptor(arr, '1'); |
| 154 | assertFalse(desc.configurable); |
| 155 | assertFalse(desc.writable); |
| 156 | assertEquals(43, desc.value); |
| 157 | |
| 158 | arr[0] = 'foo'; |
| 159 | |
| 160 | assertEquals(arr[0], 42); |
| 161 | |
| 162 | |
| 163 | // Test that isFrozen return the correct value even if configurable has been set |
| 164 | // to false on all properties manually and the extensible flag has also been set |
| 165 | // to false manually. |
| 166 | var obj3 = { x: 42, y: 'foo' }; |
| 167 | |
| 168 | assertFalse(Object.isFrozen(obj3)); |
| 169 | |
| 170 | Object.defineProperty(obj3, 'x', {configurable: false, writable: false}); |
| 171 | Object.defineProperty(obj3, 'y', {configurable: false, writable: false}); |
| 172 | Object.preventExtensions(obj3); |
| 173 | |
| 174 | assertTrue(Object.isFrozen(obj3)); |
| 175 | |
| 176 | |
| 177 | // Make sure that an object that has only non-configurable, but one |
| 178 | // writable property, is not classified as frozen. |
| 179 | var obj4 = {}; |
| 180 | Object.defineProperty(obj4, 'x', {configurable: false, writable: true}); |
| 181 | Object.defineProperty(obj4, 'y', {configurable: false, writable: false}); |
| 182 | Object.preventExtensions(obj4); |
| 183 | |
| 184 | assertFalse(Object.isFrozen(obj4)); |
| 185 | |
| 186 | // Make sure that an object that has only non-writable, but one |
| 187 | // configurable property, is not classified as frozen. |
| 188 | var obj5 = {}; |
| 189 | Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); |
| 190 | Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); |
| 191 | Object.preventExtensions(obj5); |
| 192 | |
| 193 | assertFalse(Object.isFrozen(obj5)); |