From 0704d2be63b484cb579c1507223db3f914b1338a Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Thu, 30 Oct 2014 22:30:01 +0100 Subject: Get rid of !this and similar constructs The C++ standard doesn't allow calling member functions on a mull object. Fix all such places, by moving the checks to the caller where required. Change-Id: I10fb22acaf0324d8ffd3a6d8e19152e5d32f56bb Reviewed-by: Simon Hausmann --- src/qml/qml/qqmlbinding.cpp | 3 ++- src/qml/qml/qqmlcomponent.cpp | 6 +++--- src/qml/qml/qqmlcontextwrapper.cpp | 16 ++++++---------- src/qml/qml/qqmllistwrapper.cpp | 24 ++++++++++-------------- src/qml/qml/qqmllocale_p.h | 3 ++- src/qml/qml/qqmltypewrapper.cpp | 19 ++++++++----------- src/qml/qml/qqmlvaluetypewrapper.cpp | 27 ++++++++++----------------- src/qml/qml/qqmlxmlhttprequest.cpp | 28 +++++++++++----------------- 8 files changed, 52 insertions(+), 74 deletions(-) (limited to 'src/qml/qml') diff --git a/src/qml/qml/qqmlbinding.cpp b/src/qml/qml/qqmlbinding.cpp index 6033395629..7babcd2f4e 100644 --- a/src/qml/qml/qqmlbinding.cpp +++ b/src/qml/qml/qqmlbinding.cpp @@ -171,7 +171,8 @@ void QQmlBinding::update(QQmlPropertyPrivate::WriteFlags flags) QV4::ScopedFunctionObject f(scope, v4function.value()); Q_ASSERT(f); if (f->bindingKeyFlag()) { - QQmlSourceLocation loc = f->as()->d()->bindingLocation; + Q_ASSERT(f->as()); + QQmlSourceLocation loc = static_cast(f.getPointer())->d()->bindingLocation; url = loc.sourceFile; lineNumber = loc.line; columnNumber = loc.column; diff --git a/src/qml/qml/qqmlcomponent.cpp b/src/qml/qml/qqmlcomponent.cpp index c772e16ca9..63a43966b1 100644 --- a/src/qml/qml/qqmlcomponent.cpp +++ b/src/qml/qml/qqmlcomponent.cpp @@ -1500,13 +1500,13 @@ void QmlIncubatorObject::setInitialState(QObject *o) void QmlIncubatorObject::destroy(Managed *that) { - that->as()->d()->~Data(); + static_cast(that)->d()->~Data(); } void QmlIncubatorObject::markObjects(QV4::Managed *that, QV4::ExecutionEngine *e) { - QmlIncubatorObject *o = that->as(); - Q_ASSERT(o); + QmlIncubatorObject *o = static_cast(that); + Q_ASSERT(that->as()); o->d()->valuemap.mark(e); o->d()->qmlGlobal.mark(e); o->d()->statusChanged.mark(e); diff --git a/src/qml/qml/qqmlcontextwrapper.cpp b/src/qml/qml/qqmlcontextwrapper.cpp index e78f9cf7a4..0816bc05df 100644 --- a/src/qml/qml/qqmlcontextwrapper.cpp +++ b/src/qml/qml/qqmlcontextwrapper.cpp @@ -97,7 +97,7 @@ ReturnedValue QmlContextWrapper::urlScope(QV8Engine *v8, const QUrl &url) QQmlContextData *QmlContextWrapper::callingContext(ExecutionEngine *v4) { Scope scope(v4); - QV4::Scoped c(scope, v4->qmlContextObject()->getPointer()->as()); + QV4::Scoped c(scope, v4->qmlContextObject(), QV4::Scoped::Cast); return !!c ? c->getContext() : 0; } @@ -128,11 +128,10 @@ void QmlContextWrapper::takeContextOwnership(const ValueRef qmlglobal) ReturnedValue QmlContextWrapper::get(Managed *m, String *name, bool *hasProperty) { + Q_ASSERT(m->as()); QV4::ExecutionEngine *v4 = m->engine(); QV4::Scope scope(v4); - QmlContextWrapper *resource = m->as(); - if (!resource) - return v4->currentContext()->throwTypeError(); + QmlContextWrapper *resource = static_cast(m); // In V8 the JS global object would come _before_ the QML global object, // so simulate that here. @@ -273,15 +272,12 @@ ReturnedValue QmlContextWrapper::get(Managed *m, String *name, bool *hasProperty void QmlContextWrapper::put(Managed *m, String *name, const ValueRef value) { + Q_ASSERT(m->as()); ExecutionEngine *v4 = m->engine(); QV4::Scope scope(v4); if (scope.hasException()) return; - QV4::Scoped wrapper(scope, m->as()); - if (!wrapper) { - v4->currentContext()->throwTypeError(); - return; - } + QV4::Scoped wrapper(scope, static_cast(m)); PropertyAttributes attrs; Property *pd = wrapper->__getOwnProperty__(name, &attrs); @@ -372,7 +368,7 @@ void QmlContextWrapper::registerQmlDependencies(ExecutionEngine *engine, const C return; QV4::Scope scope(engine); - QV4::Scoped contextWrapper(scope, engine->qmlContextObject()->getPointer()->as()); + QV4::Scoped contextWrapper(scope, engine->qmlContextObject(), QV4::Scoped::Cast); QQmlContextData *qmlContext = contextWrapper->getContext(); const quint32 *idObjectDependency = compiledFunction->qmlIdObjectDependencyTable(); diff --git a/src/qml/qml/qqmllistwrapper.cpp b/src/qml/qml/qqmllistwrapper.cpp index 9a51767ef1..13e5e49b55 100644 --- a/src/qml/qml/qqmllistwrapper.cpp +++ b/src/qml/qml/qqmllistwrapper.cpp @@ -98,10 +98,9 @@ QVariant QmlListWrapper::toVariant() const ReturnedValue QmlListWrapper::get(Managed *m, String *name, bool *hasProperty) { + Q_ASSERT(m->as()); QV4::ExecutionEngine *v4 = m->engine(); - QmlListWrapper *w = m->as(); - if (!w) - return v4->currentContext()->throwTypeError(); + QmlListWrapper *w = static_cast(m); if (name->equals(v4->id_length) && !w->d()->object.isNull()) { quint32 count = w->d()->property.count ? w->d()->property.count(&w->d()->property) : 0; @@ -119,19 +118,15 @@ ReturnedValue QmlListWrapper::getIndexed(Managed *m, uint index, bool *hasProper { Q_UNUSED(hasProperty); - QV4::ExecutionEngine *e = m->engine(); - QmlListWrapper *w = m->as(); - if (!w) { - if (hasProperty) - *hasProperty = false; - return e->currentContext()->throwTypeError(); - } + Q_ASSERT(m->as()); + QV4::ExecutionEngine *v4 = m->engine(); + QmlListWrapper *w = static_cast(m); quint32 count = w->d()->property.count ? w->d()->property.count(&w->d()->property) : 0; if (index < count && w->d()->property.at) { if (hasProperty) *hasProperty = true; - return QV4::QObjectWrapper::wrap(e, w->d()->property.at(&w->d()->property, index)); + return QV4::QObjectWrapper::wrap(v4, w->d()->property.at(&w->d()->property, index)); } if (hasProperty) @@ -149,15 +144,16 @@ void QmlListWrapper::put(Managed *m, String *name, const ValueRef value) void QmlListWrapper::destroy(Managed *that) { - QmlListWrapper *w = that->as(); - w->d()->~Data(); + Q_ASSERT(that->as()); + static_cast(that)->d()->~Data(); } void QmlListWrapper::advanceIterator(Managed *m, ObjectIterator *it, String *&name, uint *index, Property *p, PropertyAttributes *attrs) { name = (String *)0; *index = UINT_MAX; - QmlListWrapper *w = m->as(); + Q_ASSERT(m->as()); + QmlListWrapper *w = static_cast(m); quint32 count = w->d()->property.count ? w->d()->property.count(&w->d()->property) : 0; if (it->arrayIndex < count) { *index = it->arrayIndex; diff --git a/src/qml/qml/qqmllocale_p.h b/src/qml/qml/qqmllocale_p.h index 24cb3a8c42..a29b86fbea 100644 --- a/src/qml/qml/qqmllocale_p.h +++ b/src/qml/qml/qqmllocale_p.h @@ -134,7 +134,8 @@ struct QQmlLocaleData : public QV4::Object V4_OBJECT(Object) static QLocale *getThisLocale(QV4::CallContext *ctx) { - QQmlLocaleData *thisObject = ctx->d()->callData->thisObject.asObject()->as(); + QV4::Object *o = ctx->d()->callData->thisObject.asObject(); + QQmlLocaleData *thisObject = o ? o->as() : 0; if (!thisObject) { ctx->throwTypeError(); return 0; diff --git a/src/qml/qml/qqmltypewrapper.cpp b/src/qml/qml/qqmltypewrapper.cpp index 627b701512..d8f282c030 100644 --- a/src/qml/qml/qqmltypewrapper.cpp +++ b/src/qml/qml/qqmltypewrapper.cpp @@ -125,13 +125,12 @@ ReturnedValue QmlTypeWrapper::create(QV8Engine *v8, QObject *o, QQmlTypeNameCach ReturnedValue QmlTypeWrapper::get(Managed *m, String *name, bool *hasProperty) { + Q_ASSERT(m->as()); + QV4::ExecutionEngine *v4 = m->engine(); QV4::Scope scope(v4); - Scoped w(scope, m->as()); - if (!w) - return v4->currentContext()->throwTypeError(); - + Scoped w(scope, static_cast(m)); if (hasProperty) *hasProperty = true; @@ -235,14 +234,11 @@ ReturnedValue QmlTypeWrapper::get(Managed *m, String *name, bool *hasProperty) void QmlTypeWrapper::put(Managed *m, String *name, const ValueRef value) { - QmlTypeWrapper *w = m->as(); + Q_ASSERT(m->as()); + QmlTypeWrapper *w = static_cast(m); QV4::ExecutionEngine *v4 = m->engine(); if (v4->hasException) return; - if (!w) { - v4->currentContext()->throwTypeError(); - return; - } QV4::Scope scope(v4); QV8Engine *v8engine = v4->v8Engine; @@ -290,8 +286,9 @@ void QmlTypeWrapper::destroy(Managed *that) bool QmlTypeWrapper::isEqualTo(Managed *a, Managed *b) { - QV4::QmlTypeWrapper *qmlTypeWrapperA = a->asObject()->as(); - if (QV4::QmlTypeWrapper *qmlTypeWrapperB = b->asObject()->as()) + Q_ASSERT(a->as()); + QV4::QmlTypeWrapper *qmlTypeWrapperA = static_cast(a); + if (QV4::QmlTypeWrapper *qmlTypeWrapperB = b->as()) return qmlTypeWrapperA->toVariant() == qmlTypeWrapperB->toVariant(); else if (QV4::QObjectWrapper *qobjectWrapper = b->as()) return qmlTypeWrapperA->toVariant().value() == qobjectWrapper->object(); diff --git a/src/qml/qml/qqmlvaluetypewrapper.cpp b/src/qml/qml/qqmlvaluetypewrapper.cpp index 0599745d95..b0125b4c13 100644 --- a/src/qml/qml/qqmlvaluetypewrapper.cpp +++ b/src/qml/qml/qqmlvaluetypewrapper.cpp @@ -180,7 +180,8 @@ QVariant QmlValueTypeWrapper::toVariant() const void QmlValueTypeWrapper::destroy(Managed *that) { - QmlValueTypeWrapper *w = that->as(); + Q_ASSERT(that->as()); + QmlValueTypeWrapper *w = static_cast(that); if (w->d()->objectType == Reference) static_cast(w)->d()->~Data(); else @@ -189,8 +190,8 @@ void QmlValueTypeWrapper::destroy(Managed *that) bool QmlValueTypeWrapper::isEqualTo(Managed *m, Managed *other) { - QV4::QmlValueTypeWrapper *lv = m->as(); - Q_ASSERT(lv); + Q_ASSERT(m && m->as() && other); + QV4::QmlValueTypeWrapper *lv = static_cast(m); if (QV4::VariantObject *rv = other->as()) return lv->isEqual(rv->d()->data); @@ -203,12 +204,8 @@ bool QmlValueTypeWrapper::isEqualTo(Managed *m, Managed *other) PropertyAttributes QmlValueTypeWrapper::query(const Managed *m, String *name) { - const QmlValueTypeWrapper *r = m->as(); - QV4::ExecutionEngine *v4 = m->engine(); - if (!r) { - v4->currentContext()->throwTypeError(); - return PropertyAttributes(); - } + Q_ASSERT(m->as()); + const QmlValueTypeWrapper *r = static_cast(m); QQmlPropertyData local; QQmlPropertyData *result = 0; @@ -267,10 +264,9 @@ ReturnedValue QmlValueTypeWrapper::method_toString(CallContext *ctx) ReturnedValue QmlValueTypeWrapper::get(Managed *m, String *name, bool *hasProperty) { - QmlValueTypeWrapper *r = m->as(); + Q_ASSERT(m->as()); + QmlValueTypeWrapper *r = static_cast(m); QV4::ExecutionEngine *v4 = m->engine(); - if (!r) - return v4->currentContext()->throwTypeError(); // Note: readReferenceValue() can change the reference->type. if (r->d()->objectType == QmlValueTypeWrapper::Reference) { @@ -329,16 +325,13 @@ ReturnedValue QmlValueTypeWrapper::get(Managed *m, String *name, bool *hasProper void QmlValueTypeWrapper::put(Managed *m, String *name, const ValueRef value) { + Q_ASSERT(m->as()); ExecutionEngine *v4 = m->engine(); Scope scope(v4); if (scope.hasException()) return; - Scoped r(scope, m->as()); - if (!r) { - v4->currentContext()->throwTypeError(); - return; - } + Scoped r(scope, static_cast(m)); QByteArray propName = name->toQString().toUtf8(); if (r->d()->objectType == QmlValueTypeWrapper::Reference) { diff --git a/src/qml/qml/qqmlxmlhttprequest.cpp b/src/qml/qml/qqmlxmlhttprequest.cpp index e374e471e3..0730cbc363 100644 --- a/src/qml/qml/qqmlxmlhttprequest.cpp +++ b/src/qml/qml/qqmlxmlhttprequest.cpp @@ -203,7 +203,7 @@ public: // JS API static void destroy(Managed *that) { - that->as()->d()->~Data(); + static_cast(that)->d()->~Data(); } static ReturnedValue get(Managed *m, String *name, bool *hasProperty); static ReturnedValue getIndexed(Managed *m, uint index, bool *hasProperty); @@ -234,7 +234,7 @@ public: // JS API static void destroy(Managed *that) { - that->as()->d()->~Data(); + static_cast(that)->d()->~Data(); } static ReturnedValue get(Managed *m, String *name, bool *hasProperty); static ReturnedValue getIndexed(Managed *m, uint index, bool *hasProperty); @@ -324,7 +324,7 @@ struct Node : public Object // JS API static void destroy(Managed *that) { - that->as()->d()->~Data(); + static_cast(that)->d()->~Data(); } // C++ API @@ -916,10 +916,9 @@ ReturnedValue NamedNodeMap::getIndexed(Managed *m, uint index, bool *hasProperty ReturnedValue NamedNodeMap::get(Managed *m, String *name, bool *hasProperty) { - NamedNodeMap *r = m->as(); + Q_ASSERT(m->as()); + NamedNodeMap *r = static_cast(m); QV4::ExecutionEngine *v4 = m->engine(); - if (!r) - return v4->currentContext()->throwTypeError(); name->makeIdentifier(); if (name->equals(v4->id_length)) @@ -949,13 +948,9 @@ ReturnedValue NamedNodeMap::create(QV8Engine *engine, NodeImpl *data, const QLis ReturnedValue NodeList::getIndexed(Managed *m, uint index, bool *hasProperty) { + Q_ASSERT(m->as()); QV4::ExecutionEngine *v4 = m->engine(); - NodeList *r = m->as(); - if (!r) { - if (hasProperty) - *hasProperty = false; - return v4->currentContext()->throwTypeError(); - } + NodeList *r = static_cast(m); QV8Engine *engine = v4->v8Engine; @@ -971,10 +966,9 @@ ReturnedValue NodeList::getIndexed(Managed *m, uint index, bool *hasProperty) ReturnedValue NodeList::get(Managed *m, String *name, bool *hasProperty) { + Q_ASSERT(m->as()); QV4::ExecutionEngine *v4 = m->engine(); - NodeList *r = m->as(); - if (!r) - return v4->currentContext()->throwTypeError(); + NodeList *r = static_cast(m); name->makeIdentifier(); @@ -1608,7 +1602,7 @@ struct QQmlXMLHttpRequestWrapper : public Object V4_OBJECT(Object) static void destroy(Managed *that) { - that->as()->d()->~Data(); + static_cast(that)->d()->~Data(); } }; @@ -1638,7 +1632,7 @@ struct QQmlXMLHttpRequestCtor : public FunctionObject }; V4_OBJECT(FunctionObject) static void markObjects(Managed *that, ExecutionEngine *e) { - QQmlXMLHttpRequestCtor *c = that->as(); + QQmlXMLHttpRequestCtor *c = static_cast(that); if (c->d()->proto) c->d()->proto->mark(e); FunctionObject::markObjects(that, e); -- cgit v1.2.3