From be46366689eb2ed26c00b516c17b42d12c12070e Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Tue, 17 Jan 2012 09:12:10 +0100 Subject: [PATCH] Remove QJSValue::instanceOf() function MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Rationale: This is a remnant from QtScript. There is no good reason for providing this type of low-level "prototype inheritance chain" checks in this high-level QJSValue class. If you want to check if an object is of the "right type", you can check if it has the properties you require using property(). Task-number: QTBUG-23604 Change-Id: I3a274212cc57c38228fab73423af481e1b95d8a5 Reviewed-by: Simon Hausmann Reviewed-by: Jędrzej Nowacki --- src/declarative/qml/v8/qjsvalue.cpp | 22 ----------- src/declarative/qml/v8/qjsvalue.h | 2 - src/declarative/qml/v8/qjsvalue_impl_p.h | 31 --------------- src/declarative/qml/v8/qjsvalue_p.h | 2 - tests/auto/declarative/qjsengine/tst_qjsengine.cpp | 1 - tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp | 45 +--------------------- tests/auto/declarative/qjsvalue/tst_qjsvalue.h | 2 - 7 files changed, 2 insertions(+), 103 deletions(-) diff --git a/src/declarative/qml/v8/qjsvalue.cpp b/src/declarative/qml/v8/qjsvalue.cpp index b1e31b4..3a49e94 100644 --- a/src/declarative/qml/v8/qjsvalue.cpp +++ b/src/declarative/qml/v8/qjsvalue.cpp @@ -891,28 +891,6 @@ bool QJSValue::strictlyEquals(const QJSValue& other) const return d_ptr->strictlyEquals(o); } -#ifdef QT_DEPRECATED - -/*! - \obsolete - - Returns true if this QJSValue is an instance of - \a other; otherwise returns false. - - This QJSValue is considered to be an instance of \a other if - \a other is a function and the value of the \c{prototype} - property of \a other is in the prototype chain of this - QJSValue. -*/ -bool QJSValue::instanceOf(const QJSValue &other) const -{ - Q_D(const QJSValue); - QScriptIsolate api(d->engine()); - return d->instanceOf(QJSValuePrivate::get(other)); -} - -#endif // QT_DEPRECATED - /*! Returns the value of this QJSValue's property with the given \a name. If no such property exists, an invalid QJSValue is returned. diff --git a/src/declarative/qml/v8/qjsvalue.h b/src/declarative/qml/v8/qjsvalue.h index 98d04f9..f0be07e 100644 --- a/src/declarative/qml/v8/qjsvalue.h +++ b/src/declarative/qml/v8/qjsvalue.h @@ -142,8 +142,6 @@ public: QT_DEPRECATED qint32 toInt32() const; QT_DEPRECATED quint32 toUInt32() const; - QT_DEPRECATED bool instanceOf(const QJSValue &other) const; - QT_DEPRECATED QJSValue::PropertyFlags propertyFlags(const QString &name) const; QT_DEPRECATED QJSValue call(const QJSValue &thisObject = QJSValue(), diff --git a/src/declarative/qml/v8/qjsvalue_impl_p.h b/src/declarative/qml/v8/qjsvalue_impl_p.h index 304ed34..386b203 100644 --- a/src/declarative/qml/v8/qjsvalue_impl_p.h +++ b/src/declarative/qml/v8/qjsvalue_impl_p.h @@ -603,37 +603,6 @@ inline bool QJSValuePrivate::lessThan(QJSValuePrivate *other) const return nthis < nother; } -inline bool QJSValuePrivate::instanceOf(QJSValuePrivate* other) const -{ - if (!isObject() || !other->isFunction()) - return false; - if (engine() != other->engine()) { - qWarning("QJSValue::instanceof: cannot perform operation on a value created in a different engine"); - return false; - } - v8::HandleScope handleScope; - return instanceOf(v8::Handle::Cast(other->m_value)); -} - -inline bool QJSValuePrivate::instanceOf(v8::Handle other) const -{ - Q_ASSERT(isObject()); - Q_ASSERT(other->IsFunction()); - - v8::Handle self = v8::Handle::Cast(m_value); - v8::Handle selfPrototype = self->GetPrototype(); - v8::Handle otherPrototype = other->Get(v8::String::New("prototype")); - - while (!selfPrototype->IsNull()) { - if (selfPrototype->StrictEquals(otherPrototype)) - return true; - // In general a prototype can be an object or null, but in the loop it can't be null, so - // we can cast it safely. - selfPrototype = v8::Handle::Cast(selfPrototype)->GetPrototype(); - } - return false; -} - inline QScriptPassPointer QJSValuePrivate::prototype() const { if (isObject()) { diff --git a/src/declarative/qml/v8/qjsvalue_p.h b/src/declarative/qml/v8/qjsvalue_p.h index ca349d2..fd32a14 100644 --- a/src/declarative/qml/v8/qjsvalue_p.h +++ b/src/declarative/qml/v8/qjsvalue_p.h @@ -112,8 +112,6 @@ public: inline bool equals(QJSValuePrivate* other); inline bool strictlyEquals(QJSValuePrivate* other); inline bool lessThan(QJSValuePrivate *other) const; - inline bool instanceOf(QJSValuePrivate*) const; - inline bool instanceOf(v8::Handle other) const; inline QScriptPassPointer prototype() const; inline void setPrototype(QJSValuePrivate* prototype); diff --git a/tests/auto/declarative/qjsengine/tst_qjsengine.cpp b/tests/auto/declarative/qjsengine/tst_qjsengine.cpp index b24aad8..2aed67a 100644 --- a/tests/auto/declarative/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/declarative/qjsengine/tst_qjsengine.cpp @@ -4090,7 +4090,6 @@ void tst_QJSEngine::jsNumberClass() QVERIFY(ctor.property("POSITIVE_INFINITY").isNumber()); QCOMPARE(ctor.propertyFlags("POSITIVE_INFINITY"), flags); } - QVERIFY(proto.instanceOf(eng.globalObject().property("Object"))); QCOMPARE(proto.toNumber(), qreal(0)); QVERIFY(proto.property("constructor").strictlyEquals(ctor)); diff --git a/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp b/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp index c7396c9..88be29c 100644 --- a/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp +++ b/tests/auto/declarative/qjsvalue/tst_qjsvalue.cpp @@ -1242,47 +1242,6 @@ void tst_QJSValue::toRegExp() QVERIFY(qjsvalue_cast(eng.undefinedValue()).isEmpty()); } -void tst_QJSValue::instanceOf_twoEngines() -{ - QJSEngine eng; - QJSValue obj = eng.newObject(); - QJSEngine otherEngine; - QTest::ignoreMessage(QtWarningMsg, "QJSValue::instanceof: cannot perform operation on a value created in a different engine"); - QCOMPARE(obj.instanceOf(otherEngine.globalObject().property("Object")), false); -} - -void tst_QJSValue::instanceOf() -{ - QJSEngine eng; - QJSValue obj = eng.newObject(); - QCOMPARE(obj.instanceOf(eng.evaluate("Object.prototype")), false); - QCOMPARE(obj.instanceOf(eng.evaluate("Array.prototype")), false); - QCOMPARE(obj.instanceOf(eng.evaluate("Function.prototype")), false); - QCOMPARE(obj.instanceOf(eng.evaluate("QObject.prototype")), false); - QCOMPARE(obj.instanceOf(QJSValue(&eng, 123)), false); - QCOMPARE(obj.instanceOf(eng.undefinedValue()), false); - QCOMPARE(obj.instanceOf(eng.nullValue()), false); - QCOMPARE(obj.instanceOf(QJSValue()), false); - - QCOMPARE(obj.instanceOf(eng.evaluate("Object")), true); - QCOMPARE(obj.instanceOf(eng.evaluate("Array")), false); - QCOMPARE(obj.instanceOf(eng.evaluate("Function")), false); - QCOMPARE(obj.instanceOf(eng.evaluate("QObject")), false); - - QJSValue arr = eng.newArray(); - QVERIFY(arr.isArray()); - QCOMPARE(arr.instanceOf(eng.evaluate("Object.prototype")), false); - QCOMPARE(arr.instanceOf(eng.evaluate("Array.prototype")), false); - QCOMPARE(arr.instanceOf(eng.evaluate("Function.prototype")), false); - QCOMPARE(arr.instanceOf(eng.evaluate("QObject.prototype")), false); - QCOMPARE(arr.instanceOf(eng.evaluate("Object")), true); - QCOMPARE(arr.instanceOf(eng.evaluate("Array")), true); - QCOMPARE(arr.instanceOf(eng.evaluate("Function")), false); - QCOMPARE(arr.instanceOf(eng.evaluate("QObject")), false); - - QCOMPARE(QJSValue().instanceOf(arr), false); -} - void tst_QJSValue::isArray_data() { newEngine(); @@ -2823,7 +2782,7 @@ void tst_QJSValue::construct_simple() QVERIFY(fun.isCallable()); QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); - QVERIFY(ret.instanceOf(fun)); + QVERIFY(ret.prototype().strictlyEquals(fun.property("prototype"))); QCOMPARE(ret.property("foo").toInt(), 123); } @@ -2835,7 +2794,7 @@ void tst_QJSValue::construct_newObjectJS() QVERIFY(fun.isCallable()); QJSValue ret = fun.callAsConstructor(); QVERIFY(ret.isObject()); - QVERIFY(!ret.instanceOf(fun)); + QVERIFY(!ret.prototype().strictlyEquals(fun.property("prototype"))); QCOMPARE(ret.property("bar").toInt(), 456); } diff --git a/tests/auto/declarative/qjsvalue/tst_qjsvalue.h b/tests/auto/declarative/qjsvalue/tst_qjsvalue.h index b008377..5fcd768 100644 --- a/tests/auto/declarative/qjsvalue/tst_qjsvalue.h +++ b/tests/auto/declarative/qjsvalue/tst_qjsvalue.h @@ -91,8 +91,6 @@ private slots: void toQObject(); void toDateTime(); void toRegExp(); - void instanceOf_twoEngines(); - void instanceOf(); void isArray_data(); void isArray(); void isDate(); -- 2.7.4