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