Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: src/array.js

Issue 132333019: Use `CHECK_OBJECT_COERCIBLE` macro where possible (Closed) Base URL: git@github.com:v8/v8.git@master
Patch Set: Don’t update copyright year Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/harmony-array.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 function ArrayToLocaleString() { 369 function ArrayToLocaleString() {
370 var array = ToObject(this); 370 var array = ToObject(this);
371 var arrayLen = array.length; 371 var arrayLen = array.length;
372 var len = TO_UINT32(arrayLen); 372 var len = TO_UINT32(arrayLen);
373 if (len === 0) return ""; 373 if (len === 0) return "";
374 return Join(array, len, ',', ConvertToLocaleString); 374 return Join(array, len, ',', ConvertToLocaleString);
375 } 375 }
376 376
377 377
378 function ArrayJoin(separator) { 378 function ArrayJoin(separator) {
379 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 379 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.join");
380 throw MakeTypeError("called_on_null_or_undefined",
381 ["Array.prototype.join"]);
382 }
383 380
384 var length = TO_UINT32(this.length); 381 var length = TO_UINT32(this.length);
385 if (IS_UNDEFINED(separator)) { 382 if (IS_UNDEFINED(separator)) {
386 separator = ','; 383 separator = ',';
387 } else if (!IS_STRING(separator)) { 384 } else if (!IS_STRING(separator)) {
388 separator = NonStringToString(separator); 385 separator = NonStringToString(separator);
389 } 386 }
390 387
391 var result = %_FastAsciiArrayJoin(this, separator); 388 var result = %_FastAsciiArrayJoin(this, separator);
392 if (!IS_UNDEFINED(result)) return result; 389 if (!IS_UNDEFINED(result)) return result;
(...skipping 14 matching lines...) Expand all
407 EndPerformSplice(this); 404 EndPerformSplice(this);
408 EnqueueSpliceRecord(this, n, [value], 0); 405 EnqueueSpliceRecord(this, n, [value], 0);
409 } 406 }
410 407
411 return value; 408 return value;
412 } 409 }
413 410
414 // Removes the last element from the array and returns it. See 411 // Removes the last element from the array and returns it. See
415 // ECMA-262, section 15.4.4.6. 412 // ECMA-262, section 15.4.4.6.
416 function ArrayPop() { 413 function ArrayPop() {
417 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 414 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.pop");
418 throw MakeTypeError("called_on_null_or_undefined",
419 ["Array.prototype.pop"]);
420 }
421 415
422 var n = TO_UINT32(this.length); 416 var n = TO_UINT32(this.length);
423 if (n == 0) { 417 if (n == 0) {
424 this.length = n; 418 this.length = n;
425 return; 419 return;
426 } 420 }
427 421
428 if ($Object.isSealed(this)) { 422 if ($Object.isSealed(this)) {
429 throw MakeTypeError("array_functions_change_sealed", 423 throw MakeTypeError("array_functions_change_sealed",
430 ["Array.prototype.pop"]); 424 ["Array.prototype.pop"]);
(...skipping 24 matching lines...) Expand all
455 EndPerformSplice(this); 449 EndPerformSplice(this);
456 EnqueueSpliceRecord(this, n, [], m); 450 EnqueueSpliceRecord(this, n, [], m);
457 } 451 }
458 452
459 return this.length; 453 return this.length;
460 } 454 }
461 455
462 // Appends the arguments to the end of the array and returns the new 456 // Appends the arguments to the end of the array and returns the new
463 // length of the array. See ECMA-262, section 15.4.4.7. 457 // length of the array. See ECMA-262, section 15.4.4.7.
464 function ArrayPush() { 458 function ArrayPush() {
465 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 459 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.push");
466 throw MakeTypeError("called_on_null_or_undefined",
467 ["Array.prototype.push"]);
468 }
469 460
470 var n = TO_UINT32(this.length); 461 var n = TO_UINT32(this.length);
471 var m = %_ArgumentsLength(); 462 var m = %_ArgumentsLength();
472 if (m > 0 && $Object.isSealed(this)) { 463 if (m > 0 && $Object.isSealed(this)) {
473 throw MakeTypeError("array_functions_change_sealed", 464 throw MakeTypeError("array_functions_change_sealed",
474 ["Array.prototype.push"]); 465 ["Array.prototype.push"]);
475 } 466 }
476 467
477 if (%IsObserved(this)) 468 if (%IsObserved(this))
478 return ObservedArrayPush.apply(this, arguments); 469 return ObservedArrayPush.apply(this, arguments);
479 470
480 for (var i = 0; i < m; i++) { 471 for (var i = 0; i < m; i++) {
481 this[i+n] = %_Arguments(i); 472 this[i+n] = %_Arguments(i);
482 } 473 }
483 this.length = n + m; 474 this.length = n + m;
484 return this.length; 475 return this.length;
485 } 476 }
486 477
487 478
488 // Returns an array containing the array elements of the object followed 479 // Returns an array containing the array elements of the object followed
489 // by the array elements of each argument in order. See ECMA-262, 480 // by the array elements of each argument in order. See ECMA-262,
490 // section 15.4.4.7. 481 // section 15.4.4.7.
491 function ArrayConcat(arg1) { // length == 1 482 function ArrayConcat(arg1) { // length == 1
492 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 483 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.concat");
493 throw MakeTypeError("called_on_null_or_undefined",
494 ["Array.prototype.concat"]);
495 }
496 484
497 var array = ToObject(this); 485 var array = ToObject(this);
498 var arg_count = %_ArgumentsLength(); 486 var arg_count = %_ArgumentsLength();
499 var arrays = new InternalArray(1 + arg_count); 487 var arrays = new InternalArray(1 + arg_count);
500 arrays[0] = array; 488 arrays[0] = array;
501 for (var i = 0; i < arg_count; i++) { 489 for (var i = 0; i < arg_count; i++) {
502 arrays[i + 1] = %_Arguments(i); 490 arrays[i + 1] = %_Arguments(i);
503 } 491 }
504 492
505 return %ArrayConcat(arrays); 493 return %ArrayConcat(arrays);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 if (!IS_UNDEFINED(current_j) || high in array) { 532 if (!IS_UNDEFINED(current_j) || high in array) {
545 array[low] = current_j; 533 array[low] = current_j;
546 delete array[high]; 534 delete array[high];
547 } 535 }
548 } 536 }
549 } 537 }
550 } 538 }
551 539
552 540
553 function ArrayReverse() { 541 function ArrayReverse() {
554 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 542 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reverse");
555 throw MakeTypeError("called_on_null_or_undefined",
556 ["Array.prototype.reverse"]);
557 }
558 543
559 var j = TO_UINT32(this.length) - 1; 544 var j = TO_UINT32(this.length) - 1;
560 545
561 if (UseSparseVariant(this, j, IS_ARRAY(this))) { 546 if (UseSparseVariant(this, j, IS_ARRAY(this))) {
562 SparseReverse(this, j+1); 547 SparseReverse(this, j+1);
563 return this; 548 return this;
564 } 549 }
565 550
566 for (var i = 0; i < j; i++, j--) { 551 for (var i = 0; i < j; i++, j--) {
567 var current_i = this[i]; 552 var current_i = this[i];
(...skipping 27 matching lines...) Expand all
595 this.length = len - 1; 580 this.length = len - 1;
596 } finally { 581 } finally {
597 EndPerformSplice(this); 582 EndPerformSplice(this);
598 EnqueueSpliceRecord(this, 0, [first], 0); 583 EnqueueSpliceRecord(this, 0, [first], 0);
599 } 584 }
600 585
601 return first; 586 return first;
602 } 587 }
603 588
604 function ArrayShift() { 589 function ArrayShift() {
605 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 590 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.shift");
606 throw MakeTypeError("called_on_null_or_undefined",
607 ["Array.prototype.shift"]);
608 }
609 591
610 var len = TO_UINT32(this.length); 592 var len = TO_UINT32(this.length);
611 593
612 if (len === 0) { 594 if (len === 0) {
613 this.length = 0; 595 this.length = 0;
614 return; 596 return;
615 } 597 }
616 598
617 if ($Object.isSealed(this)) { 599 if ($Object.isSealed(this)) {
618 throw MakeTypeError("array_functions_change_sealed", 600 throw MakeTypeError("array_functions_change_sealed",
(...skipping 29 matching lines...) Expand all
648 this.length = len + num_arguments; 630 this.length = len + num_arguments;
649 } finally { 631 } finally {
650 EndPerformSplice(this); 632 EndPerformSplice(this);
651 EnqueueSpliceRecord(this, 0, [], num_arguments); 633 EnqueueSpliceRecord(this, 0, [], num_arguments);
652 } 634 }
653 635
654 return len + num_arguments; 636 return len + num_arguments;
655 } 637 }
656 638
657 function ArrayUnshift(arg1) { // length == 1 639 function ArrayUnshift(arg1) { // length == 1
658 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 640 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.unshift");
659 throw MakeTypeError("called_on_null_or_undefined",
660 ["Array.prototype.unshift"]);
661 }
662 641
663 var len = TO_UINT32(this.length); 642 var len = TO_UINT32(this.length);
664 var num_arguments = %_ArgumentsLength(); 643 var num_arguments = %_ArgumentsLength();
665 var is_sealed = $Object.isSealed(this); 644 var is_sealed = $Object.isSealed(this);
666 645
667 if (num_arguments > 0 && is_sealed) { 646 if (num_arguments > 0 && is_sealed) {
668 throw MakeTypeError("array_functions_change_sealed", 647 throw MakeTypeError("array_functions_change_sealed",
669 ["Array.prototype.unshift"]); 648 ["Array.prototype.unshift"]);
670 } 649 }
671 650
(...skipping 21 matching lines...) Expand all
693 this[i] = %_Arguments(i); 672 this[i] = %_Arguments(i);
694 } 673 }
695 674
696 this.length = len + num_arguments; 675 this.length = len + num_arguments;
697 676
698 return this.length; 677 return this.length;
699 } 678 }
700 679
701 680
702 function ArraySlice(start, end) { 681 function ArraySlice(start, end) {
703 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 682 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.slice");
704 throw MakeTypeError("called_on_null_or_undefined",
705 ["Array.prototype.slice"]);
706 }
707 683
708 var len = TO_UINT32(this.length); 684 var len = TO_UINT32(this.length);
709 var start_i = TO_INTEGER(start); 685 var start_i = TO_INTEGER(start);
710 var end_i = len; 686 var end_i = len;
711 687
712 if (!IS_UNDEFINED(end)) end_i = TO_INTEGER(end); 688 if (!IS_UNDEFINED(end)) end_i = TO_INTEGER(end);
713 689
714 if (start_i < 0) { 690 if (start_i < 0) {
715 start_i += len; 691 start_i += len;
716 if (start_i < 0) start_i = 0; 692 if (start_i < 0) start_i = 0;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 num_elements_to_add); 786 num_elements_to_add);
811 } 787 }
812 } 788 }
813 789
814 // Return the deleted elements. 790 // Return the deleted elements.
815 return deleted_elements; 791 return deleted_elements;
816 } 792 }
817 793
818 794
819 function ArraySplice(start, delete_count) { 795 function ArraySplice(start, delete_count) {
820 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 796 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.splice");
821 throw MakeTypeError("called_on_null_or_undefined",
822 ["Array.prototype.splice"]);
823 }
824 797
825 if (%IsObserved(this)) 798 if (%IsObserved(this))
826 return ObservedArraySplice.apply(this, arguments); 799 return ObservedArraySplice.apply(this, arguments);
827 800
828 var num_arguments = %_ArgumentsLength(); 801 var num_arguments = %_ArgumentsLength();
829 var len = TO_UINT32(this.length); 802 var len = TO_UINT32(this.length);
830 var start_i = ComputeSpliceStartIndex(TO_INTEGER(start), len); 803 var start_i = ComputeSpliceStartIndex(TO_INTEGER(start), len);
831 var del_count = ComputeSpliceDeleteCount(delete_count, num_arguments, len, 804 var del_count = ComputeSpliceDeleteCount(delete_count, num_arguments, len,
832 start_i); 805 start_i);
833 var deleted_elements = []; 806 var deleted_elements = [];
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
871 this[i++] = %_Arguments(arguments_index++); 844 this[i++] = %_Arguments(arguments_index++);
872 } 845 }
873 this.length = len - del_count + num_elements_to_add; 846 this.length = len - del_count + num_elements_to_add;
874 847
875 // Return the deleted elements. 848 // Return the deleted elements.
876 return deleted_elements; 849 return deleted_elements;
877 } 850 }
878 851
879 852
880 function ArraySort(comparefn) { 853 function ArraySort(comparefn) {
881 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 854 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.sort");
882 throw MakeTypeError("called_on_null_or_undefined",
883 ["Array.prototype.sort"]);
884 }
885 855
886 // In-place QuickSort algorithm. 856 // In-place QuickSort algorithm.
887 // For short (length <= 22) arrays, insertion sort is used for efficiency. 857 // For short (length <= 22) arrays, insertion sort is used for efficiency.
888 858
889 if (!IS_SPEC_FUNCTION(comparefn)) { 859 if (!IS_SPEC_FUNCTION(comparefn)) {
890 comparefn = function (x, y) { 860 comparefn = function (x, y) {
891 if (x === y) return 0; 861 if (x === y) return 0;
892 if (%_IsSmi(x) && %_IsSmi(y)) { 862 if (%_IsSmi(x) && %_IsSmi(y)) {
893 return %SmiLexicographicCompare(x, y); 863 return %SmiLexicographicCompare(x, y);
894 } 864 }
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 } 1134 }
1165 1135
1166 return this; 1136 return this;
1167 } 1137 }
1168 1138
1169 1139
1170 // The following functions cannot be made efficient on sparse arrays while 1140 // The following functions cannot be made efficient on sparse arrays while
1171 // preserving the semantics, since the calls to the receiver function can add 1141 // preserving the semantics, since the calls to the receiver function can add
1172 // or delete elements from the array. 1142 // or delete elements from the array.
1173 function ArrayFilter(f, receiver) { 1143 function ArrayFilter(f, receiver) {
1174 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1144 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
1175 throw MakeTypeError("called_on_null_or_undefined",
1176 ["Array.prototype.filter"]);
1177 }
1178 1145
1179 // Pull out the length so that modifications to the length in the 1146 // Pull out the length so that modifications to the length in the
1180 // loop will not affect the looping and side effects are visible. 1147 // loop will not affect the looping and side effects are visible.
1181 var array = ToObject(this); 1148 var array = ToObject(this);
1182 var length = ToUint32(array.length); 1149 var length = ToUint32(array.length);
1183 1150
1184 if (!IS_SPEC_FUNCTION(f)) { 1151 if (!IS_SPEC_FUNCTION(f)) {
1185 throw MakeTypeError('called_non_callable', [ f ]); 1152 throw MakeTypeError('called_non_callable', [ f ]);
1186 } 1153 }
1187 if (IS_NULL_OR_UNDEFINED(receiver)) { 1154 if (IS_NULL_OR_UNDEFINED(receiver)) {
(...skipping 27 matching lines...) Expand all
1215 } 1182 }
1216 } 1183 }
1217 // End of duplicate. 1184 // End of duplicate.
1218 } 1185 }
1219 %MoveArrayContents(accumulator, result); 1186 %MoveArrayContents(accumulator, result);
1220 return result; 1187 return result;
1221 } 1188 }
1222 1189
1223 1190
1224 function ArrayForEach(f, receiver) { 1191 function ArrayForEach(f, receiver) {
1225 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1192 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.forEach");
1226 throw MakeTypeError("called_on_null_or_undefined",
1227 ["Array.prototype.forEach"]);
1228 }
1229 1193
1230 // 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
1231 // loop will not affect the looping and side effects are visible. 1195 // loop will not affect the looping and side effects are visible.
1232 var array = ToObject(this); 1196 var array = ToObject(this);
1233 var length = TO_UINT32(array.length); 1197 var length = TO_UINT32(array.length);
1234 1198
1235 if (!IS_SPEC_FUNCTION(f)) { 1199 if (!IS_SPEC_FUNCTION(f)) {
1236 throw MakeTypeError('called_non_callable', [ f ]); 1200 throw MakeTypeError('called_non_callable', [ f ]);
1237 } 1201 }
1238 if (IS_NULL_OR_UNDEFINED(receiver)) { 1202 if (IS_NULL_OR_UNDEFINED(receiver)) {
(...skipping 20 matching lines...) Expand all
1259 } 1223 }
1260 } 1224 }
1261 // End of duplicate. 1225 // End of duplicate.
1262 } 1226 }
1263 } 1227 }
1264 1228
1265 1229
1266 // Executes the function once for each element present in the 1230 // Executes the function once for each element present in the
1267 // array until it finds one where callback returns true. 1231 // array until it finds one where callback returns true.
1268 function ArraySome(f, receiver) { 1232 function ArraySome(f, receiver) {
1269 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1233 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some");
1270 throw MakeTypeError("called_on_null_or_undefined",
1271 ["Array.prototype.some"]);
1272 }
1273 1234
1274 // Pull out the length so that modifications to the length in the 1235 // Pull out the length so that modifications to the length in the
1275 // loop will not affect the looping and side effects are visible. 1236 // loop will not affect the looping and side effects are visible.
1276 var array = ToObject(this); 1237 var array = ToObject(this);
1277 var length = TO_UINT32(array.length); 1238 var length = TO_UINT32(array.length);
1278 1239
1279 if (!IS_SPEC_FUNCTION(f)) { 1240 if (!IS_SPEC_FUNCTION(f)) {
1280 throw MakeTypeError('called_non_callable', [ f ]); 1241 throw MakeTypeError('called_non_callable', [ f ]);
1281 } 1242 }
1282 if (IS_NULL_OR_UNDEFINED(receiver)) { 1243 if (IS_NULL_OR_UNDEFINED(receiver)) {
(...skipping 19 matching lines...) Expand all
1302 if (%_CallFunction(receiver, element, i, array, f)) return true; 1263 if (%_CallFunction(receiver, element, i, array, f)) return true;
1303 } 1264 }
1304 } 1265 }
1305 // End of duplicate. 1266 // End of duplicate.
1306 } 1267 }
1307 return false; 1268 return false;
1308 } 1269 }
1309 1270
1310 1271
1311 function ArrayEvery(f, receiver) { 1272 function ArrayEvery(f, receiver) {
1312 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1273 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.every");
1313 throw MakeTypeError("called_on_null_or_undefined",
1314 ["Array.prototype.every"]);
1315 }
1316 1274
1317 // Pull out the length so that modifications to the length in the 1275 // Pull out the length so that modifications to the length in the
1318 // loop will not affect the looping and side effects are visible. 1276 // loop will not affect the looping and side effects are visible.
1319 var array = ToObject(this); 1277 var array = ToObject(this);
1320 var length = TO_UINT32(array.length); 1278 var length = TO_UINT32(array.length);
1321 1279
1322 if (!IS_SPEC_FUNCTION(f)) { 1280 if (!IS_SPEC_FUNCTION(f)) {
1323 throw MakeTypeError('called_non_callable', [ f ]); 1281 throw MakeTypeError('called_non_callable', [ f ]);
1324 } 1282 }
1325 if (IS_NULL_OR_UNDEFINED(receiver)) { 1283 if (IS_NULL_OR_UNDEFINED(receiver)) {
(...skipping 18 matching lines...) Expand all
1344 var element = array[i]; 1302 var element = array[i];
1345 if (!%_CallFunction(receiver, element, i, array, f)) return false; 1303 if (!%_CallFunction(receiver, element, i, array, f)) return false;
1346 } 1304 }
1347 } 1305 }
1348 // End of duplicate. 1306 // End of duplicate.
1349 } 1307 }
1350 return true; 1308 return true;
1351 } 1309 }
1352 1310
1353 function ArrayMap(f, receiver) { 1311 function ArrayMap(f, receiver) {
1354 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1312 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
1355 throw MakeTypeError("called_on_null_or_undefined",
1356 ["Array.prototype.map"]);
1357 }
1358 1313
1359 // Pull out the length so that modifications to the length in the 1314 // Pull out the length so that modifications to the length in the
1360 // loop will not affect the looping and side effects are visible. 1315 // loop will not affect the looping and side effects are visible.
1361 var array = ToObject(this); 1316 var array = ToObject(this);
1362 var length = TO_UINT32(array.length); 1317 var length = TO_UINT32(array.length);
1363 1318
1364 if (!IS_SPEC_FUNCTION(f)) { 1319 if (!IS_SPEC_FUNCTION(f)) {
1365 throw MakeTypeError('called_non_callable', [ f ]); 1320 throw MakeTypeError('called_non_callable', [ f ]);
1366 } 1321 }
1367 if (IS_NULL_OR_UNDEFINED(receiver)) { 1322 if (IS_NULL_OR_UNDEFINED(receiver)) {
(...skipping 22 matching lines...) Expand all
1390 } 1345 }
1391 } 1346 }
1392 // End of duplicate. 1347 // End of duplicate.
1393 } 1348 }
1394 %MoveArrayContents(accumulator, result); 1349 %MoveArrayContents(accumulator, result);
1395 return result; 1350 return result;
1396 } 1351 }
1397 1352
1398 1353
1399 function ArrayIndexOf(element, index) { 1354 function ArrayIndexOf(element, index) {
1400 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1355 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
1401 throw MakeTypeError("called_on_null_or_undefined",
1402 ["Array.prototype.indexOf"]);
1403 }
1404 1356
1405 var length = TO_UINT32(this.length); 1357 var length = TO_UINT32(this.length);
1406 if (length == 0) return -1; 1358 if (length == 0) return -1;
1407 if (IS_UNDEFINED(index)) { 1359 if (IS_UNDEFINED(index)) {
1408 index = 0; 1360 index = 0;
1409 } else { 1361 } else {
1410 index = TO_INTEGER(index); 1362 index = TO_INTEGER(index);
1411 // If index is negative, index from the end of the array. 1363 // If index is negative, index from the end of the array.
1412 if (index < 0) { 1364 if (index < 0) {
1413 index = length + index; 1365 index = length + index;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1449 for (var i = min; i < max; i++) { 1401 for (var i = min; i < max; i++) {
1450 if (IS_UNDEFINED(this[i]) && i in this) { 1402 if (IS_UNDEFINED(this[i]) && i in this) {
1451 return i; 1403 return i;
1452 } 1404 }
1453 } 1405 }
1454 return -1; 1406 return -1;
1455 } 1407 }
1456 1408
1457 1409
1458 function ArrayLastIndexOf(element, index) { 1410 function ArrayLastIndexOf(element, index) {
1459 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1411 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
1460 throw MakeTypeError("called_on_null_or_undefined",
1461 ["Array.prototype.lastIndexOf"]);
1462 }
1463 1412
1464 var length = TO_UINT32(this.length); 1413 var length = TO_UINT32(this.length);
1465 if (length == 0) return -1; 1414 if (length == 0) return -1;
1466 if (%_ArgumentsLength() < 2) { 1415 if (%_ArgumentsLength() < 2) {
1467 index = length - 1; 1416 index = length - 1;
1468 } else { 1417 } else {
1469 index = TO_INTEGER(index); 1418 index = TO_INTEGER(index);
1470 // If index is negative, index from end of the array. 1419 // If index is negative, index from end of the array.
1471 if (index < 0) index += length; 1420 if (index < 0) index += length;
1472 // If index is still negative, do not search the array. 1421 // If index is still negative, do not search the array.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 for (var i = max; i >= min; i--) { 1453 for (var i = max; i >= min; i--) {
1505 if (IS_UNDEFINED(this[i]) && i in this) { 1454 if (IS_UNDEFINED(this[i]) && i in this) {
1506 return i; 1455 return i;
1507 } 1456 }
1508 } 1457 }
1509 return -1; 1458 return -1;
1510 } 1459 }
1511 1460
1512 1461
1513 function ArrayReduce(callback, current) { 1462 function ArrayReduce(callback, current) {
1514 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1463 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
1515 throw MakeTypeError("called_on_null_or_undefined",
1516 ["Array.prototype.reduce"]);
1517 }
1518 1464
1519 // Pull out the length so that modifications to the length in the 1465 // Pull out the length so that modifications to the length in the
1520 // loop will not affect the looping and side effects are visible. 1466 // loop will not affect the looping and side effects are visible.
1521 var array = ToObject(this); 1467 var array = ToObject(this);
1522 var length = ToUint32(array.length); 1468 var length = ToUint32(array.length);
1523 1469
1524 if (!IS_SPEC_FUNCTION(callback)) { 1470 if (!IS_SPEC_FUNCTION(callback)) {
1525 throw MakeTypeError('called_non_callable', [callback]); 1471 throw MakeTypeError('called_non_callable', [callback]);
1526 } 1472 }
1527 1473
(...skipping 29 matching lines...) Expand all
1557 current = 1503 current =
1558 %_CallFunction(receiver, current, element, i, array, callback); 1504 %_CallFunction(receiver, current, element, i, array, callback);
1559 } 1505 }
1560 } 1506 }
1561 // End of duplicate. 1507 // End of duplicate.
1562 } 1508 }
1563 return current; 1509 return current;
1564 } 1510 }
1565 1511
1566 function ArrayReduceRight(callback, current) { 1512 function ArrayReduceRight(callback, current) {
1567 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { 1513 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
1568 throw MakeTypeError("called_on_null_or_undefined",
1569 ["Array.prototype.reduceRight"]);
1570 }
1571 1514
1572 // Pull out the length so that side effects are visible before the 1515 // Pull out the length so that side effects are visible before the
1573 // callback function is checked. 1516 // callback function is checked.
1574 var array = ToObject(this); 1517 var array = ToObject(this);
1575 var length = ToUint32(array.length); 1518 var length = ToUint32(array.length);
1576 1519
1577 if (!IS_SPEC_FUNCTION(callback)) { 1520 if (!IS_SPEC_FUNCTION(callback)) {
1578 throw MakeTypeError('called_non_callable', [callback]); 1521 throw MakeTypeError('called_non_callable', [callback]);
1579 } 1522 }
1580 1523
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1692 )); 1635 ));
1693 1636
1694 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( 1637 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array(
1695 "join", getFunction("join", ArrayJoin), 1638 "join", getFunction("join", ArrayJoin),
1696 "pop", getFunction("pop", ArrayPop), 1639 "pop", getFunction("pop", ArrayPop),
1697 "push", getFunction("push", ArrayPush) 1640 "push", getFunction("push", ArrayPush)
1698 )); 1641 ));
1699 } 1642 }
1700 1643
1701 SetUpArray(); 1644 SetUpArray();
OLDNEW
« no previous file with comments | « no previous file | src/harmony-array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698