OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Tests the Object.freeze and Object.isFrozen methods - ES 15.2.3.9 and | 28 // Tests the Object.freeze and Object.isFrozen methods - ES 15.2.3.9 and |
29 // ES 15.2.3.12 | 29 // ES 15.2.3.12 |
30 | 30 |
31 // Flags: --allow-natives-syntax | |
32 | 31 |
33 // Test that we throw an error if an object is not passed as argument. | 32 // Test that we throw an error if an object is not passed as argument. |
34 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); | 33 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); |
35 for (var key in non_objects) { | 34 for (var key in non_objects) { |
36 var exception = false; | 35 var exception = false; |
37 try { | 36 try { |
38 Object.freeze(non_objects[key]); | 37 Object.freeze(non_objects[key]); |
39 } catch(e) { | 38 } catch(e) { |
40 exception = true; | 39 exception = true; |
41 assertTrue(/Object.freeze called on non-object/.test(e)); | 40 assertTrue(/Object.freeze called on non-object/.test(e)); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 var obj5 = {}; | 184 var obj5 = {}; |
186 Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); | 185 Object.defineProperty(obj5, 'x', {configurable: true, writable: false}); |
187 Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); | 186 Object.defineProperty(obj5, 'y', {configurable: false, writable: false}); |
188 Object.preventExtensions(obj5); | 187 Object.preventExtensions(obj5); |
189 | 188 |
190 assertFalse(Object.isFrozen(obj5)); | 189 assertFalse(Object.isFrozen(obj5)); |
191 | 190 |
192 // Make sure that Object.freeze returns the frozen object. | 191 // Make sure that Object.freeze returns the frozen object. |
193 var obj6 = {} | 192 var obj6 = {} |
194 assertTrue(obj6 === Object.freeze(obj6)) | 193 assertTrue(obj6 === Object.freeze(obj6)) |
195 | |
196 // Test that the enumerable attribute is unperturbed by freezing. | |
197 obj = { x: 42, y: 'foo' }; | |
198 Object.defineProperty(obj, 'y', {enumerable: false}); | |
199 Object.freeze(obj); | |
200 desc = Object.getOwnPropertyDescriptor(obj, 'x'); | |
201 assertTrue(desc.enumerable); | |
202 desc = Object.getOwnPropertyDescriptor(obj, 'y'); | |
203 assertFalse(desc.enumerable); | |
204 | |
205 // Fast properties should remain fast | |
206 obj = { x: 42, y: 'foo' }; | |
207 assertTrue(%HasFastProperties(obj)); | |
208 Object.freeze(obj); | |
209 assertTrue(%HasFastProperties(obj)); | |
210 | |
211 // Frozen objects should share maps where possible | |
212 obj = { prop1: 1, prop2: 2 }; | |
213 obj2 = { prop1: 3, prop2: 4 }; | |
214 assertTrue(%HaveSameMap(obj, obj2)); | |
215 Object.freeze(obj); | |
216 Object.freeze(obj2); | |
217 assertTrue(%HaveSameMap(obj, obj2)); | |
218 | |
219 // Frozen objects should share maps even when they have elements | |
220 obj = { prop1: 1, prop2: 2, 75: 'foo' }; | |
221 obj2 = { prop1: 3, prop2: 4, 150: 'bar' }; | |
222 assertTrue(%HaveSameMap(obj, obj2)); | |
223 Object.freeze(obj); | |
224 Object.freeze(obj2); | |
225 assertTrue(%HaveSameMap(obj, obj2)); | |
226 | |
227 // Setting elements after freezing should not be allowed | |
228 obj = { prop: 'thing' }; | |
229 Object.freeze(obj); | |
230 obj[0] = 'hello'; | |
231 assertFalse(obj.hasOwnProperty(0)); | |
232 | |
233 // Freezing an object in dictionary mode should work | |
234 obj = { }; | |
235 for (var i = 0; i < 100; ++i) { | |
236 obj['x' + i] = i; | |
237 } | |
238 assertFalse(%HasFastProperties(obj)); | |
239 Object.freeze(obj); | |
240 assertFalse(%HasFastProperties(obj)); | |
241 assertTrue(Object.isFrozen(obj)); | |
242 assertFalse(Object.isExtensible(obj)); | |
243 for (var i = 0; i < 100; ++i) { | |
244 desc = Object.getOwnPropertyDescriptor(obj, 'x' + i); | |
245 assertFalse(desc.writable); | |
246 assertFalse(desc.configurable); | |
247 } | |
248 | |
249 // Freezing arguments should work | |
250 var func = function(arg) { | |
251 Object.freeze(arguments); | |
252 assertTrue(Object.isFrozen(arguments)); | |
253 }; | |
254 func('hello', 'world'); | |
255 func('goodbye', 'world'); | |
256 | |
257 // Freezing sparse arrays | |
258 var sparseArr = [0, 1]; | |
259 sparseArr[10000] = 10000; | |
260 Object.freeze(sparseArr); | |
261 assertTrue(Object.isFrozen(sparseArr)); | |
OLD | NEW |