From 0704d2be63b484cb579c1507223db3f914b1338a Mon Sep 17 00:00:00 2001
From: Lars Knoll <lars.knoll@theqtcompany.com>
Date: Thu, 30 Oct 2014 22:30:01 +0100
Subject: [PATCH] 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 <simon.hausmann@digia.com>
---
 src/imports/localstorage/plugin.cpp           |  3 +-
 src/imports/statemachine/signaltransition.cpp |  5 +---
 src/qml/jsruntime/qv4arraydata.cpp            |  6 ++--
 src/qml/jsruntime/qv4arraydata_p.h            |  4 +--
 src/qml/jsruntime/qv4identifier_p.h           | 12 ++++----
 src/qml/jsruntime/qv4managed_p.h              | 16 ++++-------
 src/qml/jsruntime/qv4qobjectwrapper.cpp       | 10 +++++--
 src/qml/jsruntime/qv4runtime.cpp              |  9 +++---
 src/qml/jsruntime/qv4scopedvalue_p.h          | 10 +++++++
 src/qml/jsruntime/qv4sequenceobject.cpp       |  2 ++
 src/qml/jsruntime/qv4variantobject.cpp        |  4 +--
 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 ++++++++-----------
 src/qml/types/qqmldelegatemodel.cpp           | 13 ++++-----
 src/quick/items/context2d/qquickcontext2d.cpp | 12 ++++----
 21 files changed, 108 insertions(+), 124 deletions(-)

