From 13cf7c64acd1652bad90966e06464b35b84e9513 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Tue, 6 Oct 2009 12:52:58 +0200 Subject: implement property getters&setters for setProperty(quint32) overload It's better that this works rather than asserts. Reviewed-by: Olivier Goffart --- src/script/api/qscriptvalue.cpp | 6 ++++-- tests/auto/qscriptvalue/tst_qscriptvalue.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index f2716e4..92c987c 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -1738,7 +1738,7 @@ QScriptValue QScriptValue::property(quint32 arrayIndex, void QScriptValue::setProperty(quint32 arrayIndex, const QScriptValue &value, const PropertyFlags &flags) { - Q_D(const QScriptValue); + Q_D(QScriptValue); if (!d || !d->isObject()) return; if (value.engine() && (value.engine() != engine())) { @@ -1752,7 +1752,9 @@ void QScriptValue::setProperty(quint32 arrayIndex, const QScriptValue &value, JSC::asObject(d->jscValue)->deleteProperty(exec, arrayIndex, /*checkDontDelete=*/false); } else { if ((flags & QScriptValue::PropertyGetter) || (flags & QScriptValue::PropertySetter)) { - Q_ASSERT_X(false, Q_FUNC_INFO, "property getters and setters not implemented"); + // fall back to string-based setProperty(), since there is no + // JSC::JSObject::defineGetter(unsigned) + d->setProperty(JSC::Identifier::from(exec, arrayIndex), value, flags); } else { if (flags != QScriptValue::KeepExistingFlags) { // if (JSC::asObject(d->jscValue)->hasOwnProperty(exec, arrayIndex)) diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp index 6b64e76..5636c54 100644 --- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp +++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp @@ -87,6 +87,7 @@ private slots: void getSetPrototype(); void getSetScope(); void getSetProperty(); + void arrayElementGetterSetter(); void getSetData(); void getSetScriptClass(); void call(); @@ -2115,6 +2116,32 @@ void tst_QScriptValue::getSetProperty() QVERIFY(object.propertyFlags(foo) == 0); } +void tst_QScriptValue::arrayElementGetterSetter() +{ + QScriptEngine eng; + QScriptValue obj = eng.newObject(); + obj.setProperty(1, eng.newFunction(getterSetter), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); + { + QScriptValue num(123); + obj.setProperty("x", num); + QScriptValue ret = obj.property(1); + QVERIFY(ret.isValid()); + QVERIFY(ret.equals(num)); + } + { + QScriptValue num(456); + obj.setProperty(1, num); + QScriptValue ret = obj.property(1); + QVERIFY(ret.isValid()); + QVERIFY(ret.equals(num)); + QVERIFY(ret.equals(obj.property("1"))); + } + QCOMPARE(obj.propertyFlags("1"), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); + + obj.setProperty(1, QScriptValue(), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); + QVERIFY(obj.propertyFlags("1") == 0); +} + void tst_QScriptValue::getSetPrototype() { QScriptEngine eng; -- cgit v0.12