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

Unified Diff: src/array.js

Issue 1139663005: Implement %TypedArray%.prototype.{map,filter,some,reduce,reduceRight} (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | src/harmony-typedarray.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/array.js
diff --git a/src/array.js b/src/array.js
index 93378cfb00b1b14c0150e5b179c6de5f6a8852a2..3e5b2e33e0a58859b745d359c870eccbd2d0eee0 100644
--- a/src/array.js
+++ b/src/array.js
@@ -12,6 +12,11 @@ var $arraySplice;
var $arrayUnshift;
var $innerArrayForEach;
var $innerArrayEvery;
+var $innerArrayFilter;
+var $innerArrayMap;
+var $innerArrayReduce;
+var $innerArrayReduceRight;
+var $innerArraySome;
(function(global, shared, exports) {
@@ -1150,14 +1155,7 @@ function ArraySort(comparefn) {
// The following functions cannot be made efficient on sparse arrays while
// preserving the semantics, since the calls to the receiver function can add
// or delete elements from the array.
-function ArrayFilter(f, receiver) {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
-
- // Pull out the length so that modifications to the length in the
- // loop will not affect the looping and side effects are visible.
- var array = $toObject(this);
- var length = $toUint32(array.length);
-
+function InnerArrayFilter(f, receiver, array, length) {
if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
var needs_wrapper = false;
if (IS_NULL(receiver)) {
@@ -1185,6 +1183,18 @@ function ArrayFilter(f, receiver) {
%MoveArrayContents(accumulator, result);
return result;
}
+$innerArrayFilter = InnerArrayFilter;
+
+function ArrayFilter(f, receiver) {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.filter");
+
+ // Pull out the length so that modifications to the length in the
+ // loop will not affect the looping and side effects are visible.
+ var array = $toObject(this);
+ var length = $toUint32(array.length);
+
+ return InnerArrayFilter(f, receiver, array, length);
+}
function InnerArrayForEach(f, receiver, array, length) {
if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
@@ -1219,16 +1229,7 @@ function ArrayForEach(f, receiver) {
}
-// Executes the function once for each element present in the
-// array until it finds one where callback returns true.
-function ArraySome(f, receiver) {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some");
-
- // Pull out the length so that modifications to the length in the
- // loop will not affect the looping and side effects are visible.
- var array = $toObject(this);
- var length = TO_UINT32(array.length);
-
+function InnerArraySome(f, receiver, array, length) {
if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
var needs_wrapper = false;
if (IS_NULL(receiver)) {
@@ -1250,6 +1251,20 @@ function ArraySome(f, receiver) {
}
return false;
}
+$innerArraySome = InnerArraySome;
arv (Not doing code reviews) 2015/05/18 22:54:23 Can you put all of these at the end?
dehrenberg 2015/05/19 00:13:45 Done.
+
+
+// Executes the function once for each element present in the
+// array until it finds one where callback returns true.
+function ArraySome(f, receiver) {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.some");
+
+ // Pull out the length so that modifications to the length in the
+ // loop will not affect the looping and side effects are visible.
+ var array = $toObject(this);
+ var length = TO_UINT32(array.length);
+ return InnerArraySome(f, receiver, array, length);
+}
function InnerArrayEvery(f, receiver, array, length) {
@@ -1286,14 +1301,7 @@ function ArrayEvery(f, receiver) {
}
-function ArrayMap(f, receiver) {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
-
- // Pull out the length so that modifications to the length in the
- // loop will not affect the looping and side effects are visible.
- var array = $toObject(this);
- var length = TO_UINT32(array.length);
-
+function InnerArrayMap(f, receiver, array, length) {
if (!IS_SPEC_FUNCTION(f)) throw MakeTypeError(kCalledNonCallable, f);
var needs_wrapper = false;
if (IS_NULL(receiver)) {
@@ -1318,6 +1326,18 @@ function ArrayMap(f, receiver) {
%MoveArrayContents(accumulator, result);
return result;
}
+$innerArrayMap = InnerArrayMap;
+
+
+function ArrayMap(f, receiver) {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.map");
+
+ // Pull out the length so that modifications to the length in the
+ // loop will not affect the looping and side effects are visible.
+ var array = $toObject(this);
+ var length = TO_UINT32(array.length);
+ return InnerArrayMap(f, receiver, array, length);
+}
function ArrayIndexOf(element, index) {
@@ -1430,21 +1450,14 @@ function ArrayLastIndexOf(element, index) {
}
-function ArrayReduce(callback, current) {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
-
- // Pull out the length so that modifications to the length in the
- // loop will not affect the looping and side effects are visible.
- var array = $toObject(this);
- var length = $toUint32(array.length);
-
+function InnerArrayReduce(callback, current, array, length, argumentsLength) {
if (!IS_SPEC_FUNCTION(callback)) {
throw MakeTypeError(kCalledNonCallable, callback);
}
var is_array = IS_ARRAY(array);
var i = 0;
- find_initial: if (%_ArgumentsLength() < 2) {
+ find_initial: if (argumentsLength < 2) {
for (; i < length; i++) {
if (HAS_INDEX(array, i, is_array)) {
current = array[i++];
@@ -1465,23 +1478,30 @@ function ArrayReduce(callback, current) {
}
return current;
}
+$innerArrayReduce = InnerArrayReduce;
-function ArrayReduceRight(callback, current) {
- CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
+function ArrayReduce(callback, current) {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduce");
- // Pull out the length so that side effects are visible before the
- // callback function is checked.
+ // Pull out the length so that modifications to the length in the
+ // loop will not affect the looping and side effects are visible.
var array = $toObject(this);
var length = $toUint32(array.length);
+ return InnerArrayReduce(callback, current, array, length,
+ %_ArgumentsLength());
+}
+
+function InnerArrayReduceRight(callback, current, array, length,
+ argumentsLength) {
if (!IS_SPEC_FUNCTION(callback)) {
throw MakeTypeError(kCalledNonCallable, callback);
}
var is_array = IS_ARRAY(array);
var i = length - 1;
- find_initial: if (%_ArgumentsLength() < 2) {
+ find_initial: if (argumentsLength < 2) {
for (; i >= 0; i--) {
if (HAS_INDEX(array, i, is_array)) {
current = array[i--];
@@ -1502,6 +1522,19 @@ function ArrayReduceRight(callback, current) {
}
return current;
}
+$innerArrayReduceRight = InnerArrayReduceRight;
+
+
+function ArrayReduceRight(callback, current) {
+ CHECK_OBJECT_COERCIBLE(this, "Array.prototype.reduceRight");
+
+ // Pull out the length so that side effects are visible before the
+ // callback function is checked.
+ var array = $toObject(this);
+ var length = $toUint32(array.length);
+ return InnerArrayReduceRight(callback, current, array, length,
+ %_ArgumentsLength());
+}
// ES5, 15.4.3.2
function ArrayIsArray(obj) {
« no previous file with comments | « no previous file | src/harmony-typedarray.js » ('j') | src/harmony-typedarray.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698