OLD | NEW |
---|---|
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 22 matching lines...) Expand all Loading... | |
33 | 33 |
34 // Global list of arrays visited during toString, toLocaleString and | 34 // Global list of arrays visited during toString, toLocaleString and |
35 // join invocations. | 35 // join invocations. |
36 var visited_arrays = new InternalArray(); | 36 var visited_arrays = new InternalArray(); |
37 | 37 |
38 | 38 |
39 // Gets a sorted array of array keys. Useful for operations on sparse | 39 // Gets a sorted array of array keys. Useful for operations on sparse |
40 // arrays. Dupes have not been removed. | 40 // arrays. Dupes have not been removed. |
41 function GetSortedArrayKeys(array, intervals) { | 41 function GetSortedArrayKeys(array, intervals) { |
42 var length = intervals.length; | 42 var length = intervals.length; |
43 var keys = []; | 43 var keys = new InternalArray(); |
44 for (var k = 0; k < length; k++) { | 44 if (length == 2 && intervals[0] == -1) { |
rossberg
2013/03/28 12:27:07
Nit: using === might be more accurate (and margina
rossberg
2013/03/28 12:27:07
Do you actually need to test that length is 2? An
| |
45 var key = intervals[k]; | 45 // It's an interval |
46 if (key < 0) { | 46 var limit = intervals[1]; |
47 var j = -1 - key; | 47 for (var i = 0; i < limit; ++i) { |
48 var limit = j + intervals[++k]; | 48 var e = array[i]; |
49 for (; j < limit; j++) { | 49 if (!IS_UNDEFINED(e) || i in array) { |
50 var e = array[j]; | 50 keys.push(i); |
51 if (!IS_UNDEFINED(e) || j in array) { | |
52 keys.push(j); | |
53 } | |
54 } | 51 } |
55 } else { | 52 } |
56 // The case where key is undefined also ends here. | 53 } else { |
54 for (var k = 0; k < length; ++k) { | |
55 var key = intervals[k]; | |
57 if (!IS_UNDEFINED(key)) { | 56 if (!IS_UNDEFINED(key)) { |
58 var e = array[key]; | 57 var e = array[key]; |
59 if (!IS_UNDEFINED(e) || key in array) { | 58 if (!IS_UNDEFINED(e) || key in array) { |
60 keys.push(key); | 59 keys.push(key); |
61 } | 60 } |
62 } | 61 } |
63 } | 62 } |
63 %_CallFunction(keys, function(a, b) { return a - b; }, ArraySort); | |
64 } | 64 } |
65 %_CallFunction(keys, function(a, b) { return a - b; }, ArraySort); | |
66 return keys; | 65 return keys; |
67 } | 66 } |
68 | 67 |
69 | 68 |
70 function SparseJoinWithSeparator(array, len, convert, separator) { | 69 function SparseJoinWithSeparator(array, len, convert, separator) { |
71 var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len)); | 70 var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len)); |
72 var totalLength = 0; | 71 var totalLength = 0; |
73 var elements = new InternalArray(keys.length * 2); | 72 var elements = new InternalArray(keys.length * 2); |
74 var previousKey = -1; | 73 var previousKey = -1; |
75 for (var i = 0; i < keys.length; i++) { | 74 for (var i = 0; i < keys.length; i++) { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 } | 212 } |
214 | 213 |
215 | 214 |
216 // This function implements the optimized splice implementation that can use | 215 // This function implements the optimized splice implementation that can use |
217 // special array operations to handle sparse arrays in a sensible fashion. | 216 // special array operations to handle sparse arrays in a sensible fashion. |
218 function SmartSlice(array, start_i, del_count, len, deleted_elements) { | 217 function SmartSlice(array, start_i, del_count, len, deleted_elements) { |
219 // Move deleted elements to a new array (the return value from splice). | 218 // Move deleted elements to a new array (the return value from splice). |
220 // Intervals array can contain keys and intervals. See comment in Concat. | 219 // Intervals array can contain keys and intervals. See comment in Concat. |
221 var intervals = %GetArrayKeys(array, start_i + del_count); | 220 var intervals = %GetArrayKeys(array, start_i + del_count); |
222 var length = intervals.length; | 221 var length = intervals.length; |
223 for (var k = 0; k < length; k++) { | 222 if (length == 2 && intervals[0] == -1) { |
224 var key = intervals[k]; | 223 var interval_limit = intervals[1]; |
225 if (key < 0) { | 224 for (var i = start_i; i < interval_limit; ++i) { |
226 var j = -1 - key; | 225 var current = array[i]; |
227 var interval_limit = j + intervals[++k]; | 226 if (!IS_UNDEFINED(current) || i in array) { |
228 if (j < start_i) { | 227 deleted_elements[i - start_i] = current; |
229 j = start_i; | |
230 } | 228 } |
231 for (; j < interval_limit; j++) { | 229 } |
232 // ECMA-262 15.4.4.12 line 10. The spec could also be | 230 } else { |
233 // interpreted such that %HasLocalProperty would be the | 231 for (var k = 0; k < length; ++k) { |
234 // appropriate test. We follow KJS in consulting the | 232 var key = intervals[k]; |
235 // prototype. | |
236 var current = array[j]; | |
237 if (!IS_UNDEFINED(current) || j in array) { | |
238 deleted_elements[j - start_i] = current; | |
239 } | |
240 } | |
241 } else { | |
242 if (!IS_UNDEFINED(key)) { | 233 if (!IS_UNDEFINED(key)) { |
243 if (key >= start_i) { | 234 if (key >= start_i) { |
244 // ECMA-262 15.4.4.12 line 10. The spec could also be | |
245 // interpreted such that %HasLocalProperty would be the | |
246 // appropriate test. We follow KJS in consulting the | |
247 // prototype. | |
248 var current = array[key]; | 235 var current = array[key]; |
249 if (!IS_UNDEFINED(current) || key in array) { | 236 if (!IS_UNDEFINED(current) || key in array) { |
250 deleted_elements[key - start_i] = current; | 237 deleted_elements[key - start_i] = current; |
251 } | 238 } |
252 } | 239 } |
253 } | 240 } |
254 } | 241 } |
255 } | 242 } |
256 } | 243 } |
257 | 244 |
258 | 245 |
259 // This function implements the optimized splice implementation that can use | 246 // This function implements the optimized splice implementation that can use |
260 // special array operations to handle sparse arrays in a sensible fashion. | 247 // special array operations to handle sparse arrays in a sensible fashion. |
261 function SmartMove(array, start_i, del_count, len, num_additional_args) { | 248 function SmartMove(array, start_i, del_count, len, num_additional_args) { |
262 // Move data to new array. | 249 // Move data to new array. |
263 var new_array = new InternalArray(len - del_count + num_additional_args); | 250 var new_array = new InternalArray(len - del_count + num_additional_args); |
264 var intervals = %GetArrayKeys(array, len); | 251 var intervals = %GetArrayKeys(array, len); |
265 var length = intervals.length; | 252 var length = intervals.length; |
266 for (var k = 0; k < length; k++) { | 253 if (length == 2 && intervals[0] == -1) { |
267 var key = intervals[k]; | 254 var interval_limit = intervals[1]; |
268 if (key < 0) { | 255 for (var i = 0; i < start_i && i < interval_limit; ++i) { |
269 var j = -1 - key; | 256 var current = array[i]; |
270 var interval_limit = j + intervals[++k]; | 257 if (!IS_UNDEFINED(current) || i in array) { |
271 while (j < start_i && j < interval_limit) { | 258 new_array[i] = current; |
272 // The spec could also be interpreted such that | |
273 // %HasLocalProperty would be the appropriate test. We follow | |
274 // KJS in consulting the prototype. | |
275 var current = array[j]; | |
276 if (!IS_UNDEFINED(current) || j in array) { | |
277 new_array[j] = current; | |
278 } | |
279 j++; | |
280 } | 259 } |
281 j = start_i + del_count; | 260 } |
282 while (j < interval_limit) { | 261 for (var i = start_i + del_count; i < interval_limit; ++i) { |
283 // ECMA-262 15.4.4.12 lines 24 and 41. The spec could also be | 262 var current = array[i]; |
284 // interpreted such that %HasLocalProperty would be the | 263 if (!IS_UNDEFINED(current) || i in array) { |
285 // appropriate test. We follow KJS in consulting the | 264 new_array[i - del_count + num_additional_args] = current; |
286 // prototype. | |
287 var current = array[j]; | |
288 if (!IS_UNDEFINED(current) || j in array) { | |
289 new_array[j - del_count + num_additional_args] = current; | |
290 } | |
291 j++; | |
292 } | 265 } |
293 } else { | 266 } |
267 } else { | |
268 for (var k = 0; k < length; ++k) { | |
269 var key = intervals[k]; | |
294 if (!IS_UNDEFINED(key)) { | 270 if (!IS_UNDEFINED(key)) { |
295 if (key < start_i) { | 271 if (key < start_i) { |
296 // The spec could also be interpreted such that | |
297 // %HasLocalProperty would be the appropriate test. We follow | |
298 // KJS in consulting the prototype. | |
299 var current = array[key]; | 272 var current = array[key]; |
300 if (!IS_UNDEFINED(current) || key in array) { | 273 if (!IS_UNDEFINED(current) || key in array) { |
301 new_array[key] = current; | 274 new_array[key] = current; |
302 } | 275 } |
303 } else if (key >= start_i + del_count) { | 276 } else if (key >= start_i + del_count) { |
304 // ECMA-262 15.4.4.12 lines 24 and 41. The spec could also | |
305 // be interpreted such that %HasLocalProperty would be the | |
306 // appropriate test. We follow KJS in consulting the | |
307 // prototype. | |
308 var current = array[key]; | 277 var current = array[key]; |
309 if (!IS_UNDEFINED(current) || key in array) { | 278 if (!IS_UNDEFINED(current) || key in array) { |
310 new_array[key - del_count + num_additional_args] = current; | 279 new_array[key - del_count + num_additional_args] = current; |
311 } | 280 } |
312 } | 281 } |
313 } | 282 } |
314 } | 283 } |
315 } | 284 } |
316 // Move contents of new_array into this array | 285 // Move contents of new_array into this array |
317 %MoveArrayContents(new_array, array); | 286 %MoveArrayContents(new_array, array); |
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
880 } | 849 } |
881 }; | 850 }; |
882 | 851 |
883 // Copy elements in the range 0..length from obj's prototype chain | 852 // Copy elements in the range 0..length from obj's prototype chain |
884 // to obj itself, if obj has holes. Return one more than the maximal index | 853 // to obj itself, if obj has holes. Return one more than the maximal index |
885 // of a prototype property. | 854 // of a prototype property. |
886 var CopyFromPrototype = function CopyFromPrototype(obj, length) { | 855 var CopyFromPrototype = function CopyFromPrototype(obj, length) { |
887 var max = 0; | 856 var max = 0; |
888 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { | 857 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { |
889 var indices = %GetArrayKeys(proto, length); | 858 var indices = %GetArrayKeys(proto, length); |
890 if (indices.length > 0) { | 859 if (indices.length == 2 && indices[0] == -1) { |
891 if (indices[0] == -1) { | 860 // It's an interval. |
892 // It's an interval. | 861 var proto_length = indices[1]; |
893 var proto_length = indices[1]; | 862 for (var i = 0; i < proto_length; i++) { |
894 for (var i = 0; i < proto_length; i++) { | 863 if (!obj.hasOwnProperty(i) && proto.hasOwnProperty(i)) { |
895 if (!obj.hasOwnProperty(i) && proto.hasOwnProperty(i)) { | 864 obj[i] = proto[i]; |
896 obj[i] = proto[i]; | 865 if (i >= max) { max = i + 1; } |
897 if (i >= max) { max = i + 1; } | |
898 } | |
899 } | 866 } |
900 } else { | 867 } |
901 for (var i = 0; i < indices.length; i++) { | 868 } else { |
902 var index = indices[i]; | 869 for (var i = 0; i < indices.length; i++) { |
903 if (!IS_UNDEFINED(index) && | 870 var index = indices[i]; |
904 !obj.hasOwnProperty(index) && proto.hasOwnProperty(index)) { | 871 if (!IS_UNDEFINED(index) && |
905 obj[index] = proto[index]; | 872 !obj.hasOwnProperty(index) && proto.hasOwnProperty(index)) { |
906 if (index >= max) { max = index + 1; } | 873 obj[index] = proto[index]; |
907 } | 874 if (index >= max) { max = index + 1; } |
908 } | 875 } |
909 } | 876 } |
910 } | 877 } |
911 } | 878 } |
912 return max; | 879 return max; |
913 }; | 880 }; |
914 | 881 |
915 // Set a value of "undefined" on all indices in the range from..to | 882 // Set a value of "undefined" on all indices in the range from..to |
916 // where a prototype of obj has an element. I.e., shadow all prototype | 883 // where a prototype of obj has an element. I.e., shadow all prototype |
917 // elements in that range. | 884 // elements in that range. |
918 var ShadowPrototypeElements = function(obj, from, to) { | 885 var ShadowPrototypeElements = function(obj, from, to) { |
919 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { | 886 for (var proto = %GetPrototype(obj); proto; proto = %GetPrototype(proto)) { |
920 var indices = %GetArrayKeys(proto, to); | 887 var indices = %GetArrayKeys(proto, to); |
921 if (indices.length > 0) { | 888 if (indices.length == 2 && indices[0] == -1) { |
922 if (indices[0] == -1) { | 889 // It's an interval. |
923 // It's an interval. | 890 var proto_length = indices[1]; |
924 var proto_length = indices[1]; | 891 for (var i = from; i < proto_length; i++) { |
925 for (var i = from; i < proto_length; i++) { | 892 if (proto.hasOwnProperty(i)) { |
926 if (proto.hasOwnProperty(i)) { | 893 obj[i] = void 0; |
927 obj[i] = void 0; | |
928 } | |
929 } | 894 } |
930 } else { | 895 } |
931 for (var i = 0; i < indices.length; i++) { | 896 } else { |
932 var index = indices[i]; | 897 for (var i = 0; i < indices.length; i++) { |
933 if (!IS_UNDEFINED(index) && from <= index && | 898 var index = indices[i]; |
934 proto.hasOwnProperty(index)) { | 899 if (!IS_UNDEFINED(index) && from <= index && |
935 obj[index] = void 0; | 900 proto.hasOwnProperty(index)) { |
936 } | 901 obj[index] = void 0; |
937 } | 902 } |
938 } | 903 } |
939 } | 904 } |
940 } | 905 } |
941 }; | 906 }; |
942 | 907 |
943 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) { | 908 var SafeRemoveArrayHoles = function SafeRemoveArrayHoles(obj) { |
944 // Copy defined elements from the end to fill in all holes and undefineds | 909 // Copy defined elements from the end to fill in all holes and undefineds |
945 // in the beginning of the array. Write undefineds and holes at the end | 910 // in the beginning of the array. Write undefineds and holes at the end |
946 // after loop is finished. | 911 // after loop is finished. |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1278 if (index < 0) { | 1243 if (index < 0) { |
1279 index = length + index; | 1244 index = length + index; |
1280 // If index is still negative, search the entire array. | 1245 // If index is still negative, search the entire array. |
1281 if (index < 0) index = 0; | 1246 if (index < 0) index = 0; |
1282 } | 1247 } |
1283 } | 1248 } |
1284 var min = index; | 1249 var min = index; |
1285 var max = length; | 1250 var max = length; |
1286 if (UseSparseVariant(this, length, IS_ARRAY(this))) { | 1251 if (UseSparseVariant(this, length, IS_ARRAY(this))) { |
1287 var intervals = %GetArrayKeys(this, length); | 1252 var intervals = %GetArrayKeys(this, length); |
1288 if (intervals.length == 2 && intervals[0] < 0) { | 1253 if (intervals.length == 2 && intervals[0] == -1) { |
1289 // A single interval. | 1254 // A single interval. |
rossberg
2013/03/28 12:27:07
Nit: adjust comment ("An interval")
| |
1290 var intervalMin = -(intervals[0] + 1); | 1255 max = intervals[1]; // Capped by length already. |
1291 var intervalMax = intervalMin + intervals[1]; | |
1292 if (min < intervalMin) min = intervalMin; | |
1293 max = intervalMax; // Capped by length already. | |
1294 // Fall through to loop below. | 1256 // Fall through to loop below. |
1295 } else { | 1257 } else { |
1296 if (intervals.length == 0) return -1; | 1258 if (intervals.length == 0) return -1; |
1297 // Get all the keys in sorted order. | 1259 // Get all the keys in sorted order. |
1298 var sortedKeys = GetSortedArrayKeys(this, intervals); | 1260 var sortedKeys = GetSortedArrayKeys(this, intervals); |
1299 var n = sortedKeys.length; | 1261 var n = sortedKeys.length; |
1300 var i = 0; | 1262 var i = 0; |
1301 while (i < n && sortedKeys[i] < index) i++; | 1263 while (i < n && sortedKeys[i] < index) i++; |
1302 while (i < n) { | 1264 while (i < n) { |
1303 var key = sortedKeys[i]; | 1265 var key = sortedKeys[i]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1339 // If index is negative, index from end of the array. | 1301 // If index is negative, index from end of the array. |
1340 if (index < 0) index += length; | 1302 if (index < 0) index += length; |
1341 // If index is still negative, do not search the array. | 1303 // If index is still negative, do not search the array. |
1342 if (index < 0) return -1; | 1304 if (index < 0) return -1; |
1343 else if (index >= length) index = length - 1; | 1305 else if (index >= length) index = length - 1; |
1344 } | 1306 } |
1345 var min = 0; | 1307 var min = 0; |
1346 var max = index; | 1308 var max = index; |
1347 if (UseSparseVariant(this, length, IS_ARRAY(this))) { | 1309 if (UseSparseVariant(this, length, IS_ARRAY(this))) { |
1348 var intervals = %GetArrayKeys(this, index + 1); | 1310 var intervals = %GetArrayKeys(this, index + 1); |
1349 if (intervals.length == 2 && intervals[0] < 0) { | 1311 if (intervals.length == 2 && intervals[0] == -1) { |
1350 // A single interval. | 1312 // A single interval. |
rossberg
2013/03/28 12:27:07
Same here
| |
1351 var intervalMin = -(intervals[0] + 1); | 1313 max = intervals[1]; // Capped by index already. |
1352 var intervalMax = intervalMin + intervals[1]; | |
1353 if (min < intervalMin) min = intervalMin; | |
1354 max = intervalMax; // Capped by index already. | |
1355 // Fall through to loop below. | 1314 // Fall through to loop below. |
1356 } else { | 1315 } else { |
1357 if (intervals.length == 0) return -1; | 1316 if (intervals.length == 0) return -1; |
1358 // Get all the keys in sorted order. | 1317 // Get all the keys in sorted order. |
1359 var sortedKeys = GetSortedArrayKeys(this, intervals); | 1318 var sortedKeys = GetSortedArrayKeys(this, intervals); |
1360 var i = sortedKeys.length - 1; | 1319 var i = sortedKeys.length - 1; |
1361 while (i >= 0) { | 1320 while (i >= 0) { |
1362 var key = sortedKeys[i]; | 1321 var key = sortedKeys[i]; |
1363 if (!IS_UNDEFINED(key) && this[key] === element) return key; | 1322 if (!IS_UNDEFINED(key) && this[key] === element) return key; |
1364 i--; | 1323 i--; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1562 )); | 1521 )); |
1563 | 1522 |
1564 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( | 1523 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( |
1565 "join", getFunction("join", ArrayJoin), | 1524 "join", getFunction("join", ArrayJoin), |
1566 "pop", getFunction("pop", ArrayPop), | 1525 "pop", getFunction("pop", ArrayPop), |
1567 "push", getFunction("push", ArrayPush) | 1526 "push", getFunction("push", ArrayPush) |
1568 )); | 1527 )); |
1569 } | 1528 } |
1570 | 1529 |
1571 SetUpArray(); | 1530 SetUpArray(); |
OLD | NEW |