summaryrefslogtreecommitdiff
path: root/src/qml/jsruntime/qv4arrayobject.cpp
diff options
context:
space:
mode:
authorKai Uwe Broulik <kde@privat.broulik.de>2016-11-08 11:09:13 +0100
committerKai Uwe Broulik <kde@privat.broulik.de>2016-11-10 09:32:04 +0000
commit2b8a9fc66309038dac21624293cf3b91d87165c3 (patch)
treede1ede83c5fe2a2cd29c97588272142c215e8d4f /src/qml/jsruntime/qv4arrayobject.cpp
parentc5cfce8553d591418f4752b62ac26f8b37e70515 (diff)
downloadqtdeclarative-2b8a9fc66309038dac21624293cf3b91d87165c3.tar.gz
Add ECMAScript 6 Array find and findIndex methods
Change-Id: I318fa243ab68b8a714923ab816d62e8c544fca8c Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4arrayobject.cpp')
-rw-r--r--src/qml/jsruntime/qv4arrayobject.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4arrayobject.cpp b/src/qml/jsruntime/qv4arrayobject.cpp
index 659ede7552..20ba11fd75 100644
--- a/src/qml/jsruntime/qv4arrayobject.cpp
+++ b/src/qml/jsruntime/qv4arrayobject.cpp
@@ -96,6 +96,8 @@ void ArrayPrototype::init(ExecutionEngine *engine, Object *ctor)
defineDefaultProperty(engine->id_toString(), method_toString, 0);
defineDefaultProperty(QStringLiteral("toLocaleString"), method_toLocaleString, 0);
defineDefaultProperty(QStringLiteral("concat"), method_concat, 1);
+ defineDefaultProperty(QStringLiteral("find"), method_find, 1);
+ defineDefaultProperty(QStringLiteral("findIndex"), method_findIndex, 1);
defineDefaultProperty(QStringLiteral("join"), method_join, 1);
defineDefaultProperty(QStringLiteral("pop"), method_pop, 0);
defineDefaultProperty(QStringLiteral("push"), method_push, 1);
@@ -182,6 +184,80 @@ ReturnedValue ArrayPrototype::method_concat(CallContext *ctx)
return result.asReturnedValue();
}
+ReturnedValue ArrayPrototype::method_find(CallContext *ctx)
+{
+ Scope scope(ctx);
+ ScopedObject instance(scope, ctx->thisObject().toObject(scope.engine));
+ if (!instance)
+ return Encode::undefined();
+
+ uint len = instance->getLength();
+
+ ScopedFunctionObject callback(scope, ctx->argument(0));
+ if (!callback)
+ return ctx->engine()->throwTypeError();
+
+ ScopedCallData callData(scope, 3);
+ callData->thisObject = ctx->argument(1);
+ callData->args[2] = instance;
+
+ ScopedValue v(scope);
+
+ for (uint k = 0; k < len; ++k) {
+ v = instance->getIndexed(k);
+ if (scope.hasException())
+ return Encode::undefined();
+
+ callData->args[0] = v;
+ callData->args[1] = Primitive::fromDouble(k);
+ callback->call(scope, callData);
+
+ if (scope.hasException())
+ return Encode::undefined();
+ else if (scope.result.toBoolean())
+ return v->asReturnedValue();
+ }
+
+ return Encode::undefined();
+}
+
+ReturnedValue ArrayPrototype::method_findIndex(CallContext *ctx)
+{
+ Scope scope(ctx);
+ ScopedObject instance(scope, ctx->thisObject().toObject(scope.engine));
+ if (!instance)
+ return Encode::undefined();
+
+ uint len = instance->getLength();
+
+ ScopedFunctionObject callback(scope, ctx->argument(0));
+ if (!callback)
+ return ctx->engine()->throwTypeError();
+
+ ScopedCallData callData(scope, 3);
+ callData->thisObject = ctx->argument(1);
+ callData->args[2] = instance;
+
+ ScopedValue v(scope);
+
+ for (uint k = 0; k < len; ++k) {
+ v = instance->getIndexed(k);
+ if (scope.hasException())
+ return Encode::undefined();
+
+ callData->args[0] = v;
+ callData->args[1] = Primitive::fromDouble(k);
+ callback->call(scope, callData);
+
+ if (scope.hasException())
+ return Encode::undefined();
+ else if (scope.result.toBoolean())
+ return Encode(k);
+ }
+
+ return Encode(-1);
+}
+
ReturnedValue ArrayPrototype::method_join(CallContext *ctx)
{
Scope scope(ctx);