Fix more QJSValue autotest errors
authorLars Knoll <lars.knoll@digia.com>
Fri, 7 Jun 2013 12:31:19 +0000 (14:31 +0200)
committerSimon Hausmann <simon.hausmann@digia.com>
Fri, 7 Jun 2013 17:18:06 +0000 (19:18 +0200)
Fixes to setPrototype and setProperty

Change-Id: I72a5c754e65e4795c1c293525118191f78d91bbe
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
src/qml/qml/v8/qjsvalue.cpp

index 2fb3cc6..d483076 100644 (file)
@@ -622,12 +622,15 @@ QJSValue QJSValue::prototype() const
     Object *o = d->value.asObject();
     if (!o)
         return QJSValue();
+    if (!o->prototype)
+        return QJSValue(NullValue);
     return new QJSValuePrivate(o->internalClass->engine, Value::fromObject(o->prototype));
 }
 
 /*!
   If this QJSValue is an object, sets the internal prototype
   (\c{__proto__} property) of this object to be \a prototype;
+  if the QJSValue is null, it sets the prototype to null;
   otherwise does nothing.
 
   The internal prototype should not be confused with the public
@@ -641,9 +644,26 @@ void QJSValue::setPrototype(const QJSValue& prototype)
     Object *o = d->value.asObject();
     if (!o)
         return;
+    if (prototype.d->value.isNull()) {
+        o->prototype = 0;
+        return;
+    }
+
     Object *p = prototype.d->value.asObject();
     if (!p)
         return;
+    if (o->engine() != p->engine()) {
+        qWarning("QJSValue::setPrototype() failed: cannot set a prototype created in a different engine");
+        return;
+    }
+    Object *pp = p;
+    while (pp) {
+        if (pp == o) {
+            qWarning("QJSValue::setPrototype() failed: cyclic prototype value");
+            return;
+        }
+        pp = pp->prototype;
+    }
     o->prototype = p;
 }
 
@@ -801,6 +821,12 @@ void QJSValue::setProperty(const QString& name, const QJSValue& value)
     if (!o)
         return;
 
+    ExecutionEngine *otherEngine = value.d->engine();
+    if (otherEngine && otherEngine != o->engine()) {
+        qWarning("QJSValue::setProperty(%s) failed: cannot set value created in a different engine", name.toUtf8().constData());
+        return;
+    }
+
     ExecutionEngine *engine = d->engine();
     String *s = engine->newString(name);
     uint idx = s->asArrayIndex();