Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 | 368 |
| 369 function ArrayToLocaleString() { | 369 function ArrayToLocaleString() { |
| 370 if (!IS_ARRAY(this)) { | 370 if (!IS_ARRAY(this)) { |
| 371 throw new $TypeError('Array.prototype.toString is not generic'); | 371 throw new $TypeError('Array.prototype.toString is not generic'); |
| 372 } | 372 } |
| 373 return Join(this, this.length, ',', ConvertToLocaleString); | 373 return Join(this, this.length, ',', ConvertToLocaleString); |
| 374 } | 374 } |
| 375 | 375 |
| 376 | 376 |
| 377 function ArrayJoin(separator) { | 377 function ArrayJoin(separator) { |
| 378 if (IS_NULL_OR_UNDEFINED(this)) { | |
|
Lasse Reichstein
2011/04/28 10:44:38
Is this correct behavior for an undetectable objec
Lasse Reichstein
2011/04/28 10:50:01
Just checked Firefox 4. It does not throw when pas
MarkM
2011/04/28 15:40:45
Do we have any undetectable objects other that doc
Rico
2011/05/03 08:47:20
Added checks for the undetectable object in all ca
| |
| 379 throw MakeTypeError("obj_ctor_property_non_object", ["Array.join"]); | |
|
Lasse Reichstein
2011/04/28 10:44:38
The name shouldn't be "Array.prototype.join"?
MarkM
2011/04/28 18:58:21
For all these generics, I wonder about the string
Rico
2011/05/03 08:47:20
Changed
Rico
2011/05/03 08:47:20
Changed to "FUNCTIONNAME called on null or undefin
| |
| 380 } | |
| 381 | |
| 378 if (IS_UNDEFINED(separator)) { | 382 if (IS_UNDEFINED(separator)) { |
| 379 separator = ','; | 383 separator = ','; |
| 380 } else if (!IS_STRING(separator)) { | 384 } else if (!IS_STRING(separator)) { |
| 381 separator = NonStringToString(separator); | 385 separator = NonStringToString(separator); |
| 382 } | 386 } |
| 383 | 387 |
| 384 var result = %_FastAsciiArrayJoin(this, separator); | 388 var result = %_FastAsciiArrayJoin(this, separator); |
| 385 if (!IS_UNDEFINED(result)) return result; | 389 if (!IS_UNDEFINED(result)) return result; |
| 386 | 390 |
| 387 return Join(this, TO_UINT32(this.length), separator, ConvertToString); | 391 return Join(this, TO_UINT32(this.length), separator, ConvertToString); |
| 388 } | 392 } |
| 389 | 393 |
| 390 | 394 |
| 391 // Removes the last element from the array and returns it. See | 395 // Removes the last element from the array and returns it. See |
| 392 // ECMA-262, section 15.4.4.6. | 396 // ECMA-262, section 15.4.4.6. |
| 393 function ArrayPop() { | 397 function ArrayPop() { |
| 398 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 399 throw MakeTypeError("obj_ctor_property_non_object", ["Array.pop"]); | |
| 400 } | |
| 401 | |
| 394 var n = TO_UINT32(this.length); | 402 var n = TO_UINT32(this.length); |
| 395 if (n == 0) { | 403 if (n == 0) { |
| 396 this.length = n; | 404 this.length = n; |
| 397 return; | 405 return; |
| 398 } | 406 } |
| 399 n--; | 407 n--; |
| 400 var value = this[n]; | 408 var value = this[n]; |
| 401 this.length = n; | 409 this.length = n; |
| 402 delete this[n]; | 410 delete this[n]; |
| 403 return value; | 411 return value; |
| 404 } | 412 } |
| 405 | 413 |
| 406 | 414 |
| 407 // Appends the arguments to the end of the array and returns the new | 415 // Appends the arguments to the end of the array and returns the new |
| 408 // length of the array. See ECMA-262, section 15.4.4.7. | 416 // length of the array. See ECMA-262, section 15.4.4.7. |
| 409 function ArrayPush() { | 417 function ArrayPush() { |
| 418 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 419 throw MakeTypeError("obj_ctor_property_non_object", ["Array.push"]); | |
| 420 } | |
| 421 | |
| 410 var n = TO_UINT32(this.length); | 422 var n = TO_UINT32(this.length); |
| 411 var m = %_ArgumentsLength(); | 423 var m = %_ArgumentsLength(); |
| 412 for (var i = 0; i < m; i++) { | 424 for (var i = 0; i < m; i++) { |
| 413 this[i+n] = %_Arguments(i); | 425 this[i+n] = %_Arguments(i); |
| 414 } | 426 } |
| 415 this.length = n + m; | 427 this.length = n + m; |
| 416 return this.length; | 428 return this.length; |
| 417 } | 429 } |
| 418 | 430 |
| 419 | 431 |
| 420 function ArrayConcat(arg1) { // length == 1 | 432 function ArrayConcat(arg1) { // length == 1 |
| 433 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 434 throw MakeTypeError("obj_ctor_property_non_object", ["Array.concat"]); | |
| 435 } | |
| 436 | |
| 421 var arg_count = %_ArgumentsLength(); | 437 var arg_count = %_ArgumentsLength(); |
| 422 var arrays = new InternalArray(1 + arg_count); | 438 var arrays = new InternalArray(1 + arg_count); |
| 423 arrays[0] = this; | 439 arrays[0] = this; |
| 424 for (var i = 0; i < arg_count; i++) { | 440 for (var i = 0; i < arg_count; i++) { |
| 425 arrays[i + 1] = %_Arguments(i); | 441 arrays[i + 1] = %_Arguments(i); |
| 426 } | 442 } |
| 427 | 443 |
| 428 return %ArrayConcat(arrays); | 444 return %ArrayConcat(arrays); |
| 429 } | 445 } |
| 430 | 446 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 if (!IS_UNDEFINED(current_j) || high in array) { | 483 if (!IS_UNDEFINED(current_j) || high in array) { |
| 468 array[low] = current_j; | 484 array[low] = current_j; |
| 469 delete array[high]; | 485 delete array[high]; |
| 470 } | 486 } |
| 471 } | 487 } |
| 472 } | 488 } |
| 473 } | 489 } |
| 474 | 490 |
| 475 | 491 |
| 476 function ArrayReverse() { | 492 function ArrayReverse() { |
| 493 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 494 throw MakeTypeError("obj_ctor_property_non_object", ["Array.reverse"]); | |
| 495 } | |
| 496 | |
| 477 var j = TO_UINT32(this.length) - 1; | 497 var j = TO_UINT32(this.length) - 1; |
| 478 | 498 |
| 479 if (UseSparseVariant(this, j, IS_ARRAY(this))) { | 499 if (UseSparseVariant(this, j, IS_ARRAY(this))) { |
| 480 SparseReverse(this, j+1); | 500 SparseReverse(this, j+1); |
| 481 return this; | 501 return this; |
| 482 } | 502 } |
| 483 | 503 |
| 484 for (var i = 0; i < j; i++, j--) { | 504 for (var i = 0; i < j; i++, j--) { |
| 485 var current_i = this[i]; | 505 var current_i = this[i]; |
| 486 if (!IS_UNDEFINED(current_i) || i in this) { | 506 if (!IS_UNDEFINED(current_i) || i in this) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 498 this[i] = current_j; | 518 this[i] = current_j; |
| 499 delete this[j]; | 519 delete this[j]; |
| 500 } | 520 } |
| 501 } | 521 } |
| 502 } | 522 } |
| 503 return this; | 523 return this; |
| 504 } | 524 } |
| 505 | 525 |
| 506 | 526 |
| 507 function ArrayShift() { | 527 function ArrayShift() { |
| 528 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 529 throw MakeTypeError("obj_ctor_property_non_object", ["Array.shift"]); | |
| 530 } | |
| 531 | |
| 508 var len = TO_UINT32(this.length); | 532 var len = TO_UINT32(this.length); |
| 509 | 533 |
| 510 if (len === 0) { | 534 if (len === 0) { |
| 511 this.length = 0; | 535 this.length = 0; |
| 512 return; | 536 return; |
| 513 } | 537 } |
| 514 | 538 |
| 515 var first = this[0]; | 539 var first = this[0]; |
| 516 | 540 |
| 517 if (IS_ARRAY(this)) | 541 if (IS_ARRAY(this)) |
| 518 SmartMove(this, 0, 1, len, 0); | 542 SmartMove(this, 0, 1, len, 0); |
| 519 else | 543 else |
| 520 SimpleMove(this, 0, 1, len, 0); | 544 SimpleMove(this, 0, 1, len, 0); |
| 521 | 545 |
| 522 this.length = len - 1; | 546 this.length = len - 1; |
| 523 | 547 |
| 524 return first; | 548 return first; |
| 525 } | 549 } |
| 526 | 550 |
| 527 | 551 |
| 528 function ArrayUnshift(arg1) { // length == 1 | 552 function ArrayUnshift(arg1) { // length == 1 |
| 553 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 554 throw MakeTypeError("obj_ctor_property_non_object", ["Array.unshift"]); | |
| 555 } | |
| 556 | |
| 529 var len = TO_UINT32(this.length); | 557 var len = TO_UINT32(this.length); |
| 530 var num_arguments = %_ArgumentsLength(); | 558 var num_arguments = %_ArgumentsLength(); |
| 531 | 559 |
| 532 if (IS_ARRAY(this)) | 560 if (IS_ARRAY(this)) |
| 533 SmartMove(this, 0, 0, len, num_arguments); | 561 SmartMove(this, 0, 0, len, num_arguments); |
| 534 else | 562 else |
| 535 SimpleMove(this, 0, 0, len, num_arguments); | 563 SimpleMove(this, 0, 0, len, num_arguments); |
| 536 | 564 |
| 537 for (var i = 0; i < num_arguments; i++) { | 565 for (var i = 0; i < num_arguments; i++) { |
| 538 this[i] = %_Arguments(i); | 566 this[i] = %_Arguments(i); |
| 539 } | 567 } |
| 540 | 568 |
| 541 this.length = len + num_arguments; | 569 this.length = len + num_arguments; |
| 542 | 570 |
| 543 return len + num_arguments; | 571 return len + num_arguments; |
| 544 } | 572 } |
| 545 | 573 |
| 546 | 574 |
| 547 function ArraySlice(start, end) { | 575 function ArraySlice(start, end) { |
| 576 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 577 throw MakeTypeError("obj_ctor_property_non_object", ["Array.slice"]); | |
| 578 } | |
| 579 | |
| 548 var len = TO_UINT32(this.length); | 580 var len = TO_UINT32(this.length); |
| 549 var start_i = TO_INTEGER(start); | 581 var start_i = TO_INTEGER(start); |
| 550 var end_i = len; | 582 var end_i = len; |
| 551 | 583 |
| 552 if (end !== void 0) end_i = TO_INTEGER(end); | 584 if (end !== void 0) end_i = TO_INTEGER(end); |
| 553 | 585 |
| 554 if (start_i < 0) { | 586 if (start_i < 0) { |
| 555 start_i += len; | 587 start_i += len; |
| 556 if (start_i < 0) start_i = 0; | 588 if (start_i < 0) start_i = 0; |
| 557 } else { | 589 } else { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 575 SimpleSlice(this, start_i, end_i - start_i, len, result); | 607 SimpleSlice(this, start_i, end_i - start_i, len, result); |
| 576 } | 608 } |
| 577 | 609 |
| 578 result.length = end_i - start_i; | 610 result.length = end_i - start_i; |
| 579 | 611 |
| 580 return result; | 612 return result; |
| 581 } | 613 } |
| 582 | 614 |
| 583 | 615 |
| 584 function ArraySplice(start, delete_count) { | 616 function ArraySplice(start, delete_count) { |
| 617 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 618 throw MakeTypeError("obj_ctor_property_non_object", ["Array.splice"]); | |
| 619 } | |
| 620 | |
| 585 var num_arguments = %_ArgumentsLength(); | 621 var num_arguments = %_ArgumentsLength(); |
| 586 | 622 |
| 587 var len = TO_UINT32(this.length); | 623 var len = TO_UINT32(this.length); |
| 588 var start_i = TO_INTEGER(start); | 624 var start_i = TO_INTEGER(start); |
| 589 | 625 |
| 590 if (start_i < 0) { | 626 if (start_i < 0) { |
| 591 start_i += len; | 627 start_i += len; |
| 592 if (start_i < 0) start_i = 0; | 628 if (start_i < 0) start_i = 0; |
| 593 } else { | 629 } else { |
| 594 if (start_i > len) start_i = len; | 630 if (start_i > len) start_i = len; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 this[i++] = %_Arguments(arguments_index++); | 682 this[i++] = %_Arguments(arguments_index++); |
| 647 } | 683 } |
| 648 this.length = len - del_count + num_additional_args; | 684 this.length = len - del_count + num_additional_args; |
| 649 | 685 |
| 650 // Return the deleted elements. | 686 // Return the deleted elements. |
| 651 return deleted_elements; | 687 return deleted_elements; |
| 652 } | 688 } |
| 653 | 689 |
| 654 | 690 |
| 655 function ArraySort(comparefn) { | 691 function ArraySort(comparefn) { |
| 692 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 693 throw MakeTypeError("obj_ctor_property_non_object", ["Array.sort"]); | |
| 694 } | |
| 695 | |
| 656 // In-place QuickSort algorithm. | 696 // In-place QuickSort algorithm. |
| 657 // For short (length <= 22) arrays, insertion sort is used for efficiency. | 697 // For short (length <= 22) arrays, insertion sort is used for efficiency. |
| 658 | 698 |
| 659 if (!IS_FUNCTION(comparefn)) { | 699 if (!IS_FUNCTION(comparefn)) { |
| 660 comparefn = function (x, y) { | 700 comparefn = function (x, y) { |
| 661 if (x === y) return 0; | 701 if (x === y) return 0; |
| 662 if (%_IsSmi(x) && %_IsSmi(y)) { | 702 if (%_IsSmi(x) && %_IsSmi(y)) { |
| 663 return %SmiLexicographicCompare(x, y); | 703 return %SmiLexicographicCompare(x, y); |
| 664 } | 704 } |
| 665 x = ToString(x); | 705 x = ToString(x); |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 907 } | 947 } |
| 908 | 948 |
| 909 return this; | 949 return this; |
| 910 } | 950 } |
| 911 | 951 |
| 912 | 952 |
| 913 // The following functions cannot be made efficient on sparse arrays while | 953 // The following functions cannot be made efficient on sparse arrays while |
| 914 // preserving the semantics, since the calls to the receiver function can add | 954 // preserving the semantics, since the calls to the receiver function can add |
| 915 // or delete elements from the array. | 955 // or delete elements from the array. |
| 916 function ArrayFilter(f, receiver) { | 956 function ArrayFilter(f, receiver) { |
| 957 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 958 throw MakeTypeError("obj_ctor_property_non_object", ["Array.filter"]); | |
| 959 } | |
| 960 | |
| 917 if (!IS_FUNCTION(f)) { | 961 if (!IS_FUNCTION(f)) { |
| 918 throw MakeTypeError('called_non_callable', [ f ]); | 962 throw MakeTypeError('called_non_callable', [ f ]); |
| 919 } | 963 } |
| 920 // Pull out the length so that modifications to the length in the | 964 // Pull out the length so that modifications to the length in the |
| 921 // loop will not affect the looping. | 965 // loop will not affect the looping. |
| 922 var length = this.length; | 966 var length = this.length; |
| 923 var result = []; | 967 var result = []; |
| 924 var result_length = 0; | 968 var result_length = 0; |
| 925 for (var i = 0; i < length; i++) { | 969 for (var i = 0; i < length; i++) { |
| 926 var current = this[i]; | 970 var current = this[i]; |
| 927 if (!IS_UNDEFINED(current) || i in this) { | 971 if (!IS_UNDEFINED(current) || i in this) { |
| 928 if (f.call(receiver, current, i, this)) { | 972 if (f.call(receiver, current, i, this)) { |
| 929 result[result_length++] = current; | 973 result[result_length++] = current; |
| 930 } | 974 } |
| 931 } | 975 } |
| 932 } | 976 } |
| 933 return result; | 977 return result; |
| 934 } | 978 } |
| 935 | 979 |
| 936 | 980 |
| 937 function ArrayForEach(f, receiver) { | 981 function ArrayForEach(f, receiver) { |
| 982 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 983 throw MakeTypeError("obj_ctor_property_non_object", ["Array.forEach"]); | |
| 984 } | |
| 985 | |
| 938 if (!IS_FUNCTION(f)) { | 986 if (!IS_FUNCTION(f)) { |
| 939 throw MakeTypeError('called_non_callable', [ f ]); | 987 throw MakeTypeError('called_non_callable', [ f ]); |
| 940 } | 988 } |
| 941 // Pull out the length so that modifications to the length in the | 989 // Pull out the length so that modifications to the length in the |
| 942 // loop will not affect the looping. | 990 // loop will not affect the looping. |
| 943 var length = TO_UINT32(this.length); | 991 var length = TO_UINT32(this.length); |
| 944 for (var i = 0; i < length; i++) { | 992 for (var i = 0; i < length; i++) { |
| 945 var current = this[i]; | 993 var current = this[i]; |
| 946 if (!IS_UNDEFINED(current) || i in this) { | 994 if (!IS_UNDEFINED(current) || i in this) { |
| 947 f.call(receiver, current, i, this); | 995 f.call(receiver, current, i, this); |
|
Lasse Reichstein
2011/04/28 10:44:38
While you are here, make this use %_CallFunction (
Rico
2011/05/03 08:47:20
I don't want to make this cl even larger, lets do
| |
| 948 } | 996 } |
| 949 } | 997 } |
| 950 } | 998 } |
| 951 | 999 |
| 952 | 1000 |
| 953 // Executes the function once for each element present in the | 1001 // Executes the function once for each element present in the |
| 954 // array until it finds one where callback returns true. | 1002 // array until it finds one where callback returns true. |
| 955 function ArraySome(f, receiver) { | 1003 function ArraySome(f, receiver) { |
| 1004 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1005 throw MakeTypeError("obj_ctor_property_non_object", ["Array.some"]); | |
| 1006 } | |
| 1007 | |
| 956 if (!IS_FUNCTION(f)) { | 1008 if (!IS_FUNCTION(f)) { |
| 957 throw MakeTypeError('called_non_callable', [ f ]); | 1009 throw MakeTypeError('called_non_callable', [ f ]); |
| 958 } | 1010 } |
| 959 // Pull out the length so that modifications to the length in the | 1011 // Pull out the length so that modifications to the length in the |
| 960 // loop will not affect the looping. | 1012 // loop will not affect the looping. |
| 961 var length = TO_UINT32(this.length); | 1013 var length = TO_UINT32(this.length); |
| 962 for (var i = 0; i < length; i++) { | 1014 for (var i = 0; i < length; i++) { |
| 963 var current = this[i]; | 1015 var current = this[i]; |
| 964 if (!IS_UNDEFINED(current) || i in this) { | 1016 if (!IS_UNDEFINED(current) || i in this) { |
| 965 if (f.call(receiver, current, i, this)) return true; | 1017 if (f.call(receiver, current, i, this)) return true; |
| 966 } | 1018 } |
| 967 } | 1019 } |
| 968 return false; | 1020 return false; |
| 969 } | 1021 } |
| 970 | 1022 |
| 971 | 1023 |
| 972 function ArrayEvery(f, receiver) { | 1024 function ArrayEvery(f, receiver) { |
| 1025 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1026 throw MakeTypeError("obj_ctor_property_non_object", ["Array.every"]); | |
| 1027 } | |
| 1028 | |
| 973 if (!IS_FUNCTION(f)) { | 1029 if (!IS_FUNCTION(f)) { |
| 974 throw MakeTypeError('called_non_callable', [ f ]); | 1030 throw MakeTypeError('called_non_callable', [ f ]); |
| 975 } | 1031 } |
| 976 // Pull out the length so that modifications to the length in the | 1032 // Pull out the length so that modifications to the length in the |
| 977 // loop will not affect the looping. | 1033 // loop will not affect the looping. |
| 978 var length = TO_UINT32(this.length); | 1034 var length = TO_UINT32(this.length); |
| 979 for (var i = 0; i < length; i++) { | 1035 for (var i = 0; i < length; i++) { |
| 980 var current = this[i]; | 1036 var current = this[i]; |
| 981 if (!IS_UNDEFINED(current) || i in this) { | 1037 if (!IS_UNDEFINED(current) || i in this) { |
| 982 if (!f.call(receiver, current, i, this)) return false; | 1038 if (!f.call(receiver, current, i, this)) return false; |
| 983 } | 1039 } |
| 984 } | 1040 } |
| 985 return true; | 1041 return true; |
| 986 } | 1042 } |
| 987 | 1043 |
| 988 function ArrayMap(f, receiver) { | 1044 function ArrayMap(f, receiver) { |
| 1045 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1046 throw MakeTypeError("obj_ctor_property_non_object", ["Array.map"]); | |
| 1047 } | |
| 1048 | |
| 989 if (!IS_FUNCTION(f)) { | 1049 if (!IS_FUNCTION(f)) { |
| 990 throw MakeTypeError('called_non_callable', [ f ]); | 1050 throw MakeTypeError('called_non_callable', [ f ]); |
| 991 } | 1051 } |
| 992 // Pull out the length so that modifications to the length in the | 1052 // Pull out the length so that modifications to the length in the |
| 993 // loop will not affect the looping. | 1053 // loop will not affect the looping. |
| 994 var length = TO_UINT32(this.length); | 1054 var length = TO_UINT32(this.length); |
| 995 var result = new $Array(); | 1055 var result = new $Array(); |
| 996 var accumulator = new InternalArray(length); | 1056 var accumulator = new InternalArray(length); |
| 997 for (var i = 0; i < length; i++) { | 1057 for (var i = 0; i < length; i++) { |
| 998 var current = this[i]; | 1058 var current = this[i]; |
| 999 if (!IS_UNDEFINED(current) || i in this) { | 1059 if (!IS_UNDEFINED(current) || i in this) { |
| 1000 accumulator[i] = f.call(receiver, current, i, this); | 1060 accumulator[i] = f.call(receiver, current, i, this); |
| 1001 } | 1061 } |
| 1002 } | 1062 } |
| 1003 %MoveArrayContents(accumulator, result); | 1063 %MoveArrayContents(accumulator, result); |
| 1004 return result; | 1064 return result; |
| 1005 } | 1065 } |
| 1006 | 1066 |
| 1007 | 1067 |
| 1008 function ArrayIndexOf(element, index) { | 1068 function ArrayIndexOf(element, index) { |
| 1069 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1070 throw MakeTypeError("obj_ctor_property_non_object", ["Array.indexOf"]); | |
| 1071 } | |
| 1072 | |
| 1009 var length = TO_UINT32(this.length); | 1073 var length = TO_UINT32(this.length); |
| 1010 if (length == 0) return -1; | 1074 if (length == 0) return -1; |
| 1011 if (IS_UNDEFINED(index)) { | 1075 if (IS_UNDEFINED(index)) { |
| 1012 index = 0; | 1076 index = 0; |
| 1013 } else { | 1077 } else { |
| 1014 index = TO_INTEGER(index); | 1078 index = TO_INTEGER(index); |
| 1015 // If index is negative, index from the end of the array. | 1079 // If index is negative, index from the end of the array. |
| 1016 if (index < 0) { | 1080 if (index < 0) { |
| 1017 index = length + index; | 1081 index = length + index; |
| 1018 // If index is still negative, search the entire array. | 1082 // If index is still negative, search the entire array. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1056 for (var i = min; i < max; i++) { | 1120 for (var i = min; i < max; i++) { |
| 1057 if (IS_UNDEFINED(this[i]) && i in this) { | 1121 if (IS_UNDEFINED(this[i]) && i in this) { |
| 1058 return i; | 1122 return i; |
| 1059 } | 1123 } |
| 1060 } | 1124 } |
| 1061 return -1; | 1125 return -1; |
| 1062 } | 1126 } |
| 1063 | 1127 |
| 1064 | 1128 |
| 1065 function ArrayLastIndexOf(element, index) { | 1129 function ArrayLastIndexOf(element, index) { |
| 1130 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1131 throw MakeTypeError("obj_ctor_property_non_object", ["Array.lastIndexOf"]); | |
| 1132 } | |
| 1133 | |
| 1066 var length = TO_UINT32(this.length); | 1134 var length = TO_UINT32(this.length); |
| 1067 if (length == 0) return -1; | 1135 if (length == 0) return -1; |
| 1068 if (%_ArgumentsLength() < 2) { | 1136 if (%_ArgumentsLength() < 2) { |
| 1069 index = length - 1; | 1137 index = length - 1; |
| 1070 } else { | 1138 } else { |
| 1071 index = TO_INTEGER(index); | 1139 index = TO_INTEGER(index); |
| 1072 // If index is negative, index from end of the array. | 1140 // If index is negative, index from end of the array. |
| 1073 if (index < 0) index += length; | 1141 if (index < 0) index += length; |
| 1074 // If index is still negative, do not search the array. | 1142 // If index is still negative, do not search the array. |
| 1075 if (index < 0) return -1; | 1143 if (index < 0) return -1; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1109 for (var i = max; i >= min; i--) { | 1177 for (var i = max; i >= min; i--) { |
| 1110 if (IS_UNDEFINED(this[i]) && i in this) { | 1178 if (IS_UNDEFINED(this[i]) && i in this) { |
| 1111 return i; | 1179 return i; |
| 1112 } | 1180 } |
| 1113 } | 1181 } |
| 1114 return -1; | 1182 return -1; |
| 1115 } | 1183 } |
| 1116 | 1184 |
| 1117 | 1185 |
| 1118 function ArrayReduce(callback, current) { | 1186 function ArrayReduce(callback, current) { |
| 1187 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1188 throw MakeTypeError("obj_ctor_property_non_object", ["Array.reduce"]); | |
| 1189 } | |
| 1190 | |
| 1119 if (!IS_FUNCTION(callback)) { | 1191 if (!IS_FUNCTION(callback)) { |
| 1120 throw MakeTypeError('called_non_callable', [callback]); | 1192 throw MakeTypeError('called_non_callable', [callback]); |
| 1121 } | 1193 } |
| 1122 // Pull out the length so that modifications to the length in the | 1194 // Pull out the length so that modifications to the length in the |
| 1123 // loop will not affect the looping. | 1195 // loop will not affect the looping. |
| 1124 var length = this.length; | 1196 var length = this.length; |
| 1125 var i = 0; | 1197 var i = 0; |
| 1126 | 1198 |
| 1127 find_initial: if (%_ArgumentsLength() < 2) { | 1199 find_initial: if (%_ArgumentsLength() < 2) { |
| 1128 for (; i < length; i++) { | 1200 for (; i < length; i++) { |
| 1129 current = this[i]; | 1201 current = this[i]; |
| 1130 if (!IS_UNDEFINED(current) || i in this) { | 1202 if (!IS_UNDEFINED(current) || i in this) { |
| 1131 i++; | 1203 i++; |
| 1132 break find_initial; | 1204 break find_initial; |
| 1133 } | 1205 } |
| 1134 } | 1206 } |
| 1135 throw MakeTypeError('reduce_no_initial', []); | 1207 throw MakeTypeError('reduce_no_initial', []); |
| 1136 } | 1208 } |
| 1137 | 1209 |
| 1138 for (; i < length; i++) { | 1210 for (; i < length; i++) { |
| 1139 var element = this[i]; | 1211 var element = this[i]; |
| 1140 if (!IS_UNDEFINED(element) || i in this) { | 1212 if (!IS_UNDEFINED(element) || i in this) { |
| 1141 current = callback.call(null, current, element, i, this); | 1213 current = callback.call(null, current, element, i, this); |
| 1142 } | 1214 } |
| 1143 } | 1215 } |
| 1144 return current; | 1216 return current; |
| 1145 } | 1217 } |
| 1146 | 1218 |
| 1147 function ArrayReduceRight(callback, current) { | 1219 function ArrayReduceRight(callback, current) { |
| 1220 if (IS_NULL_OR_UNDEFINED(this)) { | |
| 1221 throw MakeTypeError("obj_ctor_property_non_object", ["Array.reduceRight"]); | |
| 1222 } | |
| 1223 | |
| 1148 if (!IS_FUNCTION(callback)) { | 1224 if (!IS_FUNCTION(callback)) { |
| 1149 throw MakeTypeError('called_non_callable', [callback]); | 1225 throw MakeTypeError('called_non_callable', [callback]); |
| 1150 } | 1226 } |
| 1151 var i = this.length - 1; | 1227 var i = this.length - 1; |
| 1152 | 1228 |
| 1153 find_initial: if (%_ArgumentsLength() < 2) { | 1229 find_initial: if (%_ArgumentsLength() < 2) { |
| 1154 for (; i >= 0; i--) { | 1230 for (; i >= 0; i--) { |
| 1155 current = this[i]; | 1231 current = this[i]; |
| 1156 if (!IS_UNDEFINED(current) || i in this) { | 1232 if (!IS_UNDEFINED(current) || i in this) { |
| 1157 i--; | 1233 i--; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1240 InternalArray.prototype.join = getFunction("join", ArrayJoin); | 1316 InternalArray.prototype.join = getFunction("join", ArrayJoin); |
| 1241 InternalArray.prototype.pop = getFunction("pop", ArrayPop); | 1317 InternalArray.prototype.pop = getFunction("pop", ArrayPop); |
| 1242 InternalArray.prototype.push = getFunction("push", ArrayPush); | 1318 InternalArray.prototype.push = getFunction("push", ArrayPush); |
| 1243 InternalArray.prototype.toString = function() { | 1319 InternalArray.prototype.toString = function() { |
| 1244 return "Internal Array, length " + this.length; | 1320 return "Internal Array, length " + this.length; |
| 1245 }; | 1321 }; |
| 1246 } | 1322 } |
| 1247 | 1323 |
| 1248 | 1324 |
| 1249 SetupArray(); | 1325 SetupArray(); |
| OLD | NEW |