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} 38 39var values = [123, "asdf", true]; 40 41values.forEach(TestNonObjectPrototype); 42 43 44// Test moving between non-object and object values. 45function F() {}; 46var f = new F(); 47assertEquals(f.__proto__, F.prototype); 48F.prototype = 42; 49f = new F(); 50assertEquals(Object.prototype, f.__proto__); 51assertEquals(42, F.prototype); 52F.prototype = { a: 42 }; 53f = new F(); 54assertEquals(42, F.prototype.a); 55assertEquals(f.__proto__, F.prototype); 56 57 58// Test that the fast case optimizations can handle non-functions, 59// functions with no prototypes (yet), non-object prototypes, 60// functions without initial maps, and the fully initialized 61// functions. 62function GetPrototypeOf(f) { 63 return f.prototype; 64}; 65 66// Seed the GetPrototypeOf function to enable the fast case 67// optimizations. 68var p = GetPrototypeOf(GetPrototypeOf); 69 70// Check that getting the prototype of a tagged integer works. 71assertTrue(typeof GetPrototypeOf(1) == 'undefined'); 72 73function NoPrototypeYet() { } 74var p = GetPrototypeOf(NoPrototypeYet); 75assertEquals(NoPrototypeYet.prototype, p); 76 77function NonObjectPrototype() { } 78NonObjectPrototype.prototype = 42; 79assertEquals(42, GetPrototypeOf(NonObjectPrototype)); 80 81function NoInitialMap() { } 82var p = NoInitialMap.prototype; 83assertEquals(p, GetPrototypeOf(NoInitialMap)); 84 85// Check the standard fast case. 86assertEquals(F.prototype, GetPrototypeOf(F)); 87 88// Check that getting the prototype of a non-function works. This must 89// be the last thing we do because this will clobber the optimizations 90// in GetPrototypeOf and go to a monomorphic IC load instead. 91assertEquals(87, GetPrototypeOf({prototype:87})); 92 93// Check the prototype is not enumerable, for compatibility with 94// safari. This is deliberately incompatible with ECMA262, 15.3.5.2. 95var foo = new Function("return x"); 96var result = "" 97for (var n in foo) result += n; 98assertEquals(result, ""); 99