OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
| 28 function assertPrototypeOf(func, expected) { |
| 29 assertEquals(expected, Object.getPrototypeOf(func)); |
| 30 } |
| 31 |
| 32 |
28 assertThrows(function() { | 33 assertThrows(function() { |
29 Object.getPrototypeOf(undefined); | 34 Object.getPrototypeOf(undefined); |
30 }, TypeError); | 35 }, TypeError); |
31 | 36 |
| 37 |
32 assertThrows(function() { | 38 assertThrows(function() { |
33 Object.getPrototypeOf(null); | 39 Object.getPrototypeOf(null); |
34 }, TypeError); | 40 }, TypeError); |
35 | 41 |
36 | 42 |
37 function F(){}; | 43 function F(){}; |
38 var y = new F(); | 44 var y = new F(); |
39 | 45 |
40 assertSame(Object.getPrototypeOf(y), F.prototype); | 46 assertPrototypeOf(y, F.prototype); |
41 assertSame(Object.getPrototypeOf(F), Function.prototype); | 47 assertPrototypeOf(F, Function.prototype); |
42 | 48 |
43 assertSame(Object.getPrototypeOf({x: 5}), Object.prototype); | 49 assertPrototypeOf({x: 5}, Object.prototype); |
44 assertSame(Object.getPrototypeOf({x: 5, __proto__: null}), null); | 50 assertPrototypeOf({x: 5, __proto__: null}, null); |
45 | 51 |
46 assertSame(Object.getPrototypeOf([1, 2]), Array.prototype); | 52 assertPrototypeOf([1, 2], Array.prototype); |
47 | 53 |
48 | 54 |
49 assertSame(Object.getPrototypeOf(1), Number.prototype); | 55 assertPrototypeOf(1, Number.prototype); |
50 assertSame(Object.getPrototypeOf(true), Boolean.prototype); | 56 assertPrototypeOf(true, Boolean.prototype); |
51 assertSame(Object.getPrototypeOf(false), Boolean.prototype); | 57 assertPrototypeOf(false, Boolean.prototype); |
52 assertSame(Object.getPrototypeOf('str'), String.prototype); | 58 assertPrototypeOf('str', String.prototype); |
53 assertSame(Object.getPrototypeOf(Symbol()), Symbol.prototype); | 59 assertPrototypeOf(Symbol(), Symbol.prototype); |
| 60 |
| 61 |
| 62 var errorFunctions = [ |
| 63 EvalError, |
| 64 RangeError, |
| 65 ReferenceError, |
| 66 SyntaxError, |
| 67 TypeError, |
| 68 URIError, |
| 69 ]; |
| 70 |
| 71 for (var f of errorFunctions) { |
| 72 assertPrototypeOf(f, Error); |
| 73 assertPrototypeOf(new f(), f.prototype); |
| 74 } |
54 | 75 |
55 | 76 |
56 // Builtin constructors. | 77 // Builtin constructors. |
57 var functions = [ | 78 var functions = [ |
58 Array, | 79 Array, |
59 ArrayBuffer, | 80 ArrayBuffer, |
60 Boolean, | 81 Boolean, |
61 // DataView, | 82 // DataView, |
62 Date, | 83 Date, |
63 Error, | 84 Error, |
64 EvalError, | |
65 Float32Array, | 85 Float32Array, |
66 Float64Array, | 86 Float64Array, |
67 Function, | 87 Function, |
68 Int16Array, | 88 Int16Array, |
69 Int32Array, | 89 Int32Array, |
70 Int8Array, | 90 Int8Array, |
71 Map, | 91 Map, |
72 Number, | 92 Number, |
73 Object, | 93 Object, |
74 // Promise, | 94 // Promise, |
75 RangeError, | |
76 ReferenceError, | |
77 RegExp, | 95 RegExp, |
78 Set, | 96 Set, |
79 String, | 97 String, |
80 // Symbol, not constructible | 98 // Symbol, not constructible |
81 SyntaxError, | |
82 TypeError, | |
83 URIError, | |
84 Uint16Array, | 99 Uint16Array, |
85 Uint32Array, | 100 Uint32Array, |
86 Uint8Array, | 101 Uint8Array, |
87 Uint8ClampedArray, | 102 Uint8ClampedArray, |
88 WeakMap, | 103 WeakMap, |
89 WeakSet, | 104 WeakSet, |
90 ]; | 105 ]; |
91 | 106 |
92 for (var f of functions) { | 107 for (var f of functions) { |
93 assertSame(Object.getPrototypeOf(f), Function.prototype); | 108 assertPrototypeOf(f, Function.prototype); |
94 assertSame(Object.getPrototypeOf(new f), f.prototype); | 109 assertPrototypeOf(new f(), f.prototype); |
95 } | 110 } |
96 | 111 |
97 var p = new Promise(function() {}); | 112 var p = new Promise(function() {}); |
98 assertSame(Object.getPrototypeOf(p), Promise.prototype); | 113 assertPrototypeOf(p, Promise.prototype); |
99 | 114 |
100 var dv = new DataView(new ArrayBuffer()); | 115 var dv = new DataView(new ArrayBuffer()); |
101 assertSame(Object.getPrototypeOf(dv), DataView.prototype); | 116 assertPrototypeOf(dv, DataView.prototype); |
OLD | NEW |