Get rid of inner MethodClosure object in QV8QObjectWrapper
authorSimon Hausmann <simon.hausmann@digia.com>
Mon, 3 Jun 2013 14:41:14 +0000 (16:41 +0200)
committerLars Knoll <lars.knoll@digia.com>
Mon, 3 Jun 2013 18:41:04 +0000 (20:41 +0200)
Instead let's have a convenient create function in QV4::QObjectMethod

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

index ed64b34..c298728 100644 (file)
@@ -133,7 +133,7 @@ Value QObjectWrapper::getProperty(ExecutionContext *ctx, String *name, QObjectWr
 
         if (!hasProp) {
             int index = name->isEqualTo(m_destroy) ? QV4::QObjectMethod::DestroyMethod : QV4::QObjectMethod::ToStringMethod;
-            method = QV4::Value::fromObject(new (ctx->engine->memoryManager) QV4::QObjectMethod(ctx->engine->rootContext, m_object, index, QV4::Value::undefinedValue()));
+            method = QV4::QObjectMethod::create(ctx->engine->rootContext, m_object, index);
             QV4::Object::put(this, ctx, name, method);
         }
 
@@ -512,21 +512,6 @@ QV4::Value QV8QObjectWrapper::GetProperty(QV8Engine *engine, QObject *object,
                                                      QQmlContextData *context,
                                                      QV4::QObjectWrapper::RevisionMode revisionMode)
 {
-    // XXX More recent versions of V8 introduced "Callable" objects.  It is possible that these
-    // will be a faster way of creating QObject method objects.
-    struct MethodClosure {
-       static QV4::Value create(QV8Engine *engine, QObject *object,
-                                           int index) {
-           QV4::ExecutionEngine *v4 = QV8Engine::getV4(engine);
-           return QV4::Value::fromObject(new (v4->memoryManager) QV4::QObjectMethod(v4->rootContext, object, index, QV4::Value::undefinedValue()));
-       }
-       static QV4::Value createWithGlobal(QV8Engine *engine, QObject *object,
-                                                     int index) {
-           QV4::ExecutionEngine *v4 = QV8Engine::getV4(engine);
-           return QV4::Value::fromObject(new (v4->memoryManager) QV4::QObjectMethod(v4->rootContext, object, index, QV4::Value::fromObject(QV8Engine::getV4(engine)->qmlContextObject())));
-       }
-    };
-
     if (QQmlData::wasDeleted(object))
         return QV4::Value::emptyValue();
 
@@ -551,15 +536,16 @@ QV4::Value QV8QObjectWrapper::GetProperty(QV8Engine *engine, QObject *object,
             return QV4::Value::emptyValue();
     }
 
+    QV4::ExecutionEngine *v4 = QV8Engine::getV4(engine);
+
     if (result->isFunction() && !result->isVarProperty()) {
         if (result->isVMEFunction()) {
             QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(object);
             Q_ASSERT(vmemo);
             return vmemo->vmeMethod(result->coreIndex);
         } else if (result->isV4Function()) {
-            return MethodClosure::createWithGlobal(engine, object, result->coreIndex);
+            return QV4::QObjectMethod::create(v4->rootContext, object, result->coreIndex, QV4::Value::fromObject(v4->qmlContextObject()));
         } else if (result->isSignalHandler()) {
-            QV4::ExecutionEngine *v4 = QV8Engine::getV4(engine);
             QV4::QmlSignalHandler *handler = new (v4->memoryManager) QV4::QmlSignalHandler(v4, object, result->coreIndex);
 
             QV4::String *connect = v4->newIdentifier(QStringLiteral("connect"));
@@ -569,7 +555,7 @@ QV4::Value QV8QObjectWrapper::GetProperty(QV8Engine *engine, QObject *object,
 
             return QV4::Value::fromObject(handler);
         } else {
-            return MethodClosure::create(engine, object, result->coreIndex);
+            return QV4::QObjectMethod::create(v4->rootContext, object, result->coreIndex);
         }
     }
 
@@ -1838,6 +1824,11 @@ QV4::Value CallArgument::toValue(QV8Engine *engine)
     }
 }
 
+Value QObjectMethod::create(ExecutionContext *scope, QObject *object, int index, const Value &qmlGlobal)
+{
+    return Value::fromObject(new (scope->engine->memoryManager) QObjectMethod(scope, object, index, qmlGlobal));
+}
+
 QObjectMethod::QObjectMethod(ExecutionContext *scope, QObject *object, int index, const Value &qmlGlobal)
     : FunctionObject(scope)
     , m_object(object)
index f8d128f..c50fd46 100644 (file)
@@ -119,12 +119,14 @@ struct QObjectMethod : public QV4::FunctionObject
 
     enum { DestroyMethod = -1, ToStringMethod = -2 };
 
-    QObjectMethod(QV4::ExecutionContext *scope, QObject *object, int index, const QV4::Value &qmlGlobal);
+    static Value create(QV4::ExecutionContext *scope, QObject *object, int index, const Value &qmlGlobal = Value::undefinedValue());
 
     int methodIndex() const { return m_index; }
     QObject *object() const { return m_object.data(); }
 
 private:
+    QObjectMethod(QV4::ExecutionContext *scope, QObject *object, int index, const QV4::Value &qmlGlobal);
+
     QV4::Value method_toString(QV4::ExecutionContext *ctx);
     QV4::Value method_destroy(QV4::ExecutionContext *ctx, Value *args, int argc);