diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2018-04-16 13:53:03 +0200 |
---|---|---|
committer | Aapo Keskimolo <aapo.keskimolo@qt.io> | 2018-04-16 17:52:10 +0000 |
commit | 8e2cfa1d77dd4568a126f5ed5736dfef844a28ef (patch) | |
tree | 07c5c76c95bbca05d4afd3e52da130b635aa30c9 | |
parent | 539d724890aec3f00eadb39aeae973670e664ec6 (diff) |
Fix JSON.stringify with sequence types
Stringify::JA takes an ArrayObject* but it merely gets the length
property and does indexed get calls. Those work also on array-like
objects such as our sequence wrappers.
Task-number: QTBUG-45018
Change-Id: I4ec4f89a2e09c918fbc2ff1d48ae5915e67ce280
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/qml/jsruntime/qv4jsonobject.cpp | 8 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/sequenceConversion.array.qml | 14 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 13 |
3 files changed, 25 insertions, 10 deletions
diff --git a/src/qml/jsruntime/qv4jsonobject.cpp b/src/qml/jsruntime/qv4jsonobject.cpp index 99666806be..c3569c29d2 100644 --- a/src/qml/jsruntime/qv4jsonobject.cpp +++ b/src/qml/jsruntime/qv4jsonobject.cpp @@ -637,7 +637,7 @@ struct Stringify Stringify(ExecutionEngine *e) : v4(e), replacerFunction(nullptr), propertyList(nullptr), propertyListSize(0) {} QString Str(const QString &key, const Value &v); - QString JA(ArrayObject *a); + QString JA(Object *a); QString JO(Object *o); QString makeMember(const QString &key, const Value &v); @@ -743,8 +743,8 @@ QString Stringify::Str(const QString &key, const Value &v) o = value->asReturnedValue(); if (o) { if (!o->as<FunctionObject>()) { - if (o->as<ArrayObject>()) { - return JA(static_cast<ArrayObject *>(o.getPointer())); + if (o->as<ArrayObject>() || o->isListType()) { + return JA(o.getPointer()); } else { return JO(o); } @@ -827,7 +827,7 @@ QString Stringify::JO(Object *o) return result; } -QString Stringify::JA(ArrayObject *a) +QString Stringify::JA(Object *a) { if (stackContains(a)) { v4->throwTypeError(); diff --git a/tests/auto/qml/qqmlecmascript/data/sequenceConversion.array.qml b/tests/auto/qml/qqmlecmascript/data/sequenceConversion.array.qml index 5103168fd3..99c49ebf62 100644 --- a/tests/auto/qml/qqmlecmascript/data/sequenceConversion.array.qml +++ b/tests/auto/qml/qqmlecmascript/data/sequenceConversion.array.qml @@ -10,6 +10,11 @@ Item { objectName: "msco" } + Component { + id: mscoComponent + MySequenceConversionObject { } + } + property bool success: false property variant intList @@ -252,4 +257,13 @@ Item { if (testSequence.valueOf() == prevValueOf) referenceDeletion = false; if (testSequence.length == prevLength) referenceDeletion = false; } + + function jsonConversion() { + success = true + var msco = mscoComponent.createObject() + if (JSON.stringify(msco.intListProperty) != "[1,2,3,4]") success = false; + if (JSON.stringify(msco.qrealListProperty) != "[1.1,2.2,3.3,4.4]") success = false; + if (JSON.stringify(msco.boolListProperty) != "[true,false,true,false]") success = false; + if (JSON.stringify(msco.stringListProperty) != "[\"first\",\"second\",\"third\",\"fourth\"]") success = false; + } } diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index e0b8127dfb..c0cf123243 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -5562,17 +5562,18 @@ void tst_qqmlecmascript::sequenceConversionArray() // ensure that in JS the returned sequences act just like normal JS Arrays. QUrl qmlFile = testFileUrl("sequenceConversion.array.qml"); QQmlComponent component(&engine, qmlFile); - QObject *object = component.create(); + QScopedPointer<QObject> object(component.create()); QVERIFY(object != nullptr); - QMetaObject::invokeMethod(object, "indexedAccess"); + QMetaObject::invokeMethod(object.data(), "indexedAccess"); QVERIFY(object->property("success").toBool()); - QMetaObject::invokeMethod(object, "arrayOperations"); + QMetaObject::invokeMethod(object.data(), "arrayOperations"); QVERIFY(object->property("success").toBool()); - QMetaObject::invokeMethod(object, "testEqualitySemantics"); + QMetaObject::invokeMethod(object.data(), "testEqualitySemantics"); QVERIFY(object->property("success").toBool()); - QMetaObject::invokeMethod(object, "testReferenceDeletion"); + QMetaObject::invokeMethod(object.data(), "testReferenceDeletion"); QCOMPARE(object->property("referenceDeletion").toBool(), true); - delete object; + QMetaObject::invokeMethod(object.data(), "jsonConversion"); + QVERIFY(object->property("success").toBool()); } |