Get rid of Value::engine()
authorLars Knoll <lars.knoll@theqtcompany.com>
Sat, 10 Jan 2015 21:04:54 +0000 (22:04 +0100)
committerSimon Hausmann <simon.hausmann@digia.com>
Mon, 12 Jan 2015 13:52:50 +0000 (14:52 +0100)
This method is not guaranteed to return an engine. We're safer
checking for the value being an object first and then getting
the engine from there.

Change-Id: I5c95e675337e545f2421613bd31c42d1e58d6f9a
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
src/qml/jsapi/qjsvalue.cpp
src/qml/jsruntime/qv4include.cpp
src/qml/jsruntime/qv4jsonobject.cpp
src/qml/jsruntime/qv4objectiterator.cpp
src/qml/jsruntime/qv4qobjectwrapper.cpp
src/qml/jsruntime/qv4sequenceobject.cpp
src/qml/jsruntime/qv4value_inl_p.h
src/qml/jsruntime/qv4value_p.h
src/qml/qml/qqmlcontextwrapper.cpp
src/qml/types/qqmldelegatemodel.cpp

index 672cb23..d888ea3 100644 (file)
@@ -516,8 +516,8 @@ QVariant QJSValue::toVariant() const
     if (d->value.isEmpty())
         return d->unboundData;
 
-    if (d->value.asObject())
-        return d->value.engine()->toVariant(d->value, /*typeHint*/ -1, /*createJSValueForObjects*/ false);
+    if (Object *o = d->value.asObject())
+        return o->engine()->toVariant(d->value, /*typeHint*/ -1, /*createJSValueForObjects*/ false);
 
     if (d->value.isString())
         return QVariant(d->value.stringValue()->toQString());
index 03eb150..2627e26 100644 (file)
@@ -92,9 +92,9 @@ QV4::ReturnedValue QV4Include::resultValue(QV4::ExecutionEngine *v4, Status stat
 
 void QV4Include::callback(const QV4::ValueRef callback, const QV4::ValueRef status)
 {
-    QV4::ExecutionEngine *v4 = callback->engine();
-    if (!v4)
+    if (!callback->isObject())
         return;
+    QV4::ExecutionEngine *v4 = callback->asObject()->engine();
     QV4::Scope scope(v4);
     QV4::ScopedFunctionObject f(scope, callback);
     if (!f)
index f320630..0de2aa7 100644 (file)
@@ -984,9 +984,11 @@ QJsonValue JsonObject::toJsonValue(const ValueRef value,
         return QJsonValue(QJsonValue::Null);
     else if (value->isUndefined())
         return QJsonValue(QJsonValue::Undefined);
+    else if (value->isString())
+        return QJsonValue(value->toQString());
 
-    Q_ASSERT(value->engine());
-    Scope scope(value->engine());
+    Q_ASSERT(value->isObject());
+    Scope scope(value->asObject()->engine());
     ScopedArrayObject a(scope, value);
     if (a)
         return toJsonArray(a, visitedObjects);
index f0970d1..2ce5c98 100644 (file)
@@ -73,7 +73,7 @@ void ObjectIterator::init(Object *o)
 #endif
 
     if (object->as<ArgumentsObject>()) {
-        Scope scope(object->engine());
+        Scope scope(engine);
         Scoped<ArgumentsObject> (scope, object->asReturnedValue())->fullyCreate();
     }
 }
@@ -136,7 +136,7 @@ ReturnedValue ObjectIterator::nextPropertyName(ValueRef value)
 
     PropertyAttributes attrs;
     uint index;
-    Scope scope(object->engine());
+    Scope scope(engine);
     ScopedProperty p(scope);
     ScopedString name(scope);
     next(name.getRef(), &index, p, &attrs);
@@ -158,7 +158,7 @@ ReturnedValue ObjectIterator::nextPropertyNameAsString(ValueRef value)
 
     PropertyAttributes attrs;
     uint index;
-    Scope scope(object->engine());
+    Scope scope(engine);
     ScopedProperty p(scope);
     ScopedString name(scope);
     next(name.getRef(), &index, p, &attrs);
@@ -170,7 +170,7 @@ ReturnedValue ObjectIterator::nextPropertyNameAsString(ValueRef value)
     if (!!name)
         return name->asReturnedValue();
     assert(index < UINT_MAX);
-    return Encode(object->engine()->newString(QString::number(index)));
+    return Encode(engine->newString(QString::number(index)));
 }
 
 ReturnedValue ObjectIterator::nextPropertyNameAsString()
@@ -180,7 +180,7 @@ ReturnedValue ObjectIterator::nextPropertyNameAsString()
 
     PropertyAttributes attrs;
     uint index;
-    Scope scope(object->engine());
+    Scope scope(engine);
     ScopedProperty p(scope);
     ScopedString name(scope);
     next(name.getRef(), &index, p, &attrs);
@@ -190,7 +190,7 @@ ReturnedValue ObjectIterator::nextPropertyNameAsString()
     if (!!name)
         return name->asReturnedValue();
     Q_ASSERT(index < UINT_MAX);
-    return Encode(object->engine()->newString(QString::number(index)));
+    return Encode(engine->newString(QString::number(index)));
 }
 
 
