Commit 97427d86 authored by lrn@chromium.org's avatar lrn@chromium.org

Add ES5 Array methods reduce and reduceRight, with test.


git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1749 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b8dc312b
......@@ -879,6 +879,62 @@ function ArrayLastIndexOf(element, index) {
}
function ArrayReduce(callback, current) {
if (!IS_FUNCTION(callback)) {
throw MakeTypeError('called_non_callable', [callback]);
}
// Pull out the length so that modifications to the length in the
// loop will not affect the looping.
var length = this.length;
var i = 0;
find_initial: if (%_ArgumentsLength() < 2) {
for (; i < length; i++) {
current = this[i];
if (!IS_UNDEFINED(current) || i in this) {
i++;
break find_initial;
}
}
throw MakeTypeError('reduce_no_initial', []);
}
for (; i < length; i++) {
var element = this[i];
if (!IS_UNDEFINED(element) || i in this) {
current = callback.call(null, current, element, i, this);
}
}
return current;
}
function ArrayReduceRight(callback, current) {
if (!IS_FUNCTION(callback)) {
throw MakeTypeError('called_non_callable', [callback]);
}
var i = this.length - 1;
find_initial: if (%_ArgumentsLength() < 2) {
for (; i >= 0; i--) {
current = this[i];
if (!IS_UNDEFINED(current) || i in this) {
i--;
break find_initial;
}
}
throw MakeTypeError('reduce_no_initial', []);
}
for (; i >= 0; i--) {
var element = this[i];
if (!IS_UNDEFINED(element) || i in this) {
current = callback.call(null, current, element, i, this);
}
}
return current;
}
// -------------------------------------------------------------------
......@@ -917,7 +973,9 @@ function SetupArray() {
"every", ArrayEvery,
"map", ArrayMap,
"indexOf", ArrayIndexOf,
"lastIndexOf", ArrayLastIndexOf
"lastIndexOf", ArrayLastIndexOf,
"reduce", ArrayReduce,
"reduceRight", ArrayReduceRight
));
// Manipulate the length of some of the functions to meet
......@@ -930,7 +988,9 @@ function SetupArray() {
ArrayMap: 1,
ArrayIndexOf: 1,
ArrayLastIndexOf: 1,
ArrayPush: 1
ArrayPush: 1,
ArrayReduce: 1,
ArrayReduceRight: 1
});
}
......
......@@ -98,6 +98,7 @@ const kMessages = {
instanceof_function_expected: "Expecting a function in instanceof check, but got %0",
instanceof_nonobject_proto: "Function has non-object prototype '%0' in instanceof check",
null_to_object: "Cannot convert null to object",
reduce_no_initial: "Reduce of empty array with no initial value",
// RangeError
invalid_array_length: "Invalid array length",
invalid_array_apply_length: "Function.prototype.apply supports only up to 1024 arguments",
......
This diff is collapsed.
......@@ -53,6 +53,9 @@ function fail(expected, found, name_opt) {
function deepEquals(a, b) {
if (a == b) return true;
if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) {
return true;
}
if ((typeof a) !== 'object' || (typeof b) !== 'object' ||
(a === null) || (b === null))
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment