diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-02 15:44:12 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-08-02 20:25:41 +0000 |
commit | 0754f55287f4652382332bce42cd8c7d27846ef1 (patch) | |
tree | 490bbd3c5d66586a1427ba21dde76b2af65230dc /src/qml/jsruntime/qv4sequenceobject.cpp | |
parent | 57640959320c87850315eae44c783803d113dc85 (diff) |
Introduce a new mechanism to iterate over object properties
The old advanceIterator schema was extremely ugly and in addition
not flexible enough to support the requirements for Proxy.ownKeys
and some of the methods in Object
Implemented a new scheme through a OwnPropertyKeys method in the
Object VTable that creates and returns an iterator object. Ported
QJSValueIterator and for-in to use the new mechanism.
There's still many places where we use the old ObjectIterator (that
relies on advanceIterator). Those will be ported in subsequent
commits.
Change-Id: I091a9bea9ff6b2b63630cc336814700757a718be
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4sequenceobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4sequenceobject.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4sequenceobject.cpp b/src/qml/jsruntime/qv4sequenceobject.cpp index 1ba889ee82..ab05a819c1 100644 --- a/src/qml/jsruntime/qv4sequenceobject.cpp +++ b/src/qml/jsruntime/qv4sequenceobject.cpp @@ -365,6 +365,38 @@ public: QV4::Object::virtualAdvanceIterator(this, it, name, index, p, attrs); } + struct OwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator + { + ~OwnPropertyKeyIterator() override = default; + PropertyKey next(const Object *o, Property *pd = nullptr, PropertyAttributes *attrs = nullptr) override + { + const QQmlSequence *s = static_cast<const QQmlSequence *>(o); + + if (s->d()->isReference) { + if (!s->d()->object) + return ObjectOwnPropertyKeyIterator::next(o, pd, attrs); + s->loadReference(); + } + + if (arrayIndex < static_cast<uint>(s->d()->container->size())) { + uint index = arrayIndex; + ++arrayIndex; + if (attrs) + *attrs = QV4::Attr_Data; + if (pd) + pd->value = convertElementToValue(s->engine(), s->d()->container->at(index)); + return PropertyKey::fromArrayIndex(index); + } + + return ObjectOwnPropertyKeyIterator::next(o, pd, attrs); + } + }; + + static OwnPropertyKeyIterator *containerOwnPropertyKeys(const Object *) + { + return new OwnPropertyKeyIterator; + } + bool containerDeleteIndexedProperty(uint index) { /* Qt containers have int (rather than uint) allowable indexes. */ @@ -589,6 +621,8 @@ public: { return static_cast<QQmlSequence<Container> *>(that)->containerIsEqualTo(other); } static void virtualAdvanceIterator(Managed *that, ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attrs) { return static_cast<QQmlSequence<Container> *>(that)->containerAdvanceIterator(it, name, index, p, attrs); } + static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m) + { return static_cast<const QQmlSequence<Container> *>(m)->containerOwnPropertyKeys(m);} }; |