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   var exception = false;
     36   try {
     37     Object.freeze(non_objects[key]);
     38   } catch(e) {
     39     exception = true;
     40     assertTrue(/Object.freeze called on non-object/.test(e));
     41   }
     42   assertTrue(exception);
     43 }
     44 
     45 for (var key in non_objects) {
     46   exception = false;
     47   try {
     48     Object.isFrozen(non_objects[key]);
     49   } catch(e) {
     50     exception = true;
     51     assertTrue(/Object.isFrozen called on non-object/.test(e));
     52   }
     53   assertTrue(exception);
     54 }
     55 
     56 // Test normal data properties.
     57 var obj = { x: 42, z: 'foobar' };
     58 var desc = Object.getOwnPropertyDescriptor(obj, 'x');
     59 assertTrue(desc.writable);
     60 assertTrue(desc.configurable);
     61 assertEquals(42, desc.value);
     62 
     63 desc = Object.getOwnPropertyDescriptor(obj, 'z');
     64 assertTrue(desc.writable);
     65 assertTrue(desc.configurable);
     66 assertEquals('foobar', desc.value);
     67 
     68 assertTrue(Object.isExtensible(obj));
     69 assertFalse(Object.isFrozen(obj));
     70 
     71 Object.freeze(obj);
     72 
     73 // Make sure we are no longer extensible.
     74 assertFalse(Object.isExtensible(obj));
     75 assertTrue(Object.isFrozen(obj));
     76 
     77 obj.foo = 42;
     78 assertEquals(obj.foo, undefined);
     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 obj2.foo = 42;
    122 assertEquals(obj2.foo, undefined);
    123 
    124 
    125 // Test freeze on arrays.
    126 var arr = new Array(42,43);
    127 
    128 desc = Object.getOwnPropertyDescriptor(arr, '0');
    129 assertTrue(desc.configurable);
    130 assertTrue(desc.writable);
    131 assertEquals(42, desc.value);
    132 
    133 desc = Object.getOwnPropertyDescriptor(arr, '1');
    134 assertTrue(desc.configurable);
    135 assertTrue(desc.writable);
    136 assertEquals(43, desc.value);
    137 
    138 assertTrue(Object.isExtensible(arr));
    139 assertFalse(Object.isFrozen(arr));
    140 Object.freeze(arr);
    141 assertTrue(Object.isFrozen(arr));
    142 assertFalse(Object.isExtensible(arr));
    143 
    144 desc = Object.getOwnPropertyDescriptor(arr, '0');
    145 assertFalse(desc.configurable);
    146 assertFalse(desc.writable);
    147 assertEquals(42, desc.value);
    148 
    149 desc = Object.getOwnPropertyDescriptor(arr, '1');
    150 assertFalse(desc.configurable);
    151 assertFalse(desc.writable);
    152 assertEquals(43, desc.value);
    153 
    154 arr[0] = 'foo';
    155 
    156 assertEquals(arr[0], 42);
    157 
    158 
    159 // Test that isFrozen return the correct value even if configurable has been set
    160 // to false on all properties manually and the extensible flag has also been set
    161 // to false manually.
    162 var obj3 = { x: 42, y: 'foo' };
    163 
    164 assertFalse(Object.isFrozen(obj3));
    165 
    166 Object.defineProperty(obj3, 'x', {configurable: false, writable: false});
    167 Object.defineProperty(obj3, 'y', {configurable: false, writable: false});
    168 Object.preventExtensions(obj3);
    169 
    170 assertTrue(Object.isFrozen(obj3));
    171 
    172 
    173 // Make sure that an object that has only non-configurable, but one
    174 // writable property, is not classified as frozen.
    175 var obj4 = {};
    176 Object.defineProperty(obj4, 'x', {configurable: false, writable: true});
    177 Object.defineProperty(obj4, 'y', {configurable: false, writable: false});
    178 Object.preventExtensions(obj4);
    179 
    180 assertFalse(Object.isFrozen(obj4));
    181 
    182 // Make sure that an object that has only non-writable, but one
    183 // configurable property, is not classified as frozen.
    184 var obj5 = {};
    185 Object.defineProperty(obj5, 'x', {configurable: true, writable: false});
    186 Object.defineProperty(obj5, 'y', {configurable: false, writable: false});
    187 Object.preventExtensions(obj5);
    188 
    189 assertFalse(Object.isFrozen(obj5));
    190 
    191 // Make sure that Object.freeze returns the frozen object.
    192 var obj6 = {}
    193 assertTrue(obj6 === Object.freeze(obj6))
    194