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

Side by Side Diff: src/array.js

Issue 1154423014: Implement %TypedArray%.prototype.{reduce,reduceRight} (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 6 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
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | test/mjsunit/harmony/typedarray-reduce.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 var $arrayConcat; 5 var $arrayConcat;
6 var $arrayPush; 6 var $arrayPush;
7 var $arrayPop; 7 var $arrayPop;
8 var $arrayShift; 8 var $arrayShift;
9 var $arraySlice; 9 var $arraySlice;
10 var $arraySplice; 10 var $arraySplice;
(...skipping 1463 matching lines...) Expand 10 before | Expand all | Expand 10 after
1474 1474
1475 function ArrayLastIndexOf(element, index) { 1475 function ArrayLastIndexOf(element, index) {
1476 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf"); 1476 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.lastIndexOf");
1477 1477
1478 var length = TO_UINT32(this.length); 1478 var length = TO_UINT32(this.length);
1479 return %_CallFunction(this, element, index, length, 1479 return %_CallFunction(this, element, index, length,
1480 %_ArgumentsLength(), InnerArrayLastIndexOf); 1480 %_ArgumentsLength(), InnerArrayLastIndexOf);
1481 } 1481 }
1482 1482
1483 1483
1484 function ArrayReduce(callback, current) { 1484 function InnerArrayReduce(callback, current, array, length, argumentsLength) {
1485 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
1486
1487 // Pull out the length so that modifications to the length in the
1488 // loop will not affect the looping and side effects are visible.
1489 var array = $toObject(this);
1490 var length = $toUint32(array.length);
1491
1492 if (!IS_SPEC_FUNCTION(callback)) { 1485 if (!IS_SPEC_FUNCTION(callback)) {
1493 throw MakeTypeError(kCalledNonCallable, callback); 1486 throw MakeTypeError(kCalledNonCallable, callback);
1494 } 1487 }
1495 1488
1496 var is_array = IS_ARRAY(array); 1489 var is_array = IS_ARRAY(array);
1497 var i = 0; 1490 var i = 0;
1498 find_initial: if (%_ArgumentsLength() < 2) { 1491 find_initial: if (argumentsLength < 2) {
1499 for (; i < length; i++) { 1492 for (; i < length; i++) {
1500 if (HAS_INDEX(array, i, is_array)) { 1493 if (HAS_INDEX(array, i, is_array)) {
1501 current = array[i++]; 1494 current = array[i++];
1502 break find_initial; 1495 break find_initial;
1503 } 1496 }
1504 } 1497 }
1505 throw MakeTypeError(kReduceNoInitial); 1498 throw MakeTypeError(kReduceNoInitial);
1506 } 1499 }
1507 1500
1508 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1501 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1509 for (; i < length; i++) { 1502 for (; i < length; i++) {
1510 if (HAS_INDEX(array, i, is_array)) { 1503 if (HAS_INDEX(array, i, is_array)) {
1511 var element = array[i]; 1504 var element = array[i];
1512 // Prepare break slots for debugger step in. 1505 // Prepare break slots for debugger step in.
1513 if (stepping) %DebugPrepareStepInIfStepping(callback); 1506 if (stepping) %DebugPrepareStepInIfStepping(callback);
1514 current = %_CallFunction(UNDEFINED, current, element, i, array, callback); 1507 current = %_CallFunction(UNDEFINED, current, element, i, array, callback);
1515 } 1508 }
1516 } 1509 }
1517 return current; 1510 return current;
1518 } 1511 }
1519 1512
1520 1513
1521 function ArrayReduceRight(callback, current) { 1514 function ArrayReduce(callback, current) {
1522 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight"); 1515 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
1523 1516
1524 // Pull out the length so that side effects are visible before the 1517 // Pull out the length so that modifications to the length in the
1525 // callback function is checked. 1518 // loop will not affect the looping and side effects are visible.
1526 var array = $toObject(this); 1519 var array = $toObject(this);
1527 var length = $toUint32(array.length); 1520 var length = $toUint32(array.length);
1521 return InnerArrayReduce(callback, current, array, length,
1522 %_ArgumentsLength());
1523 }
1528 1524
1525
1526 function InnerArrayReduceRight(callback, current, array, length,
1527 argumentsLength) {
1529 if (!IS_SPEC_FUNCTION(callback)) { 1528 if (!IS_SPEC_FUNCTION(callback)) {
1530 throw MakeTypeError(kCalledNonCallable, callback); 1529 throw MakeTypeError(kCalledNonCallable, callback);
1531 } 1530 }
1532 1531
1533 var is_array = IS_ARRAY(array); 1532 var is_array = IS_ARRAY(array);
1534 var i = length - 1; 1533 var i = length - 1;
1535 find_initial: if (%_ArgumentsLength() < 2) { 1534 find_initial: if (argumentsLength < 2) {
1536 for (; i >= 0; i--) { 1535 for (; i >= 0; i--) {
1537 if (HAS_INDEX(array, i, is_array)) { 1536 if (HAS_INDEX(array, i, is_array)) {
1538 current = array[i--]; 1537 current = array[i--];
1539 break find_initial; 1538 break find_initial;
1540 } 1539 }
1541 } 1540 }
1542 throw MakeTypeError(kReduceNoInitial); 1541 throw MakeTypeError(kReduceNoInitial);
1543 } 1542 }
1544 1543
1545 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback); 1544 var stepping = DEBUG_IS_ACTIVE && %DebugCallbackSupportsStepping(callback);
1546 for (; i >= 0; i--) { 1545 for (; i >= 0; i--) {
1547 if (HAS_INDEX(array, i, is_array)) { 1546 if (HAS_INDEX(array, i, is_array)) {
1548 var element = array[i]; 1547 var element = array[i];
1549 // Prepare break slots for debugger step in. 1548 // Prepare break slots for debugger step in.
1550 if (stepping) %DebugPrepareStepInIfStepping(callback); 1549 if (stepping) %DebugPrepareStepInIfStepping(callback);
1551 current = %_CallFunction(UNDEFINED, current, element, i, array, callback); 1550 current = %_CallFunction(UNDEFINED, current, element, i, array, callback);
1552 } 1551 }
1553 } 1552 }
1554 return current; 1553 return current;
1555 } 1554 }
1556 1555
1556
1557 function ArrayReduceRight(callback, current) {
1558 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
1559
1560 // Pull out the length so that side effects are visible before the
1561 // callback function is checked.
1562 var array = $toObject(this);
1563 var length = $toUint32(array.length);
1564 return InnerArrayReduceRight(callback, current, array, length,
1565 %_ArgumentsLength());
1566 }
1567
1557 // ES5, 15.4.3.2 1568 // ES5, 15.4.3.2
1558 function ArrayIsArray(obj) { 1569 function ArrayIsArray(obj) {
1559 return IS_ARRAY(obj); 1570 return IS_ARRAY(obj);
1560 } 1571 }
1561 1572
1562 1573
1563 // ------------------------------------------------------------------- 1574 // -------------------------------------------------------------------
1564 1575
1565 // Set up non-enumerable constructor property on the Array.prototype 1576 // Set up non-enumerable constructor property on the Array.prototype
1566 // object. 1577 // object.
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1653 // Exports 1664 // Exports
1654 1665
1655 utils.Export(function(to) { 1666 utils.Export(function(to) {
1656 to.ArrayJoin = ArrayJoin; 1667 to.ArrayJoin = ArrayJoin;
1657 to.InnerArrayEvery = InnerArrayEvery; 1668 to.InnerArrayEvery = InnerArrayEvery;
1658 to.InnerArrayFilter = InnerArrayFilter; 1669 to.InnerArrayFilter = InnerArrayFilter;
1659 to.InnerArrayForEach = InnerArrayForEach; 1670 to.InnerArrayForEach = InnerArrayForEach;
1660 to.InnerArrayIndexOf = InnerArrayIndexOf; 1671 to.InnerArrayIndexOf = InnerArrayIndexOf;
1661 to.InnerArrayLastIndexOf = InnerArrayLastIndexOf; 1672 to.InnerArrayLastIndexOf = InnerArrayLastIndexOf;
1662 to.InnerArrayMap = InnerArrayMap; 1673 to.InnerArrayMap = InnerArrayMap;
1674 to.InnerArrayReduce = InnerArrayReduce;
1675 to.InnerArrayReduceRight = InnerArrayReduceRight;
1663 to.InnerArrayReverse = InnerArrayReverse; 1676 to.InnerArrayReverse = InnerArrayReverse;
1664 to.InnerArraySome = InnerArraySome; 1677 to.InnerArraySome = InnerArraySome;
1665 to.InnerArraySort = InnerArraySort; 1678 to.InnerArraySort = InnerArraySort;
1666 }); 1679 });
1667 1680
1668 $arrayConcat = ArrayConcatJS; 1681 $arrayConcat = ArrayConcatJS;
1669 $arrayPush = ArrayPush; 1682 $arrayPush = ArrayPush;
1670 $arrayPop = ArrayPop; 1683 $arrayPop = ArrayPop;
1671 $arrayShift = ArrayShift; 1684 $arrayShift = ArrayShift;
1672 $arraySlice = ArraySlice; 1685 $arraySlice = ArraySlice;
1673 $arraySplice = ArraySplice; 1686 $arraySplice = ArraySplice;
1674 $arrayUnshift = ArrayUnshift; 1687 $arrayUnshift = ArrayUnshift;
1675 1688
1676 }); 1689 });
OLDNEW
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | test/mjsunit/harmony/typedarray-reduce.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698