OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 | 456 |
457 // Appends the arguments to the end of the array and returns the new | 457 // Appends the arguments to the end of the array and returns the new |
458 // length of the array. See ECMA-262, section 15.4.4.7. | 458 // length of the array. See ECMA-262, section 15.4.4.7. |
459 function ArrayPush() { | 459 function ArrayPush() { |
460 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.push"); | 460 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.push"); |
461 | 461 |
462 var array = TO_OBJECT(this); | 462 var array = TO_OBJECT(this); |
463 var n = TO_LENGTH(array.length); | 463 var n = TO_LENGTH(array.length); |
464 var m = arguments.length; | 464 var m = arguments.length; |
465 | 465 |
466 // It appears that there is no enforced, absolute limit on the number of | 466 // Subtract n from kMaxSafeInteger rather than testing m + n > |
467 // arguments, but it would surely blow the stack to use 2**30 or more. | 467 // kMaxSafeInteger. n may already be kMaxSafeInteger. In that case adding |
468 // To avoid integer overflow, do the comparison to the max safe integer | 468 // e.g., 1 would not be safe. |
469 // after subtracting 2**30 from both sides. (2**31 would seem like a | 469 if (m > kMaxSafeInteger - n) throw MakeTypeError(kPushPastSafeLength, m, n); |
470 // natural value, but it is negative in JS, and 2**32 is 1.) | |
471 if (m > (1 << 30) || (n - (1 << 30)) + m > kMaxSafeInteger - (1 << 30)) { | |
472 throw MakeTypeError(kPushPastSafeLength, m, n); | |
473 } | |
474 | 470 |
475 for (var i = 0; i < m; i++) { | 471 for (var i = 0; i < m; i++) { |
476 array[i+n] = arguments[i]; | 472 array[i+n] = arguments[i]; |
477 } | 473 } |
478 | 474 |
479 var new_length = n + m; | 475 var new_length = n + m; |
480 array.length = new_length; | 476 array.length = new_length; |
481 return new_length; | 477 return new_length; |
482 } | 478 } |
483 | 479 |
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1794 %InstallToContext([ | 1790 %InstallToContext([ |
1795 "array_pop", ArrayPop, | 1791 "array_pop", ArrayPop, |
1796 "array_push", ArrayPush, | 1792 "array_push", ArrayPush, |
1797 "array_shift", ArrayShift, | 1793 "array_shift", ArrayShift, |
1798 "array_splice", ArraySplice, | 1794 "array_splice", ArraySplice, |
1799 "array_slice", ArraySlice, | 1795 "array_slice", ArraySlice, |
1800 "array_unshift", ArrayUnshift, | 1796 "array_unshift", ArrayUnshift, |
1801 ]); | 1797 ]); |
1802 | 1798 |
1803 }); | 1799 }); |
OLD | NEW |