Simplify QObjectWrapper calls from within other type wrappers
authorSimon Hausmann <simon.hausmann@digia.com>
Thu, 6 Jun 2013 13:59:41 +0000 (15:59 +0200)
committerLars Knoll <lars.knoll@digia.com>
Fri, 7 Jun 2013 16:33:05 +0000 (18:33 +0200)
... with the help of a getQmlProperty static function.

Change-Id: Ia823ba9ac995cb1a0591081bf18418f48d60e04d
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
src/qml/qml/qqmlcontextwrapper.cpp
src/qml/qml/qqmltypewrapper.cpp
src/qml/qml/qqmlvaluetypewrapper.cpp
src/qml/qml/v8/qv8qobjectwrapper.cpp
src/qml/qml/v8/qv8qobjectwrapper_p.h

index dd7d28c..ddeaead 100644 (file)
@@ -228,14 +228,12 @@ Value QmlContextWrapper::get(Managed *m, ExecutionContext *ctx, String *name, bo
 
         // Search scope object
         if (scopeObject) {
-            if (QV4::QObjectWrapper *o = QV4::QObjectWrapper::wrap(ctx->engine, scopeObject).as<QV4::QObjectWrapper>()) {
-                bool hasProp = false;
-                QV4::Value result = o->getQmlProperty(ctx, context, name, QV4::QObjectWrapper::CheckRevision, &hasProp);
-                if (hasProp) {
-                    if (hasProperty)
-                        *hasProperty = true;
-                    return result;
-                }
+            bool hasProp = false;
+            QV4::Value result = QV4::QObjectWrapper::getQmlProperty(ctx, context, scopeObject, name, QV4::QObjectWrapper::CheckRevision, &hasProp);
+            if (hasProp) {
+                if (hasProperty)
+                    *hasProperty = true;
+                return result;
             }
         }
         scopeObject = 0;
@@ -243,14 +241,12 @@ Value QmlContextWrapper::get(Managed *m, ExecutionContext *ctx, String *name, bo
 
         // Search context object
         if (context->contextObject) {
-            if (QV4::QObjectWrapper *o = QV4::QObjectWrapper::wrap(ctx->engine, context->contextObject).as<QV4::QObjectWrapper>()) {
-                bool hasProp = false;
-                QV4::Value result = o->getQmlProperty(ctx, context, name, QV4::QObjectWrapper::CheckRevision, &hasProp);
-                if (hasProp) {
-                    if (hasProperty)
-                        *hasProperty = true;
-                    return result;
-                }
+            bool hasProp = false;
+            QV4::Value result = QV4::QObjectWrapper::getQmlProperty(ctx, context, context->contextObject, name, QV4::QObjectWrapper::CheckRevision, &hasProp);
+            if (hasProp) {
+                if (hasProperty)
+                    *hasProperty = true;
+                return result;
             }
         }
 
index e98117e..b3b1ded 100644 (file)
@@ -158,10 +158,7 @@ Value QmlTypeWrapper::get(Managed *m, ExecutionContext *ctx, String *name, bool
                 }
 
                 // check for property.
-                if (QV4::QObjectWrapper *o = QV4::QObjectWrapper::wrap(ctx->engine, qobjectSingleton).as<QV4::QObjectWrapper>())
-                    return o->getQmlProperty(ctx, context, name, QV4::QObjectWrapper::IgnoreRevision);
-                else
-                    return QV4::Value::undefinedValue();
+                return QV4::QObjectWrapper::getQmlProperty(ctx, context, qobjectSingleton, name, QV4::QObjectWrapper::IgnoreRevision);
             } else if (!siinfo->scriptApi(e).isUndefined()) {
                 QV4::ExecutionEngine *engine = QV8Engine::getV4(v8engine);
                 // NOTE: if used in a binding, changes will not trigger re-evaluation since non-NOTIFYable.
@@ -184,11 +181,8 @@ Value QmlTypeWrapper::get(Managed *m, ExecutionContext *ctx, String *name, bool
 
             } else if (w->object) {
                 QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object);
-                if (ao) {
-                    QV4::QObjectWrapper *wrapper = QV4::QObjectWrapper::wrap(ctx->engine, ao).as<QV4::QObjectWrapper>();
-                    if (wrapper)
-                        return wrapper->getQmlProperty(ctx, context, name, QV4::QObjectWrapper::IgnoreRevision);
-                }
+                if (ao)
+                    return QV4::QObjectWrapper::getQmlProperty(ctx, context, ao, name, QV4::QObjectWrapper::IgnoreRevision);
 
                 // Fall through to base implementation
             }
index 955ea04..ae3934f 100644 (file)
@@ -285,7 +285,7 @@ Value QmlValueTypeWrapper::get(Managed *m, ExecutionContext *ctx, String *name,
     if (result->isFunction()) {
         // calling a Q_INVOKABLE function of a value type
         QQmlContextData *qmlContext = QV4::QmlContextWrapper::callingContext(ctx->engine);
-        return QV4::QObjectWrapper::wrap(ctx->engine, r->type).as<QV4::QObjectWrapper>()->getQmlProperty(ctx, qmlContext, name, QV4::QObjectWrapper::IgnoreRevision);
+        return QV4::QObjectWrapper::getQmlProperty(ctx, qmlContext, r->type, name, QV4::QObjectWrapper::IgnoreRevision);
     }
 
 #define VALUE_TYPE_LOAD(metatype, cpptype, constructor) \
index 74cc998..128fd25 100644 (file)
@@ -412,6 +412,29 @@ Value QObjectWrapper::getQmlProperty(ExecutionContext *ctx, QQmlContextData *qml
     }
 }
 
+Value QObjectWrapper::getQmlProperty(ExecutionContext *ctx, QQmlContextData *qmlContext, QObject *object, String *name, QObjectWrapper::RevisionMode revisionMode, bool *hasProperty)
+{
+    if (QQmlData::wasDeleted(object)) {
+        if (hasProperty)
+            *hasProperty = false;
+        return QV4::Value::nullValue();
+    }
+
+    if (!QQmlData::get(object, true)) {
+        if (hasProperty)
+            *hasProperty = false;
+        return QV4::Value::nullValue();
+    }
+
+    QObjectWrapper *wrapper = wrap(ctx->engine, object).as<QV4::QObjectWrapper>();
+    if (!wrapper) {
+        if (hasProperty)
+            *hasProperty = false;
+        return QV4::Value::nullValue();
+    }
+    return wrapper->getQmlProperty(ctx, qmlContext, name, revisionMode, hasProperty);
+}
+
 Value QObjectWrapper::wrap(ExecutionEngine *engine, QObject *object)
 {
     if (QQmlData::wasDeleted(object))
index d360bcb..5c423b7 100644 (file)
@@ -93,6 +93,7 @@ struct Q_QML_EXPORT QObjectWrapper : public QV4::Object
     void deleteQObject(bool deleteInstantly = false);
 
     Value getQmlProperty(ExecutionContext *ctx, QQmlContextData *qmlContext, String *name, RevisionMode revisionMode, bool *hasProperty = 0, bool includeImports = false);
+    static Value getQmlProperty(ExecutionContext *ctx, QQmlContextData *qmlContext, QObject *object, String *name, RevisionMode revisionMode, bool *hasProperty = 0);
 
     static Value wrap(ExecutionEngine *engine, QObject *object);