| 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); | |
| 31 | 28 |
| 32 assertThrows(function() { | 29 function TryGetPrototypeOfNonObject(x) { |
| 33 Object.getPrototypeOf(null); | 30 var caught = 0; |
| 34 }, TypeError); | 31 try { |
| 32 Object.getPrototypeOf(x); |
| 33 } catch (e) { |
| 34 caught = e; |
| 35 } |
| 35 | 36 |
| 37 assertTrue(caught instanceof TypeError); |
| 38 }; |
| 39 |
| 40 function GetPrototypeOfObject(x) { |
| 41 assertDoesNotThrow(Object.getPrototypeOf(x)); |
| 42 assertNotNull(Object.getPrototypeOf(x)); |
| 43 assertEquals(Object.getPrototypeOf(x), x.__proto__); |
| 44 } |
| 36 | 45 |
| 37 function F(){}; | 46 function F(){}; |
| 47 |
| 48 // Non object |
| 49 var x = 10; |
| 50 |
| 51 // Object |
| 38 var y = new F(); | 52 var y = new F(); |
| 39 | 53 |
| 40 assertSame(Object.getPrototypeOf(y), F.prototype); | 54 // Make sure that TypeError exceptions are thrown when non-objects are passed |
| 41 assertSame(Object.getPrototypeOf(F), Function.prototype); | 55 // as argument |
| 56 TryGetPrototypeOfNonObject(0); |
| 57 TryGetPrototypeOfNonObject(null); |
| 58 TryGetPrototypeOfNonObject('Testing'); |
| 59 TryGetPrototypeOfNonObject(x); |
| 42 | 60 |
| 43 assertSame(Object.getPrototypeOf({x: 5}), Object.prototype); | 61 // Make sure the real objects have this method and that it returns the |
| 44 assertSame(Object.getPrototypeOf({x: 5, __proto__: null}), null); | 62 // actual prototype object. Also test for Functions and RegExp. |
| 45 | 63 GetPrototypeOfObject(this); |
| 46 assertSame(Object.getPrototypeOf([1, 2]), Array.prototype); | 64 GetPrototypeOfObject(y); |
| 47 | 65 GetPrototypeOfObject({x:5}); |
| 48 | 66 GetPrototypeOfObject(F); |
| 49 assertSame(Object.getPrototypeOf(1), Number.prototype); | 67 GetPrototypeOfObject(RegExp); |
| 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); | |
| 95 } | |
| 96 | |
| 97 var p = new Promise(function() {}); | |
| 98 assertSame(Object.getPrototypeOf(p), Promise.prototype); | |
| 99 | |
| 100 var dv = new DataView(new ArrayBuffer()); | |
| 101 assertSame(Object.getPrototypeOf(dv), DataView.prototype); | |
| OLD | NEW |