Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | function assertPrototypeOf(func, expected) { |
| 29 | assertEquals(expected, Object.getPrototypeOf(func)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | |
| 33 | assertThrows(function() { |
| 34 | Object.getPrototypeOf(undefined); |
| 35 | }, TypeError); |
| 36 | |
| 37 | |
| 38 | assertThrows(function() { |
| 39 | Object.getPrototypeOf(null); |
| 40 | }, TypeError); |
| 41 | |
| 42 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 43 | function F(){}; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 44 | var y = new F(); |
| 45 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | assertPrototypeOf(y, F.prototype); |
| 47 | assertPrototypeOf(F, Function.prototype); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 48 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | assertPrototypeOf({x: 5}, Object.prototype); |
| 50 | assertPrototypeOf({x: 5, __proto__: null}, null); |
| 51 | |
| 52 | assertPrototypeOf([1, 2], Array.prototype); |
| 53 | |
| 54 | |
| 55 | assertPrototypeOf(1, Number.prototype); |
| 56 | assertPrototypeOf(true, Boolean.prototype); |
| 57 | assertPrototypeOf(false, Boolean.prototype); |
| 58 | assertPrototypeOf('str', String.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 | } |
| 75 | |
| 76 | |
| 77 | // Builtin constructors. |
| 78 | var functions = [ |
| 79 | Array, |
| 80 | ArrayBuffer, |
| 81 | Boolean, |
| 82 | // DataView, |
| 83 | Date, |
| 84 | Error, |
| 85 | // Float32Array, prototype is %TypedArray% |
| 86 | // Float64Array, |
| 87 | Function, |
| 88 | // Int16Array, |
| 89 | // Int32Array, |
| 90 | // Int8Array, |
| 91 | Map, |
| 92 | Number, |
| 93 | Object, |
| 94 | // Promise, |
| 95 | RegExp, |
| 96 | Set, |
| 97 | String, |
| 98 | // Symbol, not constructible |
| 99 | // Uint16Array, |
| 100 | // Uint32Array, |
| 101 | // Uint8Array, |
| 102 | // Uint8ClampedArray, |
| 103 | WeakMap, |
| 104 | WeakSet, |
| 105 | ]; |
| 106 | |
| 107 | for (var f of functions) { |
| 108 | assertPrototypeOf(f, Function.prototype); |
| 109 | assertPrototypeOf(new f(), f.prototype); |
| 110 | } |
| 111 | |
| 112 | var p = new Promise(function() {}); |
| 113 | assertPrototypeOf(p, Promise.prototype); |
| 114 | |
| 115 | var dv = new DataView(new ArrayBuffer()); |
| 116 | assertPrototypeOf(dv, DataView.prototype); |