From 7b8af027e5d4188311429dc8df1de67f44e5252d Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sun, 28 Apr 2013 21:40:12 +0200 Subject: [PATCH] Convert QObject methods inQV8Enginer to v4 based API Change-Id: I954daac83b36d4cb667ecd7923577af90cfc4f9e Fixup newQObject Change-Id: I7b81754165b319a7fd81838313d0d654aad1132e Reviewed-by: Simon Hausmann --- src/qml/qml/qqmlcomponent.cpp | 6 +++--- src/qml/qml/v8/qjsengine.cpp | 2 +- src/qml/qml/v8/qqmlbuiltinfunctions.cpp | 6 +++--- src/qml/qml/v8/qv8engine.cpp | 13 ++++++------- src/qml/qml/v8/qv8engine_p.h | 26 +++++++++++++------------- src/qml/qml/v8/qv8qobjectwrapper.cpp | 10 +++++----- src/qml/qml/v8/qv8worker.cpp | 4 ++-- src/quick/items/context2d/qquickcanvasitem.cpp | 2 +- src/quick/items/context2d/qquickcontext2d.cpp | 6 +++--- src/quick/items/qquickitem.cpp | 4 ++-- 10 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/qml/qml/qqmlcomponent.cpp b/src/qml/qml/qqmlcomponent.cpp index 70e4ec2..fdba47b 100644 --- a/src/qml/qml/qqmlcomponent.cpp +++ b/src/qml/qml/qqmlcomponent.cpp @@ -1187,7 +1187,7 @@ void QQmlComponent::createObject(QQmlV8Function *args) v8::Local valuemap; if (args->Length() >= 1) - parent = args->engine()->toQObject((*args)[0]); + parent = args->engine()->toQObject((*args)[0]->v4Value()); if (args->Length() >= 2) { v8::Local v = (*args)[1]; @@ -1308,7 +1308,7 @@ void QQmlComponent::incubateObject(QQmlV8Function *args) QQmlIncubator::IncubationMode mode = QQmlIncubator::Asynchronous; if (args->Length() >= 1) - parent = args->engine()->toQObject((*args)[0]); + parent = args->engine()->toQObject((*args)[0]->v4Value()); if (args->Length() >= 2) { v8::Local v = (*args)[1]; @@ -1464,7 +1464,7 @@ void QV8IncubatorResource::setInitialState(QObject *o) QV4::ExecutionEngine *v4engine = QV8Engine::getV4(engine); QV4::Value f = engine->evaluateScript(QString::fromLatin1(INITIALPROPERTIES_SOURCE), qmlGlobal->v4Value().asObject()); - QV4::Value args[] = { engine->newQObject(o)->v4Value(), valuemap->v4Value() }; + QV4::Value args[] = { engine->newQObject(o), valuemap->v4Value() }; f.asFunctionObject()->call(v4engine->current, QV4::Value::fromObject(v4engine->globalObject), args, 2); } } diff --git a/src/qml/qml/v8/qjsengine.cpp b/src/qml/qml/v8/qjsengine.cpp index 63a176b..b842e05 100644 --- a/src/qml/qml/v8/qjsengine.cpp +++ b/src/qml/qml/v8/qjsengine.cpp @@ -322,7 +322,7 @@ QJSValue QJSEngine::newQObject(QObject *object) Q_D(QJSEngine); QScriptIsolate api(d, QScriptIsolate::NotNullEngine); v8::HandleScope handleScope; - return d->scriptValueFromInternal(d->newQObject(object, QV8Engine::JavaScriptOwnership)->v4Value()); + return d->scriptValueFromInternal(d->newQObject(object, QV8Engine::JavaScriptOwnership)); } /*! diff --git a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp index c726f4c..9389709 100644 --- a/src/qml/qml/v8/qqmlbuiltinfunctions.cpp +++ b/src/qml/qml/v8/qqmlbuiltinfunctions.cpp @@ -395,7 +395,7 @@ v8::Handle isQtObject(const v8::Arguments &args) if (args.Length() == 0) return v8::Boolean::New(false); - return v8::Boolean::New(0 != V8ENGINE()->toQObject(args[0])); + return v8::Boolean::New(0 != V8ENGINE()->toQObject(args[0]->v4Value())); } /*! @@ -1195,7 +1195,7 @@ v8::Handle createQmlObject(const v8::Arguments &args) if (url.isValid() && url.isRelative()) url = context->resolvedUrl(url); - QObject *parentArg = v8engine->toQObject(args[1]); + QObject *parentArg = v8engine->toQObject(args[1]->v4Value()); if (!parentArg) V8THROW_ERROR("Qt.createQmlObject(): Missing parent object"); @@ -1309,7 +1309,7 @@ v8::Handle createComponent(const v8::Arguments &args) if (consumedCount < args.Length()) { if (lastArg->IsObject()) { - parentArg = v8engine->toQObject(lastArg); + parentArg = v8engine->toQObject(lastArg->v4Value()); if (!parentArg) V8THROW_ERROR(invalidParent); } else if (lastArg->IsNull()) { diff --git a/src/qml/qml/v8/qv8engine.cpp b/src/qml/qml/v8/qv8engine.cpp index 7aa7cef..fb9ae4f 100644 --- a/src/qml/qml/v8/qv8engine.cpp +++ b/src/qml/qml/v8/qv8engine.cpp @@ -261,7 +261,7 @@ QVariant QV8Engine::toVariant(v8::Handle value, int typeHint) for (uint32_t ii = 0; ii < length; ++ii) { v8::Local arrayItem = array->Get(ii); if (arrayItem->IsObject()) { - list << toQObject(arrayItem->ToObject()); + list << toQObject(arrayItem->ToObject()->v4Value()); } else { list << 0; } @@ -1060,7 +1060,7 @@ QV4::Value QV8Engine::metaTypeToJS(int type, const void *data) result = QJSConverter::toRegExp(*reinterpret_cast(data))->v4Value(); break; case QMetaType::QObjectStar: - result = newQObject(*reinterpret_cast(data))->v4Value(); + result = newQObject(*reinterpret_cast(data)); break; case QMetaType::QVariant: result = variantToJS(*reinterpret_cast(data)); @@ -1159,9 +1159,8 @@ bool QV8Engine::metaTypeFromJS(const QV4::Value &value, int type, void *data) { return true; } break; case QMetaType::QObjectStar: { - v8::Handle v = v8::Value::fromV4Value(value); - if (isQObject(v) || value.isNull()) { - *reinterpret_cast(data) = qtObjectFromJS(v->v4Value()); + if (isQObject(value) || value.isNull()) { + *reinterpret_cast(data) = qtObjectFromJS(value); return true; } break; } @@ -1236,7 +1235,7 @@ bool QV8Engine::metaTypeFromJS(const QV4::Value &value, int type, void *data) { canCast = (type == variantWrapper()->variantValue(proto).userType()) || (valueType && (valueType == variantWrapper()->variantValue(proto).userType())); } - else if (isQObject(proto)) { + else if (isQObject(proto->v4Value())) { QByteArray className = name.left(name.size()-1); if (QObject *qobject = qtObjectFromJS(proto->v4Value())) canCast = qobject->qt_metacast(className) != 0; @@ -1304,7 +1303,7 @@ QVariant QV8Engine::variantFromJS(const QV4::Value &value, return QJSConverter::toRegExp(v8::Handle::Cast(v8::Value::fromV4Value(value))); if (isVariant(v8::Value::fromV4Value(value))) return variantWrapper()->variantValue(v8::Value::fromV4Value(value)); - if (isQObject(v8::Value::fromV4Value(value))) + if (isQObject(value)) return qVariantFromValue(qtObjectFromJS(value)); if (isValueType(value)) return toValueType(value); diff --git a/src/qml/qml/v8/qv8engine_p.h b/src/qml/qml/v8/qv8engine_p.h index cc535ac..f7e73a0 100644 --- a/src/qml/qml/v8/qv8engine_p.h +++ b/src/qml/qml/v8/qv8engine_p.h @@ -322,10 +322,10 @@ public: inline v8::Local qmlScope(QQmlContextData *ctxt, QObject *scope); // Return a JS wrapper for the given QObject \a object - inline v8::Handle newQObject(QObject *object); - inline v8::Handle newQObject(QObject *object, const ObjectOwnership ownership); - inline bool isQObject(v8::Handle); - inline QObject *toQObject(v8::Handle); + inline QV4::Value newQObject(QObject *object); + inline QV4::Value newQObject(QObject *object, const ObjectOwnership ownership); + inline bool isQObject(const QV4::Value &value); + inline QObject *toQObject(const QV4::Value &value); // Return a JS string for the given QString \a string v8::Local toString(const QString &string); @@ -535,27 +535,27 @@ v8::Local QV8Engine::qmlScope(QQmlContextData *ctxt, QObject *scope) return m_contextWrapper.qmlScope(ctxt, scope); } -bool QV8Engine::isQObject(v8::Handle obj) +bool QV8Engine::isQObject(const QV4::Value &value) { - return obj->IsObject()?m_qobjectWrapper.isQObject(v8::Handle::Cast(obj)):false; + return value.isObject() ? m_qobjectWrapper.isQObject(value) : false; } -QObject *QV8Engine::toQObject(v8::Handle obj) +QObject *QV8Engine::toQObject(const QV4::Value &value) { - return obj->IsObject()?m_qobjectWrapper.toQObject(v8::Handle::Cast(obj)):0; + return value.isObject() ? m_qobjectWrapper.toQObject(value) : 0; } -v8::Handle QV8Engine::newQObject(QObject *object) +QV4::Value QV8Engine::newQObject(QObject *object) { - return m_qobjectWrapper.newQObject(object); + return m_qobjectWrapper.newQObject(object)->v4Value(); } -v8::Handle QV8Engine::newQObject(QObject *object, const ObjectOwnership ownership) +QV4::Value QV8Engine::newQObject(QObject *object, const ObjectOwnership ownership) { if (!object) - return v8::Null(); + return QV4::Value::nullValue(); - v8::Handle result = newQObject(object); + QV4::Value result = newQObject(object); QQmlData *ddata = QQmlData::get(object, true); if (ownership == JavaScriptOwnership && ddata) { ddata->indestructible = false; diff --git a/src/qml/qml/v8/qv8qobjectwrapper.cpp b/src/qml/qml/v8/qv8qobjectwrapper.cpp index 444c263..b4acd0c 100644 --- a/src/qml/qml/v8/qv8qobjectwrapper.cpp +++ b/src/qml/qml/v8/qv8qobjectwrapper.cpp @@ -1208,7 +1208,7 @@ QPair QV8QObjectWrapper::ExtractQtMethod(QV8Engine *engine, v8:: if (data->IsArray()) { v8::Local array = v8::Local::Cast(data); - return qMakePair(engine->toQObject(array->Get(0)), array->Get(1)->Int32Value()); + return qMakePair(engine->toQObject(array->Get(0)->v4Value()), array->Get(1)->Int32Value()); } // In theory this can't fall through, but I suppose V8 might run out of memory or something @@ -2168,7 +2168,7 @@ void CallArgument::fromValue(int callType, QV8Engine *engine, v8::Handlev4Value().toQString()); type = callType; } else if (callType == QMetaType::QObjectStar) { - qobjectPtr = engine->toQObject(value); + qobjectPtr = engine->toQObject(value->v4Value()); type = callType; } else if (callType == qMetaTypeId()) { qvariantPtr = new (&allocData) QVariant(engine->toVariant(value, -1)); @@ -2179,9 +2179,9 @@ void CallArgument::fromValue(int callType, QV8Engine *engine, v8::Handle array = v8::Handle::Cast(value); uint32_t length = array->Length(); for (uint32_t ii = 0; ii < length; ++ii) - qlistPtr->append(engine->toQObject(array->Get(ii))); + qlistPtr->append(engine->toQObject(array->Get(ii)->v4Value())); } else { - qlistPtr->append(engine->toQObject(value)); + qlistPtr->append(engine->toQObject(value->v4Value())); } type = callType; } else if (callType == qMetaTypeId()) { @@ -2270,7 +2270,7 @@ v8::Handle CallArgument::toValue(QV8Engine *engine) } else if (type == -1 || type == qMetaTypeId()) { QVariant value = *qvariantPtr; v8::Handle rv = engine->fromVariant(value); - if (QObject *object = engine->toQObject(rv)) + if (QObject *object = engine->toQObject(rv->v4Value())) QQmlData::get(object, true)->setImplicitDestructible(); return rv; } else { diff --git a/src/qml/qml/v8/qv8worker.cpp b/src/qml/qml/v8/qv8worker.cpp index 8aef8c2..0369f47 100644 --- a/src/qml/qml/v8/qv8worker.cpp +++ b/src/qml/qml/v8/qv8worker.cpp @@ -241,10 +241,10 @@ void QV8Worker::serialize(QByteArray &data, v8::Handle v, QV8Engine * serialize(data, val, engine); } } - } else if (engine->isQObject(v)) { + } else if (engine->isQObject(v->v4Value())) { // XXX TODO: Generalize passing objects between the main thread and worker scripts so // that others can trivially plug in their elements. - QQmlListModel *lm = qobject_cast(engine->toQObject(v)); + QQmlListModel *lm = qobject_cast(engine->toQObject(v->v4Value())); if (lm && lm->agent()) { QQmlListModelWorkerAgent *agent = lm->agent(); agent->addref(); diff --git a/src/quick/items/context2d/qquickcanvasitem.cpp b/src/quick/items/context2d/qquickcanvasitem.cpp index 531fac8..bb08794 100644 --- a/src/quick/items/context2d/qquickcanvasitem.cpp +++ b/src/quick/items/context2d/qquickcanvasitem.cpp @@ -657,7 +657,7 @@ void QQuickCanvasItem::updatePolish() foreach (int key, animationCallbacks.keys()) { v8::HandleScope handle_scope; - v8::Handle self = QQmlEnginePrivate::getV8Engine(qmlEngine(this))->newQObject(this).As(); + v8::Handle self = QQmlEnginePrivate::getV8Engine(qmlEngine(this))->newQObject(this); v8::Handle args[] = { v8::Uint32::New(QDateTime::currentDateTimeUtc().toTime_t()) }; v8::Persistent f = animationCallbacks.value(key); f->Call(self, 1, args); diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp index 7892821..1ab6c4a 100644 --- a/src/quick/items/context2d/qquickcontext2d.cpp +++ b/src/quick/items/context2d/qquickcontext2d.cpp @@ -1516,7 +1516,7 @@ static void ctx2d_path_set(v8::Local, v8::Local value, co r->context->beginPath(); if (value->IsObject()) { - QQuickPath* path = qobject_cast(engine->toQObject(value)); + QQuickPath* path = qobject_cast(engine->toQObject(value->v4Value())); if (path) r->context->m_path = path->path(); } else { @@ -2308,8 +2308,8 @@ static v8::Handle ctx2d_drawImage(const v8::Arguments &args) pixmap = r->context->createPixmap(url); } else if (args[0]->IsObject()) { - QQuickImage *imageItem = qobject_cast(engine->toQObject(args[0]->ToObject())); - QQuickCanvasItem *canvas = qobject_cast(engine->toQObject(args[0]->ToObject())); + QQuickImage *imageItem = qobject_cast(engine->toQObject(args[0]->v4Value())); + QQuickCanvasItem *canvas = qobject_cast(engine->toQObject(args[0]->v4Value())); QV8Context2DPixelArrayResource *pix = v8_resource_cast(args[0]->ToObject()->GetInternalField(0)->ToObject()); if (pix && !pix->image.isNull()) { diff --git a/src/quick/items/qquickitem.cpp b/src/quick/items/qquickitem.cpp index f5be518..eb7c481 100644 --- a/src/quick/items/qquickitem.cpp +++ b/src/quick/items/qquickitem.cpp @@ -3759,7 +3759,7 @@ void QQuickItem::mapFromItem(QQmlV8Function *args) const QQuickItem *itemObj = 0; if (!item->IsNull()) - itemObj = qobject_cast(engine->toQObject(item)); + itemObj = qobject_cast(engine->toQObject(item->v4Value())); if (!itemObj && !item->IsNull()) { qmlInfo(this) << "mapFromItem() given argument \"" << item->v4Value().toQString() @@ -3831,7 +3831,7 @@ void QQuickItem::mapToItem(QQmlV8Function *args) const QQuickItem *itemObj = 0; if (!item->IsNull()) - itemObj = qobject_cast(engine->toQObject(item)); + itemObj = qobject_cast(engine->toQObject(item->v4Value())); if (!itemObj && !item->IsNull()) { qmlInfo(this) << "mapToItem() given argument \"" << item->v4Value().toQString() -- 2.7.4