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 testReflectConstructArity() { | |
| 9 assertEquals(2, Reflect.construct.length); | |
| 10 })(); | |
| 11 | |
| 12 | |
| 13 (function testReflectConstructNonConstructor() { | |
| 14 assertThrows(function() { | |
| 15 new Reflect.construct(function(){}, []); | |
| 16 }, TypeError); | |
| 17 })(); | |
| 18 | |
| 19 | |
| 20 (function testReflectConstructBasic() { | |
| 21 function Constructor() { "use strict"; } | |
| 22 assertInstanceof(Reflect.construct(Constructor, []), Constructor); | |
| 23 })(); | |
| 24 | |
| 25 | |
| 26 (function testReflectConstructBasicSloppy() { | |
| 27 function Constructor() {} | |
| 28 assertInstanceof(Reflect.construct(Constructor, []), Constructor); | |
| 29 })(); | |
| 30 | |
| 31 | |
| 32 (function testReflectConstructReturnSomethingElseStrict() { | |
| 33 var R = {}; | |
| 34 function Constructor() { "use strict"; return R; } | |
| 35 assertSame(R, Reflect.construct(Constructor, [])); | |
| 36 })(); | |
| 37 | |
| 38 | |
| 39 (function testReflectConstructReturnSomethingElseSloppy() { | |
| 40 var R = {}; | |
| 41 function Constructor() { return R; } | |
| 42 assertSame(R, Reflect.construct(Constructor, [])); | |
| 43 })(); | |
| 44 | |
| 45 | |
| 46 (function testReflectConstructNewTargetStrict() { | |
| 47 "use strict"; | |
| 48 function Constructor() { this[9] = 1; } | |
| 49 var O = Reflect.construct(Constructor, [], Array); | |
| 50 assertSame(Array.prototype, Object.getPrototypeOf(O)); | |
| 51 assertSame(1, O[9]); | |
|
arv (Not doing code reviews)
2015/03/04 09:45:01
also check length and Array.isArray to ensure we c
caitp (gmail)
2015/03/04 13:24:14
That's not exactly correct: https://people.mozilla
arv (Not doing code reviews)
2015/03/04 13:30:33
You are right.
Maybe a more interesting test then
caitp (gmail)
2015/03/04 13:54:56
Hmm, we seem to be doing something wrong though. I
| |
| 52 })(); | |
| 53 | |
| 54 | |
| 55 (function testReflectConstructNewTargetSloppy() { | |
| 56 function Constructor() { this[9] = 1; } | |
| 57 var O = Reflect.construct(Constructor, [], Array); | |
| 58 assertSame(Array.prototype, Object.getPrototypeOf(O)); | |
| 59 assertSame(1, O[9]); | |
| 60 })(); | |
| 61 | |
| 62 | |
| 63 (function testAppliedArgumentsLength() { | |
| 64 function lengthStrict() { 'use strict'; this.a = arguments.length; } | |
| 65 function lengthSloppy() { this.a = arguments.length; } | |
| 66 | |
| 67 assertEquals(0, Reflect.construct(lengthStrict, []).a); | |
| 68 assertEquals(0, Reflect.construct(lengthSloppy, []).a); | |
| 69 assertEquals(0, Reflect.construct(lengthStrict, {}).a); | |
| 70 assertEquals(0, Reflect.construct(lengthSloppy, {}).a); | |
| 71 | |
| 72 for (var i = 0; i < 256; ++i) { | |
| 73 assertEquals(i, Reflect.construct(lengthStrict, new Array(i)).a); | |
| 74 assertEquals(i, Reflect.construct(lengthSloppy, new Array(i)).a); | |
| 75 assertEquals(i, Reflect.construct(lengthStrict, { length: i }).a); | |
| 76 assertEquals(i, Reflect.construct(lengthSloppy, { length: i }).a); | |
| 77 } | |
| 78 })(); | |
| 79 | |
| 80 | |
| 81 (function testAppliedArgumentsLengthThrows() { | |
| 82 function noopStrict() { 'use strict'; } | |
| 83 function noopSloppy() { } | |
| 84 function MyError() {} | |
| 85 | |
| 86 var argsList = {}; | |
| 87 Object.defineProperty(argsList, "length", { | |
| 88 get: function() { throw new MyError(); } | |
| 89 }); | |
| 90 | |
| 91 assertThrows(function() { | |
| 92 Reflect.construct(noopStrict, argsList); | |
| 93 }, MyError); | |
| 94 | |
| 95 assertThrows(function() { | |
| 96 Reflect.construct(noopSloppy, argsList); | |
| 97 }, MyError); | |
| 98 })(); | |
| 99 | |
| 100 | |
| 101 (function testAppliedArgumentsElementThrows() { | |
| 102 function noopStrict() { 'use strict'; } | |
| 103 function noopSloppy() { } | |
| 104 function MyError() {} | |
| 105 | |
| 106 var argsList = { length: 1 }; | |
| 107 Object.defineProperty(argsList, "0", { | |
| 108 get: function() { throw new MyError(); } | |
| 109 }); | |
| 110 | |
| 111 assertThrows(function() { | |
| 112 Reflect.construct(noopStrict, argsList); | |
| 113 }, MyError); | |
| 114 | |
| 115 assertThrows(function() { | |
| 116 Reflect.construct(noopSloppy, argsList); | |
| 117 }, MyError); | |
| 118 })(); | |
| 119 | |
| 120 | |
| 121 (function testAppliedNonFunctionStrict() { | |
| 122 'use strict'; | |
| 123 assertThrows(function() { Reflect.construct(void 0, []); }, TypeError); | |
| 124 assertThrows(function() { Reflect.construct(null, []); }, TypeError); | |
| 125 assertThrows(function() { Reflect.construct(123, []); }, TypeError); | |
| 126 assertThrows(function() { Reflect.construct("str", []); }, TypeError); | |
| 127 assertThrows(function() { Reflect.construct(Symbol("x"), []); }, TypeError); | |
| 128 assertThrows(function() { Reflect.construct(/123/, []); }, TypeError); | |
| 129 assertThrows(function() { Reflect.construct(NaN, []); }, TypeError); | |
| 130 assertThrows(function() { Reflect.construct({}, []); }, TypeError); | |
| 131 assertThrows(function() { Reflect.construct([], []); }, TypeError); | |
| 132 })(); | |
| 133 | |
| 134 | |
| 135 (function testAppliedNonFunctionSloppy() { | |
| 136 assertThrows(function() { Reflect.construct(void 0, []); }, TypeError); | |
| 137 assertThrows(function() { Reflect.construct(null, []); }, TypeError); | |
| 138 assertThrows(function() { Reflect.construct(123, []); }, TypeError); | |
| 139 assertThrows(function() { Reflect.construct("str", []); }, TypeError); | |
| 140 assertThrows(function() { Reflect.construct(Symbol("x"), []); }, TypeError); | |
| 141 assertThrows(function() { Reflect.construct(/123/, []); }, TypeError); | |
| 142 assertThrows(function() { Reflect.construct(NaN, []); }, TypeError); | |
| 143 assertThrows(function() { Reflect.construct({}, []); }, TypeError); | |
| 144 assertThrows(function() { Reflect.construct([], []); }, TypeError); | |
| 145 })(); | |
| 146 | |
| 147 | |
| 148 (function testAppliedArgumentsNonList() { | |
| 149 function noopStrict() { 'use strict'; } | |
| 150 function noopSloppy() {} | |
| 151 assertThrows(function() { Reflect.construct(noopStrict, null); }, TypeError); | |
| 152 assertThrows(function() { Reflect.construct(noopSloppy, null); }, TypeError); | |
| 153 assertThrows(function() { Reflect.construct(noopStrict, 1); }, TypeError); | |
| 154 assertThrows(function() { Reflect.construct(noopSloppy, 1); }, TypeError); | |
| 155 assertThrows(function() { Reflect.construct(noopStrict, "BAD"); }, TypeError); | |
| 156 assertThrows(function() { Reflect.construct(noopSloppy, "BAD"); }, TypeError); | |
| 157 assertThrows(function() { Reflect.construct(noopStrict, true); }, TypeError); | |
| 158 assertThrows(function() { Reflect.construct(noopSloppy, true); }, TypeError); | |
| 159 var sym = Symbol("x"); | |
| 160 assertThrows(function() { Reflect.construct(noopStrict, sym); }, TypeError); | |
| 161 assertThrows(function() { Reflect.construct(noopSloppy, sym); }, TypeError); | |
| 162 })(); | |
| 163 | |
| 164 | |
| 165 (function testAppliedArgumentValue() { | |
| 166 function firstStrict(a) { 'use strict'; this.a = a; } | |
| 167 function firstSloppy(a) { this.a = a; } | |
| 168 function lastStrict(a) { | |
| 169 'use strict'; this.a = arguments[arguments.length - 1]; } | |
| 170 function lastSloppy(a) { this.a = arguments[arguments.length - 1]; } | |
| 171 function sumStrict() { | |
| 172 'use strict'; | |
| 173 var sum = arguments[0]; | |
| 174 for (var i = 1; i < arguments.length; ++i) { | |
| 175 sum += arguments[i]; | |
| 176 } | |
| 177 this.a = sum; | |
| 178 } | |
| 179 function sumSloppy() { | |
| 180 var sum = arguments[0]; | |
| 181 for (var i = 1; i < arguments.length; ++i) { | |
| 182 sum += arguments[i]; | |
| 183 } | |
| 184 this.a = sum; | |
| 185 } | |
| 186 | |
| 187 assertEquals("OK!", Reflect.construct(firstStrict, ["OK!"]).a); | |
| 188 assertEquals("OK!", Reflect.construct(firstSloppy, ["OK!"]).a); | |
| 189 assertEquals("OK!", Reflect.construct(firstStrict, | |
| 190 { 0: "OK!", length: 1 }).a); | |
| 191 assertEquals("OK!", Reflect.construct(firstSloppy, | |
| 192 { 0: "OK!", length: 1 }).a); | |
| 193 assertEquals("OK!", Reflect.construct(lastStrict, | |
| 194 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]).a); | |
| 195 assertEquals("OK!", Reflect.construct(lastSloppy, | |
| 196 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]).a); | |
| 197 assertEquals("OK!", Reflect.construct(lastStrict, | |
| 198 { 9: "OK!", length: 10 }).a); | |
| 199 assertEquals("OK!", Reflect.construct(lastSloppy, | |
| 200 { 9: "OK!", length: 10 }).a); | |
| 201 assertEquals("TEST", Reflect.construct(sumStrict, | |
| 202 ["T", "E", "S", "T"]).a); | |
| 203 assertEquals("TEST!!", Reflect.construct(sumStrict, | |
| 204 ["T", "E", "S", "T", "!", "!"]).a); | |
| 205 assertEquals(10, Reflect.construct(sumStrict, | |
| 206 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }).a); | |
| 207 assertEquals("TEST", Reflect.construct(sumSloppy, | |
| 208 ["T", "E", "S", "T"]).a); | |
| 209 assertEquals("TEST!!", Reflect.construct(sumSloppy, | |
| 210 ["T", "E", "S", "T", "!", "!"]).a); | |
| 211 assertEquals(10, Reflect.construct(sumSloppy, | |
| 212 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }).a); | |
| 213 })(); | |
| OLD | NEW |