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

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: Avoid unneccesary calls. 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 = IS_OBJECT(this) ? this : ToObject(this);
363 throw new $TypeError('Array.prototype.toString is not generic'); 365 var length = TO_UINT32(array.length);
366 var func = array.join;
367 if (func == ArrayJoin) {
368 return Join(array, length, ',', ConvertToString);
364 } 369 }
365 return Join(this, this.length, ',', ConvertToString); 370 if (!IS_FUNCTION(func)) {
371 func = ObjectToString;
372 }
373 return %_CallFunction(array, func);
366 } 374 }
367 375
368 376
369 function ArrayToLocaleString() { 377 function ArrayToLocaleString() {
370 if (!IS_ARRAY(this)) { 378 var array = IS_OBJECT(this) ? this : ToObject(this);
371 throw new $TypeError('Array.prototype.toString is not generic'); 379 var length = TO_UINT32(array.length);
372 } 380 return Join(array, length, ',', ConvertToLocaleString);
373 return Join(this, this.length, ',', ConvertToLocaleString);
374 } 381 }
375 382
376 383
377 function ArrayJoin(separator) { 384 function ArrayJoin(separator) {
385 var length = TO_UINT32(this.length);
378 if (IS_UNDEFINED(separator)) { 386 if (IS_UNDEFINED(separator)) {
379 separator = ','; 387 separator = ',';
380 } else if (!IS_STRING(separator)) { 388 } else if (!IS_STRING(separator)) {
381 separator = NonStringToString(separator); 389 separator = NonStringToString(separator);
382 } 390 }
383 391
384 var result = %_FastAsciiArrayJoin(this, separator); 392 var result = %_FastAsciiArrayJoin(this, separator);
385 if (!IS_UNDEFINED(result)) return result; 393 if (!IS_UNDEFINED(result)) return result;
386 394
387 return Join(this, TO_UINT32(this.length), separator, ConvertToString); 395 return Join(this, length, separator, ConvertToString);
388 } 396 }
389 397
390 398
391 // Removes the last element from the array and returns it. See 399 // Removes the last element from the array and returns it. See
392 // ECMA-262, section 15.4.4.6. 400 // ECMA-262, section 15.4.4.6.
393 function ArrayPop() { 401 function ArrayPop() {
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;
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 } 915 }
908 916
909 return this; 917 return this;
910 } 918 }
911 919
912 920
913 // The following functions cannot be made efficient on sparse arrays while 921 // The following functions cannot be made efficient on sparse arrays while
914 // preserving the semantics, since the calls to the receiver function can add 922 // preserving the semantics, since the calls to the receiver function can add
915 // or delete elements from the array. 923 // or delete elements from the array.
916 function ArrayFilter(f, receiver) { 924 function ArrayFilter(f, receiver) {
925 var length = TO_UINT32(this.length);
917 if (!IS_FUNCTION(f)) { 926 if (!IS_FUNCTION(f)) {
918 throw MakeTypeError('called_non_callable', [ f ]); 927 throw MakeTypeError('called_non_callable', [ f ]);
919 } 928 }
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 = []; 929 var result = [];
924 var result_length = 0; 930 var result_length = 0;
925 for (var i = 0; i < length; i++) { 931 for (var i = 0; i < length; i++) {
926 var current = this[i]; 932 var current = this[i];
927 if (!IS_UNDEFINED(current) || i in this) { 933 if (!IS_UNDEFINED(current) || i in this) {
928 if (f.call(receiver, current, i, this)) { 934 if (f.call(receiver, current, i, this)) {
929 result[result_length++] = current; 935 result[result_length++] = current;
930 } 936 }
931 } 937 }
932 } 938 }
933 return result; 939 return result;
934 } 940 }
935 941
936 942
937 function ArrayForEach(f, receiver) { 943 function ArrayForEach(f, receiver) {
944 var length = TO_UINT32(this.length);
938 if (!IS_FUNCTION(f)) { 945 if (!IS_FUNCTION(f)) {
939 throw MakeTypeError('called_non_callable', [ f ]); 946 throw MakeTypeError('called_non_callable', [ f ]);
940 } 947 }
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++) { 948 for (var i = 0; i < length; i++) {
945 var current = this[i]; 949 var current = this[i];
946 if (!IS_UNDEFINED(current) || i in this) { 950 if (!IS_UNDEFINED(current) || i in this) {
947 f.call(receiver, current, i, this); 951 f.call(receiver, current, i, this);
948 } 952 }
949 } 953 }
950 } 954 }
951 955
952 956
953 // Executes the function once for each element present in the 957 // Executes the function once for each element present in the
954 // array until it finds one where callback returns true. 958 // array until it finds one where callback returns true.
955 function ArraySome(f, receiver) { 959 function ArraySome(f, receiver) {
960 var length = TO_UINT32(this.length);
956 if (!IS_FUNCTION(f)) { 961 if (!IS_FUNCTION(f)) {
957 throw MakeTypeError('called_non_callable', [ f ]); 962 throw MakeTypeError('called_non_callable', [ f ]);
958 } 963 }
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++) { 964 for (var i = 0; i < length; i++) {
963 var current = this[i]; 965 var current = this[i];
964 if (!IS_UNDEFINED(current) || i in this) { 966 if (!IS_UNDEFINED(current) || i in this) {
965 if (f.call(receiver, current, i, this)) return true; 967 if (f.call(receiver, current, i, this)) return true;
966 } 968 }
967 } 969 }
968 return false; 970 return false;
969 } 971 }
970 972
971 973
972 function ArrayEvery(f, receiver) { 974 function ArrayEvery(f, receiver) {
975 var length = TO_UINT32(this.length);
973 if (!IS_FUNCTION(f)) { 976 if (!IS_FUNCTION(f)) {
974 throw MakeTypeError('called_non_callable', [ f ]); 977 throw MakeTypeError('called_non_callable', [ f ]);
975 } 978 }
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++) { 979 for (var i = 0; i < length; i++) {
980 var current = this[i]; 980 var current = this[i];
981 if (!IS_UNDEFINED(current) || i in this) { 981 if (!IS_UNDEFINED(current) || i in this) {
982 if (!f.call(receiver, current, i, this)) return false; 982 if (!f.call(receiver, current, i, this)) return false;
983 } 983 }
984 } 984 }
985 return true; 985 return true;
986 } 986 }
987 987
988 function ArrayMap(f, receiver) { 988 function ArrayMap(f, receiver) {
989 var length = TO_UINT32(this.length);
989 if (!IS_FUNCTION(f)) { 990 if (!IS_FUNCTION(f)) {
990 throw MakeTypeError('called_non_callable', [ f ]); 991 throw MakeTypeError('called_non_callable', [ f ]);
991 } 992 }
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(); 993 var result = new $Array();
996 var accumulator = new InternalArray(length); 994 var accumulator = new InternalArray(length);
997 for (var i = 0; i < length; i++) { 995 for (var i = 0; i < length; i++) {
998 var current = this[i]; 996 var current = this[i];
999 if (!IS_UNDEFINED(current) || i in this) { 997 if (!IS_UNDEFINED(current) || i in this) {
1000 accumulator[i] = f.call(receiver, current, i, this); 998 accumulator[i] = f.call(receiver, current, i, this);
1001 } 999 }
1002 } 1000 }
1003 %MoveArrayContents(accumulator, result); 1001 %MoveArrayContents(accumulator, result);
1004 return result; 1002 return result;
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1109 for (var i = max; i >= min; i--) { 1107 for (var i = max; i >= min; i--) {
1110 if (IS_UNDEFINED(this[i]) && i in this) { 1108 if (IS_UNDEFINED(this[i]) && i in this) {
1111 return i; 1109 return i;
1112 } 1110 }
1113 } 1111 }
1114 return -1; 1112 return -1;
1115 } 1113 }
1116 1114
1117 1115
1118 function ArrayReduce(callback, current) { 1116 function ArrayReduce(callback, current) {
1117 var length = TO_UINT32(this.length);
1119 if (!IS_FUNCTION(callback)) { 1118 if (!IS_FUNCTION(callback)) {
1120 throw MakeTypeError('called_non_callable', [callback]); 1119 throw MakeTypeError('called_non_callable', [callback]);
1121 } 1120 }
1122 // Pull out the length so that modifications to the length in the 1121 // Pull out the length so that modifications to the length in the
1123 // loop will not affect the looping. 1122 // loop will not affect the looping.
1124 var length = this.length;
1125 var i = 0; 1123 var i = 0;
1126 1124
1127 find_initial: if (%_ArgumentsLength() < 2) { 1125 find_initial: if (%_ArgumentsLength() < 2) {
1128 for (; i < length; i++) { 1126 for (; i < length; i++) {
1129 current = this[i]; 1127 current = this[i];
1130 if (!IS_UNDEFINED(current) || i in this) { 1128 if (!IS_UNDEFINED(current) || i in this) {
1131 i++; 1129 i++;
1132 break find_initial; 1130 break find_initial;
1133 } 1131 }
1134 } 1132 }
1135 throw MakeTypeError('reduce_no_initial', []); 1133 throw MakeTypeError('reduce_no_initial', []);
1136 } 1134 }
1137 1135
1138 for (; i < length; i++) { 1136 for (; i < length; i++) {
1139 var element = this[i]; 1137 var element = this[i];
1140 if (!IS_UNDEFINED(element) || i in this) { 1138 if (!IS_UNDEFINED(element) || i in this) {
1141 current = callback.call(null, current, element, i, this); 1139 current = callback.call(null, current, element, i, this);
1142 } 1140 }
1143 } 1141 }
1144 return current; 1142 return current;
1145 } 1143 }
1146 1144
1147 function ArrayReduceRight(callback, current) { 1145 function ArrayReduceRight(callback, current) {
1146 var i = TO_UINT32(this.length) - 1;
1148 if (!IS_FUNCTION(callback)) { 1147 if (!IS_FUNCTION(callback)) {
1149 throw MakeTypeError('called_non_callable', [callback]); 1148 throw MakeTypeError('called_non_callable', [callback]);
1150 } 1149 }
1151 var i = this.length - 1;
1152 1150
1153 find_initial: if (%_ArgumentsLength() < 2) { 1151 find_initial: if (%_ArgumentsLength() < 2) {
1154 for (; i >= 0; i--) { 1152 for (; i >= 0; i--) {
1155 current = this[i]; 1153 current = this[i];
1156 if (!IS_UNDEFINED(current) || i in this) { 1154 if (!IS_UNDEFINED(current) || i in this) {
1157 i--; 1155 i--;
1158 break find_initial; 1156 break find_initial;
1159 } 1157 }
1160 } 1158 }
1161 throw MakeTypeError('reduce_no_initial', []); 1159 throw MakeTypeError('reduce_no_initial', []);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 InternalArray.prototype.join = getFunction("join", ArrayJoin); 1238 InternalArray.prototype.join = getFunction("join", ArrayJoin);
1241 InternalArray.prototype.pop = getFunction("pop", ArrayPop); 1239 InternalArray.prototype.pop = getFunction("pop", ArrayPop);
1242 InternalArray.prototype.push = getFunction("push", ArrayPush); 1240 InternalArray.prototype.push = getFunction("push", ArrayPush);
1243 InternalArray.prototype.toString = function() { 1241 InternalArray.prototype.toString = function() {
1244 return "Internal Array, length " + this.length; 1242 return "Internal Array, length " + this.length;
1245 }; 1243 };
1246 } 1244 }
1247 1245
1248 1246
1249 SetupArray(); 1247 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