Implement iteration on QObject properties
authorSimon Hausmann <simon.hausmann@digia.com>
Wed, 12 Jun 2013 07:19:37 +0000 (09:19 +0200)
committerLars Knoll <lars.knoll@digia.com>
Wed, 12 Jun 2013 08:51:02 +0000 (10:51 +0200)
Change-Id: I036e3d835f1c0375460acf142738123c1728279e
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/qml/v4/qv4qobjectwrapper.cpp
src/qml/qml/v4/qv4qobjectwrapper_p.h

index 815c278..bbdcbe9 100644 (file)
@@ -696,42 +696,26 @@ PropertyAttributes QObjectWrapper::query(const Managed *m, String *name)
         return QV4::Object::query(m, name);
 }
 
-QV4::Value QObjectWrapper::enumerateProperties(Object *object)
+Property *QObjectWrapper::advanceIterator(Managed *m, ObjectIterator *it, String **name, uint *index, PropertyAttributes *attributes)
 {
-    QObjectWrapper *that = static_cast<QObjectWrapper*>(object);
+    *name = 0;
+    *index = UINT_MAX;
 
-    if (that->m_object.isNull())
-        return QV4::Value::undefinedValue();
-
-    QStringList result;
-
-    QV8Engine *v8Engine = that->engine()->v8Engine;
-    QQmlEnginePrivate *ep = v8Engine->engine()
-            ? QQmlEnginePrivate::get(v8Engine->engine())
-            : 0;
-
-    QQmlPropertyCache *cache = 0;
-    QQmlData *ddata = QQmlData::get(that->m_object);
-    if (ddata)
-        cache = ddata->propertyCache;
+    QObjectWrapper *that = static_cast<QObjectWrapper*>(m);
 
-    if (!cache) {
-        cache = ep ? ep->cache(that->m_object) : 0;
-        if (cache) {
-            if (ddata) { cache->addref(); ddata->propertyCache = cache; }
-        } else {
-            // Not cachable - fall back to QMetaObject (eg. dynamic meta object)
-            const QMetaObject *mo = that->m_object->metaObject();
-            int pc = mo->propertyCount();
-            int po = mo->propertyOffset();
-            for (int i=po; i<pc; ++i)
-                result << QString::fromUtf8(mo->property(i).name());
-        }
-    } else {
-        result = cache->propertyNames();
+    if (!that->m_object)
+        return QV4::Object::advanceIterator(m, it, name, index, attributes);
+
+    const QMetaObject *mo = that->m_object->metaObject();
+    if (it->arrayIndex < mo->propertyCount()) {
+        *name = that->engine()->newString(QString::fromUtf8(mo->property(it->arrayIndex).name()));
+        ++it->arrayIndex;
+        if (attributes)
+            *attributes = QV4::Attr_Data;
+        it->tmpDynamicProperty.value = that->get(*name);
+        return &it->tmpDynamicProperty;
     }
-
-    return QV4::Value::fromObject(that->engine()->newArrayObject(result));
+    return QV4::Object::advanceIterator(m, it, name, index, attributes);
 }
 
 namespace QV4 {
index 5729b70..97d2fb4 100644 (file)
@@ -96,6 +96,8 @@ struct Q_QML_EXPORT QObjectWrapper : public QV4::Object
 
     static Value wrap(ExecutionEngine *engine, QObject *object);
 
+    using Object::get;
+
 private:
     static Value create(ExecutionEngine *engine, QQmlData *ddata, QObject *object);
 
@@ -110,10 +112,9 @@ private:
     static Value get(Managed *m, ExecutionContext *ctx, String *name, bool *hasProperty);
     static void put(Managed *m, ExecutionContext *ctx, String *name, const Value &value);
     static PropertyAttributes query(const Managed *, String *name);
+    static Property *advanceIterator(Managed *m, ObjectIterator *it, String **name, uint *index, PropertyAttributes *attributes);
     static void markObjects(Managed *that);
 
-    static Value enumerateProperties(Object *m_object);
-
     static void destroy(Managed *that)
     {
         static_cast<QObjectWrapper *>(that)->~QObjectWrapper();