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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
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 19.1.2.5 and |
29 // ES 15.2.3.12 | 29 // ES 19.1.2.12 |
30 | 30 |
31 // Flags: --allow-natives-syntax | 31 // Flags: --allow-natives-syntax |
32 | 32 |
33 // Test that we throw an error if an object is not passed as argument. | 33 // Test that we return obj if non-object is passed as argument |
34 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43); | 34 var non_objects = new Array(undefined, null, 1, -1, 0, 42.43, Symbol("test")); |
35 for (var key in non_objects) { | 35 for (var key in non_objects) { |
36 var exception = false; | 36 assertSame(non_objects[key], Object.freeze(non_objects[key])); |
37 try { | |
38 Object.freeze(non_objects[key]); | |
39 } catch(e) { | |
40 exception = true; | |
41 assertTrue(/Object.freeze called on non-object/.test(e)); | |
42 } | |
43 assertTrue(exception); | |
44 } | 37 } |
45 | 38 |
39 // Test that isFrozen always returns true for non-objects | |
46 for (var key in non_objects) { | 40 for (var key in non_objects) { |
47 exception = false; | 41 assertTrue(Object.isFrozen(non_objects[key])); |
48 try { | |
49 Object.isFrozen(non_objects[key]); | |
50 } catch(e) { | |
51 exception = true; | |
52 assertTrue(/Object.isFrozen called on non-object/.test(e)); | |
53 } | |
54 assertTrue(exception); | |
55 } | 42 } |
56 | 43 |
57 // Test normal data properties. | 44 // Test normal data properties. |
58 var obj = { x: 42, z: 'foobar' }; | 45 var obj = { x: 42, z: 'foobar' }; |
59 var desc = Object.getOwnPropertyDescriptor(obj, 'x'); | 46 var desc = Object.getOwnPropertyDescriptor(obj, 'x'); |
60 assertTrue(desc.writable); | 47 assertTrue(desc.writable); |
61 assertTrue(desc.configurable); | 48 assertTrue(desc.configurable); |
62 assertEquals(42, desc.value); | 49 assertEquals(42, desc.value); |
63 | 50 |
64 desc = Object.getOwnPropertyDescriptor(obj, 'z'); | 51 desc = Object.getOwnPropertyDescriptor(obj, 'z'); |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 assertThrows(function() { obj.unshift(); }, TypeError); | 328 assertThrows(function() { obj.unshift(); }, TypeError); |
342 | 329 |
343 // Sealing and then Freezing should do the right thing. | 330 // Sealing and then Freezing should do the right thing. |
344 var obj = { foo: 'bar', 0: 'element' }; | 331 var obj = { foo: 'bar', 0: 'element' }; |
345 Object.seal(obj); | 332 Object.seal(obj); |
346 assertTrue(Object.isSealed(obj)); | 333 assertTrue(Object.isSealed(obj)); |
347 assertFalse(Object.isFrozen(obj)); | 334 assertFalse(Object.isFrozen(obj)); |
348 Object.freeze(obj); | 335 Object.freeze(obj); |
349 assertTrue(Object.isSealed(obj)); | 336 assertTrue(Object.isSealed(obj)); |
350 assertTrue(Object.isFrozen(obj)); | 337 assertTrue(Object.isFrozen(obj)); |
338 | |
339 | |
340 (function propertiesOfFrozenObjectNotFrozen() { | |
arv (Not doing code reviews)
2015/03/19 19:38:08
How are these tests related to this CL?
| |
341 function Frozen() {} | |
342 Object.freeze(Frozen); | |
343 assertDoesNotThrow(function() { return new Frozen(); }); | |
344 Frozen.prototype.prototypeExists = true; | |
345 assertTrue((new Frozen()).prototypeExists); | |
346 })(); | |
347 | |
348 | |
349 (function frozenPrototypePreventsPUT() { | |
350 // A read-only property on the prototype should prevent a [[Put]] . | |
351 function Constructor() {} | |
352 Constructor.prototype.foo = 1; | |
353 Object.freeze(Constructor.prototype); | |
354 var obj = new Constructor(); | |
355 obj.foo = 2; | |
356 assertSame(1, obj.foo); | |
357 })(); | |
358 | |
359 | |
360 (function frozenFunctionSloppy() { | |
361 // Check that freezing a function works correctly. | |
362 var func = Object.freeze(function foo(){}); | |
363 assertTrue(Object.isFrozen(func)); | |
364 func.prototype = 42; | |
365 assertFalse(func.prototype === 42); | |
366 assertFalse(Object.getOwnPropertyDescriptor(func, "prototype").writable); | |
367 })(); | |
368 | |
369 | |
370 (function frozenFunctionStrict() { | |
371 // Check that freezing a strict function works correctly. | |
372 var func = Object.freeze(function foo(){ "use strict"; }); | |
373 assertTrue(Object.isFrozen(func)); | |
374 func.prototype = 42; | |
375 assertFalse(func.prototype === 42); | |
376 assertFalse(Object.getOwnPropertyDescriptor(func, "prototype").writable); | |
377 })(); | |
378 | |
379 | |
380 (function frozenArrayObject() { | |
381 // Check that freezing array objects works correctly. | |
382 var array = Object.freeze([0,1,2]); | |
383 assertTrue(Object.isFrozen(array)); | |
384 array[0] = 3; | |
385 assertEquals(0, array[0]); | |
386 assertFalse(Object.getOwnPropertyDescriptor(array, "length").writable); | |
387 })(); | |
388 | |
389 | |
390 (function frozenArgumentsObject() { | |
391 // Check that freezing arguments objects works correctly. | |
392 var args = Object.freeze((function(){ return arguments; })(0,1,2)); | |
393 assertTrue(Object.isFrozen(args)); | |
394 args[0] = 3; | |
395 assertEquals(0, args[0]); | |
396 assertFalse(Object.getOwnPropertyDescriptor(args, "length").writable); | |
397 assertFalse(Object.getOwnPropertyDescriptor(args, "callee").writable); | |
398 })(); | |
OLD | NEW |