deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / function-prototype.js
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   // Test that map transitions don't break anything.
38   F.property = "value";
39   assertEquals(value, F.prototype);
40 }
41
42 var values = [123, "asdf", true];
43
44 values.forEach(TestNonObjectPrototype);
45
46
47 // Test moving between non-object and object values.
48 function F() {};
49 var f = new F();
50 assertEquals(f.__proto__, F.prototype);
51 F.prototype = 42;
52 f = new F();
53 assertEquals(Object.prototype, f.__proto__);
54 assertEquals(42, F.prototype);
55 F.prototype = { a: 42 };
56 f = new F();
57 assertEquals(42, F.prototype.a);
58 assertEquals(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.
65 function GetPrototypeOf(f) {
66   return f.prototype;
67 };
68
69 // Seed the GetPrototypeOf function to enable the fast case
70 // optimizations.
71 var p = GetPrototypeOf(GetPrototypeOf);
72
73 // Check that getting the prototype of a tagged integer works.
74 assertTrue(typeof GetPrototypeOf(1) == 'undefined');
75
76 function NoPrototypeYet() { }
77 var p = GetPrototypeOf(NoPrototypeYet);
78 assertEquals(NoPrototypeYet.prototype, p);
79
80 function NonObjectPrototype() { }
81 NonObjectPrototype.prototype = 42;
82 assertEquals(42, GetPrototypeOf(NonObjectPrototype));
83
84 function NoInitialMap() { }
85 var p = NoInitialMap.prototype;
86 assertEquals(p, GetPrototypeOf(NoInitialMap));
87
88 // Check the standard fast case.
89 assertEquals(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.
94 assertEquals(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).
99 var foo = new Function("return x");
100 var result  = ""
101 for (var n in foo) result += n;
102 assertEquals(result, "");
103
104 f = new Function('return 1;')
105 var desc = Object.getOwnPropertyDescriptor(f, "prototype");
106 assertFalse(desc.configurable);
107 assertFalse(desc.enumerable);
108 assertTrue(desc.writable);
109
110 f = Function('return 1;')
111 var desc = Object.getOwnPropertyDescriptor(f, "prototype");
112 assertFalse(desc.configurable);
113 assertFalse(desc.enumerable);
114 assertTrue(desc.writable);
115
116 f = function () { return 1; }
117 var desc = Object.getOwnPropertyDescriptor(f, "prototype");
118 assertFalse(desc.configurable);
119 assertFalse(desc.enumerable);
120 assertTrue(desc.writable);