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

Side by Side Diff: src/array.js

Issue 88022: Add Array.prototype.reduce and .reduceRight. (Closed)
Patch Set: Created 11 years, 8 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/messages.js » ('j') | test/mjsunit/array-reduce.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 861 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 for (var i = index; i >= 0; i--) { 872 for (var i = index; i >= 0; i--) {
873 var current = this[i]; 873 var current = this[i];
874 if (!IS_UNDEFINED(current) || i in this) { 874 if (!IS_UNDEFINED(current) || i in this) {
875 if (current === element) return i; 875 if (current === element) return i;
876 } 876 }
877 } 877 }
878 return -1; 878 return -1;
879 } 879 }
880 880
881 881
882 function ArrayReduce(callback, current) {
883 if (!IS_FUNCTION(callback)) {
884 throw MakeTypeError('called_non_callable', [ callback ]);
885 }
886 // Pull out the length so that modifications to the length in the
887 // loop will not affect the looping.
888 var length = this.length;
889 var i = 0;
890
891 find_initial: if (%_ArgumentsLength() < 2) {
Dean McNamee 2009/04/21 09:19:50 I am missing why you need the label.
Christian Plesner Hansen 2009/04/21 09:39:17 I think the code style is find_initial: if (%_
Lasse Reichstein 2009/04/21 09:53:50 I use the label to have a location to break to, to
Lasse Reichstein 2009/04/21 09:53:50 Done.
892 for (;i < length; i++) {
Dean McNamee 2009/04/21 09:19:50 space after ;
Lasse Reichstein 2009/04/21 09:53:50 Done.
893 current = this[i];
894 if (!IS_UNDEFINED(current) || i in this) {
895 i++;
896 break find_initial;
897 }
898 }
899 throw MakeTypeError('reduce_no_initial', []);
900 }
901
902 for (; i < length; i++) {
903 var element = this[i];
904 if (!IS_UNDEFINED(element) || i in this) {
905 current = callback.call(null, current, element, i, this);
906 }
907 }
908 return current;
909 }
910
911 function ArrayReduceRight(callback, current) {
912 if (!IS_FUNCTION(callback)) {
913 throw MakeTypeError('called_non_callable', [ callback ]);
Dean McNamee 2009/04/21 09:19:50 no spaces around [ ] (consistency with [])
Lasse Reichstein 2009/04/21 09:53:50 Done.
914 }
915 var i = this.length - 1;
916
917 find_initial: if (%_ArgumentsLength() < 2) {
918 for (;i >= 0; i--) {
919 current = this[i];
920 if (!IS_UNDEFINED(current) || i in this) {
921 i--;
922 break find_initial;
923 }
924 }
925 throw MakeTypeError('reduce_no_initial', []);
926 }
927
928 for (; i >= 0; i--) {
929 var element = this[i];
930 if (!IS_UNDEFINED(element) || i in this) {
931 current = callback.call(null, current, element, i, this);
932 }
933 }
934 return current;
935 }
936
937
882 // ------------------------------------------------------------------- 938 // -------------------------------------------------------------------
883 939
884 940
885 function UpdateFunctionLengths(lengths) { 941 function UpdateFunctionLengths(lengths) {
886 for (var key in lengths) { 942 for (var key in lengths) {
887 %FunctionSetLength(this[key], lengths[key]); 943 %FunctionSetLength(this[key], lengths[key]);
888 } 944 }
889 } 945 }
890 946
891 947
(...skipping 18 matching lines...) Expand all
910 "unshift", ArrayUnshift, 966 "unshift", ArrayUnshift,
911 "slice", ArraySlice, 967 "slice", ArraySlice,
912 "splice", ArraySplice, 968 "splice", ArraySplice,
913 "sort", ArraySort, 969 "sort", ArraySort,
914 "filter", ArrayFilter, 970 "filter", ArrayFilter,
915 "forEach", ArrayForEach, 971 "forEach", ArrayForEach,
916 "some", ArraySome, 972 "some", ArraySome,
917 "every", ArrayEvery, 973 "every", ArrayEvery,
918 "map", ArrayMap, 974 "map", ArrayMap,
919 "indexOf", ArrayIndexOf, 975 "indexOf", ArrayIndexOf,
920 "lastIndexOf", ArrayLastIndexOf 976 "lastIndexOf", ArrayLastIndexOf,
977 "reduce", ArrayReduce,
978 "reduceRight", ArrayReduceRight
921 )); 979 ));
922 980
923 // Manipulate the length of some of the functions to meet 981 // Manipulate the length of some of the functions to meet
924 // expectations set by ECMA-262 or Mozilla. 982 // expectations set by ECMA-262 or Mozilla.
925 UpdateFunctionLengths({ 983 UpdateFunctionLengths({
926 ArrayFilter: 1, 984 ArrayFilter: 1,
927 ArrayForEach: 1, 985 ArrayForEach: 1,
928 ArraySome: 1, 986 ArraySome: 1,
929 ArrayEvery: 1, 987 ArrayEvery: 1,
930 ArrayMap: 1, 988 ArrayMap: 1,
931 ArrayIndexOf: 1, 989 ArrayIndexOf: 1,
932 ArrayLastIndexOf: 1, 990 ArrayLastIndexOf: 1,
933 ArrayPush: 1 991 ArrayPush: 1,
992 ArrayReduce: 1,
993 ArrayReduceRight: 1
934 }); 994 });
935 } 995 }
936 996
937 997
938 SetupArray(); 998 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | src/messages.js » ('j') | test/mjsunit/array-reduce.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698