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.
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<v8::Object>::Cast(other->m_value));
-}
-
-inline bool QJSValuePrivate::instanceOf(v8::Handle<v8::Object> other) const
-{
- Q_ASSERT(isObject());
- Q_ASSERT(other->IsFunction());
-
- v8::Handle<v8::Object> self = v8::Handle<v8::Object>::Cast(m_value);
- v8::Handle<v8::Value> selfPrototype = self->GetPrototype();
- v8::Handle<v8::Value> 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<v8::Object>::Cast(selfPrototype)->GetPrototype();
- }
- return false;
-}
-
inline QScriptPassPointer<QJSValuePrivate> QJSValuePrivate::prototype() const
{
if (isObject()) {
QVERIFY(qjsvalue_cast<QRegExp>(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();
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);
}
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);
}