index 3f3e499..284d20d 100644 (file)
@@ -96,8 +96,8 @@ static QPair<QObject *, int> extractQtMethod(QV4::FunctionObject *function)
 
 static QPair<QObject *, int> extractQtSignal(const ValueRef value)
 {
-    QV4::ExecutionEngine *v4 = value->engine();
-    if (v4) {
+    if (value->isObject()) {
+        QV4::ExecutionEngine *v4 = value->asObject()->engine();
         QV4::Scope scope(v4);
         QV4::ScopedFunctionObject function(scope, value);
         if (function)
index a60a446..8c6d283 100644 (file)
@@ -650,7 +650,7 @@ QVariant SequencePrototype::toVariant(const QV4::ValueRef array, int typeHint, b
         *succeeded = false;
         return QVariant();
     }
-    QV4::Scope scope(array->engine());
+    QV4::Scope scope(array->asObject()->engine());
     QV4::ScopedArrayObject a(scope, array);
 
     FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_TO_VARIANT) { /* else */ *succeeded = false; return QVariant(); }
index ea69c26..a551ac7 100644 (file)
@@ -70,12 +70,6 @@ inline String *Value::asString() const
     return 0;
 }
 
-inline ExecutionEngine *Value::engine() const
-{
-    Managed *m = asManaged();
-    return m ? m->engine() : 0;
-}
-
 inline void Value::mark(ExecutionEngine *e) const
 {
     if (!val)
index 526cb01..8fff22e 100644 (file)
@@ -379,8 +379,6 @@ struct Q_QML_PRIVATE_EXPORT Value
     inline uint asArrayIndex() const;
     inline uint asArrayLength(bool *ok) const;
 
-    inline ExecutionEngine *engine() const;
-
     ReturnedValue asReturnedValue() const { return val; }
     static Value fromReturnedValue(ReturnedValue val) { Value v; v.val = val; return v; }
 
index f3551d8..78a77f7 100644 (file)
@@ -102,10 +102,10 @@ QQmlContextData *QmlContextWrapper::callingContext(ExecutionEngine *v4)
 
 QQmlContextData *QmlContextWrapper::getContext(const ValueRef value)
 {
-    QV4::ExecutionEngine *v4 = value->engine();
-    if (!v4)
+    if (!value->isObject())
         return 0;
 
+    QV4::ExecutionEngine *v4 = value->asObject()->engine();
     Scope scope(v4);
     QV4::Scoped<QmlContextWrapper> c(scope, value);
 
@@ -114,9 +114,9 @@ QQmlContextData *QmlContextWrapper::getContext(const ValueRef value)
 
 void QmlContextWrapper::takeContextOwnership(const ValueRef qmlglobal)
 {
-    QV4::ExecutionEngine *v4 = qmlglobal->engine();
-    Q_ASSERT(v4);
+    Q_ASSERT(qmlglobal->isObject());
 
+    QV4::ExecutionEngine *v4 = qmlglobal->asObject()->engine();
     Scope scope(v4);
     QV4::Scoped<QmlContextWrapper> c(scope, qmlglobal);
     Q_ASSERT(c);
index 0f4d115..88cb086 100644 (file)
@@ -1630,10 +1630,10 @@ bool QQmlDelegateModelPrivate::insert(Compositor::insert_iterator &before, const
     QQmlDelegateModelItem *cacheItem = m_adaptorModel.createItem(m_cacheMetaType, m_context->engine(), -1);
     if (!cacheItem)
         return false;
-    QV4::ExecutionEngine *v4 = object->engine();
-    if (!v4)
+    if (!object->isObject())
         return false;
 
+    QV4::ExecutionEngine *v4 = object->asObject()->engine();
     QV4::Scope scope(v4);
     QV4::ScopedObject o(scope, object);
     if (!o)
@@ -2502,10 +2502,10 @@ bool QQmlDelegateModelGroupPrivate::parseIndex(const QV4::ValueRef value, int *i
         return true;
     }
 
-    QV4::ExecutionEngine *v4 = value->engine();
-    if (!v4)
+    if (!value->isObject())
         return false;
 
+    QV4::ExecutionEngine *v4 = value->asObject()->engine();
     QV4::Scope scope(v4);
     QV4::Scoped<QQmlDelegateModelItemObject> object(scope, value);