Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // This file relies on the fact that the following declarations have been made |
| 29 | // in runtime.js: |
| 30 | // const $Array = global.Array; |
| 31 | |
| 32 | // ------------------------------------------------------------------- |
| 33 | |
| 34 | // Global list of arrays visited during toString, toLocaleString and |
| 35 | // join invocations. |
| 36 | var visited_arrays = new $Array(); |
| 37 | |
| 38 | |
| 39 | // Gets a sorted array of array keys. Useful for operations on sparse |
| 40 | // arrays. Dupes have not been removed. |
| 41 | function GetSortedArrayKeys(array, intervals) { |
| 42 | var length = intervals.length; |
| 43 | var keys = []; |
| 44 | for (var k = 0; k < length; k++) { |
| 45 | var key = intervals[k]; |
| 46 | if (key < 0) { |
| 47 | var j = -1 - key; |
| 48 | var limit = j + intervals[++k]; |
| 49 | for (; j < limit; j++) { |
| 50 | var e = array[j]; |
| 51 | if (!IS_UNDEFINED(e) || j in array) { |
| 52 | keys.push(j); |
| 53 | } |
| 54 | } |
| 55 | } else { |
| 56 | // The case where key is undefined also ends here. |
| 57 | if (!IS_UNDEFINED(key)) { |
| 58 | var e = array[key]; |
| 59 | if (!IS_UNDEFINED(e) || key in array) { |
| 60 | keys.push(key); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | keys.sort(function(a, b) { return a - b; }); |
| 66 | return keys; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | // Optimized for sparse arrays if separator is ''. |
| 71 | function SparseJoin(array, len, convert) { |
| 72 | var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | var last_key = -1; |
| 74 | var keys_length = keys.length; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 75 | |
| 76 | var elements = new $Array(keys_length); |
| 77 | var elements_length = 0; |
| 78 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | for (var i = 0; i < keys_length; i++) { |
| 80 | var key = keys[i]; |
| 81 | if (key != last_key) { |
| 82 | var e = array[key]; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 83 | if (!IS_STRING(e)) e = convert(e); |
| 84 | elements[elements_length++] = e; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | last_key = key; |
| 86 | } |
| 87 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 88 | return %StringBuilderConcat(elements, elements_length, ''); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | function UseSparseVariant(object, length, is_array) { |
| 93 | return is_array && |
| 94 | length > 1000 && |
| 95 | (!%_IsSmi(length) || |
| 96 | %EstimateNumberOfElements(object) < (length >> 2)); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | function Join(array, length, separator, convert) { |
| 101 | if (length == 0) return ''; |
| 102 | |
| 103 | var is_array = IS_ARRAY(array); |
| 104 | |
| 105 | if (is_array) { |
| 106 | // If the array is cyclic, return the empty string for already |
| 107 | // visited arrays. |
| 108 | if (!%PushIfAbsent(visited_arrays, array)) return ''; |
| 109 | } |
| 110 | |
| 111 | // Attempt to convert the elements. |
| 112 | try { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 113 | if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | return SparseJoin(array, length, convert); |
| 115 | } |
| 116 | |
| 117 | // Fast case for one-element arrays. |
| 118 | if (length == 1) { |
| 119 | var e = array[0]; |
| 120 | if (!IS_UNDEFINED(e) || (0 in array)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 121 | if (IS_STRING(e)) return e; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | return convert(e); |
| 123 | } |
| 124 | } |
| 125 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 126 | // Construct an array for the elements. |
| 127 | var elements; |
| 128 | var elements_length = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 130 | // We pull the empty separator check outside the loop for speed! |
| 131 | if (separator.length == 0) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 132 | elements = new $Array(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 133 | for (var i = 0; i < length; i++) { |
| 134 | var e = array[i]; |
| 135 | if (!IS_UNDEFINED(e) || (i in array)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 136 | if (!IS_STRING(e)) e = convert(e); |
| 137 | elements[elements_length++] = e; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 141 | elements = new $Array(length << 1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 142 | for (var i = 0; i < length; i++) { |
| 143 | var e = array[i]; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 144 | if (i != 0) elements[elements_length++] = separator; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 145 | if (!IS_UNDEFINED(e) || (i in array)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 146 | if (!IS_STRING(e)) e = convert(e); |
| 147 | elements[elements_length++] = e; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 148 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 151 | return %StringBuilderConcat(elements, elements_length, ''); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 152 | } finally { |
| 153 | // Make sure to pop the visited array no matter what happens. |
| 154 | if (is_array) visited_arrays.pop(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | function ConvertToString(e) { |
| 160 | if (e == null) return ''; |
| 161 | else return ToString(e); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | function ConvertToLocaleString(e) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 166 | if (e == null) { |
| 167 | return ''; |
| 168 | } else { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | // e_obj's toLocaleString might be overwritten, check if it is a function. |
| 170 | // Call ToString if toLocaleString is not a function. |
| 171 | // See issue 877615. |
| 172 | var e_obj = ToObject(e); |
| 173 | if (IS_FUNCTION(e_obj.toLocaleString)) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 174 | return ToString(e_obj.toLocaleString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 175 | else |
| 176 | return ToString(e); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | // This function implements the optimized splice implementation that can use |
| 182 | // special array operations to handle sparse arrays in a sensible fashion. |
| 183 | function SmartSlice(array, start_i, del_count, len, deleted_elements) { |
| 184 | // Move deleted elements to a new array (the return value from splice). |
| 185 | // Intervals array can contain keys and intervals. See comment in Concat. |
| 186 | var intervals = %GetArrayKeys(array, start_i + del_count); |
| 187 | var length = intervals.length; |
| 188 | for (var k = 0; k < length; k++) { |
| 189 | var key = intervals[k]; |
| 190 | if (key < 0) { |
| 191 | var j = -1 - key; |
| 192 | var interval_limit = j + intervals[++k]; |
| 193 | if (j < start_i) { |
| 194 | j = start_i; |
| 195 | } |
| 196 | for (; j < interval_limit; j++) { |
| 197 | // ECMA-262 15.4.4.12 line 10. The spec could also be |
| 198 | // interpreted such that %HasLocalProperty would be the |
| 199 | // appropriate test. We follow KJS in consulting the |
| 200 | // prototype. |
| 201 | var current = array[j]; |
| 202 | if (!IS_UNDEFINED(current) || j in array) { |
| 203 | deleted_elements[j - start_i] = current; |
| 204 | } |
| 205 | } |
| 206 | } else { |
| 207 | if (!IS_UNDEFINED(key)) { |
| 208 | if (key >= start_i) { |
| 209 | // ECMA-262 15.4.4.12 line 10. The spec could also be |
| 210 | // interpreted such that %HasLocalProperty would be the |
| 211 | // appropriate test. We follow KJS in consulting the |
| 212 | // prototype. |
| 213 | var current = array[key]; |
| 214 | if (!IS_UNDEFINED(current) || key in array) { |
| 215 | deleted_elements[key - start_i] = current; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
| 224 | // This function implements the optimized splice implementation that can use |
| 225 | // special array operations to handle sparse arrays in a sensible fashion. |
| 226 | function SmartMove(array, start_i, del_count, len, num_additional_args) { |
| 227 | // Move data to new array. |
| 228 | var new_array = new $Array(len - del_count + num_additional_args); |
| 229 | var intervals = %GetArrayKeys(array, len); |
| 230 | var length = intervals.length; |
| 231 | for (var k = 0; k < length; k++) { |
| 232 | var key = intervals[k]; |
| 233 | if (key < 0) { |
| 234 | var j = -1 - key; |
| 235 | var interval_limit = j + intervals[++k]; |
| 236 | while (j < start_i && j < interval_limit) { |
| 237 | // The spec could also be interpreted such that |
| 238 | // %HasLocalProperty would be the appropriate test. We follow |
| 239 | // KJS in consulting the prototype. |
| 240 | var current = array[j]; |
| 241 | if (!IS_UNDEFINED(current) || j in array) { |
| 242 | new_array[j] = current; |
| 243 | } |
| 244 | j++; |
| 245 | } |
| 246 | j = start_i + del_count; |
| 247 | while (j < interval_limit) { |
| 248 | // ECMA-262 15.4.4.12 lines 24 and 41. The spec could also be |
| 249 | // interpreted such that %HasLocalProperty would be the |
| 250 | // appropriate test. We follow KJS in consulting the |
| 251 | // prototype. |
| 252 | var current = array[j]; |
| 253 | if (!IS_UNDEFINED(current) || j in array) { |
| 254 | new_array[j - del_count + num_additional_args] = current; |
| 255 | } |
| 256 | j++; |
| 257 | } |
| 258 | } else { |
| 259 | if (!IS_UNDEFINED(key)) { |
| 260 | if (key < start_i) { |
| 261 | // The spec could also be interpreted such that |
| 262 | // %HasLocalProperty would be the appropriate test. We follow |
| 263 | // KJS in consulting the prototype. |
| 264 | var current = array[key]; |
| 265 | if (!IS_UNDEFINED(current) || key in array) { |
| 266 | new_array[key] = current; |
| 267 | } |
| 268 | } else if (key >= start_i + del_count) { |
| 269 | // ECMA-262 15.4.4.12 lines 24 and 41. The spec could also |
| 270 | // be interpreted such that %HasLocalProperty would be the |
| 271 | // appropriate test. We follow KJS in consulting the |
| 272 | // prototype. |
| 273 | var current = array[key]; |
| 274 | if (!IS_UNDEFINED(current) || key in array) { |
| 275 | new_array[key - del_count + num_additional_args] = current; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | // Move contents of new_array into this array |
| 282 | %MoveArrayContents(new_array, array); |
| 283 | } |
| 284 | |
| 285 | |
| 286 | // This is part of the old simple-minded splice. We are using it either |
| 287 | // because the receiver is not an array (so we have no choice) or because we |
| 288 | // know we are not deleting or moving a lot of elements. |
| 289 | function SimpleSlice(array, start_i, del_count, len, deleted_elements) { |
| 290 | for (var i = 0; i < del_count; i++) { |
| 291 | var index = start_i + i; |
| 292 | // The spec could also be interpreted such that %HasLocalProperty |
| 293 | // would be the appropriate test. We follow KJS in consulting the |
| 294 | // prototype. |
| 295 | var current = array[index]; |
| 296 | if (!IS_UNDEFINED(current) || index in array) |
| 297 | deleted_elements[i] = current; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | function SimpleMove(array, start_i, del_count, len, num_additional_args) { |
| 303 | if (num_additional_args !== del_count) { |
| 304 | // Move the existing elements after the elements to be deleted |
| 305 | // to the right position in the resulting array. |
| 306 | if (num_additional_args > del_count) { |
| 307 | for (var i = len - del_count; i > start_i; i--) { |
| 308 | var from_index = i + del_count - 1; |
| 309 | var to_index = i + num_additional_args - 1; |
| 310 | // The spec could also be interpreted such that |
| 311 | // %HasLocalProperty would be the appropriate test. We follow |
| 312 | // KJS in consulting the prototype. |
| 313 | var current = array[from_index]; |
| 314 | if (!IS_UNDEFINED(current) || from_index in array) { |
| 315 | array[to_index] = current; |
| 316 | } else { |
| 317 | delete array[to_index]; |
| 318 | } |
| 319 | } |
| 320 | } else { |
| 321 | for (var i = start_i; i < len - del_count; i++) { |
| 322 | var from_index = i + del_count; |
| 323 | var to_index = i + num_additional_args; |
| 324 | // The spec could also be interpreted such that |
| 325 | // %HasLocalProperty would be the appropriate test. We follow |
| 326 | // KJS in consulting the prototype. |
| 327 | var current = array[from_index]; |
| 328 | if (!IS_UNDEFINED(current) || from_index in array) { |
| 329 | array[to_index] = current; |
| 330 | } else { |
| 331 | delete array[to_index]; |
| 332 | } |
| 333 | } |
| 334 | for (var i = len; i > len - del_count + num_additional_args; i--) { |
| 335 | delete array[i - 1]; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | |
| 342 | // ------------------------------------------------------------------- |
| 343 | |
| 344 | |
| 345 | function ArrayToString() { |
| 346 | if (!IS_ARRAY(this)) { |
| 347 | throw new $TypeError('Array.prototype.toString is not generic'); |
| 348 | } |
| 349 | return Join(this, this.length, ',', ConvertToString); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | function ArrayToLocaleString() { |
| 354 | if (!IS_ARRAY(this)) { |
| 355 | throw new $TypeError('Array.prototype.toString is not generic'); |
| 356 | } |
| 357 | return Join(this, this.length, ',', ConvertToLocaleString); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | function ArrayJoin(separator) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 362 | if (IS_UNDEFINED(separator)) { |
| 363 | separator = ','; |
| 364 | } else if (!IS_STRING(separator)) { |
| 365 | separator = ToString(separator); |
| 366 | } |
| 367 | var length = TO_UINT32(this.length); |
| 368 | return Join(this, length, separator, ConvertToString); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | // Removes the last element from the array and returns it. See |
| 373 | // ECMA-262, section 15.4.4.6. |
| 374 | function ArrayPop() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 375 | var n = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | if (n == 0) { |
| 377 | this.length = n; |
| 378 | return; |
| 379 | } |
| 380 | n--; |
| 381 | var value = this[n]; |
| 382 | this.length = n; |
| 383 | delete this[n]; |
| 384 | return value; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | // Appends the arguments to the end of the array and returns the new |
| 389 | // length of the array. See ECMA-262, section 15.4.4.7. |
| 390 | function ArrayPush() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 391 | var n = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | var m = %_ArgumentsLength(); |
| 393 | for (var i = 0; i < m; i++) { |
| 394 | this[i+n] = %_Arguments(i); |
| 395 | } |
| 396 | this.length = n + m; |
| 397 | return this.length; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | function ArrayConcat(arg1) { // length == 1 |
| 402 | // TODO: can we just use arguments? |
| 403 | var arg_count = %_ArgumentsLength(); |
| 404 | var arrays = new $Array(1 + arg_count); |
| 405 | arrays[0] = this; |
| 406 | for (var i = 0; i < arg_count; i++) { |
| 407 | arrays[i + 1] = %_Arguments(i); |
| 408 | } |
| 409 | |
| 410 | return %ArrayConcat(arrays); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | // For implementing reverse() on large, sparse arrays. |
| 415 | function SparseReverse(array, len) { |
| 416 | var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, len)); |
| 417 | var high_counter = keys.length - 1; |
| 418 | var low_counter = 0; |
| 419 | while (low_counter <= high_counter) { |
| 420 | var i = keys[low_counter]; |
| 421 | var j = keys[high_counter]; |
| 422 | |
| 423 | var j_complement = len - j - 1; |
| 424 | var low, high; |
| 425 | |
| 426 | if (j_complement <= i) { |
| 427 | high = j; |
| 428 | while (keys[--high_counter] == j); |
| 429 | low = j_complement; |
| 430 | } |
| 431 | if (j_complement >= i) { |
| 432 | low = i; |
| 433 | while (keys[++low_counter] == i); |
| 434 | high = len - i - 1; |
| 435 | } |
| 436 | |
| 437 | var current_i = array[low]; |
| 438 | if (!IS_UNDEFINED(current_i) || low in array) { |
| 439 | var current_j = array[high]; |
| 440 | if (!IS_UNDEFINED(current_j) || high in array) { |
| 441 | array[low] = current_j; |
| 442 | array[high] = current_i; |
| 443 | } else { |
| 444 | array[high] = current_i; |
| 445 | delete array[low]; |
| 446 | } |
| 447 | } else { |
| 448 | var current_j = array[high]; |
| 449 | if (!IS_UNDEFINED(current_j) || high in array) { |
| 450 | array[low] = current_j; |
| 451 | delete array[high]; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | function ArrayReverse() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 459 | var j = TO_UINT32(this.length) - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | |
| 461 | if (UseSparseVariant(this, j, IS_ARRAY(this))) { |
| 462 | SparseReverse(this, j+1); |
| 463 | return this; |
| 464 | } |
| 465 | |
| 466 | for (var i = 0; i < j; i++, j--) { |
| 467 | var current_i = this[i]; |
| 468 | if (!IS_UNDEFINED(current_i) || i in this) { |
| 469 | var current_j = this[j]; |
| 470 | if (!IS_UNDEFINED(current_j) || j in this) { |
| 471 | this[i] = current_j; |
| 472 | this[j] = current_i; |
| 473 | } else { |
| 474 | this[j] = current_i; |
| 475 | delete this[i]; |
| 476 | } |
| 477 | } else { |
| 478 | var current_j = this[j]; |
| 479 | if (!IS_UNDEFINED(current_j) || j in this) { |
| 480 | this[i] = current_j; |
| 481 | delete this[j]; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | return this; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | function ArrayShift() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 490 | var len = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 491 | |
| 492 | if (len === 0) { |
| 493 | this.length = 0; |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | var first = this[0]; |
| 498 | |
| 499 | if (IS_ARRAY(this)) |
| 500 | SmartMove(this, 0, 1, len, 0); |
| 501 | else |
| 502 | SimpleMove(this, 0, 1, len, 0); |
| 503 | |
| 504 | this.length = len - 1; |
| 505 | |
| 506 | return first; |
| 507 | } |
| 508 | |
| 509 | |
| 510 | function ArrayUnshift(arg1) { // length == 1 |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 511 | var len = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | var num_arguments = %_ArgumentsLength(); |
| 513 | |
| 514 | if (IS_ARRAY(this)) |
| 515 | SmartMove(this, 0, 0, len, num_arguments); |
| 516 | else |
| 517 | SimpleMove(this, 0, 0, len, num_arguments); |
| 518 | |
| 519 | for (var i = 0; i < num_arguments; i++) { |
| 520 | this[i] = %_Arguments(i); |
| 521 | } |
| 522 | |
| 523 | this.length = len + num_arguments; |
| 524 | |
| 525 | return len + num_arguments; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | function ArraySlice(start, end) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 530 | var len = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 531 | var start_i = TO_INTEGER(start); |
| 532 | var end_i = len; |
| 533 | |
| 534 | if (end !== void 0) end_i = TO_INTEGER(end); |
| 535 | |
| 536 | if (start_i < 0) { |
| 537 | start_i += len; |
| 538 | if (start_i < 0) start_i = 0; |
| 539 | } else { |
| 540 | if (start_i > len) start_i = len; |
| 541 | } |
| 542 | |
| 543 | if (end_i < 0) { |
| 544 | end_i += len; |
| 545 | if (end_i < 0) end_i = 0; |
| 546 | } else { |
| 547 | if (end_i > len) end_i = len; |
| 548 | } |
| 549 | |
| 550 | var result = []; |
| 551 | |
| 552 | if (end_i < start_i) return result; |
| 553 | |
| 554 | if (IS_ARRAY(this)) { |
| 555 | SmartSlice(this, start_i, end_i - start_i, len, result); |
| 556 | } else { |
| 557 | SimpleSlice(this, start_i, end_i - start_i, len, result); |
| 558 | } |
| 559 | |
| 560 | result.length = end_i - start_i; |
| 561 | |
| 562 | return result; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | function ArraySplice(start, delete_count) { |
| 567 | var num_arguments = %_ArgumentsLength(); |
| 568 | |
| 569 | // SpiderMonkey and KJS return undefined in the case where no |
| 570 | // arguments are given instead of using the implicit undefined |
| 571 | // arguments. This does not follow ECMA-262, but we do the same for |
| 572 | // compatibility. |
| 573 | if (num_arguments == 0) return; |
| 574 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 575 | var len = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | var start_i = TO_INTEGER(start); |
| 577 | |
| 578 | if (start_i < 0) { |
| 579 | start_i += len; |
| 580 | if (start_i < 0) start_i = 0; |
| 581 | } else { |
| 582 | if (start_i > len) start_i = len; |
| 583 | } |
| 584 | |
| 585 | // SpiderMonkey and KJS treat the case where no delete count is |
| 586 | // given differently from when an undefined delete count is given. |
| 587 | // This does not follow ECMA-262, but we do the same for |
| 588 | // compatibility. |
| 589 | var del_count = 0; |
| 590 | if (num_arguments > 1) { |
| 591 | del_count = TO_INTEGER(delete_count); |
| 592 | if (del_count < 0) del_count = 0; |
| 593 | if (del_count > len - start_i) del_count = len - start_i; |
| 594 | } else { |
| 595 | del_count = len - start_i; |
| 596 | } |
| 597 | |
| 598 | var deleted_elements = []; |
| 599 | deleted_elements.length = del_count; |
| 600 | |
| 601 | // Number of elements to add. |
| 602 | var num_additional_args = 0; |
| 603 | if (num_arguments > 2) { |
| 604 | num_additional_args = num_arguments - 2; |
| 605 | } |
| 606 | |
| 607 | var use_simple_splice = true; |
| 608 | |
| 609 | if (IS_ARRAY(this) && num_additional_args !== del_count) { |
| 610 | // If we are only deleting/moving a few things near the end of the |
| 611 | // array then the simple version is going to be faster, because it |
| 612 | // doesn't touch most of the array. |
| 613 | var estimated_non_hole_elements = %EstimateNumberOfElements(this); |
| 614 | if (len > 20 && (estimated_non_hole_elements >> 2) < (len - start_i)) { |
| 615 | use_simple_splice = false; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if (use_simple_splice) { |
| 620 | SimpleSlice(this, start_i, del_count, len, deleted_elements); |
| 621 | SimpleMove(this, start_i, del_count, len, num_additional_args); |
| 622 | } else { |
| 623 | SmartSlice(this, start_i, del_count, len, deleted_elements); |
| 624 | SmartMove(this, start_i, del_count, len, num_additional_args); |
| 625 | } |
| 626 | |
| 627 | // Insert the arguments into the resulting array in |
| 628 | // place of the deleted elements. |
| 629 | var i = start_i; |
| 630 | var arguments_index = 2; |
| 631 | var arguments_length = %_ArgumentsLength(); |
| 632 | while (arguments_index < arguments_length) { |
| 633 | this[i++] = %_Arguments(arguments_index++); |
| 634 | } |
| 635 | this.length = len - del_count + num_additional_args; |
| 636 | |
| 637 | // Return the deleted elements. |
| 638 | return deleted_elements; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | function ArraySort(comparefn) { |
| 643 | // In-place QuickSort algorithm. |
| 644 | // For short (length <= 22) arrays, insertion sort is used for efficiency. |
| 645 | |
| 646 | var custom_compare = IS_FUNCTION(comparefn); |
| 647 | |
| 648 | function Compare(x,y) { |
| 649 | // Assume the comparefn, if any, is a consistent comparison function. |
| 650 | // If it isn't, we are allowed arbitrary behavior by ECMA 15.4.4.11. |
| 651 | if (x === y) return 0; |
| 652 | if (custom_compare) { |
| 653 | // Don't call directly to avoid exposing the builtin's global object. |
| 654 | return comparefn.call(null, x, y); |
| 655 | } |
| 656 | if (%_IsSmi(x) && %_IsSmi(y)) { |
| 657 | return %SmiLexicographicCompare(x, y); |
| 658 | } |
| 659 | x = ToString(x); |
| 660 | y = ToString(y); |
| 661 | if (x == y) return 0; |
| 662 | else return x < y ? -1 : 1; |
| 663 | }; |
| 664 | |
| 665 | function InsertionSort(a, from, to) { |
| 666 | for (var i = from + 1; i < to; i++) { |
| 667 | var element = a[i]; |
| 668 | // Pre-convert the element to a string for comparison if we know |
| 669 | // it will happen on each compare anyway. |
| 670 | var key = |
| 671 | (custom_compare || %_IsSmi(element)) ? element : ToString(element); |
| 672 | // place element in a[from..i[ |
| 673 | // binary search |
| 674 | var min = from; |
| 675 | var max = i; |
| 676 | // The search interval is a[min..max[ |
| 677 | while (min < max) { |
| 678 | var mid = min + ((max - min) >> 1); |
| 679 | var order = Compare(a[mid], key); |
| 680 | if (order == 0) { |
| 681 | min = max = mid; |
| 682 | break; |
| 683 | } |
| 684 | if (order < 0) { |
| 685 | min = mid + 1; |
| 686 | } else { |
| 687 | max = mid; |
| 688 | } |
| 689 | } |
| 690 | // place element at position min==max. |
| 691 | for (var j = i; j > min; j--) { |
| 692 | a[j] = a[j - 1]; |
| 693 | } |
| 694 | a[min] = element; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | function QuickSort(a, from, to) { |
| 699 | // Insertion sort is faster for short arrays. |
| 700 | if (to - from <= 22) { |
| 701 | InsertionSort(a, from, to); |
| 702 | return; |
| 703 | } |
| 704 | var pivot_index = $floor($random() * (to - from)) + from; |
| 705 | var pivot = a[pivot_index]; |
| 706 | // Pre-convert the element to a string for comparison if we know |
| 707 | // it will happen on each compare anyway. |
| 708 | var pivot_key = |
| 709 | (custom_compare || %_IsSmi(pivot)) ? pivot : ToString(pivot); |
| 710 | // Issue 95: Keep the pivot element out of the comparisons to avoid |
| 711 | // infinite recursion if comparefn(pivot, pivot) != 0. |
| 712 | a[pivot_index] = a[from]; |
| 713 | a[from] = pivot; |
| 714 | var low_end = from; // Upper bound of the elements lower than pivot. |
| 715 | var high_start = to; // Lower bound of the elements greater than pivot. |
| 716 | // From low_end to i are elements equal to pivot. |
| 717 | // From i to high_start are elements that haven't been compared yet. |
| 718 | for (var i = from + 1; i < high_start; ) { |
| 719 | var element = a[i]; |
| 720 | var order = Compare(element, pivot_key); |
| 721 | if (order < 0) { |
| 722 | a[i] = a[low_end]; |
| 723 | a[low_end] = element; |
| 724 | i++; |
| 725 | low_end++; |
| 726 | } else if (order > 0) { |
| 727 | high_start--; |
| 728 | a[i] = a[high_start]; |
| 729 | a[high_start] = element; |
| 730 | } else { // order == 0 |
| 731 | i++; |
| 732 | } |
| 733 | } |
| 734 | QuickSort(a, from, low_end); |
| 735 | QuickSort(a, high_start, to); |
| 736 | } |
| 737 | |
| 738 | var length; |
| 739 | |
| 740 | // Copies elements in the range 0..length from obj's prototype chain |
| 741 | // to obj itself, if obj has holes. Returns one more than the maximal index |
| 742 | // of a prototype property. |
| 743 | function CopyFromPrototype(obj, length) { |
| 744 | var max = 0; |
| 745 | for (var proto = obj.__proto__; proto; proto = proto.__proto__) { |
| 746 | var indices = %GetArrayKeys(proto, length); |
| 747 | if (indices.length > 0) { |
| 748 | if (indices[0] == -1) { |
| 749 | // It's an interval. |
| 750 | var proto_length = indices[1]; |
| 751 | for (var i = 0; i < proto_length; i++) { |
| 752 | if (!obj.hasOwnProperty(i) && proto.hasOwnProperty(i)) { |
| 753 | obj[i] = proto[i]; |
| 754 | if (i >= max) { max = i + 1; } |
| 755 | } |
| 756 | } |
| 757 | } else { |
| 758 | for (var i = 0; i < indices.length; i++) { |
| 759 | var index = indices[i]; |
| 760 | if (!IS_UNDEFINED(index) && |
| 761 | !obj.hasOwnProperty(index) && proto.hasOwnProperty(index)) { |
| 762 | obj[index] = proto[index]; |
| 763 | if (index >= max) { max = index + 1; } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | return max; |
| 770 | } |
| 771 | |
| 772 | // Set a value of "undefined" on all indices in the range from..to |
| 773 | // where a prototype of obj has an element. I.e., shadow all prototype |
| 774 | // elements in that range. |
| 775 | function ShadowPrototypeElements(obj, from, to) { |
| 776 | for (var proto = obj.__proto__; proto; proto = proto.__proto__) { |
| 777 | var indices = %GetArrayKeys(proto, to); |
| 778 | if (indices.length > 0) { |
| 779 | if (indices[0] == -1) { |
| 780 | // It's an interval. |
| 781 | var proto_length = indices[1]; |
| 782 | for (var i = from; i < proto_length; i++) { |
| 783 | if (proto.hasOwnProperty(i)) { |
| 784 | obj[i] = void 0; |
| 785 | } |
| 786 | } |
| 787 | } else { |
| 788 | for (var i = 0; i < indices.length; i++) { |
| 789 | var index = indices[i]; |
| 790 | if (!IS_UNDEFINED(index) && from <= index && |
| 791 | proto.hasOwnProperty(index)) { |
| 792 | obj[index] = void 0; |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | function SafeRemoveArrayHoles(obj) { |
| 801 | // Copy defined elements from the end to fill in all holes and undefineds |
| 802 | // in the beginning of the array. Write undefineds and holes at the end |
| 803 | // after loop is finished. |
| 804 | var first_undefined = 0; |
| 805 | var last_defined = length - 1; |
| 806 | var num_holes = 0; |
| 807 | while (first_undefined < last_defined) { |
| 808 | // Find first undefined element. |
| 809 | while (first_undefined < last_defined && |
| 810 | !IS_UNDEFINED(obj[first_undefined])) { |
| 811 | first_undefined++; |
| 812 | } |
| 813 | // Maintain the invariant num_holes = the number of holes in the original |
| 814 | // array with indices <= first_undefined or > last_defined. |
| 815 | if (!obj.hasOwnProperty(first_undefined)) { |
| 816 | num_holes++; |
| 817 | } |
| 818 | |
| 819 | // Find last defined element. |
| 820 | while (first_undefined < last_defined && |
| 821 | IS_UNDEFINED(obj[last_defined])) { |
| 822 | if (!obj.hasOwnProperty(last_defined)) { |
| 823 | num_holes++; |
| 824 | } |
| 825 | last_defined--; |
| 826 | } |
| 827 | if (first_undefined < last_defined) { |
| 828 | // Fill in hole or undefined. |
| 829 | obj[first_undefined] = obj[last_defined]; |
| 830 | obj[last_defined] = void 0; |
| 831 | } |
| 832 | } |
| 833 | // If there were any undefineds in the entire array, first_undefined |
| 834 | // points to one past the last defined element. Make this true if |
| 835 | // there were no undefineds, as well, so that first_undefined == number |
| 836 | // of defined elements. |
| 837 | if (!IS_UNDEFINED(obj[first_undefined])) first_undefined++; |
| 838 | // Fill in the undefineds and the holes. There may be a hole where |
| 839 | // an undefined should be and vice versa. |
| 840 | var i; |
| 841 | for (i = first_undefined; i < length - num_holes; i++) { |
| 842 | obj[i] = void 0; |
| 843 | } |
| 844 | for (i = length - num_holes; i < length; i++) { |
| 845 | // For compatability with Webkit, do not expose elements in the prototype. |
| 846 | if (i in obj.__proto__) { |
| 847 | obj[i] = void 0; |
| 848 | } else { |
| 849 | delete obj[i]; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // Return the number of defined elements. |
| 854 | return first_undefined; |
| 855 | } |
| 856 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 857 | length = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 858 | if (length < 2) return this; |
| 859 | |
| 860 | var is_array = IS_ARRAY(this); |
| 861 | var max_prototype_element; |
| 862 | if (!is_array) { |
| 863 | // For compatibility with JSC, we also sort elements inherited from |
| 864 | // the prototype chain on non-Array objects. |
| 865 | // We do this by copying them to this object and sorting only |
| 866 | // local elements. This is not very efficient, but sorting with |
| 867 | // inherited elements happens very, very rarely, if at all. |
| 868 | // The specification allows "implementation dependent" behavior |
| 869 | // if an element on the prototype chain has an element that |
| 870 | // might interact with sorting. |
| 871 | max_prototype_element = CopyFromPrototype(this, length); |
| 872 | } |
| 873 | |
| 874 | var num_non_undefined = %RemoveArrayHoles(this, length); |
| 875 | if (num_non_undefined == -1) { |
| 876 | // There were indexed accessors in the array. Move array holes and |
| 877 | // undefineds to the end using a Javascript function that is safe |
| 878 | // in the presence of accessors. |
| 879 | num_non_undefined = SafeRemoveArrayHoles(this); |
| 880 | } |
| 881 | |
| 882 | QuickSort(this, 0, num_non_undefined); |
| 883 | |
| 884 | if (!is_array && (num_non_undefined + 1 < max_prototype_element)) { |
| 885 | // For compatibility with JSC, we shadow any elements in the prototype |
| 886 | // chain that has become exposed by sort moving a hole to its position. |
| 887 | ShadowPrototypeElements(this, num_non_undefined, max_prototype_element); |
| 888 | } |
| 889 | |
| 890 | return this; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | // The following functions cannot be made efficient on sparse arrays while |
| 895 | // preserving the semantics, since the calls to the receiver function can add |
| 896 | // or delete elements from the array. |
| 897 | function ArrayFilter(f, receiver) { |
| 898 | if (!IS_FUNCTION(f)) { |
| 899 | throw MakeTypeError('called_non_callable', [ f ]); |
| 900 | } |
| 901 | // Pull out the length so that modifications to the length in the |
| 902 | // loop will not affect the looping. |
| 903 | var length = this.length; |
| 904 | var result = []; |
| 905 | var result_length = 0; |
| 906 | for (var i = 0; i < length; i++) { |
| 907 | var current = this[i]; |
| 908 | if (!IS_UNDEFINED(current) || i in this) { |
| 909 | if (f.call(receiver, current, i, this)) result[result_length++] = current; |
| 910 | } |
| 911 | } |
| 912 | return result; |
| 913 | } |
| 914 | |
| 915 | |
| 916 | function ArrayForEach(f, receiver) { |
| 917 | if (!IS_FUNCTION(f)) { |
| 918 | throw MakeTypeError('called_non_callable', [ f ]); |
| 919 | } |
| 920 | // Pull out the length so that modifications to the length in the |
| 921 | // loop will not affect the looping. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 922 | var length = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 923 | for (var i = 0; i < length; i++) { |
| 924 | var current = this[i]; |
| 925 | if (!IS_UNDEFINED(current) || i in this) { |
| 926 | f.call(receiver, current, i, this); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | |
| 932 | // Executes the function once for each element present in the |
| 933 | // array until it finds one where callback returns true. |
| 934 | function ArraySome(f, receiver) { |
| 935 | if (!IS_FUNCTION(f)) { |
| 936 | throw MakeTypeError('called_non_callable', [ f ]); |
| 937 | } |
| 938 | // Pull out the length so that modifications to the length in the |
| 939 | // loop will not affect the looping. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 940 | var length = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | for (var i = 0; i < length; i++) { |
| 942 | var current = this[i]; |
| 943 | if (!IS_UNDEFINED(current) || i in this) { |
| 944 | if (f.call(receiver, current, i, this)) return true; |
| 945 | } |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | function ArrayEvery(f, receiver) { |
| 952 | if (!IS_FUNCTION(f)) { |
| 953 | throw MakeTypeError('called_non_callable', [ f ]); |
| 954 | } |
| 955 | // Pull out the length so that modifications to the length in the |
| 956 | // loop will not affect the looping. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 957 | var length = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 958 | for (var i = 0; i < length; i++) { |
| 959 | var current = this[i]; |
| 960 | if (!IS_UNDEFINED(current) || i in this) { |
| 961 | if (!f.call(receiver, current, i, this)) return false; |
| 962 | } |
| 963 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 964 | return true; |
| 965 | } |
| 966 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 967 | function ArrayMap(f, receiver) { |
| 968 | if (!IS_FUNCTION(f)) { |
| 969 | throw MakeTypeError('called_non_callable', [ f ]); |
| 970 | } |
| 971 | // Pull out the length so that modifications to the length in the |
| 972 | // loop will not affect the looping. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 973 | var length = TO_UINT32(this.length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 974 | var result = new $Array(length); |
| 975 | for (var i = 0; i < length; i++) { |
| 976 | var current = this[i]; |
| 977 | if (!IS_UNDEFINED(current) || i in this) { |
| 978 | result[i] = f.call(receiver, current, i, this); |
| 979 | } |
| 980 | } |
| 981 | return result; |
| 982 | } |
| 983 | |
| 984 | |
| 985 | function ArrayIndexOf(element, index) { |
| 986 | var length = this.length; |
| 987 | if (index == null) { |
| 988 | index = 0; |
| 989 | } else { |
| 990 | index = TO_INTEGER(index); |
| 991 | // If index is negative, index from the end of the array. |
| 992 | if (index < 0) index = length + index; |
| 993 | // If index is still negative, search the entire array. |
| 994 | if (index < 0) index = 0; |
| 995 | } |
| 996 | // Lookup through the array. |
| 997 | for (var i = index; i < length; i++) { |
| 998 | var current = this[i]; |
| 999 | if (!IS_UNDEFINED(current) || i in this) { |
| 1000 | if (current === element) return i; |
| 1001 | } |
| 1002 | } |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | function ArrayLastIndexOf(element, index) { |
| 1008 | var length = this.length; |
| 1009 | if (index == null) { |
| 1010 | index = length - 1; |
| 1011 | } else { |
| 1012 | index = TO_INTEGER(index); |
| 1013 | // If index is negative, index from end of the array. |
| 1014 | if (index < 0) index = length + index; |
| 1015 | // If index is still negative, do not search the array. |
| 1016 | if (index < 0) index = -1; |
| 1017 | else if (index >= length) index = length - 1; |
| 1018 | } |
| 1019 | // Lookup through the array. |
| 1020 | for (var i = index; i >= 0; i--) { |
| 1021 | var current = this[i]; |
| 1022 | if (!IS_UNDEFINED(current) || i in this) { |
| 1023 | if (current === element) return i; |
| 1024 | } |
| 1025 | } |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | function ArrayReduce(callback, current) { |
| 1031 | if (!IS_FUNCTION(callback)) { |
| 1032 | throw MakeTypeError('called_non_callable', [callback]); |
| 1033 | } |
| 1034 | // Pull out the length so that modifications to the length in the |
| 1035 | // loop will not affect the looping. |
| 1036 | var length = this.length; |
| 1037 | var i = 0; |
| 1038 | |
| 1039 | find_initial: if (%_ArgumentsLength() < 2) { |
| 1040 | for (; i < length; i++) { |
| 1041 | current = this[i]; |
| 1042 | if (!IS_UNDEFINED(current) || i in this) { |
| 1043 | i++; |
| 1044 | break find_initial; |
| 1045 | } |
| 1046 | } |
| 1047 | throw MakeTypeError('reduce_no_initial', []); |
| 1048 | } |
| 1049 | |
| 1050 | for (; i < length; i++) { |
| 1051 | var element = this[i]; |
| 1052 | if (!IS_UNDEFINED(element) || i in this) { |
| 1053 | current = callback.call(null, current, element, i, this); |
| 1054 | } |
| 1055 | } |
| 1056 | return current; |
| 1057 | } |
| 1058 | |
| 1059 | function ArrayReduceRight(callback, current) { |
| 1060 | if (!IS_FUNCTION(callback)) { |
| 1061 | throw MakeTypeError('called_non_callable', [callback]); |
| 1062 | } |
| 1063 | var i = this.length - 1; |
| 1064 | |
| 1065 | find_initial: if (%_ArgumentsLength() < 2) { |
| 1066 | for (; i >= 0; i--) { |
| 1067 | current = this[i]; |
| 1068 | if (!IS_UNDEFINED(current) || i in this) { |
| 1069 | i--; |
| 1070 | break find_initial; |
| 1071 | } |
| 1072 | } |
| 1073 | throw MakeTypeError('reduce_no_initial', []); |
| 1074 | } |
| 1075 | |
| 1076 | for (; i >= 0; i--) { |
| 1077 | var element = this[i]; |
| 1078 | if (!IS_UNDEFINED(element) || i in this) { |
| 1079 | current = callback.call(null, current, element, i, this); |
| 1080 | } |
| 1081 | } |
| 1082 | return current; |
| 1083 | } |
| 1084 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1085 | // ES5, 15.4.3.2 |
| 1086 | function ArrayIsArray(obj) { |
| 1087 | return IS_ARRAY(obj); |
| 1088 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1089 | |
| 1090 | // ------------------------------------------------------------------- |
| 1091 | |
| 1092 | |
| 1093 | function UpdateFunctionLengths(lengths) { |
| 1094 | for (var key in lengths) { |
| 1095 | %FunctionSetLength(this[key], lengths[key]); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | // ------------------------------------------------------------------- |
| 1101 | function SetupArray() { |
| 1102 | // Setup non-enumerable constructor property on the Array.prototype |
| 1103 | // object. |
| 1104 | %SetProperty($Array.prototype, "constructor", $Array, DONT_ENUM); |
| 1105 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1106 | // Setup non-enumerable functions on the Array object. |
| 1107 | InstallFunctions($Array, DONT_ENUM, $Array( |
| 1108 | "isArray", ArrayIsArray |
| 1109 | )); |
| 1110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1111 | // Setup non-enumerable functions of the Array.prototype object and |
| 1112 | // set their names. |
| 1113 | InstallFunctionsOnHiddenPrototype($Array.prototype, DONT_ENUM, $Array( |
| 1114 | "toString", ArrayToString, |
| 1115 | "toLocaleString", ArrayToLocaleString, |
| 1116 | "join", ArrayJoin, |
| 1117 | "pop", ArrayPop, |
| 1118 | "push", ArrayPush, |
| 1119 | "concat", ArrayConcat, |
| 1120 | "reverse", ArrayReverse, |
| 1121 | "shift", ArrayShift, |
| 1122 | "unshift", ArrayUnshift, |
| 1123 | "slice", ArraySlice, |
| 1124 | "splice", ArraySplice, |
| 1125 | "sort", ArraySort, |
| 1126 | "filter", ArrayFilter, |
| 1127 | "forEach", ArrayForEach, |
| 1128 | "some", ArraySome, |
| 1129 | "every", ArrayEvery, |
| 1130 | "map", ArrayMap, |
| 1131 | "indexOf", ArrayIndexOf, |
| 1132 | "lastIndexOf", ArrayLastIndexOf, |
| 1133 | "reduce", ArrayReduce, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1134 | "reduceRight", ArrayReduceRight |
| 1135 | )); |
| 1136 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1137 | // Manipulate the length of some of the functions to meet |
| 1138 | // expectations set by ECMA-262 or Mozilla. |
| 1139 | UpdateFunctionLengths({ |
| 1140 | ArrayFilter: 1, |
| 1141 | ArrayForEach: 1, |
| 1142 | ArraySome: 1, |
| 1143 | ArrayEvery: 1, |
| 1144 | ArrayMap: 1, |
| 1145 | ArrayIndexOf: 1, |
| 1146 | ArrayLastIndexOf: 1, |
| 1147 | ArrayPush: 1, |
| 1148 | ArrayReduce: 1, |
| 1149 | ArrayReduceRight: 1 |
| 1150 | }); |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | SetupArray(); |