| 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 assertThrows(function() { |
| 29 Object.getPrototypeOf(undefined); |
| 30 }, TypeError); |
| 28 | 31 |
| 29 function TryGetPrototypeOfNonObject(x) { | 32 assertThrows(function() { |
| 30 var caught = 0; | 33 Object.getPrototypeOf(null); |
| 31 try { | 34 }, TypeError); |
| 32 Object.getPrototypeOf(x); | |
| 33 } catch (e) { | |
| 34 caught = e; | |
| 35 } | |
| 36 | 35 |
| 37 assertTrue(caught instanceof TypeError); | |
| 38 }; | |
| 39 | 36 |
| 40 function GetPrototypeOfObject(x) { | 37 function F(){}; |
| 41 assertDoesNotThrow(Object.getPrototypeOf(x)); | 38 var y = new F(); |
| 42 assertNotNull(Object.getPrototypeOf(x)); | 39 |
| 43 assertEquals(Object.getPrototypeOf(x), x.__proto__); | 40 assertSame(Object.getPrototypeOf(y), F.prototype); |
| 41 assertSame(Object.getPrototypeOf(F), Function.prototype); |
| 42 |
| 43 assertSame(Object.getPrototypeOf({x: 5}), Object.prototype); |
| 44 assertSame(Object.getPrototypeOf({x: 5, __proto__: null}), null); |
| 45 |
| 46 assertSame(Object.getPrototypeOf([1, 2]), Array.prototype); |
| 47 |
| 48 |
| 49 assertSame(Object.getPrototypeOf(1), Number.prototype); |
| 50 assertSame(Object.getPrototypeOf(true), Boolean.prototype); |
| 51 assertSame(Object.getPrototypeOf(false), Boolean.prototype); |
| 52 assertSame(Object.getPrototypeOf('str'), String.prototype); |
| 53 assertSame(Object.getPrototypeOf(Symbol()), Symbol.prototype); |
| 54 |
| 55 |
| 56 // Builtin constructors. |
| 57 var functions = [ |
| 58 Array, |
| 59 ArrayBuffer, |
| 60 Boolean, |
| 61 // DataView, |
| 62 Date, |
| 63 Error, |
| 64 EvalError, |
| 65 Float32Array, |
| 66 Float64Array, |
| 67 Function, |
| 68 Int16Array, |
| 69 Int32Array, |
| 70 Int8Array, |
| 71 Map, |
| 72 Number, |
| 73 Object, |
| 74 // Promise, |
| 75 RangeError, |
| 76 ReferenceError, |
| 77 RegExp, |
| 78 Set, |
| 79 String, |
| 80 // Symbol, not constructible |
| 81 SyntaxError, |
| 82 TypeError, |
| 83 URIError, |
| 84 Uint16Array, |
| 85 Uint32Array, |
| 86 Uint8Array, |
| 87 Uint8ClampedArray, |
| 88 WeakMap, |
| 89 WeakSet, |
| 90 ]; |
| 91 |
| 92 for (var f of functions) { |
| 93 assertSame(Object.getPrototypeOf(f), Function.prototype); |
| 94 assertSame(Object.getPrototypeOf(new f), f.prototype); |
| 44 } | 95 } |
| 45 | 96 |
| 46 function F(){}; | 97 var p = new Promise(function() {}); |
| 98 assertSame(Object.getPrototypeOf(p), Promise.prototype); |
| 47 | 99 |
| 48 // Non object | 100 var dv = new DataView(new ArrayBuffer()); |
| 49 var x = 10; | 101 assertSame(Object.getPrototypeOf(dv), DataView.prototype); |
| 50 | |
| 51 // Object | |
| 52 var y = new F(); | |
| 53 | |
| 54 // Make sure that TypeError exceptions are thrown when non-objects are passed | |
| 55 // as argument | |
| 56 TryGetPrototypeOfNonObject(0); | |
| 57 TryGetPrototypeOfNonObject(null); | |
| 58 TryGetPrototypeOfNonObject('Testing'); | |
| 59 TryGetPrototypeOfNonObject(x); | |
| 60 | |
| 61 // Make sure the real objects have this method and that it returns the | |
| 62 // actual prototype object. Also test for Functions and RegExp. | |
| 63 GetPrototypeOfObject(this); | |
| 64 GetPrototypeOfObject(y); | |
| 65 GetPrototypeOfObject({x:5}); | |
| 66 GetPrototypeOfObject(F); | |
| 67 GetPrototypeOfObject(RegExp); | |
| OLD | NEW |