aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoberto Raggi <roberto.raggi@nokia.com>2012-05-21 18:34:26 +0200
committerRoberto Raggi <roberto.raggi@nokia.com>2012-05-21 18:34:26 +0200
commit17386967b9d85d2a9c52cdcf13b9f4a76592cae5 (patch)
treee49a279d940f3f8645b675eeb0b2a5c83e4ff395
parentd3649cd9403877ed15bbd5cc5b3425172c6e840b (diff)
Implemented Array.prototype.reduceRight
-rw-r--r--qv4ecmaobjects.cpp24
-rw-r--r--tests/fun.4.js1
2 files changed, 24 insertions, 1 deletions
diff --git a/qv4ecmaobjects.cpp b/qv4ecmaobjects.cpp
index e420e27e00..2ab83b3278 100644
--- a/qv4ecmaobjects.cpp
+++ b/qv4ecmaobjects.cpp
@@ -1586,7 +1586,29 @@ void ArrayPrototype::method_reduceRight(Context *ctx)
{
Value self = ctx->thisObject;
if (ArrayObject *instance = self.asArrayObject()) {
- Q_UNIMPLEMENTED();
+ Value callback = ctx->argument(0);
+ Value initialValue = ctx->argument(1);
+ Value acc = initialValue;
+ for (int k = instance->value.size() - 1; k != -1; --k) {
+ Value v = instance->value.at(k);
+ if (v.isUndefined())
+ continue;
+
+ if (acc.isUndefined()) {
+ acc = v;
+ continue;
+ }
+
+ Value r;
+ Value args[4];
+ args[0] = acc;
+ args[1] = v;
+ args[2] = Value::fromNumber(k);
+ args[3] = ctx->thisObject;
+ __qmljs_call_value(ctx, &r, 0, &callback, args, 4);
+ acc = r;
+ }
+ ctx->result = acc;
} else {
assert(!"generic implementation");
}
diff --git a/tests/fun.4.js b/tests/fun.4.js
index c1b1916e61..b130acccd3 100644
--- a/tests/fun.4.js
+++ b/tests/fun.4.js
@@ -13,5 +13,6 @@ print([10, 20, 30].map(function (v,k,o) { return v * v }));
print([10, 20, 30].filter(function (v,k,o) { return v >= 20 }));
print([10,20,30].reduce(function(a,v,k,o) { return a + v }));
+print([10,20,30].reduceRight(function(a,v,k,o) { return a + v }));