| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014-2015 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 // This is adapted from mjsunit/harmony/set-prototype-of.js. |
| 29 |
| 30 // Flags: --harmony-reflect |
| 31 |
| 32 |
| 28 | 33 |
| 29 function getObjects() { | 34 function getObjects() { |
| 30 function func() {} | 35 function func() {} |
| 31 return [ | 36 return [ |
| 32 func, | 37 func, |
| 33 new func(), | 38 new func(), |
| 34 {x: 5}, | 39 {x: 5}, |
| 35 /regexp/, | 40 /regexp/, |
| 36 ['array'], | 41 ['array'], |
| 37 // new Error(), | 42 // new Error(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 ]; | 63 ]; |
| 59 | 64 |
| 60 | 65 |
| 61 var valuesWithoutNull = coercibleValues.concat(undefined); | 66 var valuesWithoutNull = coercibleValues.concat(undefined); |
| 62 | 67 |
| 63 | 68 |
| 64 function TestSetPrototypeOfCoercibleValues() { | 69 function TestSetPrototypeOfCoercibleValues() { |
| 65 for (var i = 0; i < coercibleValues.length; i++) { | 70 for (var i = 0; i < coercibleValues.length; i++) { |
| 66 var value = coercibleValues[i]; | 71 var value = coercibleValues[i]; |
| 67 var proto = Object.getPrototypeOf(value); | 72 var proto = Object.getPrototypeOf(value); |
| 68 assertEquals(Object.setPrototypeOf(value, {}), value); | 73 assertThrows(function() { Reflect.setPrototypeOf(value, {}) }, TypeError); |
| 69 assertSame(proto, Object.getPrototypeOf(value)); | 74 assertSame(proto, Object.getPrototypeOf(value)); |
| 70 } | 75 } |
| 71 } | 76 } |
| 72 TestSetPrototypeOfCoercibleValues(); | 77 TestSetPrototypeOfCoercibleValues(); |
| 73 | 78 |
| 74 | 79 |
| 75 function TestSetPrototypeOfNonCoercibleValues() { | 80 function TestSetPrototypeOfNonCoercibleValues() { |
| 76 for (var i = 0; i < nonCoercibleValues.length; i++) { | 81 for (var i = 0; i < nonCoercibleValues.length; i++) { |
| 77 var value = nonCoercibleValues[i]; | 82 var value = nonCoercibleValues[i]; |
| 78 assertThrows(function() { | 83 assertThrows(function() { |
| 79 Object.setPrototypeOf(value, {}); | 84 Reflect.setPrototypeOf(value, {}); |
| 80 }, TypeError); | 85 }, TypeError); |
| 81 } | 86 } |
| 82 } | 87 } |
| 83 TestSetPrototypeOfNonCoercibleValues(); | 88 TestSetPrototypeOfNonCoercibleValues(); |
| 84 | 89 |
| 85 | 90 |
| 86 function TestSetPrototypeToNonObject(proto) { | 91 function TestSetPrototypeToNonObject(proto) { |
| 87 var objects = getObjects(); | 92 var objects = getObjects(); |
| 88 for (var i = 0; i < objects.length; i++) { | 93 for (var i = 0; i < objects.length; i++) { |
| 89 var object = objects[i]; | 94 var object = objects[i]; |
| 90 for (var j = 0; j < valuesWithoutNull.length; j++) { | 95 for (var j = 0; j < valuesWithoutNull.length; j++) { |
| 91 var proto = valuesWithoutNull[j]; | 96 var proto = valuesWithoutNull[j]; |
| 92 assertThrows(function() { | 97 assertThrows(function() { |
| 93 Object.setPrototypeOf(object, proto); | 98 Reflect.setPrototypeOf(object, proto); |
| 94 }, TypeError); | 99 }, TypeError); |
| 95 } | 100 } |
| 96 } | 101 } |
| 97 } | 102 } |
| 98 TestSetPrototypeToNonObject(); | 103 TestSetPrototypeToNonObject(); |
| 99 | 104 |
| 100 | 105 |
| 101 function TestSetPrototypeOf(object, proto) { | 106 function TestSetPrototypeOf(object, proto) { |
| 102 assertEquals(Object.setPrototypeOf(object, proto), object); | 107 assertTrue(Reflect.setPrototypeOf(object, proto)); |
| 103 assertEquals(Object.getPrototypeOf(object), proto); | 108 assertEquals(Object.getPrototypeOf(object), proto); |
| 104 } | 109 } |
| 105 | 110 |
| 106 | 111 |
| 107 function TestSetPrototypeOfForObjects() { | 112 function TestSetPrototypeOfForObjects() { |
| 108 var objects1 = getObjects(); | 113 var objects1 = getObjects(); |
| 109 var objects2 = getObjects(); | 114 var objects2 = getObjects(); |
| 110 for (var i = 0; i < objects1.length; i++) { | 115 for (var i = 0; i < objects1.length; i++) { |
| 111 for (var j = 0; j < objects2.length; j++) { | 116 for (var j = 0; j < objects2.length; j++) { |
| 112 TestSetPrototypeOf(objects1[i], objects2[j]); | 117 TestSetPrototypeOf(objects1[i], objects2[j]); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 124 } | 129 } |
| 125 TestSetPrototypeToNull(); | 130 TestSetPrototypeToNull(); |
| 126 | 131 |
| 127 | 132 |
| 128 function TestSetPrototypeOfNonExtensibleObject() { | 133 function TestSetPrototypeOfNonExtensibleObject() { |
| 129 var objects = getObjects(); | 134 var objects = getObjects(); |
| 130 var proto = {}; | 135 var proto = {}; |
| 131 for (var i = 0; i < objects.length; i++) { | 136 for (var i = 0; i < objects.length; i++) { |
| 132 var object = objects[i]; | 137 var object = objects[i]; |
| 133 Object.preventExtensions(object); | 138 Object.preventExtensions(object); |
| 134 assertThrows(function() { | 139 assertFalse(Reflect.setPrototypeOf(object, proto)); |
| 135 Object.setPrototypeOf(object, proto); | |
| 136 }, TypeError); | |
| 137 } | 140 } |
| 138 } | 141 } |
| 139 TestSetPrototypeOfNonExtensibleObject(); | 142 TestSetPrototypeOfNonExtensibleObject(); |
| 140 | 143 |
| 141 | 144 |
| 142 function TestSetPrototypeCyclic() { | 145 function TestSetPrototypeCyclic() { |
| 143 var objects = [ | 146 var objects = [ |
| 144 Object.prototype, {}, | 147 Object.prototype, {}, |
| 145 Array.prototype, [], | 148 Array.prototype, [], |
| 146 Error.prototype, new TypeError, | 149 Error.prototype, new TypeError, |
| 147 // etc ... | 150 // etc ... |
| 148 ]; | 151 ]; |
| 149 for (var i = 0; i < objects.length; i += 2) { | 152 for (var i = 0; i < objects.length; i += 2) { |
| 150 var object = objects[i]; | 153 var object = objects[i]; |
| 151 var value = objects[i + 1]; | 154 var value = objects[i + 1]; |
| 152 assertThrows(function() { | 155 assertFalse(Reflect.setPrototypeOf(object, value)); |
| 153 Object.setPrototypeOf(object, value); | |
| 154 }, TypeError); | |
| 155 } | 156 } |
| 156 } | 157 } |
| 157 TestSetPrototypeCyclic(); | 158 TestSetPrototypeCyclic(); |
| 158 | 159 |
| 159 | 160 |
| 160 function TestLookup() { | 161 function TestLookup() { |
| 161 var object = {}; | 162 var object = {}; |
| 162 assertFalse('x' in object); | 163 assertFalse('x' in object); |
| 163 assertFalse('y' in object); | 164 assertFalse('y' in object); |
| 164 | 165 |
| 165 var oldProto = { | 166 var oldProto = { |
| 166 x: 'old x', | 167 x: 'old x', |
| 167 y: 'old y' | 168 y: 'old y' |
| 168 }; | 169 }; |
| 169 Object.setPrototypeOf(object, oldProto); | 170 assertTrue(Reflect.setPrototypeOf(object, oldProto)); |
| 170 assertEquals(object.x, 'old x'); | 171 assertEquals(object.x, 'old x'); |
| 171 assertEquals(object.y, 'old y'); | 172 assertEquals(object.y, 'old y'); |
| 172 | 173 |
| 173 var newProto = { | 174 var newProto = { |
| 174 x: 'new x' | 175 x: 'new x' |
| 175 }; | 176 }; |
| 176 Object.setPrototypeOf(object, newProto); | 177 assertTrue(Reflect.setPrototypeOf(object, newProto)); |
| 177 assertEquals(object.x, 'new x'); | 178 assertEquals(object.x, 'new x'); |
| 178 assertFalse('y' in object); | 179 assertFalse('y' in object); |
| 179 } | 180 } |
| 180 TestLookup(); | 181 TestLookup(); |
| OLD | NEW |