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 testReflectConstructReturnSomethingElse() { |
| 33 var R = {}; |
| 34 function Constructor() { "use strict"; return R; } |
| 35 assertSame(R, Reflect.construct(Constructor, [])); |
| 36 })(); |
| 37 |
| 38 |
| 39 (function testReflectConstructReturnSomethingElse() { |
| 40 var R = {}; |
| 41 function Constructor() { return R; } |
| 42 assertSame(R, Reflect.construct(Constructor, [])); |
| 43 })(); |
| 44 |
| 45 |
| 46 (function testAppliedArgumentsLength() { |
| 47 function lengthStrict() { 'use strict'; this.a = arguments.length; } |
| 48 function lengthSloppy() { this.a = arguments.length; } |
| 49 |
| 50 assertEquals(0, Reflect.construct(lengthStrict, []).a); |
| 51 assertEquals(0, Reflect.construct(lengthSloppy, []).a); |
| 52 assertEquals(0, Reflect.construct(lengthStrict, {}).a); |
| 53 assertEquals(0, Reflect.construct(lengthSloppy, {}).a); |
| 54 |
| 55 for (var i = 0; i < 256; ++i) { |
| 56 assertEquals(i, Reflect.construct(lengthStrict, new Array(i)).a); |
| 57 assertEquals(i, Reflect.construct(lengthSloppy, new Array(i)).a); |
| 58 assertEquals(i, Reflect.construct(lengthStrict, { length: i }).a); |
| 59 assertEquals(i, Reflect.construct(lengthSloppy, { length: i }).a); |
| 60 } |
| 61 })(); |
| 62 |
| 63 |
| 64 (function testAppliedArgumentsLengthThrows() { |
| 65 function noopStrict() { 'use strict'; } |
| 66 function noopSloppy() { } |
| 67 function MyError() {} |
| 68 |
| 69 var argsList = {}; |
| 70 Object.defineProperty(argsList, "length", { |
| 71 get: function() { throw new MyError(); } |
| 72 }); |
| 73 |
| 74 assertThrows(function() { |
| 75 Reflect.construct(noopStrict, argsList); |
| 76 }, MyError); |
| 77 |
| 78 assertThrows(function() { |
| 79 Reflect.construct(noopSloppy, argsList); |
| 80 }, MyError); |
| 81 })(); |
| 82 |
| 83 |
| 84 (function testAppliedArgumentsElementThrows() { |
| 85 function noopStrict() { 'use strict'; } |
| 86 function noopSloppy() { } |
| 87 function MyError() {} |
| 88 |
| 89 var argsList = { length: 1 }; |
| 90 Object.defineProperty(argsList, "0", { |
| 91 get: function() { throw new MyError(); } |
| 92 }); |
| 93 |
| 94 assertThrows(function() { |
| 95 Reflect.construct(noopStrict, argsList); |
| 96 }, MyError); |
| 97 |
| 98 assertThrows(function() { |
| 99 Reflect.construct(noopSloppy, argsList); |
| 100 }, MyError); |
| 101 })(); |
| 102 |
| 103 |
| 104 (function testAppliedNonFunctionStrict() { |
| 105 'use strict'; |
| 106 assertThrows(function() { Reflect.construct(void 0, []); }, TypeError); |
| 107 assertThrows(function() { Reflect.construct(null, []); }, TypeError); |
| 108 assertThrows(function() { Reflect.construct(123, []); }, TypeError); |
| 109 assertThrows(function() { Reflect.construct("str", []); }, TypeError); |
| 110 assertThrows(function() { Reflect.construct(Symbol("x"), []); }, TypeError); |
| 111 assertThrows(function() { Reflect.construct(/123/, []); }, TypeError); |
| 112 assertThrows(function() { Reflect.construct(NaN, []); }, TypeError); |
| 113 assertThrows(function() { Reflect.construct({}, []); }, TypeError); |
| 114 assertThrows(function() { Reflect.construct([], []); }, TypeError); |
| 115 })(); |
| 116 |
| 117 |
| 118 (function testAppliedNonFunctionSloppy() { |
| 119 assertThrows(function() { Reflect.construct(void 0, []); }, TypeError); |
| 120 assertThrows(function() { Reflect.construct(null, []); }, TypeError); |
| 121 assertThrows(function() { Reflect.construct(123, []); }, TypeError); |
| 122 assertThrows(function() { Reflect.construct("str", []); }, TypeError); |
| 123 assertThrows(function() { Reflect.construct(Symbol("x"), []); }, TypeError); |
| 124 assertThrows(function() { Reflect.construct(/123/, []); }, TypeError); |
| 125 assertThrows(function() { Reflect.construct(NaN, []); }, TypeError); |
| 126 assertThrows(function() { Reflect.construct({}, []); }, TypeError); |
| 127 assertThrows(function() { Reflect.construct([], []); }, TypeError); |
| 128 })(); |
| 129 |
| 130 |
| 131 (function testAppliedArgumentsNonList() { |
| 132 function noopStrict() { 'use strict'; } |
| 133 function noopSloppy() {} |
| 134 assertThrows(function() { Reflect.construct(noopStrict, null); }, TypeError); |
| 135 assertThrows(function() { Reflect.construct(noopSloppy, null); }, TypeError); |
| 136 assertThrows(function() { Reflect.construct(noopStrict, 1); }, TypeError); |
| 137 assertThrows(function() { Reflect.construct(noopSloppy, 1); }, TypeError); |
| 138 assertThrows(function() { Reflect.construct(noopStrict, "BAD"); }, TypeError); |
| 139 assertThrows(function() { Reflect.construct(noopSloppy, "BAD"); }, TypeError); |
| 140 assertThrows(function() { Reflect.construct(noopStrict, true); }, TypeError); |
| 141 assertThrows(function() { Reflect.construct(noopSloppy, true); }, TypeError); |
| 142 var sym = Symbol("x"); |
| 143 assertThrows(function() { Reflect.construct(noopStrict, sym); }, TypeError); |
| 144 assertThrows(function() { Reflect.construct(noopSloppy, sym); }, TypeError); |
| 145 })(); |
| 146 |
| 147 |
| 148 (function testAppliedArgumentValue() { |
| 149 function firstStrict(a) { 'use strict'; this.a = a; } |
| 150 function firstSloppy(a) { this.a = a; } |
| 151 function lastStrict(a) { |
| 152 'use strict'; this.a = arguments[arguments.length - 1]; } |
| 153 function lastSloppy(a) { this.a = arguments[arguments.length - 1]; } |
| 154 function sumStrict() { |
| 155 'use strict'; |
| 156 var sum = arguments[0]; |
| 157 for (var i = 1; i < arguments.length; ++i) { |
| 158 sum += arguments[i]; |
| 159 } |
| 160 this.a = sum; |
| 161 } |
| 162 function sumSloppy() { |
| 163 var sum = arguments[0]; |
| 164 for (var i = 1; i < arguments.length; ++i) { |
| 165 sum += arguments[i]; |
| 166 } |
| 167 this.a = sum; |
| 168 } |
| 169 |
| 170 assertEquals("OK!", Reflect.construct(firstStrict, ["OK!"]).a); |
| 171 assertEquals("OK!", Reflect.construct(firstSloppy, ["OK!"]).a); |
| 172 assertEquals("OK!", Reflect.construct(firstStrict, |
| 173 { 0: "OK!", length: 1 }).a); |
| 174 assertEquals("OK!", Reflect.construct(firstSloppy, |
| 175 { 0: "OK!", length: 1 }).a); |
| 176 assertEquals("OK!", Reflect.construct(lastStrict, |
| 177 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]).a); |
| 178 assertEquals("OK!", Reflect.construct(lastSloppy, |
| 179 [0, 1, 2, 3, 4, 5, 6, 7, 8, "OK!"]).a); |
| 180 assertEquals("OK!", Reflect.construct(lastStrict, |
| 181 { 9: "OK!", length: 10 }).a); |
| 182 assertEquals("OK!", Reflect.construct(lastSloppy, |
| 183 { 9: "OK!", length: 10 }).a); |
| 184 assertEquals("TEST", Reflect.construct(sumStrict, |
| 185 ["T", "E", "S", "T"]).a); |
| 186 assertEquals("TEST!!", Reflect.construct(sumStrict, |
| 187 ["T", "E", "S", "T", "!", "!"]).a); |
| 188 assertEquals(10, Reflect.construct(sumStrict, |
| 189 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }).a); |
| 190 assertEquals("TEST", Reflect.construct(sumSloppy, |
| 191 ["T", "E", "S", "T"]).a); |
| 192 assertEquals("TEST!!", Reflect.construct(sumSloppy, |
| 193 ["T", "E", "S", "T", "!", "!"]).a); |
| 194 assertEquals(10, Reflect.construct(sumSloppy, |
| 195 { 0: 1, 1: 2, 2: 3, 3: 4, length: 4 }).a); |
| 196 })(); |
OLD | NEW |