OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 15 matching lines...) Expand all Loading... |
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 // Test that redefining the 'prototype' property of a function object | 28 // Test that redefining the 'prototype' property of a function object |
29 // does actually set the internal value and does not screw up any | 29 // does actually set the internal value and does not screw up any |
30 // shadowing between said property and the internal value. | 30 // shadowing between said property and the internal value. |
31 | 31 |
32 var f = function() {}; | 32 var f = function() {}; |
33 | 33 |
34 // Verify that normal assignment of 'prototype' property works properly | 34 // Verify that normal assignment of 'prototype' property works properly |
35 // and updates the internal value. | 35 // and updates the internal value. |
36 var x = { foo: 'bar' }; | 36 var a = { foo: 'bar' }; |
37 f.prototype = x; | 37 f.prototype = a; |
38 assertSame(f.prototype, x); | 38 assertSame(f.prototype, a); |
39 assertSame(f.prototype.foo, 'bar'); | 39 assertSame(f.prototype.foo, 'bar'); |
40 assertSame(new f().foo, 'bar'); | 40 assertSame(new f().foo, 'bar'); |
41 assertSame(Object.getPrototypeOf(new f()), x); | 41 assertSame(Object.getPrototypeOf(new f()), a); |
42 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, x); | 42 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, a); |
| 43 assertTrue(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
43 | 44 |
44 // Verify that 'prototype' behaves like a data property when it comes to | 45 // Verify that 'prototype' behaves like a data property when it comes to |
45 // redefining with Object.defineProperty() and the internal value gets | 46 // redefining with Object.defineProperty() and the internal value gets |
46 // updated. | 47 // updated. |
47 var y = { foo: 'baz' }; | 48 var b = { foo: 'baz' }; |
48 Object.defineProperty(f, 'prototype', { value: y, writable: true }); | 49 Object.defineProperty(f, 'prototype', { value: b, writable: true }); |
49 assertSame(f.prototype, y); | 50 assertSame(f.prototype, b); |
50 assertSame(f.prototype.foo, 'baz'); | 51 assertSame(f.prototype.foo, 'baz'); |
51 assertSame(new f().foo, 'baz'); | 52 assertSame(new f().foo, 'baz'); |
52 assertSame(Object.getPrototypeOf(new f()), y); | 53 assertSame(Object.getPrototypeOf(new f()), b); |
53 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, y); | 54 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, b); |
| 55 assertTrue(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
54 | 56 |
55 // Verify that the previous redefinition didn't screw up callbacks and | 57 // Verify that the previous redefinition didn't screw up callbacks and |
56 // the internal value still gets updated. | 58 // the internal value still gets updated. |
57 var z = { foo: 'other' }; | 59 var c = { foo: 'other' }; |
58 f.prototype = z; | 60 f.prototype = c; |
59 assertSame(f.prototype, z); | 61 assertSame(f.prototype, c); |
60 assertSame(f.prototype.foo, 'other'); | 62 assertSame(f.prototype.foo, 'other'); |
61 assertSame(new f().foo, 'other'); | 63 assertSame(new f().foo, 'other'); |
62 assertSame(Object.getPrototypeOf(new f()), z); | 64 assertSame(Object.getPrototypeOf(new f()), c); |
63 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, z); | 65 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, c); |
| 66 assertTrue(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
| 67 |
| 68 // Verify that 'prototype' can be redefined to contain a different value |
| 69 // and have a different writability attribute at the same time. |
| 70 var d = { foo: 'final' }; |
| 71 Object.defineProperty(f, 'prototype', { value: d, writable: false }); |
| 72 assertSame(f.prototype, d); |
| 73 assertSame(f.prototype.foo, 'final'); |
| 74 assertSame(new f().foo, 'final'); |
| 75 assertSame(Object.getPrototypeOf(new f()), d); |
| 76 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, d); |
| 77 assertFalse(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
| 78 |
| 79 // Verify that non-writability of redefined 'prototype' is respected. |
| 80 assertThrows("'use strict'; f.prototype = {}"); |
| 81 assertThrows("Object.defineProperty(f, 'prototype', { value: {} })"); |
64 | 82 |
65 // Verify that non-writability of other properties is respected. | 83 // Verify that non-writability of other properties is respected. |
66 assertThrows("Object.defineProperty(f, 'name', { value: {} })"); | 84 assertThrows("Object.defineProperty(f, 'name', { value: {} })"); |
67 assertThrows("Object.defineProperty(f, 'length', { value: {} })"); | 85 assertThrows("Object.defineProperty(f, 'length', { value: {} })"); |
68 assertThrows("Object.defineProperty(f, 'caller', { value: {} })"); | 86 assertThrows("Object.defineProperty(f, 'caller', { value: {} })"); |
69 assertThrows("Object.defineProperty(f, 'arguments', { value: {} })"); | 87 assertThrows("Object.defineProperty(f, 'arguments', { value: {} })"); |
OLD | NEW |