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

Side by Side Diff: src/array.js

Issue 6672027: Change order of operations in array functions to match the specification. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-75562.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 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 } finally { 165 } finally {
166 // Make sure to remove the last element of the visited array no 166 // Make sure to remove the last element of the visited array no
167 // matter what happens. 167 // matter what happens.
168 if (is_array) visited_arrays.length = visited_arrays.length - 1; 168 if (is_array) visited_arrays.length = visited_arrays.length - 1;
169 } 169 }
170 } 170 }
171 171
172 172
173 function ConvertToString(x) { 173 function ConvertToString(x) {
174 // Assumes x is a non-string. 174 // Assumes x is a non-string.
175 // Differs from NonStringToString in handling of null and undefined.
175 if (IS_NUMBER(x)) return %_NumberToString(x); 176 if (IS_NUMBER(x)) return %_NumberToString(x);
176 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; 177 if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
177 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); 178 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x));
178 } 179 }
179 180
180 181
181 function ConvertToLocaleString(e) { 182 function ConvertToLocaleString(e) {
182 if (e == null) { 183 if (e == null) {
183 return ''; 184 return '';
184 } else { 185 } else {
185 // e_obj's toLocaleString might be overwritten, check if it is a function. 186 var o = ToObject(e);
186 // Call ToString if toLocaleString is not a function. 187 var func = o.toLocaleString;
187 // See issue 877615. 188 if (IS_FUNCTION(func)) {
188 var e_obj = ToObject(e); 189 return ToString(%_CallFunction(o, func));
189 if (IS_FUNCTION(e_obj.toLocaleString)) 190 }
190 return ToString(e_obj.toLocaleString()); 191 // For compatibility with JSC, we default to ToString on the original
191 else 192 // value if toLocaleString isn't usable, instead of throwing a
192 return ToString(e); 193 // TypeError.
194 return ToString(e);
193 } 195 }
194 } 196 }
195 197
196 198
197 // This function implements the optimized splice implementation that can use 199 // This function implements the optimized splice implementation that can use
198 // special array operations to handle sparse arrays in a sensible fashion. 200 // special array operations to handle sparse arrays in a sensible fashion.
199 function SmartSlice(array, start_i, del_count, len, deleted_elements) { 201 function SmartSlice(array, start_i, del_count, len, deleted_elements) {
200 // Move deleted elements to a new array (the return value from splice). 202 // Move deleted elements to a new array (the return value from splice).
201 // Intervals array can contain keys and intervals. See comment in Concat. 203 // Intervals array can contain keys and intervals. See comment in Concat.
202 var intervals = %GetArrayKeys(array, start_i + del_count); 204 var intervals = %GetArrayKeys(array, start_i + del_count);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 } 354 }
353 } 355 }
354 } 356 }
355 } 357 }
356 358
357 359
358 // ------------------------------------------------------------------- 360 // -------------------------------------------------------------------
359 361
360 362
361 function ArrayToString() { 363 function ArrayToString() {
362 if (!IS_ARRAY(this)) { 364 var array = ToObject(this);
sandholm 2011/03/11 13:16:53 You probably want IS_ARRAY(this) to be the fast ca
363 throw new $TypeError('Array.prototype.toString is not generic'); 365 var func = array.join;
sandholm 2011/03/11 13:16:53 Not calling "Join" directly may have performance i
366 if (!IS_FUNCTION(func)) {
367 func = ObjectToString;
364 } 368 }
365 return Join(this, this.length, ',', ConvertToString); 369 return %_CallFunction(array, func);
366 } 370 }
367 371
368 372
369 function ArrayToLocaleString() { 373 function ArrayToLocaleString() {
370 if (!IS_ARRAY(this)) { 374 var array = ToObject(this);
sandholm 2011/03/11 13:16:53 Same comment re IS_ARRAY and ToObject.
371 throw new $TypeError('Array.prototype.toString is not generic'); 375 var length = TO_UINT32(array.length);
372 } 376 return Join(array, length, ',', ConvertToLocaleString);
373 return Join(this, this.length, ',', ConvertToLocaleString);
374 } 377 }
375 378
376 379
377 function ArrayJoin(separator) { 380 function ArrayJoin(separator) {
381 var length = TO_UINT32(this.length);
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, 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() {
394 var n = TO_UINT32(this.length); 398 var n = TO_UINT32(this.length);
395 if (n == 0) { 399 if (n == 0) {
396 this.length = n; 400 this.length = n;
397 return; 401 return;
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 } 911 }
908 912
909 return this; 913 return this;
910 } 914 }
911 915
912 916
913 // The following functions cannot be made efficient on sparse arrays while 917 // The following functions cannot be made efficient on sparse arrays while
914 // preserving the semantics, since the calls to the receiver function can add 918 // preserving the semantics, since the calls to the receiver function can add
915 // or delete elements from the array. 919 // or delete elements from the array.
916 function ArrayFilter(f, receiver) { 920 function ArrayFilter(f, receiver) {
921 var length = TO_UINT32(this.length);
917 if (!IS_FUNCTION(f)) { 922 if (!IS_FUNCTION(f)) {
918 throw MakeTypeError('called_non_callable', [ f ]); 923 throw MakeTypeError('called_non_callable', [ f ]);
919 } 924 }
920 // Pull out the length so that modifications to the length in the
921 // loop will not affect the looping.
922 var length = this.length;
923 var result = []; 925 var result = [];
924 var result_length = 0; 926 var result_length = 0;
925 for (var i = 0; i < length; i++) { 927 for (var i = 0; i < length; i++) {
926 var current = this[i]; 928 var current = this[i];
927 if (!IS_UNDEFINED(current) || i in this) { 929 if (!IS_UNDEFINED(current) || i in this) {
928 if (f.call(receiver, current, i, this)) { 930 if (f.call(receiver, current, i, this)) {
929 result[result_length++] = current; 931 result[result_length++] = current;
930 } 932 }
931 } 933 }
932 } 934 }
933 return result; 935 return result;
934 } 936 }
935 937
936 938
937 function ArrayForEach(f, receiver) { 939 function ArrayForEach(f, receiver) {
940 var length = TO_UINT32(this.length);
938 if (!IS_FUNCTION(f)) { 941 if (!IS_FUNCTION(f)) {
939 throw MakeTypeError('called_non_callable', [ f ]); 942 throw MakeTypeError('called_non_callable', [ f ]);
940 } 943 }
941 // Pull out the length so that modifications to the length in the
942 // loop will not affect the looping.
943 var length = TO_UINT32(this.length);
944 for (var i = 0; i < length; i++) { 944 for (var i = 0; i < length; i++) {
945 var current = this[i]; 945 var current = this[i];
946 if (!IS_UNDEFINED(current) || i in this) { 946 if (!IS_UNDEFINED(current) || i in this) {
947 f.call(receiver, current, i, this); 947 f.call(receiver, current, i, this);
948 } 948 }
949 } 949 }
950 } 950 }
951 951
952 952
953 // Executes the function once for each element present in the 953 // Executes the function once for each element present in the
954 // array until it finds one where callback returns true. 954 // array until it finds one where callback returns true.
955 function ArraySome(f, receiver) { 955 function ArraySome(f, receiver) {
956 var length = TO_UINT32(this.length);
956 if (!IS_FUNCTION(f)) { 957 if (!IS_FUNCTION(f)) {
957 throw MakeTypeError('called_non_callable', [ f ]); 958 throw MakeTypeError('called_non_callable', [ f ]);
958 } 959 }
959 // Pull out the length so that modifications to the length in the
960 // loop will not affect the looping.
961 var length = TO_UINT32(this.length);
962 for (var i = 0; i < length; i++) { 960 for (var i = 0; i < length; i++) {
963 var current = this[i]; 961 var current = this[i];
964 if (!IS_UNDEFINED(current) || i in this) { 962 if (!IS_UNDEFINED(current) || i in this) {
965 if (f.call(receiver, current, i, this)) return true; 963 if (f.call(receiver, current, i, this)) return true;
966 } 964 }
967 } 965 }
968 return false; 966 return false;
969 } 967 }
970 968
971 969
972 function ArrayEvery(f, receiver) { 970 function ArrayEvery(f, receiver) {
971 var length = TO_UINT32(this.length);
973 if (!IS_FUNCTION(f)) { 972 if (!IS_FUNCTION(f)) {
974 throw MakeTypeError('called_non_callable', [ f ]); 973 throw MakeTypeError('called_non_callable', [ f ]);
975 } 974 }
976 // Pull out the length so that modifications to the length in the
977 // loop will not affect the looping.
978 var length = TO_UINT32(this.length);
979 for (var i = 0; i < length; i++) { 975 for (var i = 0; i < length; i++) {
980 var current = this[i]; 976 var current = this[i];
981 if (!IS_UNDEFINED(current) || i in this) { 977 if (!IS_UNDEFINED(current) || i in this) {
982 if (!f.call(receiver, current, i, this)) return false; 978 if (!f.call(receiver, current, i, this)) return false;
983 } 979 }
984 } 980 }
985 return true; 981 return true;
986 } 982 }
987 983
988 function ArrayMap(f, receiver) { 984 function ArrayMap(f, receiver) {
985 var length = TO_UINT32(this.length);
989 if (!IS_FUNCTION(f)) { 986 if (!IS_FUNCTION(f)) {
990 throw MakeTypeError('called_non_callable', [ f ]); 987 throw MakeTypeError('called_non_callable', [ f ]);
991 } 988 }
992 // Pull out the length so that modifications to the length in the
993 // loop will not affect the looping.
994 var length = TO_UINT32(this.length);
995 var result = new $Array(); 989 var result = new $Array();
996 var accumulator = new InternalArray(length); 990 var accumulator = new InternalArray(length);
997 for (var i = 0; i < length; i++) { 991 for (var i = 0; i < length; i++) {
998 var current = this[i]; 992 var current = this[i];
999 if (!IS_UNDEFINED(current) || i in this) { 993 if (!IS_UNDEFINED(current) || i in this) {
1000 accumulator[i] = f.call(receiver, current, i, this); 994 accumulator[i] = f.call(receiver, current, i, this);
1001 } 995 }
1002 } 996 }
1003 %MoveArrayContents(accumulator, result); 997 %MoveArrayContents(accumulator, result);
1004 return result; 998 return result;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1109 for (var i = max; i >= min; i--) { 1103 for (var i = max; i >= min; i--) {
1110 if (IS_UNDEFINED(this[i]) && i in this) { 1104 if (IS_UNDEFINED(this[i]) && i in this) {
1111 return i; 1105 return i;
1112 } 1106 }
1113 } 1107 }
1114 return -1; 1108 return -1;
1115 } 1109 }
1116 1110
1117 1111
1118 function ArrayReduce(callback, current) { 1112 function ArrayReduce(callback, current) {
1113 var length = TO_UINT32(this.length);
1119 if (!IS_FUNCTION(callback)) { 1114 if (!IS_FUNCTION(callback)) {
1120 throw MakeTypeError('called_non_callable', [callback]); 1115 throw MakeTypeError('called_non_callable', [callback]);
1121 } 1116 }
1122 // Pull out the length so that modifications to the length in the 1117 // Pull out the length so that modifications to the length in the
1123 // loop will not affect the looping. 1118 // loop will not affect the looping.
1124 var length = this.length;
1125 var i = 0; 1119 var i = 0;
1126 1120
1127 find_initial: if (%_ArgumentsLength() < 2) { 1121 find_initial: if (%_ArgumentsLength() < 2) {
1128 for (; i < length; i++) { 1122 for (; i < length; i++) {
1129 current = this[i]; 1123 current = this[i];
1130 if (!IS_UNDEFINED(current) || i in this) { 1124 if (!IS_UNDEFINED(current) || i in this) {
1131 i++; 1125 i++;
1132 break find_initial; 1126 break find_initial;
1133 } 1127 }
1134 } 1128 }
1135 throw MakeTypeError('reduce_no_initial', []); 1129 throw MakeTypeError('reduce_no_initial', []);
1136 } 1130 }
1137 1131
1138 for (; i < length; i++) { 1132 for (; i < length; i++) {
1139 var element = this[i]; 1133 var element = this[i];
1140 if (!IS_UNDEFINED(element) || i in this) { 1134 if (!IS_UNDEFINED(element) || i in this) {
1141 current = callback.call(null, current, element, i, this); 1135 current = callback.call(null, current, element, i, this);
1142 } 1136 }
1143 } 1137 }
1144 return current; 1138 return current;
1145 } 1139 }
1146 1140
1147 function ArrayReduceRight(callback, current) { 1141 function ArrayReduceRight(callback, current) {
1142 var i = TO_UINT32(this.length) - 1;
1148 if (!IS_FUNCTION(callback)) { 1143 if (!IS_FUNCTION(callback)) {
1149 throw MakeTypeError('called_non_callable', [callback]); 1144 throw MakeTypeError('called_non_callable', [callback]);
1150 } 1145 }
1151 var i = this.length - 1;
1152 1146
1153 find_initial: if (%_ArgumentsLength() < 2) { 1147 find_initial: if (%_ArgumentsLength() < 2) {
1154 for (; i >= 0; i--) { 1148 for (; i >= 0; i--) {
1155 current = this[i]; 1149 current = this[i];
1156 if (!IS_UNDEFINED(current) || i in this) { 1150 if (!IS_UNDEFINED(current) || i in this) {
1157 i--; 1151 i--;
1158 break find_initial; 1152 break find_initial;
1159 } 1153 }
1160 } 1154 }
1161 throw MakeTypeError('reduce_no_initial', []); 1155 throw MakeTypeError('reduce_no_initial', []);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 InternalArray.prototype.join = getFunction("join", ArrayJoin); 1234 InternalArray.prototype.join = getFunction("join", ArrayJoin);
1241 InternalArray.prototype.pop = getFunction("pop", ArrayPop); 1235 InternalArray.prototype.pop = getFunction("pop", ArrayPop);
1242 InternalArray.prototype.push = getFunction("push", ArrayPush); 1236 InternalArray.prototype.push = getFunction("push", ArrayPush);
1243 InternalArray.prototype.toString = function() { 1237 InternalArray.prototype.toString = function() {
1244 return "Internal Array, length " + this.length; 1238 return "Internal Array, length " + this.length;
1245 }; 1239 };
1246 } 1240 }
1247 1241
1248 1242
1249 SetupArray(); 1243 SetupArray();
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/regress/regress-75562.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698