Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2011 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 | // Test that redefining the 'prototype' property of a function object |
| 29 | // does actually set the internal value and does not screw up any |
| 30 | // shadowing between said property and the internal value. |
| 31 | |
| 32 | var f = function() {}; |
| 33 | |
| 34 | // Verify that normal assignment of 'prototype' property works properly |
| 35 | // and updates the internal value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 36 | var a = { foo: 'bar' }; |
| 37 | f.prototype = a; |
| 38 | assertSame(f.prototype, a); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 39 | assertSame(f.prototype.foo, 'bar'); |
| 40 | assertSame(new f().foo, 'bar'); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | assertSame(Object.getPrototypeOf(new f()), a); |
| 42 | assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, a); |
| 43 | assertTrue(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 44 | |
| 45 | // Verify that 'prototype' behaves like a data property when it comes to |
| 46 | // redefining with Object.defineProperty() and the internal value gets |
| 47 | // updated. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | var b = { foo: 'baz' }; |
| 49 | Object.defineProperty(f, 'prototype', { value: b, writable: true }); |
| 50 | assertSame(f.prototype, b); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 51 | assertSame(f.prototype.foo, 'baz'); |
| 52 | assertSame(new f().foo, 'baz'); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 53 | assertSame(Object.getPrototypeOf(new f()), b); |
| 54 | assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, b); |
| 55 | assertTrue(Object.getOwnPropertyDescriptor(f, 'prototype').writable); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 56 | |
| 57 | // Verify that the previous redefinition didn't screw up callbacks and |
| 58 | // the internal value still gets updated. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | var c = { foo: 'other' }; |
| 60 | f.prototype = c; |
| 61 | assertSame(f.prototype, c); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 62 | assertSame(f.prototype.foo, 'other'); |
| 63 | assertSame(new f().foo, 'other'); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 64 | assertSame(Object.getPrototypeOf(new f()), c); |
| 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: {} })"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 82 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | // Verify that non-configurability of other properties is respected, but |
| 84 | // non-writability is ignored by Object.defineProperty(). |
| 85 | // name and length are configurable in ES6 |
| 86 | Object.defineProperty(f, 'name', { value: {} }); |
| 87 | Object.defineProperty(f, 'length', { value: {} }); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 88 | assertThrows("Object.defineProperty(f, 'caller', { value: {} })"); |
| 89 | assertThrows("Object.defineProperty(f, 'arguments', { value: {} })"); |