aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2018-08-02 22:40:51 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-08-04 09:16:13 +0000
commitdb780b5e3277af1a9ce1a68bb7b47c6776309806 (patch)
treebcd9219ea33426cfdad0a882503a1d0fa9aaeb79 /src/qml
parent60eab28305e7b51fe8efcb828628001674919408 (diff)
Add Object.entries()
Change-Id: Ia06813a7cac5bd8be99e9f6ab3cd30eac26e499f Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r--src/qml/jsruntime/qv4objectproto.cpp40
-rw-r--r--src/qml/jsruntime/qv4objectproto_p.h1
2 files changed, 41 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4objectproto.cpp b/src/qml/jsruntime/qv4objectproto.cpp
index dbbd59abc9..53113debb2 100644
--- a/src/qml/jsruntime/qv4objectproto.cpp
+++ b/src/qml/jsruntime/qv4objectproto.cpp
@@ -104,6 +104,7 @@ void ObjectPrototype::init(ExecutionEngine *v4, Object *ctor)
ctor->defineDefaultProperty(QStringLiteral("create"), method_create, 2);
ctor->defineDefaultProperty(QStringLiteral("defineProperty"), method_defineProperty, 3);
ctor->defineDefaultProperty(QStringLiteral("defineProperties"), method_defineProperties, 2);
+ ctor->defineDefaultProperty(QStringLiteral("entries"), method_entries, 1);
ctor->defineDefaultProperty(QStringLiteral("seal"), method_seal, 1);
ctor->defineDefaultProperty(QStringLiteral("freeze"), method_freeze, 1);
ctor->defineDefaultProperty(QStringLiteral("preventExtensions"), method_preventExtensions, 1);
@@ -338,6 +339,45 @@ ReturnedValue ObjectPrototype::method_defineProperties(const FunctionObject *b,
return O.asReturnedValue();
}
+ReturnedValue ObjectPrototype::method_entries(const FunctionObject *f, const Value *, const Value *argv, int argc)
+{
+ Scope scope(f);
+ if (!argc)
+ return scope.engine->throwTypeError();
+
+ ScopedObject o(scope, argv[0].toObject(scope.engine));
+ if (scope.engine->hasException)
+ return Encode::undefined();
+
+ ScopedArrayObject a(scope, scope.engine->newArrayObject());
+
+ ObjectIterator it(scope, o, ObjectIterator::EnumerableOnly);
+ ScopedString name(scope);
+ ScopedArrayObject entry(scope);
+ while (1) {
+ name = it.nextPropertyNameAsString();
+ if (!name)
+ break;
+ entry = scope.engine->newArrayObject();
+ entry->push_back(name);
+ a->push_back(entry);
+ }
+
+ // now add values, do this after the loop above as reading out the values can have side effects
+ uint len = a->getLength();
+ ScopedValue value(scope);
+ for (uint i = 0; i < len; ++i) {
+ entry = a->get(PropertyKey::fromArrayIndex(i));
+ name = entry->get(PropertyKey::fromArrayIndex(0));
+ value = o->get(name->toPropertyKey());
+ if (scope.engine->hasException)
+ return Encode::undefined();
+ entry->push_back(value);
+ }
+
+ return a.asReturnedValue();
+}
+
ReturnedValue ObjectPrototype::method_seal(const FunctionObject *b, const Value *, const Value *argv, int argc)
{
const Value a = argc ? argv[0] : Primitive::undefinedValue();
diff --git a/src/qml/jsruntime/qv4objectproto_p.h b/src/qml/jsruntime/qv4objectproto_p.h
index 0314c05766..51f0e32a2c 100644
--- a/src/qml/jsruntime/qv4objectproto_p.h
+++ b/src/qml/jsruntime/qv4objectproto_p.h
@@ -82,6 +82,7 @@ struct ObjectPrototype: Object
static ReturnedValue method_create(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_defineProperties(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_defineProperty(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
+ static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_freeze(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_getOwnPropertyDescriptor(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);
static ReturnedValue method_getOwnPropertyNames(const FunctionObject *, const Value *thisObject, const Value *argv, int argc);