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 
     31 var obj1 = {};
     32 // Extensible defaults to true.
     33 assertTrue(Object.isExtensible(obj1));
     34 Object.preventExtensions(obj1);
     35 
     36 // Make sure the is_extensible flag is set.
     37 assertFalse(Object.isExtensible(obj1));
     38 obj1.x = 42;
     39 assertEquals(undefined, obj1.x);
     40 
     41 // Try adding a new element.
     42 obj1[1] = 42;
     43 assertEquals(undefined, obj1[1]);
     44 
     45 
     46 // Try when the object has an existing property.
     47 var obj2 = {};
     48 assertTrue(Object.isExtensible(obj2));
     49 obj2.x = 42;
     50 assertEquals(42, obj2.x);
     51 assertTrue(Object.isExtensible(obj2));
     52 
     53 Object.preventExtensions(obj2);
     54 assertEquals(42, obj2.x);
     55 
     56 obj2.y = 42;
     57 // obj2.y should still be undefined.
     58 assertEquals(undefined, obj2.y);
     59 // Make sure we can still write values to obj.x.
     60 obj2.x = 43;
     61 assertEquals(43, obj2.x)
     62 
     63 obj2.y = new function() { return 42; };
     64 // obj2.y should still be undefined.
     65 assertEquals(undefined, obj2.y);
     66 assertEquals(43, obj2.x)
     67 
     68 try {
     69   Object.defineProperty(obj2, "y", {value: 42});
     70 } catch (e) {
     71   assertTrue(/object is not extensible/.test(e));
     72 }
     73 
     74 // obj2.y should still be undefined.
     75 assertEquals(undefined, obj2.y);
     76 assertEquals(43, obj2.x);
     77 
     78 obj2[1] = 42;
     79 assertEquals(undefined, obj2[1]);
     80 
     81 var arr = new Array();
     82 arr[1] = 10;
     83 
     84 Object.preventExtensions(arr);
     85 
     86 arr[2] = 42;
     87 assertEquals(10, arr[1]);
     88 
     89 // We should still be able to change exiting elements.
     90 arr[1]= 42;
     91 assertEquals(42, arr[1]);
     92 
     93 
     94 // Test the the extensible flag is not inherited.
     95 var parent = {};
     96 parent.x = 42;
     97 Object.preventExtensions(parent);
     98 
     99 var child = Object.create(parent);
    100 
    101 // We should be able to add new properties to the child object.
    102 child.y = 42;
    103 
    104 // This should have no influence on the parent class.
    105 parent.y = 29;
    106 
    107 
    108 // Test that attributes on functions are also handled correctly.
    109 function foo() {
    110   return 42;
    111 }
    112 
    113 Object.preventExtensions(foo);
    114 
    115 foo.x = 29;
    116 assertEquals(undefined, foo.x);
    117 
    118 // when Object.isExtensible(o) === false
    119 // assignment should return right hand side value
    120 var o = Object.preventExtensions({});
    121 var v = o.v = 50;
    122 assertEquals(undefined, o.v);
    123 assertEquals(50, v);
    124 
    125 // test same behavior as above, but for integer properties
    126 var n = o[0] = 100;
    127 assertEquals(undefined, o[0]);
    128 assertEquals(100, n);
    129