OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-arrays --harmony-classes | 5 // Flags: --harmony-arrays --harmony-classes |
6 | 6 |
7 (function testArrayConcatArity() { | 7 (function testArrayConcatArity() { |
8 "use strict"; | 8 "use strict"; |
9 assertEquals(1, Array.prototype.concat.length); | 9 assertEquals(1, Array.prototype.concat.length); |
10 })(); | 10 })(); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ta_by_len[i] = items[i] = modulo === false ? i : elems % modulo; | 225 ta_by_len[i] = items[i] = modulo === false ? i : elems % modulo; |
226 } | 226 } |
227 var ta = new type(items); | 227 var ta = new type(items); |
228 assertEquals([ta, ta], [].concat(ta, ta)); | 228 assertEquals([ta, ta], [].concat(ta, ta)); |
229 ta[Symbol.isConcatSpreadable] = true; | 229 ta[Symbol.isConcatSpreadable] = true; |
230 assertEquals(items, [].concat(ta)); | 230 assertEquals(items, [].concat(ta)); |
231 | 231 |
232 assertEquals([ta_by_len, ta_by_len], [].concat(ta_by_len, ta_by_len)); | 232 assertEquals([ta_by_len, ta_by_len], [].concat(ta_by_len, ta_by_len)); |
233 ta_by_len[Symbol.isConcatSpreadable] = true; | 233 ta_by_len[Symbol.isConcatSpreadable] = true; |
234 assertEquals(items, [].concat(ta_by_len)); | 234 assertEquals(items, [].concat(ta_by_len)); |
| 235 |
| 236 // TypedArray with fake `length`. |
| 237 ta = new type(1); |
| 238 var defValue = ta[0]; |
| 239 var expected = new Array(4000); |
| 240 expected[0] = defValue; |
| 241 |
| 242 Object.defineProperty(ta, "length", { value: 4000 }); |
| 243 ta[Symbol.isConcatSpreadable] = true; |
| 244 assertEquals(expected, [].concat(ta)); |
235 } | 245 } |
236 | 246 |
237 (function testConcatSmallTypedArray() { | 247 (function testConcatSmallTypedArray() { |
238 var max = [2^8, 2^16, 2^32, false, false]; | 248 var max = [2^8, 2^16, 2^32, false, false]; |
239 [ | 249 [ |
240 Uint8Array, | 250 Uint8Array, |
241 Uint16Array, | 251 Uint16Array, |
242 Uint32Array, | 252 Uint32Array, |
243 Float32Array, | 253 Float32Array, |
244 Float64Array | 254 Float64Array |
(...skipping 14 matching lines...) Expand all Loading... |
259 ].forEach(function(ctor, i) { | 269 ].forEach(function(ctor, i) { |
260 testConcatTypedArray(ctor, 4000, max[i]); | 270 testConcatTypedArray(ctor, 4000, max[i]); |
261 }); | 271 }); |
262 })(); | 272 })(); |
263 | 273 |
264 | 274 |
265 (function testConcatStrictArguments() { | 275 (function testConcatStrictArguments() { |
266 var args = (function(a, b, c) { "use strict"; return arguments; })(1,2,3); | 276 var args = (function(a, b, c) { "use strict"; return arguments; })(1,2,3); |
267 args[Symbol.isConcatSpreadable] = true; | 277 args[Symbol.isConcatSpreadable] = true; |
268 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); | 278 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); |
| 279 |
| 280 Object.defineProperty(args, "length", { value: 6 }); |
| 281 assertEquals([1, 2, 3, void 0, void 0, void 0], [].concat(args)); |
269 })(); | 282 })(); |
270 | 283 |
271 | 284 |
272 (function testConcatSloppyArguments() { | 285 (function testConcatSloppyArguments() { |
273 var args = (function(a, b, c) { return arguments; })(1,2,3); | 286 var args = (function(a, b, c) { return arguments; })(1,2,3); |
274 args[Symbol.isConcatSpreadable] = true; | 287 args[Symbol.isConcatSpreadable] = true; |
275 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); | 288 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); |
| 289 |
| 290 Object.defineProperty(args, "length", { value: 6 }); |
| 291 assertEquals([1, 2, 3, void 0, void 0, void 0], [].concat(args)); |
276 })(); | 292 })(); |
277 | 293 |
278 | 294 |
279 (function testConcatSloppyArgumentsWithDupes() { | 295 (function testConcatSloppyArgumentsWithDupes() { |
280 var args = (function(a, a, a) { return arguments; })(1,2,3); | 296 var args = (function(a, a, a) { return arguments; })(1,2,3); |
281 args[Symbol.isConcatSpreadable] = true; | 297 args[Symbol.isConcatSpreadable] = true; |
282 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); | 298 assertEquals([1, 2, 3, 1, 2, 3], [].concat(args, args)); |
| 299 |
| 300 Object.defineProperty(args, "length", { value: 6 }); |
| 301 assertEquals([1, 2, 3, void 0, void 0, void 0], [].concat(args)); |
283 })(); | 302 })(); |
284 | 303 |
285 | 304 |
286 (function testConcatSloppyArgumentsThrows() { | 305 (function testConcatSloppyArgumentsThrows() { |
287 function MyError() {} | 306 function MyError() {} |
288 var args = (function(a) { return arguments; })(1,2,3); | 307 var args = (function(a) { return arguments; })(1,2,3); |
289 Object.defineProperty(args, 0, { | 308 Object.defineProperty(args, 0, { |
290 get: function() { throw new MyError(); } | 309 get: function() { throw new MyError(); } |
291 }); | 310 }); |
292 args[Symbol.isConcatSpreadable] = true; | 311 args[Symbol.isConcatSpreadable] = true; |
293 assertThrows(function() { | 312 assertThrows(function() { |
294 return [].concat(args, args); | 313 return [].concat(args, args); |
295 }, MyError); | 314 }, MyError); |
296 })(); | 315 })(); |
297 | 316 |
298 | 317 |
299 (function testConcatHoleySloppyArguments() { | 318 (function testConcatHoleySloppyArguments() { |
300 var args = (function(a) { return arguments; })(1,2,3); | 319 var args = (function(a) { return arguments; })(1,2,3); |
301 delete args[1]; | 320 delete args[1]; |
302 args[Symbol.isConcatSpreadable] = true; | 321 args[Symbol.isConcatSpreadable] = true; |
303 assertEquals([1, void 0, 3, 1, void 0, 3], [].concat(args, args)); | 322 assertEquals([1, void 0, 3, 1, void 0, 3], [].concat(args, args)); |
304 })(); | 323 })(); |
305 | 324 |
306 | 325 |
| 326 (function testConcatSpreadableStringWrapper() { |
| 327 "use strict"; |
| 328 var str1 = new String("yuck\uD83D\uDCA9") |
| 329 // String wrapper objects are not concat-spreadable by default |
| 330 assertEquals([str1], [].concat(str1)); |
| 331 |
| 332 // String wrapper objects may be individually concat-spreadable |
| 333 str1[Symbol.isConcatSpreadable] = true; |
| 334 assertEquals(["y", "u", "c", "k", "\uD83D", "\uDCA9"], |
| 335 [].concat(str1)); |
| 336 |
| 337 String.prototype[Symbol.isConcatSpreadable] = true; |
| 338 // String wrapper objects may be concat-spreadable |
| 339 assertEquals(["y", "u", "c", "k", "\uD83D", "\uDCA9"], |
| 340 [].concat(new String("yuck\uD83D\uDCA9"))); |
| 341 |
| 342 // String values are never concat-spreadable |
| 343 assertEquals(["yuck\uD83D\uDCA9"], [].concat("yuck\uD83D\uDCA9")); |
| 344 delete String.prototype[Symbol.isConcatSpreadable]; |
| 345 })(); |
| 346 |
| 347 |
| 348 (function testConcatSpreadableBooleanWrapper() { |
| 349 "use strict"; |
| 350 var bool = new Boolean(true) |
| 351 // Boolean wrapper objects are not concat-spreadable by default |
| 352 assertEquals([bool], [].concat(bool)); |
| 353 |
| 354 // Boolean wrapper objects may be individually concat-spreadable |
| 355 bool[Symbol.isConcatSpreadable] = true; |
| 356 bool.length = 3; |
| 357 bool[0] = 1, bool[1] = 2, bool[2] = 3; |
| 358 assertEquals([1, 2, 3], [].concat(bool)); |
| 359 |
| 360 Boolean.prototype[Symbol.isConcatSpreadable] = true; |
| 361 // Boolean wrapper objects may be concat-spreadable |
| 362 assertEquals([], [].concat(new Boolean(true))); |
| 363 Boolean.prototype[0] = 1; |
| 364 Boolean.prototype[1] = 2; |
| 365 Boolean.prototype[2] = 3; |
| 366 Boolean.prototype.length = 3; |
| 367 assertEquals([1,2,3], [].concat(new Boolean(true))); |
| 368 |
| 369 // Boolean values are never concat-spreadable |
| 370 assertEquals([true], [].concat(true)); |
| 371 delete Boolean.prototype[Symbol.isConcatSpreadable]; |
| 372 delete Boolean.prototype[0]; |
| 373 delete Boolean.prototype[1]; |
| 374 delete Boolean.prototype[2]; |
| 375 delete Boolean.prototype.length; |
| 376 })(); |
| 377 |
| 378 |
| 379 (function testConcatSpreadableNumberWrapper() { |
| 380 "use strict"; |
| 381 var num = new Number(true) |
| 382 // Number wrapper objects are not concat-spreadable by default |
| 383 assertEquals([num], [].concat(num)); |
| 384 |
| 385 // Number wrapper objects may be individually concat-spreadable |
| 386 num[Symbol.isConcatSpreadable] = true; |
| 387 num.length = 3; |
| 388 num[0] = 1, num[1] = 2, num[2] = 3; |
| 389 assertEquals([1, 2, 3], [].concat(num)); |
| 390 |
| 391 Number.prototype[Symbol.isConcatSpreadable] = true; |
| 392 // Number wrapper objects may be concat-spreadable |
| 393 assertEquals([], [].concat(new Number(123))); |
| 394 Number.prototype[0] = 1; |
| 395 Number.prototype[1] = 2; |
| 396 Number.prototype[2] = 3; |
| 397 Number.prototype.length = 3; |
| 398 assertEquals([1,2,3], [].concat(new Number(123))); |
| 399 |
| 400 // Number values are never concat-spreadable |
| 401 assertEquals([true], [].concat(true)); |
| 402 delete Number.prototype[Symbol.isConcatSpreadable]; |
| 403 delete Number.prototype[0]; |
| 404 delete Number.prototype[1]; |
| 405 delete Number.prototype[2]; |
| 406 delete Number.prototype.length; |
| 407 })(); |
| 408 |
| 409 |
| 410 (function testConcatSpreadableFunction() { |
| 411 "use strict"; |
| 412 var fn = function(a, b, c) {} |
| 413 // Functions are not concat-spreadable by default |
| 414 assertEquals([fn], [].concat(fn)); |
| 415 |
| 416 // Functions may be individually concat-spreadable |
| 417 fn[Symbol.isConcatSpreadable] = true; |
| 418 fn[0] = 1, fn[1] = 2, fn[2] = 3; |
| 419 assertEquals([1, 2, 3], [].concat(fn)); |
| 420 |
| 421 Function.prototype[Symbol.isConcatSpreadable] = true; |
| 422 // Functions may be concat-spreadable |
| 423 assertEquals([void 0, void 0, void 0], [].concat(function(a,b,c) {})); |
| 424 Function.prototype[0] = 1; |
| 425 Function.prototype[1] = 2; |
| 426 Function.prototype[2] = 3; |
| 427 assertEquals([1,2,3], [].concat(function(a, b, c) {})); |
| 428 |
| 429 delete Function.prototype[Symbol.isConcatSpreadable]; |
| 430 delete Function.prototype[0]; |
| 431 delete Function.prototype[1]; |
| 432 delete Function.prototype[2]; |
| 433 })(); |
| 434 |
| 435 |
| 436 (function testConcatSpreadableRegExp() { |
| 437 "use strict"; |
| 438 var re = /abc/; |
| 439 // RegExps are not concat-spreadable by default |
| 440 assertEquals([re], [].concat(re)); |
| 441 |
| 442 // RegExps may be individually concat-spreadable |
| 443 re[Symbol.isConcatSpreadable] = true; |
| 444 re[0] = 1, re[1] = 2, re[2] = 3, re.length = 3; |
| 445 assertEquals([1, 2, 3], [].concat(re)); |
| 446 |
| 447 // RegExps may be concat-spreadable |
| 448 RegExp.prototype[Symbol.isConcatSpreadable] = true; |
| 449 RegExp.prototype.length = 3; |
| 450 |
| 451 assertEquals([void 0, void 0, void 0], [].concat(/abc/)); |
| 452 RegExp.prototype[0] = 1; |
| 453 RegExp.prototype[1] = 2; |
| 454 RegExp.prototype[2] = 3; |
| 455 assertEquals([1,2,3], [].concat(/abc/)); |
| 456 |
| 457 delete RegExp.prototype[Symbol.isConcatSpreadable]; |
| 458 delete RegExp.prototype[0]; |
| 459 delete RegExp.prototype[1]; |
| 460 delete RegExp.prototype[2]; |
| 461 delete RegExp.prototype.length; |
| 462 })(); |
| 463 |
| 464 |
| 465 (function testArrayConcatSpreadableSparseObject() { |
| 466 "use strict"; |
| 467 var obj = { length: 5 }; |
| 468 obj[Symbol.isConcatSpreadable] = true; |
| 469 assertEquals([void 0, void 0, void 0, void 0, void 0], [].concat(obj)); |
| 470 |
| 471 obj.length = 4000; |
| 472 assertEquals(new Array(4000), [].concat(obj)); |
| 473 })(); |
| 474 |
| 475 |
307 // ES5 tests | 476 // ES5 tests |
308 (function testArrayConcatES5() { | 477 (function testArrayConcatES5() { |
309 "use strict"; | 478 "use strict"; |
310 var poses; | 479 var poses; |
311 var pos; | 480 var pos; |
312 | 481 |
313 poses = [140, 4000000000]; | 482 poses = [140, 4000000000]; |
314 while (pos = poses.shift()) { | 483 while (pos = poses.shift()) { |
315 var a = new Array(pos); | 484 var a = new Array(pos); |
316 var array_proto = []; | 485 var array_proto = []; |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 arr3.length = 10000; | 677 arr3.length = 10000; |
509 for (var i = 0; i < 100; i++) { | 678 for (var i = 0; i < 100; i++) { |
510 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); | 679 Object.defineProperty(arr3, i * i, {get: mkGetter(i)}); |
511 expectedTrace[i] = i; | 680 expectedTrace[i] = i; |
512 expectedTrace[100 + i] = i; | 681 expectedTrace[100 + i] = i; |
513 } | 682 } |
514 var r4 = [0].concat(arr3, arr3); | 683 var r4 = [0].concat(arr3, arr3); |
515 assertEquals(1 + arr3.length * 2, r4.length); | 684 assertEquals(1 + arr3.length * 2, r4.length); |
516 assertEquals(expectedTrace, trace); | 685 assertEquals(expectedTrace, trace); |
517 })(); | 686 })(); |
OLD | NEW |