diff --git a/src/imports/localstorage/plugin.cpp b/src/imports/localstorage/plugin.cpp
index f87bee55ec76b743bf6e746e0e2e27f4f0f1e32f..8b90f5a6858caee30674c0d2edd25f7b24374764 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 eb842914a12b49a5c3e5c565e0b6395087d68e0b..37edd6eb876cb6ff8769ceea86fa34d8aa56f60a 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 12254c6384ac18374df93e6250c621d5c2945548..35bd6e5501822cb250ca0aef7f69c1b4771521fb 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 c2deb3e385056a6e63ebe81aa5a4610aa200bb4c..b69d200665186719c9e8e3a2b395655028178045 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 9ca6714b906a1279ce404d8b298c5dba505e16ae..afed5c646fcde7945925353790e7adf9693a4183 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 ce0ee973e8cf5cff3402108bb55e7f38982e15bd..e679017b7e5194dbcce89a4d0451e5ac3c5976d2 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 32379f7f1ea78999ec0bc7f0e9a2c6ec4e27cd86..f2c30e618fc564df63f534ed23352071d2cb0309 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 048ff935bf8bffcab861f79c8e3a4b3826b94867..f72f25bd58bfdb04bd908f6b5df77de3c8193017 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 9e9a6e60c90c8115786286582b173e7495dcabb5..b1b4d5da8d5cf4cd336dafd6a4c8863e46d6fc22 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 a45f4cabc5dac0fd77834f9ab6494e38b7a29acb..7a0a643a7edfa13b9289905033304a7db078aefc 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 68b08fb3ca3c69afba950d572163cac2b96b229f..3cde96992ed652443474aa10d7175872985d9688 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 60333956294f51e3143a85d5b46e1f2e3b297df1..7babcd2f4e365d43893ed3a7945905569064c7cc 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 c772e16ca985aedc4e4626d966bff0887217fe23..63a43966b17a8e9a3f307f6aa6f8a4ffd4bcbee5 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 e78f9cf7a4e2e4fac5ac8c628f9080db4cae8adc..0816bc05df5e79a132f3048e839359c05cd1b16a 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 9a51767ef160ecca0da7f176f2a4f21ee640e586..13e5e49b55a613e1aba4957c9dd6d1e20fc3fded 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 24cb3a8c42ba34c60b33f80d58e8f3b1f8c7b657..a29b86fbeac63802c5f4c8e005c7fce0840ca953 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 627b70151202656f5e4e9d5d0c2193eae4174027..d8f282c030972c261a7971cba30b733c9949b3ea 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 0599745d95aab7dcd6e4587feaa4058fb18deeb0..b0125b4c1357a0f82e323e9b0bf3cf8c826fa0a4 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 e374e471e3658006e7b9bea8b28c13ef902c52d4..0730cbc3634a81b96300f043c71b3cf1e5b3e3dc 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 e8b29b2e44e6eff024084143708cd5c2a1324eaf..639df4f846816cb02350de1103c3e062791bb9d4 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 b1af6f10dd862dbc331666cfbf83547a6dededc1..0101e0edf4136fc615762157576e51ed74e366c3 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) {