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

Side by Side Diff: src/js/array.js

Issue 2232063002: [builtins] WIP: Array indexOf in TurboFan/Runtime (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Move fast path FastElements -> FastSmiOrObjectElements Created 4 years, 4 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 | « src/elements.cc ('k') | src/js/i18n.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 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 1117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1128 for (var i = 0; i < length; i++) { 1128 for (var i = 0; i < length; i++) {
1129 if (i in array) { 1129 if (i in array) {
1130 var element = array[i]; 1130 var element = array[i];
1131 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array)); 1131 %CreateDataProperty(result, i, %_Call(f, receiver, element, i, array));
1132 } 1132 }
1133 } 1133 }
1134 return result; 1134 return result;
1135 } 1135 }
1136 1136
1137 1137
1138 // For .indexOf, we don't need to pass in the number of arguments
1139 // at the callsite since ToInteger(undefined) == 0; however, for
1140 // .lastIndexOf, we need to pass it, since the behavior for passing
1141 // undefined is 0 but for not including the argument is length-1.
1142 function InnerArrayIndexOf(array, element, index, length) {
1143 if (length == 0) return -1;
1144 if (IS_UNDEFINED(index)) {
1145 index = 0;
1146 } else {
1147 index = INVERT_NEG_ZERO(TO_INTEGER(index));
1148 // If index is negative, index from the end of the array.
1149 if (index < 0) {
1150 index = length + index;
1151 // If index is still negative, search the entire array.
1152 if (index < 0) index = 0;
1153 }
1154 }
1155 var min = index;
1156 var max = length;
1157 if (UseSparseVariant(array, length, IS_ARRAY(array), max - min)) {
1158 %NormalizeElements(array);
1159 var indices = %GetArrayKeys(array, length);
1160 if (IS_NUMBER(indices)) {
1161 // It's an interval.
1162 max = indices; // Capped by length already.
1163 // Fall through to loop below.
1164 } else {
1165 if (indices.length == 0) return -1;
1166 // Get all the keys in sorted order.
1167 var sortedKeys = GetSortedArrayKeys(array, indices);
1168 var n = sortedKeys.length;
1169 var i = 0;
1170 while (i < n && sortedKeys[i] < index) i++;
1171 while (i < n) {
1172 var key = sortedKeys[i];
1173 if (array[key] === element) return key;
1174 i++;
1175 }
1176 return -1;
1177 }
1178 }
1179 // Lookup through the array.
1180 if (!IS_UNDEFINED(element)) {
1181 for (var i = min; i < max; i++) {
1182 if (array[i] === element) return i;
1183 }
1184 return -1;
1185 }
1186 // Lookup through the array.
1187 for (var i = min; i < max; i++) {
1188 if (IS_UNDEFINED(array[i]) && i in array) {
1189 return i;
1190 }
1191 }
1192 return -1;
1193 }
1194
1195
1196 function ArrayIndexOf(element, index) {
1197 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.indexOf");
1198
1199 var length = TO_LENGTH(this.length);
1200 return InnerArrayIndexOf(this, element, index, length);
1201 }
1202
1203
1204 function InnerArrayLastIndexOf(array, element, index, length, argumentsLength) { 1138 function InnerArrayLastIndexOf(array, element, index, length, argumentsLength) {
1205 if (length == 0) return -1; 1139 if (length == 0) return -1;
1206 if (argumentsLength < 2) { 1140 if (argumentsLength < 2) {
1207 index = length - 1; 1141 index = length - 1;
1208 } else { 1142 } else {
1209 index = INVERT_NEG_ZERO(TO_INTEGER(index)); 1143 index = INVERT_NEG_ZERO(TO_INTEGER(index));
1210 // If index is negative, index from end of the array. 1144 // If index is negative, index from end of the array.
1211 if (index < 0) index += length; 1145 if (index < 0) index += length;
1212 // If index is still negative, do not search the array. 1146 // If index is still negative, do not search the array.
1213 if (index < 0) return -1; 1147 if (index < 0) return -1;
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
1619 "shift", getFunction("shift", ArrayShift), 1553 "shift", getFunction("shift", ArrayShift),
1620 "unshift", getFunction("unshift", ArrayUnshift, 1), 1554 "unshift", getFunction("unshift", ArrayUnshift, 1),
1621 "slice", getFunction("slice", ArraySlice, 2), 1555 "slice", getFunction("slice", ArraySlice, 2),
1622 "splice", getFunction("splice", ArraySplice, 2), 1556 "splice", getFunction("splice", ArraySplice, 2),
1623 "sort", getFunction("sort", ArraySort), 1557 "sort", getFunction("sort", ArraySort),
1624 "filter", getFunction("filter", ArrayFilter, 1), 1558 "filter", getFunction("filter", ArrayFilter, 1),
1625 "forEach", getFunction("forEach", ArrayForEach, 1), 1559 "forEach", getFunction("forEach", ArrayForEach, 1),
1626 "some", getFunction("some", ArraySome, 1), 1560 "some", getFunction("some", ArraySome, 1),
1627 "every", getFunction("every", ArrayEvery, 1), 1561 "every", getFunction("every", ArrayEvery, 1),
1628 "map", getFunction("map", ArrayMap, 1), 1562 "map", getFunction("map", ArrayMap, 1),
1629 "indexOf", getFunction("indexOf", ArrayIndexOf, 1), 1563 "indexOf", getFunction("indexOf", null, 1),
1630 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), 1564 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1),
1631 "reduce", getFunction("reduce", ArrayReduce, 1), 1565 "reduce", getFunction("reduce", ArrayReduce, 1),
1632 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1), 1566 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1),
1633 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2), 1567 "copyWithin", getFunction("copyWithin", ArrayCopyWithin, 2),
1634 "find", getFunction("find", ArrayFind, 1), 1568 "find", getFunction("find", ArrayFind, 1),
1635 "findIndex", getFunction("findIndex", ArrayFindIndex, 1), 1569 "findIndex", getFunction("findIndex", ArrayFindIndex, 1),
1636 "fill", getFunction("fill", ArrayFill, 1), 1570 "fill", getFunction("fill", ArrayFill, 1),
1637 "includes", getFunction("includes", null, 1) 1571 "includes", getFunction("includes", null, 1)
1638 ]); 1572 ]);
1639 1573
1640 utils.InstallGetter(GlobalArray, speciesSymbol, ArraySpecies); 1574 utils.InstallGetter(GlobalArray, speciesSymbol, ArraySpecies);
1641 1575
1642 %FinishArrayPrototypeSetup(GlobalArray.prototype); 1576 %FinishArrayPrototypeSetup(GlobalArray.prototype);
1643 1577
1644 // The internal Array prototype doesn't need to be fancy, since it's never 1578 // The internal Array prototype doesn't need to be fancy, since it's never
1645 // exposed to user code. 1579 // exposed to user code.
1646 // Adding only the functions that are actually used. 1580 // Adding only the functions that are actually used.
1647 utils.SetUpLockedPrototype(InternalArray, GlobalArray(), [ 1581 utils.SetUpLockedPrototype(InternalArray, GlobalArray(), [
1648 "indexOf", getFunction("indexOf", ArrayIndexOf), 1582 "indexOf", getFunction("indexOf", null),
1649 "join", getFunction("join", ArrayJoin), 1583 "join", getFunction("join", ArrayJoin),
1650 "pop", getFunction("pop", ArrayPop), 1584 "pop", getFunction("pop", ArrayPop),
1651 "push", getFunction("push", ArrayPush), 1585 "push", getFunction("push", ArrayPush),
1652 "shift", getFunction("shift", ArrayShift), 1586 "shift", getFunction("shift", ArrayShift),
1653 "sort", getFunction("sort", ArraySort), 1587 "sort", getFunction("sort", ArraySort),
1654 "splice", getFunction("splice", ArraySplice) 1588 "splice", getFunction("splice", ArraySplice)
1655 ]); 1589 ]);
1656 1590
1657 utils.SetUpLockedPrototype(InternalPackedArray, GlobalArray(), [ 1591 utils.SetUpLockedPrototype(InternalPackedArray, GlobalArray(), [
1658 "join", getFunction("join", ArrayJoin), 1592 "join", getFunction("join", ArrayJoin),
(...skipping 11 matching lines...) Expand all
1670 "unshift", getFunction("unshift", ArrayUnshift), 1604 "unshift", getFunction("unshift", ArrayUnshift),
1671 "splice", getFunction("splice", ArraySplice), 1605 "splice", getFunction("splice", ArraySplice),
1672 "slice", getFunction("slice", ArraySlice) 1606 "slice", getFunction("slice", ArraySlice)
1673 ]); 1607 ]);
1674 1608
1675 // ------------------------------------------------------------------- 1609 // -------------------------------------------------------------------
1676 // Exports 1610 // Exports
1677 1611
1678 utils.Export(function(to) { 1612 utils.Export(function(to) {
1679 to.ArrayFrom = ArrayFrom; 1613 to.ArrayFrom = ArrayFrom;
1680 to.ArrayIndexOf = ArrayIndexOf;
1681 to.ArrayJoin = ArrayJoin; 1614 to.ArrayJoin = ArrayJoin;
1682 to.ArrayPush = ArrayPush; 1615 to.ArrayPush = ArrayPush;
1683 to.ArrayToString = ArrayToString; 1616 to.ArrayToString = ArrayToString;
1684 to.InnerArrayCopyWithin = InnerArrayCopyWithin; 1617 to.InnerArrayCopyWithin = InnerArrayCopyWithin;
1685 to.InnerArrayEvery = InnerArrayEvery; 1618 to.InnerArrayEvery = InnerArrayEvery;
1686 to.InnerArrayFill = InnerArrayFill; 1619 to.InnerArrayFill = InnerArrayFill;
1687 to.InnerArrayFilter = InnerArrayFilter; 1620 to.InnerArrayFilter = InnerArrayFilter;
1688 to.InnerArrayFind = InnerArrayFind; 1621 to.InnerArrayFind = InnerArrayFind;
1689 to.InnerArrayFindIndex = InnerArrayFindIndex; 1622 to.InnerArrayFindIndex = InnerArrayFindIndex;
1690 to.InnerArrayForEach = InnerArrayForEach; 1623 to.InnerArrayForEach = InnerArrayForEach;
1691 to.InnerArrayIndexOf = InnerArrayIndexOf;
1692 to.InnerArrayJoin = InnerArrayJoin; 1624 to.InnerArrayJoin = InnerArrayJoin;
1693 to.InnerArrayLastIndexOf = InnerArrayLastIndexOf; 1625 to.InnerArrayLastIndexOf = InnerArrayLastIndexOf;
1694 to.InnerArrayReduce = InnerArrayReduce; 1626 to.InnerArrayReduce = InnerArrayReduce;
1695 to.InnerArrayReduceRight = InnerArrayReduceRight; 1627 to.InnerArrayReduceRight = InnerArrayReduceRight;
1696 to.InnerArraySome = InnerArraySome; 1628 to.InnerArraySome = InnerArraySome;
1697 to.InnerArraySort = InnerArraySort; 1629 to.InnerArraySort = InnerArraySort;
1698 to.InnerArrayToLocaleString = InnerArrayToLocaleString; 1630 to.InnerArrayToLocaleString = InnerArrayToLocaleString;
1699 to.PackedArrayReverse = PackedArrayReverse; 1631 to.PackedArrayReverse = PackedArrayReverse;
1700 }); 1632 });
1701 1633
1702 %InstallToContext([ 1634 %InstallToContext([
1703 "array_pop", ArrayPop, 1635 "array_pop", ArrayPop,
1704 "array_push", ArrayPush, 1636 "array_push", ArrayPush,
1705 "array_shift", ArrayShift, 1637 "array_shift", ArrayShift,
1706 "array_splice", ArraySplice, 1638 "array_splice", ArraySplice,
1707 "array_slice", ArraySlice, 1639 "array_slice", ArraySlice,
1708 "array_unshift", ArrayUnshift, 1640 "array_unshift", ArrayUnshift,
1709 ]); 1641 ]);
1710 1642
1711 }); 1643 });
OLDNEW
« no previous file with comments | « src/elements.cc ('k') | src/js/i18n.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698