Add QVector support to JS sequences

Change-Id: I731355aa1754721236f3711a65af4f96781cebc0
Task-number: QTBUG-51467
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
This commit is contained in:
Kevin Ottens 2016-05-03 09:57:24 +02:00
parent 42fd8ec249
commit 373c621bf4
5 changed files with 57 additions and 1 deletions

View File

@ -261,6 +261,9 @@ In particular, QML currently supports:
\li \c {QList<bool>}
\li \c {QList<QString>} and \c{QStringList}
\li \c {QList<QUrl>}
\li \c {QVector<int>}
\li \c {QVector<qreal>}
\li \c {QVector<bool>}
\endlist
These sequence types are implemented directly in terms of the underlying C++
@ -303,6 +306,9 @@ The default-constructed values for each sequence type are as follows:
\row \li QList<bool> \li boolean value \c {false}
\row \li QList<QString> and QStringList \li empty QString
\row \li QList<QUrl> \li empty QUrl
\row \li QVector<int> \li integer value 0
\row \li QVector<qreal> \li real value 0.0
\row \li QVector<bool> \li boolean value \c {false}
\endtable
If you wish to remove elements from a sequence rather than simply replace

View File

@ -75,6 +75,9 @@ static void generateWarning(QV4::ExecutionEngine *v4, const QString& description
// F(elementType, elementTypeName, sequenceType, defaultValue)
#define FOREACH_QML_SEQUENCE_TYPE(F) \
F(int, IntVector, QVector<int>, 0) \
F(qreal, RealVector, QVector<qreal>, 0.0) \
F(bool, BoolVector, QVector<bool>, false) \
F(int, Int, QList<int>, 0) \
F(qreal, Real, QList<qreal>, 0.0) \
F(bool, Bool, QList<bool>, false) \
@ -578,6 +581,15 @@ Heap::QQmlSequence<Container>::QQmlSequence(QObject *object, int propertyIndex)
namespace QV4 {
typedef QQmlSequence<QVector<int> > QQmlIntVectorList;
template<>
DEFINE_OBJECT_VTABLE(QQmlIntVectorList);
typedef QQmlSequence<QVector<qreal> > QQmlRealVectorList;
template<>
DEFINE_OBJECT_VTABLE(QQmlRealVectorList);
typedef QQmlSequence<QVector<bool> > QQmlBoolVectorList;
template<>
DEFINE_OBJECT_VTABLE(QQmlBoolVectorList);
typedef QQmlSequence<QStringList> QQmlQStringList;
template<>
DEFINE_OBJECT_VTABLE(QQmlQStringList);

View File

@ -64,6 +64,16 @@ Item {
var actual = msc.reals(realList);
return checkResults(expected, actual, fn);
}
function doIntVectorTest(intList, fn) {
var expected = createExpected(intList, fn);
var actual = msc.integerVector(intList);
return checkResults(expected, actual, fn);
}
function doRealVectorTest(realList, fn) {
var expected = createExpected(realList, fn);
var actual = msc.realVector(realList);
return checkResults(expected, actual, fn);
}
function test_qtbug_25269(useCustomCompare) {
return doStringTest( [ "one", "two", "three" ], null );
@ -92,4 +102,20 @@ Item {
var fn = useCustomCompare ? compareNumbers : null;
return doRealTest( [ -3.4, 1, 10, 4.23, -30.1, 4.24, 4.21, -1, -1, 12, -100, 87.4, 101.3, -8.88888, 7.76, 10.10, 1.1, -1.1, -0, 11, 12.8, 0.001, -11, -0.75, 99999.99, 11.12, 32.3, 3.333333, 9.876 ], fn );
}
function test_number_vector_insertionSort(useCustomCompare) {
var fn = useCustomCompare ? compareNumbers : null;
return doIntVectorTest( [ 7, 3, 9, 1, 0, -1, 20, -11 ], fn );
}
function test_number_vector_quickSort(useCustomCompare) {
var fn = useCustomCompare ? compareNumbers : null;
return doIntVectorTest( [ 7, 3, 37, 9, 1, 0, -1, 20, -11, -300, -87, 1, 3, -2, 100, 108, 96, 9, 99999, 12, 11, 11, 12, 11, 13, -13, 10, 10, 10, 8, 12 ], fn );
}
function test_real_vector_insertionSort(useCustomCompare) {
var fn = useCustomCompare ? compareNumbers : null;
return doRealVectorTest( [ -3.4, 1, 10, 4.23, -30.1, 4.24, 4.21, -1, -1 ], fn );
}
function test_real_vector_quickSort(useCustomCompare) {
var fn = useCustomCompare ? compareNumbers : null;
return doRealVectorTest( [ -3.4, 1, 10, 4.23, -30.1, 4.24, 4.21, -1, -1, 12, -100, 87.4, 101.3, -8.88888, 7.76, 10.10, 1.1, -1.1, -0, 11, 12.8, 0.001, -11, -0.75, 99999.99, 11.12, 32.3, 3.333333, 9.876 ], fn );
}
}

View File

@ -295,6 +295,18 @@ public:
{
return v;
}
Q_INVOKABLE QVector<int> integerVector(QVector<int> v) const
{
return v;
}
Q_INVOKABLE QVector<qreal> realVector(QVector<qreal> v) const
{
return v;
}
Q_INVOKABLE QVector<bool> boolVector(QVector<bool> v) const
{
return v;
}
};
static MyInheritedQmlObject *theSingletonObject = 0;

View File

@ -7336,7 +7336,7 @@ void tst_qqmlecmascript::sequenceSort_data()
QTest::newRow("qtbug_25269") << "test_qtbug_25269" << false;
const char *types[] = { "alphabet", "numbers", "reals" };
const char *types[] = { "alphabet", "numbers", "reals", "number_vector", "real_vector" };
const char *sort[] = { "insertionSort", "quickSort" };
for (size_t t=0 ; t < sizeof(types)/sizeof(types[0]) ; ++t) {