Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-reflect | |
| 6 | |
| 7 | |
| 8 (function testReflectApplyArity() { | |
| 9 assertEquals(3, Reflect.apply.length); | |
| 10 })(); | |
| 11 | |
| 12 | |
| 13 (function testReflectApplyNonConstructor() { | |
| 14 assertThrows(function() { | |
| 15 new Reflect.apply(function(){}, null, []); | |
| 16 }, TypeError); | |
| 17 })(); | |
| 18 | |
| 19 | |
| 20 (function testAppliedReceiverSloppy() { | |
| 21 function returnThis() { return this; } | |
| 22 var receiver = {}; | |
| 23 | |
| 24 assertSame(this, Reflect.apply(returnThis, void 0, [])); | |
| 25 assertSame(this, Reflect.apply(returnThis, null, [])); | |
| 26 assertSame(this, Reflect.apply(returnThis, this, [])); | |
| 27 assertSame(receiver, Reflect.apply(returnThis, receiver, [])); | |
| 28 | |
| 29 // Wrap JS values | |
| 30 assertInstanceof(Reflect.apply(returnThis, "str", []), String); | |
|
arv (Not doing code reviews)
2015/03/03 14:25:52
Maybe check typeof and Object.getPrototypeOf inste
caitp (gmail)
2015/03/03 15:53:49
Done.
| |
| 31 assertInstanceof(Reflect.apply(returnThis, 123, []), Number); | |
| 32 assertInstanceof(Reflect.apply(returnThis, true, []), Boolean); | |
| 33 assertInstanceof(Reflect.apply(returnThis, Symbol("test"), []), Symbol); | |
| 34 })(); | |
| 35 | |
| 36 | |
| 37 (function testAppliedReceiverStrict() { | |
| 38 function returnThis() { 'use strict'; return this; } | |
| 39 var receiver = {}; | |
| 40 | |
| 41 assertSame(void 0, Reflect.apply(returnThis, void 0, [])); | |
| 42 assertSame(this, Reflect.apply(returnThis, this, [])); | |
| 43 assertSame(receiver, Reflect.apply(returnThis, receiver, [])); | |
| 44 | |
| 45 // Don't wrap value types | |
| 46 var regexp = /123/; | |
| 47 var symbol = Symbol("test"); | |
| 48 assertSame("str", Reflect.apply(returnThis, "str", [])); | |
| 49 assertSame(123, Reflect.apply(returnThis, 123, [])); | |
| 50 assertSame(true, Reflect.apply(returnThis, true, [])); | |
| 51 assertSame(regexp, Reflect.apply(returnThis, regexp, [])); | |
| 52 assertSame(symbol, Reflect.apply(returnThis, symbol, [])); | |
| 53 })(); | |
| 54 | |
| 55 | |
| 56 (function testAppliedArgumentsLength() { | |
| 57 function returnLengthStrict() { 'use strict'; return arguments.length; } | |
| 58 function returnLengthSloppy() { return arguments.length; } | |
| 59 | |
| 60 assertEquals(0, Reflect.apply(returnLengthStrict, this, [])); | |
| 61 assertEquals(0, Reflect.apply(returnLengthSloppy, this, [])); | |
| 62 assertEquals(0, Reflect.apply(returnLengthStrict, this, {})); | |
| 63 assertEquals(0, Reflect.apply(returnLengthSloppy, this, {})); | |
| 64 | |
| 65 for (var i = 0; i < 256; ++i) { | |
| 66 assertEquals(i, Reflect.apply(returnLengthStrict, this, new Array(i))); | |
| 67 assertEquals(i, Reflect.apply(returnLengthSloppy, this, new Array(i))); | |
| 68 assertEquals(i, Reflect.apply(returnLengthStrict, this, { length: i })); | |
| 69 assertEquals(i, Reflect.apply(returnLengthSloppy, this, { length: i })); | |
| 70 } | |
| 71 })(); | |
| 72 | |
| 73 | |
| 74 (function testAppliedArgumentsLengthThrows() { | |
| 75 function noopStrict() { 'use strict'; } | |
| 76 function noopSloppy() { } | |
| 77 function MyError() {} | |
| 78 | |
| 79 var argsList = {}; | |
| 80 Object.defineProperty(argsList, "length", { | |
| 81 get: function() { throw new MyError(); } | |
| 82 }); | |
| 83 | |
| 84 assertThrows(function() { | |
| 85 Reflect.apply(noopStrict, this, argsList); | |
| 86 }, MyError); | |
| 87 | |
| 88 assertThrows(function() { | |
| 89 Reflect.apply(noopSloppy, this, argsList); | |
| 90 }, MyError); | |
| 91 })(); | |
| 92 | |
| 93 | |
| 94 (function testAppliedArgumentsElementThrows() { | |
| 95 function noopStrict() { 'use strict'; } | |
| 96 function noopSloppy() { } | |
| 97 function MyError() {} | |
| 98 | |
| 99 var argsList = { length: 1 }; | |
| 100 Object.defineProperty(argsList, "0", { | |
| 101 get: function() { throw new MyError(); } | |
| 102 }); | |
| 103 | |
| 104 assertThrows(function() { | |
| 105 Reflect.apply(noopStrict, this, argsList); | |
| 106 }, MyError); | |
| 107 | |
| 108 assertThrows(function() { | |
| 109 Reflect.apply(noopSloppy, this, argsList); | |
| 110 }, MyError); | |
| 111 })(); | |
| 112 | |
| 113 | |
| 114 (function testAppliedNonFunctionStrict() { | |
| 115 'use strict'; | |
| 116 assertThrows(function() { Reflect.apply(void 0); }, TypeError); | |
| 117 assertThrows(function() { Reflect.apply(null); }, TypeError); | |
| 118 assertThrows(function() { Reflect.apply(123); }, TypeError); | |
| 119 assertThrows(function() { Reflect.apply("str"); }, TypeError); | |
| 120 assertThrows(function() { Reflect.apply(Symbol("x")); }, TypeError); | |
| 121 assertThrows(function() { Reflect.apply(/123/); }, TypeError); | |
| 122 assertThrows(function() { Reflect.apply(NaN); }, TypeError); | |
| 123 assertThrows(function() { Reflect.apply({}); }, TypeError); | |
| 124 assertThrows(function() { Reflect.apply([]); }, TypeError); | |
| 125 })(); | |
| 126 | |
| 127 | |
| 128 (function testAppliedNonFunctionSloppy() { | |
| 129 assertThrows(function() { Reflect.apply(void 0); }, TypeError); | |
| 130 assertThrows(function() { Reflect.apply(null); }, TypeError); | |
| 131 assertThrows(function() { Reflect.apply(123); }, TypeError); | |
| 132 assertThrows(function() { Reflect.apply("str"); }, TypeError); | |
| 133 assertThrows(function() { Reflect.apply(Symbol("x")); }, TypeError); | |
| 134 assertThrows(function() { Reflect.apply(/123/); }, TypeError); | |
| 135 assertThrows(function() { Reflect.apply(NaN); }, TypeError); | |
| 136 assertThrows(function() { Reflect.apply({}); }, TypeError); | |
| 137 assertThrows(function() { Reflect.apply([]); }, TypeError); | |
| 138 })(); | |
| 139 | |
| 140 | |
| 141 (function testAppliedArgumentsNonList() { | |
| 142 function noopStrict() { 'use strict'; } | |
| 143 function noopSloppy() {} | |
| 144 var R = void 0; | |
| 145 assertThrows(function() { Reflect.apply(noopStrict, R, null); }, TypeError); | |
| 146 assertThrows(function() { Reflect.apply(noopSloppy, R, null); }, TypeError); | |
| 147 assertThrows(function() { Reflect.apply(noopStrict, R, 1); }, TypeError); | |
| 148 assertThrows(function() { Reflect.apply(noopSloppy, R, 1); }, TypeError); | |
| 149 assertThrows(function() { Reflect.apply(noopStrict, R, "BAD"); }, TypeError); | |
| 150 assertThrows(function() { Reflect.apply(noopSloppy, R, "BAD"); }, TypeError); | |
| 151 assertThrows(function() { Reflect.apply(noopStrict, R, true); }, TypeError); | |
| 152 assertThrows(function() { Reflect.apply(noopSloppy, R, true); }, TypeError); | |
| 153 var sym = Symbol("x"); | |
| 154 assertThrows(function() { Reflect.apply(noopStrict, R, sym); }, TypeError); | |
| 155 assertThrows(function() { Reflect.apply(noopSloppy, R, sym); }, TypeError); | |
| 156 })(); | |
| 157 | |
| 158 | |
| 159 (function testAppliedArgumentValue() { | |
| 160 function returnFirstStrict(a) { 'use strict'; return a; } | |
| 161 function returnFirstSloppy(a) { return a; } | |
| 162 function returnLastStrict(a) { | |
| 163 'use strict'; return arguments[arguments.length - 1]; } | |
| 164 function returnLastSloppy(a) { return arguments[arguments.length - 1]; } | |
| 165 function returnSumStrict() { | |
| 166 'use strict'; | |
| 167 var sum = arguments[0]; | |
| 168 for (var i = 1; i < arguments.length; ++i) { | |
| 169 sum += arguments[i]; | |
| 170 } | |
| 171 return sum; | |
| 172 } | |
| 173 function returnSumSloppy() { | |
| 174 var sum = arguments[0]; | |
| 175 for (var i = 1; i < arguments.length; ++i) { | |
| 176 sum += arguments[i]; | |
| 177 } | |
| 178 return sum; | |
| 179 } | |
| 180 | |
| 181 assertEquals("OK!", Reflect.apply(returnFirstStrict, this, ["OK!"])); | |
| 182 assertEquals("OK!", Reflect.apply(returnFirstSloppy, this, ["OK!"])); | |
| 183 assertEquals("OK!", Reflect.apply(returnFirstStrict, this, | |
| 184 { 0: "OK!", length: 1 })); | |
| 185 assertEquals("OK!", Reflect.apply(returnFirstSloppy, this, | |
| 186 { 0: "OK!", length: 1 })); | |
| 187 assertEquals("OK!", Reflect.apply(returnLastStrict, this, | |
| 188 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"])); | |
| 189 assertEquals("OK!", Reflect.apply(returnLastSloppy, this, | |
| 190 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"])); | |
| 191 assertEquals("OK!", Reflect.apply(returnLastStrict, this, | |
| 192 { 9: "OK!", length: 10 })); | |
| 193 assertEquals("OK!", Reflect.apply(returnLastSloppy, this, | |
| 194 { 9: "OK!", length: 10 })); | |
| 195 assertEquals("TEST", Reflect.apply(returnSumStrict, this, | |
| 196 ["T", "E", "S", "T"])); | |
| 197 assertEquals("TEST!!", Reflect.apply(returnSumStrict, this, | |
| 198 ["T", "E", "S", "T", "!", "!"])); | |
| 199 assertEquals(10, Reflect.apply(returnSumStrict, this, | |
| 200 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 })); | |
| 201 assertEquals("TEST", Reflect.apply(returnSumSloppy, this, | |
| 202 ["T", "E", "S", "T"])); | |
| 203 assertEquals("TEST!!", Reflect.apply(returnSumSloppy, this, | |
| 204 ["T", "E", "S", "T", "!", "!"])); | |
| 205 assertEquals(10, Reflect.apply(returnSumSloppy, this, | |
| 206 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 })); | |
| 207 })(); | |
| OLD | NEW |