occurred, property() returns the value that was thrown (typically
an \c{Error} object).
- \sa setProperty(), propertyFlags(), QJSValueIterator
+ \sa setProperty(), hasProperty(), QJSValueIterator
*/
QJSValue QJSValue::property(const QString& name) const
{
}
/*!
+ Returns true if this object has a property of the given \a name,
+ otherwise returns false.
+
+ \sa property(), hasOwnProperty()
+*/
+bool QJSValue::hasProperty(const QString &name) const
+{
+ Q_D(const QJSValue);
+ QScriptIsolate api(d->engine());
+ return d->hasProperty(name);
+}
+
+/*!
+ Returns true if this object has an own (not prototype-inherited)
+ property of the given \a name, otherwise returns false.
+
+ \sa property(), hasProperty()
+*/
+bool QJSValue::hasOwnProperty(const QString &name) const
+{
+ Q_D(const QJSValue);
+ QScriptIsolate api(d->engine());
+ return d->hasOwnProperty(name);
+}
+
+/*!
Returns the flags of the property with the given \a name.
\sa property()
QJSValue property(const QString &name) const;
void setProperty(const QString &name, const QJSValue &value);
+ bool hasProperty(const QString &name) const;
+ bool hasOwnProperty(const QString &name) const;
+
QJSValue property(quint32 arrayIndex) const;
void setProperty(quint32 arrayIndex, const QJSValue &value);
return self->Delete(QJSConverter::toString(name));
}
+inline bool QJSValuePrivate::hasProperty(const QString &name) const
+{
+ if (!isObject())
+ return false;
+
+ v8::HandleScope handleScope;
+ v8::Handle<v8::Object> self(v8::Handle<v8::Object>::Cast(m_value));
+ return self->Has(QJSConverter::toString(name));
+}
+
+inline bool QJSValuePrivate::hasOwnProperty(const QString &name) const
+{
+ if (!isObject())
+ return false;
+
+ v8::HandleScope handleScope;
+ v8::Handle<v8::Object> self(v8::Handle<v8::Object>::Cast(m_value));
+ return self->HasOwnProperty(QJSConverter::toString(name));
+}
+
inline QJSValue::PropertyFlags QJSValuePrivate::propertyFlags(const QString& name) const
{
if (!isObject())
template<typename T>
inline QScriptPassPointer<QJSValuePrivate> property(T name) const;
inline bool deleteProperty(const QString& name);
+ inline bool hasProperty(const QString &name) const;
+ inline bool hasOwnProperty(const QString &name) const;
inline QJSValue::PropertyFlags propertyFlags(const QString& name) const;
inline QJSValue::PropertyFlags propertyFlags(v8::Handle<v8::String> name) const;
}
#endif
+void tst_QJSValue::hasProperty_basic()
+{
+ QJSEngine eng;
+ QJSValue obj = eng.newObject();
+ QVERIFY(obj.hasProperty("hasOwnProperty")); // inherited from Object.prototype
+ QVERIFY(!obj.hasOwnProperty("hasOwnProperty"));
+
+ QVERIFY(!obj.hasProperty("foo"));
+ QVERIFY(!obj.hasOwnProperty("foo"));
+ obj.setProperty("foo", 123);
+ QVERIFY(obj.hasProperty("foo"));
+ QVERIFY(obj.hasOwnProperty("foo"));
+
+ QVERIFY(!obj.hasProperty("bar"));
+ QVERIFY(!obj.hasOwnProperty("bar"));
+}
+
+void tst_QJSValue::hasProperty_globalObject()
+{
+ QJSEngine eng;
+ QJSValue global = eng.globalObject();
+ QVERIFY(global.hasProperty("Math"));
+ QVERIFY(global.hasOwnProperty("Math"));
+ QVERIFY(!global.hasProperty("NoSuchStandardProperty"));
+ QVERIFY(!global.hasOwnProperty("NoSuchStandardProperty"));
+
+ QVERIFY(!global.hasProperty("foo"));
+ QVERIFY(!global.hasOwnProperty("foo"));
+ global.setProperty("foo", 123);
+ QVERIFY(global.hasProperty("foo"));
+ QVERIFY(global.hasOwnProperty("foo"));
+}
+
+void tst_QJSValue::hasProperty_changePrototype()
+{
+ QJSEngine eng;
+ QJSValue obj = eng.newObject();
+ QJSValue proto = eng.newObject();
+ obj.setPrototype(proto);
+
+ QVERIFY(!obj.hasProperty("foo"));
+ QVERIFY(!obj.hasOwnProperty("foo"));
+ proto.setProperty("foo", 123);
+ QVERIFY(obj.hasProperty("foo"));
+ QVERIFY(!obj.hasOwnProperty("foo"));
+
+ obj.setProperty("foo", 456); // override prototype property
+ QVERIFY(obj.hasProperty("foo"));
+ QVERIFY(obj.hasOwnProperty("foo"));
+}
+
void tst_QJSValue::getSetProperty_HooliganTask162051()
{
QJSEngine eng;
void equals();
void strictlyEquals();
+ void hasProperty_basic();
+ void hasProperty_globalObject();
+ void hasProperty_changePrototype();
+
void getSetPrototype_cyclicPrototype();
void getSetPrototype_evalCyclicPrototype();
void getSetPrototype_eval();