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. 31 function 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 39 var values = [123, "asdf", true]; 40 41 values.forEach(TestNonObjectPrototype); 42 43 44 // Test moving between non-object and object values. 45 function F() {}; 46 var f = new F(); 47 assertEquals(f.__proto__, F.prototype); 48 F.prototype = 42; 49 f = new F(); 50 assertEquals(Object.prototype, f.__proto__); 51 assertEquals(42, F.prototype); 52 F.prototype = { a: 42 }; 53 f = new F(); 54 assertEquals(42, F.prototype.a); 55 assertEquals(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. 62 function GetPrototypeOf(f) { 63 return f.prototype; 64 }; 65 66 // Seed the GetPrototypeOf function to enable the fast case 67 // optimizations. 68 var p = GetPrototypeOf(GetPrototypeOf); 69 70 // Check that getting the prototype of a tagged integer works. 71 assertTrue(typeof GetPrototypeOf(1) == 'undefined'); 72 73 function NoPrototypeYet() { } 74 var p = GetPrototypeOf(NoPrototypeYet); 75 assertEquals(NoPrototypeYet.prototype, p); 76 77 function NonObjectPrototype() { } 78 NonObjectPrototype.prototype = 42; 79 assertEquals(42, GetPrototypeOf(NonObjectPrototype)); 80 81 function NoInitialMap() { } 82 var p = NoInitialMap.prototype; 83 assertEquals(p, GetPrototypeOf(NoInitialMap)); 84 85 // Check the standard fast case. 86 assertEquals(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. 91 assertEquals(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. 95 var foo = new Function("return x"); 96 var result = "" 97 for (var n in foo) result += n; 98 assertEquals(result, ""); 99