1// Copyright 2008 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 we can set function prototypes to non-object values. The 29// prototype used for instances in that case should be the initial 30// object prototype. ECMA-262 13.2.2. 31function TestNonObjectPrototype(value) { 32 function F() {}; 33 F.prototype = value; 34 var f = new F(); 35 assertEquals(value, F.prototype); 36 assertEquals(Object.prototype, f.__proto__); 37 // Test that map transitions don't break anything. 38 F.property = "value"; 39 assertEquals(value, F.prototype); 40} 41 42var values = [123, "asdf", true]; 43 44values.forEach(TestNonObjectPrototype); 45 46 47// Test moving between non-object and object values. 48function F() {}; 49var f = new F(); 50assertEquals(f.__proto__, F.prototype); 51F.prototype = 42; 52f = new F(); 53assertEquals(Object.prototype, f.__proto__); 54assertEquals(42, F.prototype); 55F.prototype = { a: 42 }; 56f = new F(); 57assertEquals(42, F.prototype.a); 58assertEquals(f.__proto__, F.prototype); 59 60 61// Test that the fast case optimizations can handle non-functions, 62// functions with no prototypes (yet), non-object prototypes, 63// functions without initial maps, and the fully initialized 64// functions. 65function GetPrototypeOf(f) { 66 return f.prototype; 67}; 68 69// Seed the GetPrototypeOf function to enable the fast case 70// optimizations. 71var p = GetPrototypeOf(GetPrototypeOf); 72 73// Check that getting the prototype of a tagged integer works. 74assertTrue(typeof GetPrototypeOf(1) == 'undefined'); 75 76function NoPrototypeYet() { } 77var p = GetPrototypeOf(NoPrototypeYet); 78assertEquals(NoPrototypeYet.prototype, p); 79 80function NonObjectPrototype() { } 81NonObjectPrototype.prototype = 42; 82assertEquals(42, GetPrototypeOf(NonObjectPrototype)); 83 84function NoInitialMap() { } 85var p = NoInitialMap.prototype; 86assertEquals(p, GetPrototypeOf(NoInitialMap)); 87 88// Check the standard fast case. 89assertEquals(F.prototype, GetPrototypeOf(F)); 90 91// Check that getting the prototype of a non-function works. This must 92// be the last thing we do because this will clobber the optimizations 93// in GetPrototypeOf and go to a monomorphic IC load instead. 94assertEquals(87, GetPrototypeOf({prototype:87})); 95 96// Check the prototype is not enumerable, as per ES5 section 15.3.5.2. Note 97// that this is in difference to ES3, which specified that function instances 98// would have enumerable prototypes (section 15.3.5.2 also). 99var foo = new Function("return x"); 100var result = "" 101for (var n in foo) result += n; 102assertEquals(result, ""); 103 104f = new Function('return 1;') 105var desc = Object.getOwnPropertyDescriptor(f, "prototype"); 106assertFalse(desc.configurable); 107assertFalse(desc.enumerable); 108assertTrue(desc.writable); 109 110f = Function('return 1;') 111var desc = Object.getOwnPropertyDescriptor(f, "prototype"); 112assertFalse(desc.configurable); 113assertFalse(desc.enumerable); 114assertTrue(desc.writable); 115 116f = function () { return 1; } 117var desc = Object.getOwnPropertyDescriptor(f, "prototype"); 118assertFalse(desc.configurable); 119assertFalse(desc.enumerable); 120assertTrue(desc.writable); 121