diff --git a/src/imports/localstorage/plugin.cpp b/src/imports/localstorage/plugin.cpp
index f87bee55ec..8b90f5a685 100644
--- a/src/imports/localstorage/plugin.cpp
+++ b/src/imports/localstorage/plugin.cpp
@@ -234,7 +234,8 @@ static ReturnedValue qmlsqldatabase_rows_index(QQmlSqlDatabaseWrapper *r, Execut
 ReturnedValue QQmlSqlDatabaseWrapper::getIndexed(Managed *m, uint index, bool *hasProperty)
 {
     QV4::Scope scope(m->engine());
-    QV4::Scoped<QQmlSqlDatabaseWrapper> r(scope, m->as<QQmlSqlDatabaseWrapper>());
+    Q_ASSERT(m->as<QQmlSqlDatabaseWrapper>());
+    QV4::Scoped<QQmlSqlDatabaseWrapper> r(scope, static_cast<QQmlSqlDatabaseWrapper *>(m));
     if (!r || r->d()->type != QQmlSqlDatabaseWrapper::Rows)
         return Object::getIndexed(m, index, hasProperty);
 
diff --git a/src/imports/statemachine/signaltransition.cpp b/src/imports/statemachine/signaltransition.cpp
index eb842914a1..37edd6eb87 100644
--- a/src/imports/statemachine/signaltransition.cpp
+++ b/src/imports/statemachine/signaltransition.cpp
@@ -91,10 +91,7 @@ void SignalTransition::setSignal(const QJSValue &signal)
     QV4::ExecutionEngine *jsEngine = QV8Engine::getV4(QQmlEngine::contextForObject(this)->engine());
     QV4::Scope scope(jsEngine);
 
-    QV4::Scoped<QV4::FunctionObject> function(scope, QJSValuePrivate::get(m_signal)->getValue(jsEngine));
-    Q_ASSERT(function);
-
-    QV4::Scoped<QV4::QObjectMethod> qobjectSignal(scope, function->as<QV4::QObjectMethod>());
+    QV4::Scoped<QV4::QObjectMethod> qobjectSignal(scope, QJSValuePrivate::get(m_signal)->getValue(jsEngine));
     Q_ASSERT(qobjectSignal);
 
     QObject *sender = qobjectSignal->object();
diff --git a/src/qml/jsruntime/qv4arraydata.cpp b/src/qml/jsruntime/qv4arraydata.cpp
index 12254c6384..35bd6e5501 100644
--- a/src/qml/jsruntime/qv4arraydata.cpp
+++ b/src/qml/jsruntime/qv4arraydata.cpp
@@ -571,21 +571,21 @@ bool SparseArrayData::putArray(Object *o, uint index, Value *values, uint n)
 
 uint ArrayData::append(Object *obj, ArrayObject *otherObj, uint n)
 {
-    Q_ASSERT(!obj->arrayData()->hasAttributes());
+    Q_ASSERT(!obj->arrayData() || !obj->arrayData()->hasAttributes());
 
     if (!n)
         return obj->getLength();
 
     ArrayData *other = otherObj->arrayData();
 
-    if (other->isSparse())
+    if (other && other->isSparse())
         obj->initSparseArray();
     else
         obj->arrayCreate();
 
     uint oldSize = obj->getLength();
 
-    if (other->isSparse()) {
+    if (other && other->isSparse()) {
         SparseArrayData *os = static_cast<SparseArrayData *>(other);
         if (otherObj->hasAccessorProperty() && other->hasAttributes()) {
             Scope scope(obj->engine());
diff --git a/src/qml/jsruntime/qv4arraydata_p.h b/src/qml/jsruntime/qv4arraydata_p.h
index c2deb3e385..b69d200665 100644
--- a/src/qml/jsruntime/qv4arraydata_p.h
+++ b/src/qml/jsruntime/qv4arraydata_p.h
@@ -113,14 +113,14 @@ struct Q_QML_EXPORT ArrayData : public Managed
     Value *arrayData() { return &d()->arrayData[0]; }
 
     const ArrayVTable *vtable() const { return reinterpret_cast<const ArrayVTable *>(internalClass()->vtable); }
-    bool isSparse() const { return this && type() == Sparse; }
+    bool isSparse() const { return type() == Sparse; }
 
     uint length() const {
         return vtable()->length(this);
     }
 
     bool hasAttributes() const {
-        return this && attrs();
+        return attrs();
     }
     PropertyAttributes attributes(int i) const {
         Q_ASSERT(this);
diff --git a/src/qml/jsruntime/qv4identifier_p.h b/src/qml/jsruntime/qv4identifier_p.h
index 9ca6714b90..afed5c646f 100644
--- a/src/qml/jsruntime/qv4identifier_p.h
+++ b/src/qml/jsruntime/qv4identifier_p.h
@@ -56,9 +56,9 @@ struct IdentifierHashEntry {
         int value;
         void *pointer;
     };
-    int get(int *) const { return this ? value : -1; }
-    bool get(bool *) const { return this != 0; }
-    void *get(void **) const { return this ? pointer : 0; }
+    static int get(const IdentifierHashEntry *This, int *) { return This ? This->value : -1; }
+    static bool get(const IdentifierHashEntry *This, bool *) { return This != 0; }
+    static void *get(const IdentifierHashEntry *This, void **) { return This ? This->pointer : 0; }
 };
 
 struct IdentifierHashData
@@ -181,13 +181,13 @@ void IdentifierHash<T>::add(const QString &str, const T &value)
 template<typename T>
 inline T IdentifierHash<T>::value(const QString &str) const
 {
-    return lookup(str)->get((T*)0);
+    return IdentifierHashEntry::get(lookup(str), (T*)0);
 }
 
 template<typename T>
 inline T IdentifierHash<T>::value(String *str) const
 {
-    return lookup(str)->get((T*)0);
+    return IdentifierHashEntry::get(lookup(str), (T*)0);
 }
 
 
@@ -197,7 +197,7 @@ QString IdentifierHash<T>::findId(T value) const
     IdentifierHashEntry *e = d->entries;
     IdentifierHashEntry *end = e + d->alloc;
     while (e < end) {
-        if (e->identifier && e->get((T*)0) == value)
+        if (e->identifier && IdentifierHashEntry::get(e, (T*)0) == value)
             return e->identifier->string;
         ++e;
     }
diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h
index ce0ee973e8..e679017b7e 100644
--- a/src/qml/jsruntime/qv4managed_p.h
+++ b/src/qml/jsruntime/qv4managed_p.h
@@ -269,9 +269,7 @@ public:
 
     template <typename T>
     T *as() {
-        // ### FIXME:
-        if (!this || !internalClass())
-            return 0;
+        Q_ASSERT(internalClass());
 #if !defined(QT_NO_QOBJECT_CHECK)
         static_cast<T *>(this)->qt_check_for_QMANAGED_macro(static_cast<T *>(this));
 #endif
@@ -285,9 +283,7 @@ public:
     }
     template <typename T>
     const T *as() const {
-        // ### FIXME:
-        if (!this)
-            return 0;
+        Q_ASSERT(internalClass());
 #if !defined(QT_NO_QOBJECT_CHECK)
         static_cast<T *>(this)->qt_check_for_QMANAGED_macro(static_cast<T *>(const_cast<Managed *>(this)));
 #endif
@@ -362,23 +358,23 @@ inline Managed *value_cast(const Value &v) {
 template<typename T>
 inline T *managed_cast(Managed *m)
 {
-    return m->as<T>();
+    return m ? m->as<T>() : 0;
 }
 
 template<>
 inline String *managed_cast(Managed *m)
 {
-    return m->asString();
+    return m ? m->asString() : 0;
 }
 template<>
 inline Object *managed_cast(Managed *m)
 {
-    return m->asObject();
+    return m ? m->asObject() : 0;
 }
 template<>
 inline FunctionObject *managed_cast(Managed *m)
 {
-    return m->asFunctionObject();
+    return m ? m->asFunctionObject() : 0;
 }
 
 }
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp
index 32379f7f1e..f2c30e618f 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper.cpp
+++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp
@@ -650,9 +650,13 @@ void QObjectWrapper::setProperty(ExecutionContext *ctx, int propertyIndex, const
 
 bool QObjectWrapper::isEqualTo(Managed *a, Managed *b)
 {
-    QV4::QObjectWrapper *qobjectWrapper = a->as<QV4::QObjectWrapper>();
-    if (QV4::QmlTypeWrapper *qmlTypeWrapper = b->asObject()->as<QV4::QmlTypeWrapper>())
-        return qmlTypeWrapper->toVariant().value<QObject*>() == qobjectWrapper->object();
+    Q_ASSERT(a->as<QV4::QObjectWrapper>());
+    QV4::QObjectWrapper *qobjectWrapper = static_cast<QV4::QObjectWrapper *>(a);
+    QV4::Object *o = b->asObject();
+    if (o) {
+        if (QV4::QmlTypeWrapper *qmlTypeWrapper = o->as<QV4::QmlTypeWrapper>())
+            return qmlTypeWrapper->toVariant().value<QObject*>() == qobjectWrapper->object();
+    }
 
     return false;
 }
diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp
index 048ff935bf..f72f25bd58 100644
--- a/src/qml/jsruntime/qv4runtime.cpp
+++ b/src/qml/jsruntime/qv4runtime.cpp
@@ -1276,7 +1276,8 @@ ReturnedValue Runtime::regexpLiteral(ExecutionContext *ctx, int id)
 
 ReturnedValue Runtime::getQmlIdArray(NoThrowContext *ctx)
 {
-    return ctx->engine()->qmlContextObject()->getPointer()->as<QmlContextWrapper>()->idObjectsArray();
+    Q_ASSERT(ctx->engine()->qmlContextObject()->getPointer()->as<QmlContextWrapper>());
+    return static_cast<QmlContextWrapper *>(ctx->engine()->qmlContextObject()->getPointer())->idObjectsArray();
 }
 
 ReturnedValue Runtime::getQmlContextObject(NoThrowContext *ctx)
@@ -1290,7 +1291,7 @@ ReturnedValue Runtime::getQmlContextObject(NoThrowContext *ctx)
 ReturnedValue Runtime::getQmlScopeObject(NoThrowContext *ctx)
 {
     Scope scope(ctx);
-    QV4::Scoped<QmlContextWrapper> c(scope, ctx->engine()->qmlContextObject()->getPointer()->as<QmlContextWrapper>());
+    QV4::Scoped<QmlContextWrapper> c(scope, ctx->engine()->qmlContextObject(), Scoped<QmlContextWrapper>::Cast);
     return QObjectWrapper::wrap(ctx->d()->engine, c->getScopeObject());
 }
 
@@ -1308,7 +1309,7 @@ ReturnedValue Runtime::getQmlQObjectProperty(ExecutionContext *ctx, const ValueR
 QV4::ReturnedValue Runtime::getQmlAttachedProperty(ExecutionContext *ctx, int attachedPropertiesId, int propertyIndex)
 {
     Scope scope(ctx);
-    QV4::Scoped<QmlContextWrapper> c(scope, ctx->engine()->qmlContextObject()->getPointer()->as<QmlContextWrapper>());
+    QV4::Scoped<QmlContextWrapper> c(scope, ctx->engine()->qmlContextObject(), Scoped<QmlContextWrapper>::Cast);
     QObject *scopeObject = c->getScopeObject();
     QObject *attachedObject = qmlAttachedPropertiesObjectById(attachedPropertiesId, scopeObject);
 
@@ -1349,7 +1350,7 @@ ReturnedValue Runtime::getQmlImportedScripts(NoThrowContext *ctx)
 
 QV4::ReturnedValue Runtime::getQmlSingleton(QV4::NoThrowContext *ctx, String *name)
 {
-    return ctx->engine()->qmlContextObject()->getPointer()->as<QmlContextWrapper>()->qmlSingletonWrapper(ctx->engine()->v8Engine, name);
+    return static_cast<QmlContextWrapper *>(ctx->engine()->qmlContextObject()->getPointer())->qmlSingletonWrapper(ctx->engine()->v8Engine, name);
 }
 
 void Runtime::convertThisToObject(ExecutionContext *ctx)
diff --git a/src/qml/jsruntime/qv4scopedvalue_p.h b/src/qml/jsruntime/qv4scopedvalue_p.h
index 9e9a6e60c9..b1b4d5da8d 100644
--- a/src/qml/jsruntime/qv4scopedvalue_p.h
+++ b/src/qml/jsruntime/qv4scopedvalue_p.h
@@ -288,6 +288,16 @@ struct Scoped
 #endif
     }
 
+    template<typename X>
+    Scoped(const Scope &scope, Returned<X> *x, _Cast)
+    {
+        ptr = scope.engine->jsStackTop++;
+        setPointer(managed_cast<T>(x->getPointer()));
+#ifndef QT_NO_DEBUG
+        ++scope.size;
+#endif
+    }
+
     Scoped(const Scope &scope, const ReturnedValue &v)
     {
         ptr = scope.engine->jsStackTop++;
diff --git a/src/qml/jsruntime/qv4sequenceobject.cpp b/src/qml/jsruntime/qv4sequenceobject.cpp
index a45f4cabc5..7a0a643a7e 100644
--- a/src/qml/jsruntime/qv4sequenceobject.cpp
+++ b/src/qml/jsruntime/qv4sequenceobject.cpp
@@ -331,6 +331,8 @@ public:
 
     bool containerIsEqualTo(Managed *other)
     {
+        if (!other)
+            return false;
         QQmlSequence<Container> *otherSequence = other->as<QQmlSequence<Container> >();
         if (!otherSequence)
             return false;
diff --git a/src/qml/jsruntime/qv4variantobject.cpp b/src/qml/jsruntime/qv4variantobject.cpp
index 68b08fb3ca..3cde96992e 100644
--- a/src/qml/jsruntime/qv4variantobject.cpp
+++ b/src/qml/jsruntime/qv4variantobject.cpp
@@ -92,8 +92,8 @@ void VariantObject::destroy(Managed *that)
 
 bool VariantObject::isEqualTo(Managed *m, Managed *other)
 {
-    QV4::VariantObject *lv = m->as<QV4::VariantObject>();
-    assert(lv);
+    Q_ASSERT(m->as<QV4::VariantObject>());
+    QV4::VariantObject *lv = static_cast<QV4::VariantObject *>(m);
 
     if (QV4::VariantObject *rv = other->as<QV4::VariantObject>())
         return lv->d()->data == rv->d()->data;
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<QV4::QQmlBindingFunction>()->d()->bindingLocation;
+        Q_ASSERT(f->as<QV4::QQmlBindingFunction>());
+        QQmlSourceLocation loc = static_cast<QV4::QQmlBindingFunction *>(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<QmlIncubatorObject>()->d()->~Data();
+    static_cast<QmlIncubatorObject *>(that)->d()->~Data();
 }
 
 void QmlIncubatorObject::markObjects(QV4::Managed *that, QV4::ExecutionEngine *e)
 {
-    QmlIncubatorObject *o = that->as<QmlIncubatorObject>();
-    Q_ASSERT(o);
+    QmlIncubatorObject *o = static_cast<QmlIncubatorObject *>(that);
+    Q_ASSERT(that->as<QmlIncubatorObject>());
     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<QmlContextWrapper> c(scope, v4->qmlContextObject()->getPointer()->as<QmlContextWrapper>());
+    QV4::Scoped<QmlContextWrapper> c(scope, v4->qmlContextObject(), QV4::Scoped<QmlContextWrapper>::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<QmlContextWrapper>());
     QV4::ExecutionEngine *v4 = m->engine();
     QV4::Scope scope(v4);
-    QmlContextWrapper *resource = m->as<QmlContextWrapper>();
-    if (!resource)
-        return v4->currentContext()->throwTypeError();
+    QmlContextWrapper *resource = static_cast<QmlContextWrapper *>(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<QmlContextWrapper>());
     ExecutionEngine *v4 = m->engine();
     QV4::Scope scope(v4);
     if (scope.hasException())
         return;
-    QV4::Scoped<QmlContextWrapper> wrapper(scope, m->as<QmlContextWrapper>());
-    if (!wrapper) {
-        v4->currentContext()->throwTypeError();
-        return;
-    }
+    QV4::Scoped<QmlContextWrapper> wrapper(scope, static_cast<QmlContextWrapper *>(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<QmlContextWrapper> contextWrapper(scope, engine->qmlContextObject()->getPointer()->as<QmlContextWrapper>());
+    QV4::Scoped<QmlContextWrapper> contextWrapper(scope, engine->qmlContextObject(), QV4::Scoped<QmlContextWrapper>::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<QmlListWrapper>());
     QV4::ExecutionEngine *v4 = m->engine();
-    QmlListWrapper *w = m->as<QmlListWrapper>();
-    if (!w)
-        return v4->currentContext()->throwTypeError();
+    QmlListWrapper *w = static_cast<QmlListWrapper *>(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<QmlListWrapper>();
-    if (!w) {
-        if (hasProperty)
-            *hasProperty = false;
-        return e->currentContext()->throwTypeError();
-    }
+    Q_ASSERT(m->as<QmlListWrapper>());
+    QV4::ExecutionEngine *v4 = m->engine();
+    QmlListWrapper *w = static_cast<QmlListWrapper *>(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<QmlListWrapper>();
-    w->d()->~Data();
+    Q_ASSERT(that->as<QmlListWrapper>());
+    static_cast<QmlListWrapper *>(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<QmlListWrapper>();
+    Q_ASSERT(m->as<QmlListWrapper>());
+    QmlListWrapper *w = static_cast<QmlListWrapper *>(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<QQmlLocaleData>();
+        QV4::Object *o = ctx->d()->callData->thisObject.asObject();
+        QQmlLocaleData *thisObject = o ? o->as<QQmlLocaleData>() : 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<QmlTypeWrapper>());
+
     QV4::ExecutionEngine *v4 = m->engine();
     QV4::Scope scope(v4);
 
-    Scoped<QmlTypeWrapper> w(scope,  m->as<QmlTypeWrapper>());
-    if (!w)
-        return v4->currentContext()->throwTypeError();
-
+    Scoped<QmlTypeWrapper> w(scope,  static_cast<QmlTypeWrapper *>(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<QmlTypeWrapper>();
+    Q_ASSERT(m->as<QmlTypeWrapper>());
+    QmlTypeWrapper *w = static_cast<QmlTypeWrapper *>(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<QV4::QmlTypeWrapper>();
-    if (QV4::QmlTypeWrapper *qmlTypeWrapperB = b->asObject()->as<QV4::QmlTypeWrapper>())
+    Q_ASSERT(a->as<QV4::QmlTypeWrapper>());
+    QV4::QmlTypeWrapper *qmlTypeWrapperA = static_cast<QV4::QmlTypeWrapper *>(a);
+    if (QV4::QmlTypeWrapper *qmlTypeWrapperB = b->as<QV4::QmlTypeWrapper>())
         return qmlTypeWrapperA->toVariant() == qmlTypeWrapperB->toVariant();
     else if (QV4::QObjectWrapper *qobjectWrapper = b->as<QV4::QObjectWrapper>())
         return qmlTypeWrapperA->toVariant().value<QObject*>() == 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<QmlValueTypeWrapper>();
+    Q_ASSERT(that->as<QmlValueTypeWrapper>());
+    QmlValueTypeWrapper *w = static_cast<QmlValueTypeWrapper *>(that);
     if (w->d()->objectType == Reference)
         static_cast<QmlValueTypeReference *>(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<QmlValueTypeWrapper>();
-    Q_ASSERT(lv);
+    Q_ASSERT(m && m->as<QmlValueTypeWrapper>() && other);
+    QV4::QmlValueTypeWrapper *lv = static_cast<QmlValueTypeWrapper *>(m);
 
     if (QV4::VariantObject *rv = other->as<VariantObject>())
         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<const QmlValueTypeWrapper>();
-    QV4::ExecutionEngine *v4 = m->engine();
-    if (!r) {
-        v4->currentContext()->throwTypeError();
-        return PropertyAttributes();
-    }
+    Q_ASSERT(m->as<const QmlValueTypeWrapper>());
+    const QmlValueTypeWrapper *r = static_cast<const QmlValueTypeWrapper *>(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<QmlValueTypeWrapper>();
+    Q_ASSERT(m->as<QmlValueTypeWrapper>());
+    QmlValueTypeWrapper *r = static_cast<QmlValueTypeWrapper *>(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<QmlValueTypeWrapper>());
     ExecutionEngine *v4 = m->engine();
     Scope scope(v4);
     if (scope.hasException())
         return;
 
-    Scoped<QmlValueTypeWrapper> r(scope, m->as<QmlValueTypeWrapper>());
-    if (!r) {
-        v4->currentContext()->throwTypeError();
-        return;
-    }
+    Scoped<QmlValueTypeWrapper> r(scope, static_cast<QmlValueTypeWrapper *>(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<NamedNodeMap>()->d()->~Data();
+        static_cast<NamedNodeMap *>(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<NodeList>()->d()->~Data();
+        static_cast<NodeList *>(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<Node>()->d()->~Data();
+        static_cast<Node *>(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<NamedNodeMap>();
+    Q_ASSERT(m->as<NamedNodeMap>());
+    NamedNodeMap *r = static_cast<NamedNodeMap *>(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<NodeList>());
     QV4::ExecutionEngine *v4 = m->engine();
-    NodeList *r = m->as<NodeList>();
-    if (!r) {
-        if (hasProperty)
-            *hasProperty = false;
-        return v4->currentContext()->throwTypeError();
-    }
+    NodeList *r = static_cast<NodeList *>(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<NodeList>());
     QV4::ExecutionEngine *v4 = m->engine();
-    NodeList *r = m->as<NodeList>();
-    if (!r)
-        return v4->currentContext()->throwTypeError();
+    NodeList *r = static_cast<NodeList *>(m);
 
     name->makeIdentifier();
 
@@ -1608,7 +1602,7 @@ struct QQmlXMLHttpRequestWrapper : public Object
     V4_OBJECT(Object)
 
     static void destroy(Managed *that) {
-        that->as<QQmlXMLHttpRequestWrapper>()->d()->~Data();
+        static_cast<QQmlXMLHttpRequestWrapper *>(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>();
+        QQmlXMLHttpRequestCtor *c = static_cast<QQmlXMLHttpRequestCtor *>(that);
         if (c->d()->proto)
             c->d()->proto->mark(e);
         FunctionObject::markObjects(that, e);
diff --git a/src/qml/types/qqmldelegatemodel.cpp b/src/qml/types/qqmldelegatemodel.cpp
index e8b29b2e44..639df4f846 100644
--- a/src/qml/types/qqmldelegatemodel.cpp
+++ b/src/qml/types/qqmldelegatemodel.cpp
@@ -3277,11 +3277,10 @@ public:
 
     static QV4::ReturnedValue getIndexed(QV4::Managed *m, uint index, bool *hasProperty)
     {
+        Q_ASSERT(m->as<QQmlDelegateModelGroupChangeArray>());
         QV4::ExecutionEngine *v4 = m->engine();
         QV4::Scope scope(v4);
-        QV4::Scoped<QQmlDelegateModelGroupChangeArray> array(scope, m->as<QQmlDelegateModelGroupChangeArray>());
-        if (!array)
-            return v4->currentContext()->throwTypeError();
+        QV4::Scoped<QQmlDelegateModelGroupChangeArray> array(scope, static_cast<QQmlDelegateModelGroupChangeArray *>(m));
 
         if (index >= array->count()) {
             if (hasProperty)
@@ -3303,9 +3302,8 @@ public:
 
     static QV4::ReturnedValue get(QV4::Managed *m, QV4::String *name, bool *hasProperty)
     {
-        QQmlDelegateModelGroupChangeArray *array = m->as<QQmlDelegateModelGroupChangeArray>();
-        if (!array)
-            return m->engine()->currentContext()->throwTypeError();
+        Q_ASSERT(m->as<QQmlDelegateModelGroupChangeArray>());
+        QQmlDelegateModelGroupChangeArray *array = static_cast<QQmlDelegateModelGroupChangeArray *>(m);
 
         if (name->equals(m->engine()->id_length)) {
             if (hasProperty)
@@ -3316,8 +3314,7 @@ public:
         return Object::get(m, name, hasProperty);
     }
     static void destroy(Managed *that) {
-        QQmlDelegateModelGroupChangeArray *array = that->as<QQmlDelegateModelGroupChangeArray>();
-        array->d()->~Data();
+        static_cast<QQmlDelegateModelGroupChangeArray *>(that)->d()->~Data();
     }
 
 };
diff --git a/src/quick/items/context2d/qquickcontext2d.cpp b/src/quick/items/context2d/qquickcontext2d.cpp
index b1af6f10dd..0101e0edf4 100644
--- a/src/quick/items/context2d/qquickcontext2d.cpp
+++ b/src/quick/items/context2d/qquickcontext2d.cpp
@@ -3093,11 +3093,12 @@ QV4::ReturnedValue QQuickJSContext2DPixelData::proto_get_length(QV4::CallContext
 
 QV4::ReturnedValue QQuickJSContext2DPixelData::getIndexed(QV4::Managed *m, uint index, bool *hasProperty)
 {
+    Q_ASSERT(m->as<QQuickJSContext2DPixelData>());
     QV4::ExecutionEngine *v4 = m->engine();
     QV4::Scope scope(v4);
-    QV4::Scoped<QQuickJSContext2DPixelData> r(scope, m->as<QQuickJSContext2DPixelData>());
+    QV4::Scoped<QQuickJSContext2DPixelData> r(scope, static_cast<QQuickJSContext2DPixelData *>(m));
 
-    if (r && index < static_cast<quint32>(r->d()->image.width() * r->d()->image.height() * 4)) {
+    if (index < static_cast<quint32>(r->d()->image.width() * r->d()->image.height() * 4)) {
         if (hasProperty)
             *hasProperty = true;
         const quint32 w = r->d()->image.width();
@@ -3123,16 +3124,13 @@ QV4::ReturnedValue QQuickJSContext2DPixelData::getIndexed(QV4::Managed *m, uint
 
 void QQuickJSContext2DPixelData::putIndexed(QV4::Managed *m, uint index, const QV4::ValueRef value)
 {
+    Q_ASSERT(m->as<QQuickJSContext2DPixelData>());
     QV4::ExecutionEngine *v4 = m->engine();
     QV4::Scope scope(v4);
     if (scope.hasException())
         return;
 
-    QV4::Scoped<QQuickJSContext2DPixelData> r(scope, m->as<QQuickJSContext2DPixelData>());
-    if (!r) {
-        scope.engine->currentContext()->throwTypeError();
-        return;
-    }
+    QV4::Scoped<QQuickJSContext2DPixelData> r(scope, static_cast<QQuickJSContext2DPixelData *>(m));
 
     const int v = value->toInt32();
     if (r && index < static_cast<quint32>(r->d()->image.width() * r->d()->image.height() * 4) && v >= 0 && v <= 255) {
-- 
GitLab