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. 36 var x = { foo: 'bar' }; 37 f.prototype = x; 38 assertSame(f.prototype, x); 39 assertSame(f.prototype.foo, 'bar'); 40 assertSame(new f().foo, 'bar'); 41 assertSame(Object.getPrototypeOf(new f()), x); 42 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, x); 43 44 // Verify that 'prototype' behaves like a data property when it comes to 45 // redefining with Object.defineProperty() and the internal value gets 46 // updated. 47 var y = { foo: 'baz' }; 48 Object.defineProperty(f, 'prototype', { value: y, writable: true }); 49 assertSame(f.prototype, y); 50 assertSame(f.prototype.foo, 'baz'); 51 assertSame(new f().foo, 'baz'); 52 assertSame(Object.getPrototypeOf(new f()), y); 53 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, y); 54 55 // Verify that the previous redefinition didn't screw up callbacks and 56 // the internal value still gets updated. 57 var z = { foo: 'other' }; 58 f.prototype = z; 59 assertSame(f.prototype, z); 60 assertSame(f.prototype.foo, 'other'); 61 assertSame(new f().foo, 'other'); 62 assertSame(Object.getPrototypeOf(new f()), z); 63 assertSame(Object.getOwnPropertyDescriptor(f, 'prototype').value, z); 64 65 // Verify that non-writability of other properties is respected. 66 assertThrows("Object.defineProperty(f, 'name', { value: {} })"); 67 assertThrows("Object.defineProperty(f, 'length', { value: {} })"); 68 assertThrows("Object.defineProperty(f, 'caller', { value: {} })"); 69 assertThrows("Object.defineProperty(f, 'arguments', { value: {} })"); 70