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.preventExtensions method - ES 15.2.3.10
     29 
     30 // Flags: --allow-natives-syntax
     31 
     32 
     33 var obj1 = {};
     34 // Extensible defaults to true.
     35 assertTrue(Object.isExtensible(obj1));
     36 Object.preventExtensions(obj1);
     37 
     38 // Make sure the is_extensible flag is set.
     39 assertFalse(Object.isExtensible(obj1));
     40 obj1.x = 42;
     41 assertEquals(undefined, obj1.x);
     42 
     43 // Try adding a new element.
     44 obj1[1] = 42;
     45 assertEquals(undefined, obj1[1]);
     46 
     47 
     48 // Try when the object has an existing property.
     49 var obj2 = {};
     50 assertTrue(Object.isExtensible(obj2));
     51 obj2.x = 42;
     52 assertEquals(42, obj2.x);
     53 assertTrue(Object.isExtensible(obj2));
     54 
     55 Object.preventExtensions(obj2);
     56 assertEquals(42, obj2.x);
     57 
     58 obj2.y = 42;
     59 // obj2.y should still be undefined.
     60 assertEquals(undefined, obj2.y);
     61 // Make sure we can still write values to obj.x.
     62 obj2.x = 43;
     63 assertEquals(43, obj2.x)
     64 
     65 obj2.y = new function() { return 42; };
     66 // obj2.y should still be undefined.
     67 assertEquals(undefined, obj2.y);
     68 assertEquals(43, obj2.x)
     69 
     70 try {
     71   Object.defineProperty(obj2, "y", {value: 42});
     72 } catch (e) {
     73   assertTrue(/object is not extensible/.test(e));
     74 }
     75 
     76 // obj2.y should still be undefined.
     77 assertEquals(undefined, obj2.y);
     78 assertEquals(43, obj2.x);
     79 
     80 obj2[1] = 42;
     81 assertEquals(undefined, obj2[1]);
     82 
     83 var arr = new Array();
     84 arr[1] = 10;
     85 
     86 Object.preventExtensions(arr);
     87 
     88 arr[2] = 42;
     89 assertEquals(10, arr[1]);
     90 
     91 // We should still be able to change existing elements.
     92 arr[1]= 42;
     93 assertEquals(42, arr[1]);
     94 
     95 
     96 // Test the the extensible flag is not inherited.
     97 var parent = {};
     98 parent.x = 42;
     99 Object.preventExtensions(parent);
    100 
    101 var child = Object.create(parent);
    102 
    103 // We should be able to add new properties to the child object.
    104 child.y = 42;
    105 
    106 // This should have no influence on the parent class.
    107 parent.y = 29;
    108 
    109 
    110 // Test that attributes on functions are also handled correctly.
    111 function foo() {
    112   return 42;
    113 }
    114 
    115 Object.preventExtensions(foo);
    116 
    117 foo.x = 29;
    118 assertEquals(undefined, foo.x);
    119 
    120 // when Object.isExtensible(o) === false
    121 // assignment should return right hand side value
    122 var o = Object.preventExtensions({});
    123 var v = o.v = 50;
    124 assertEquals(undefined, o.v);
    125 assertEquals(50, v);
    126 
    127 // test same behavior as above, but for integer properties
    128 var n = o[0] = 100;
    129 assertEquals(undefined, o[0]);
    130 assertEquals(100, n);
    131 
    132 // Fast properties should remain fast
    133 obj = { x: 42, y: 'foo' };
    134 assertTrue(%HasFastProperties(obj));
    135 Object.preventExtensions(obj);
    136 assertFalse(Object.isExtensible(obj));
    137 assertFalse(Object.isSealed(obj));
    138 assertTrue(%HasFastProperties(obj));
    139 
    140 // Non-extensible objects should share maps where possible
    141 obj = { prop1: 1, prop2: 2 };
    142 obj2 = { prop1: 3, prop2: 4 };
    143 assertTrue(%HaveSameMap(obj, obj2));
    144 Object.preventExtensions(obj);
    145 Object.preventExtensions(obj2);
    146 assertFalse(Object.isExtensible(obj));
    147 assertFalse(Object.isExtensible(obj2));
    148 assertFalse(Object.isSealed(obj));
    149 assertFalse(Object.isSealed(obj2));
    150 assertTrue(%HaveSameMap(obj, obj2));
    151 
    152 // Non-extensible objects should share maps even when they have elements
    153 obj = { prop1: 1, prop2: 2, 75: 'foo' };
    154 obj2 = { prop1: 3, prop2: 4, 150: 'bar' };
    155 assertTrue(%HaveSameMap(obj, obj2));
    156 Object.preventExtensions(obj);
    157 Object.preventExtensions(obj2);
    158 assertFalse(Object.isExtensible(obj));
    159 assertFalse(Object.isExtensible(obj2));
    160 assertFalse(Object.isSealed(obj));
    161 assertFalse(Object.isSealed(obj2));
    162 assertTrue(%HaveSameMap(obj, obj2));
    163