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