diff options
author | Jędrzej Nowacki <jedrzej.nowacki@qt.io> | 2018-09-24 15:05:13 +0200 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@qt.io> | 2018-10-08 10:01:54 +0000 |
commit | 5f4b3fa3cade416b088fdc35f583c536269688d7 (patch) | |
tree | 0e3971e29e106d50922a42d8c321756547b5eeba /src/qml/jsruntime | |
parent | 55403f8825396b074f0785b400330033387ffdcc (diff) |
Add support for QSequentialIterable in QML
That means that now QML understand many sequential value types
and it is able to convert it to a JS array.
Fixes: QTBUG-60338
Change-Id: I46943b7c130296bb64c23008ce2e52a55d7f019d
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime')
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 8f64c84eae..f90a693265 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -1540,6 +1540,22 @@ static QV4::ReturnedValue variantListToJS(QV4::ExecutionEngine *v4, const QVaria return a.asReturnedValue(); } +// Converts a QSequentialIterable to JS. +// The result is a new Array object with length equal to the length +// of the QSequentialIterable, and the elements being the QSequentialIterable's +// elements converted to JS, recursively. +static QV4::ReturnedValue sequentialIterableToJS(QV4::ExecutionEngine *v4, const QSequentialIterable &lst) +{ + QV4::Scope scope(v4); + QV4::ScopedArrayObject a(scope, v4->newArrayObject()); + a->arrayReserve(lst.size()); + QV4::ScopedValue v(scope); + for (int i = 0; i < lst.size(); i++) + a->arrayPut(i, (v = variantToJS(v4, lst.at(i)))); + a->setArrayLengthUnchecked(lst.size()); + return a.asReturnedValue(); +} + // Converts a QVariantMap to JS. // The result is a new Object object with property names being // the keys of the QVariantMap, and values being the values of @@ -1648,6 +1664,11 @@ QV4::ReturnedValue ExecutionEngine::metaTypeToJS(int type, const void *data) Q_ASSERT(mt.metaObject()); return QV4::QQmlValueTypeWrapper::create(this, QVariant(type, data), mt.metaObject(), type); } + if (QMetaType::hasRegisteredConverterFunction(type, qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) { + auto v = QVariant(type, data); + QSequentialIterable lst = v.value<QSequentialIterable>(); + return sequentialIterableToJS(this, lst); + } // Fall back to wrapping in a QVariant. return QV4::Encode(newVariantObject(QVariant(type, data))); } |