Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2012-02-16 04:43:03 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-20 05:21:40 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 09:38:36 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-20 05:21:40 +00:00
|
|
|
**
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-20 05:21:40 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-19 09:38:36 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-02-16 04:43:03 +00:00
|
|
|
#include <QtQml/qqml.h>
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
#include "qv4sequenceobject_p.h"
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2013-05-08 05:32:45 +00:00
|
|
|
#include <private/qv4functionobject_p.h>
|
2013-05-21 14:31:05 +00:00
|
|
|
#include <private/qv4arrayobject_p.h>
|
|
|
|
#include <private/qqmlengine_p.h>
|
2013-09-05 11:22:23 +00:00
|
|
|
#include <private/qv4scopedvalue_p.h>
|
2017-09-01 08:09:45 +00:00
|
|
|
#include <private/qv4jscall_p.h>
|
2014-07-25 15:44:14 +00:00
|
|
|
#include "qv4runtime_p.h"
|
|
|
|
#include "qv4objectiterator_p.h"
|
2015-03-03 17:36:51 +00:00
|
|
|
#include <private/qqmlvaluetypewrapper_p.h>
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
2015-03-03 17:36:51 +00:00
|
|
|
#include <private/qqmlmodelindexvaluetype_p.h>
|
|
|
|
#include <QtCore/qabstractitemmodel.h>
|
2019-07-04 21:54:38 +00:00
|
|
|
#endif
|
2013-05-08 05:32:45 +00:00
|
|
|
|
2013-09-12 09:06:59 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
using namespace QV4;
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
// helper function to generate valid warnings if errors occur during sequence operations.
|
2014-11-13 11:14:09 +00:00
|
|
|
static void generateWarning(QV4::ExecutionEngine *v4, const QString& description)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-12-31 15:03:34 +00:00
|
|
|
QQmlEngine *engine = v4->qmlEngine();
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!engine)
|
|
|
|
return;
|
|
|
|
QQmlError retn;
|
|
|
|
retn.setDescription(description);
|
2013-06-07 18:49:28 +00:00
|
|
|
|
2017-08-09 15:14:11 +00:00
|
|
|
QV4::CppStackFrame *stackFrame = v4->currentStackFrame;
|
2013-06-07 18:49:28 +00:00
|
|
|
|
2017-08-21 08:59:53 +00:00
|
|
|
retn.setLine(stackFrame->lineNumber());
|
2017-08-03 11:41:13 +00:00
|
|
|
retn.setUrl(QUrl(stackFrame->source()));
|
2013-05-21 14:31:05 +00:00
|
|
|
QQmlEnginePrivate::warning(engine, retn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// F(elementType, elementTypeName, sequenceType, defaultValue)
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
|
|
|
#define FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F) \
|
|
|
|
F(QModelIndex, QModelIndex, QModelIndexList, QModelIndex()) \
|
|
|
|
F(QModelIndex, QModelIndexStdVector, std::vector<QModelIndex>, QModelIndex()) \
|
|
|
|
F(QItemSelectionRange, QItemSelectionRange, QItemSelection, QItemSelectionRange())
|
|
|
|
#else
|
|
|
|
#define FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F)
|
|
|
|
#endif
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
#define FOREACH_QML_SEQUENCE_TYPE(F) \
|
2017-04-18 13:55:20 +00:00
|
|
|
F(int, IntStdVector, std::vector<int>, 0) \
|
|
|
|
F(qreal, RealStdVector, std::vector<qreal>, 0.0) \
|
|
|
|
F(bool, BoolStdVector, std::vector<bool>, false) \
|
2013-05-21 14:31:05 +00:00
|
|
|
F(int, Int, QList<int>, 0) \
|
|
|
|
F(qreal, Real, QList<qreal>, 0.0) \
|
|
|
|
F(bool, Bool, QList<bool>, false) \
|
|
|
|
F(QString, String, QList<QString>, QString()) \
|
|
|
|
F(QString, QString, QStringList, QString()) \
|
2017-04-18 13:55:20 +00:00
|
|
|
F(QString, StringStdVector, std::vector<QString>, QString()) \
|
2015-03-03 17:36:51 +00:00
|
|
|
F(QUrl, Url, QList<QUrl>, QUrl()) \
|
2017-04-18 13:55:20 +00:00
|
|
|
F(QUrl, UrlStdVector, std::vector<QUrl>, QUrl()) \
|
2019-07-04 21:54:38 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F)
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QString &element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-26 20:07:27 +00:00
|
|
|
return engine->newString(element)->asReturnedValue();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, int element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-26 20:07:27 +00:00
|
|
|
return QV4::Encode(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QUrl &element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-26 20:07:27 +00:00
|
|
|
return engine->newString(element.toString())->asReturnedValue();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
2015-03-03 17:36:51 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QModelIndex &element)
|
|
|
|
{
|
|
|
|
const QMetaObject *vtmo = QQmlValueTypeFactory::metaObjectForMetaType(QMetaType::QModelIndex);
|
|
|
|
return QV4::QQmlValueTypeWrapper::create(engine, QVariant(element), vtmo, QMetaType::QModelIndex);
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:27:22 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QItemSelectionRange &element)
|
|
|
|
{
|
|
|
|
int metaTypeId = qMetaTypeId<QItemSelectionRange>();
|
|
|
|
const QMetaObject *vtmo = QQmlValueTypeFactory::metaObjectForMetaType(metaTypeId);
|
|
|
|
return QV4::QQmlValueTypeWrapper::create(engine, QVariant::fromValue(element), vtmo, metaTypeId);
|
|
|
|
}
|
2019-07-04 21:54:38 +00:00
|
|
|
#endif
|
2015-03-06 18:27:22 +00:00
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, qreal element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-26 20:07:27 +00:00
|
|
|
return QV4::Encode(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:07:27 +00:00
|
|
|
static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, bool element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-26 20:07:27 +00:00
|
|
|
return QV4::Encode(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(const QString &element)
|
|
|
|
{
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(int element)
|
|
|
|
{
|
|
|
|
return QString::number(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(const QUrl &element)
|
|
|
|
{
|
|
|
|
return element.toString();
|
|
|
|
}
|
|
|
|
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
2015-03-03 17:36:51 +00:00
|
|
|
static QString convertElementToString(const QModelIndex &element)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const QQmlModelIndexValueType *>(&element)->toString();
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:27:22 +00:00
|
|
|
static QString convertElementToString(const QItemSelectionRange &element)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const QQmlItemSelectionRangeValueType *>(&element)->toString();
|
|
|
|
}
|
2019-07-04 21:54:38 +00:00
|
|
|
#endif
|
2015-03-06 18:27:22 +00:00
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
static QString convertElementToString(qreal element)
|
|
|
|
{
|
|
|
|
QString qstr;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&qstr, element, 10);
|
2013-05-21 14:31:05 +00:00
|
|
|
return qstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(bool element)
|
|
|
|
{
|
|
|
|
if (element)
|
|
|
|
return QStringLiteral("true");
|
|
|
|
else
|
|
|
|
return QStringLiteral("false");
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <typename ElementType> ElementType convertValueToElement(const Value &value);
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <> QString convertValueToElement(const Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
return value.toQString();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <> int convertValueToElement(const Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
return value.toInt32();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <> QUrl convertValueToElement(const Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
return QUrl(value.toQString());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
2015-03-03 17:36:51 +00:00
|
|
|
template <> QModelIndex convertValueToElement(const Value &value)
|
|
|
|
{
|
2015-03-24 21:09:49 +00:00
|
|
|
const QQmlValueTypeWrapper *v = value.as<QQmlValueTypeWrapper>();
|
2015-03-03 17:36:51 +00:00
|
|
|
if (v)
|
|
|
|
return v->toVariant().toModelIndex();
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2015-03-06 18:27:22 +00:00
|
|
|
template <> QItemSelectionRange convertValueToElement(const Value &value)
|
|
|
|
{
|
2015-03-24 21:09:49 +00:00
|
|
|
const QQmlValueTypeWrapper *v = value.as<QQmlValueTypeWrapper>();
|
2015-03-06 18:27:22 +00:00
|
|
|
if (v)
|
|
|
|
return v->toVariant().value<QItemSelectionRange>();
|
|
|
|
return QItemSelectionRange();
|
|
|
|
}
|
2019-07-04 21:54:38 +00:00
|
|
|
#endif
|
2015-03-06 18:27:22 +00:00
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <> qreal convertValueToElement(const Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
return value.toNumber();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
template <> bool convertValueToElement(const Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
return value.toBoolean();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 01:06:42 +00:00
|
|
|
namespace QV4 {
|
|
|
|
|
|
|
|
template <typename Container> struct QQmlSequence;
|
|
|
|
|
|
|
|
namespace Heap {
|
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
struct QQmlSequence : Object {
|
2016-09-09 13:37:57 +00:00
|
|
|
void init(const Container &container);
|
2018-04-18 08:18:48 +00:00
|
|
|
void init(QObject *object, int propertyIndex, bool readOnly);
|
2016-09-09 08:06:31 +00:00
|
|
|
void destroy() {
|
|
|
|
delete container;
|
|
|
|
object.destroy();
|
2016-09-09 14:20:57 +00:00
|
|
|
Object::destroy();
|
2016-09-09 08:06:31 +00:00
|
|
|
}
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2016-09-09 08:06:31 +00:00
|
|
|
mutable Container *container;
|
2016-09-07 11:31:14 +00:00
|
|
|
QQmlQPointer<QObject> object;
|
2014-11-07 01:06:42 +00:00
|
|
|
int propertyIndex;
|
2018-04-18 08:18:48 +00:00
|
|
|
bool isReference : 1;
|
|
|
|
bool isReadOnly : 1;
|
2014-11-07 01:06:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
template <typename Container>
|
2014-05-09 13:23:18 +00:00
|
|
|
struct QQmlSequence : public QV4::Object
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-11-07 01:06:42 +00:00
|
|
|
V4_OBJECT2(QQmlSequence<Container>, QV4::Object)
|
2013-12-06 09:28:50 +00:00
|
|
|
Q_MANAGED_TYPE(QmlSequence)
|
2015-08-28 11:33:10 +00:00
|
|
|
V4_PROTOTYPE(sequencePrototype)
|
2014-11-13 20:38:25 +00:00
|
|
|
V4_NEEDS_DESTROY
|
2013-05-21 14:31:05 +00:00
|
|
|
public:
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2013-09-18 10:31:55 +00:00
|
|
|
void init()
|
2013-06-13 11:43:09 +00:00
|
|
|
{
|
2013-09-18 10:31:55 +00:00
|
|
|
defineAccessorProperty(QStringLiteral("length"), method_get_length, method_set_length);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 09:42:01 +00:00
|
|
|
QV4::ReturnedValue containerGetIndexed(uint index, bool *hasProperty) const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(engine(), QLatin1String("Index out of range during indexed get"));
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object) {
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
loadReference();
|
|
|
|
}
|
2017-04-18 13:55:20 +00:00
|
|
|
if (index < size_t(d()->container->size())) {
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2017-04-18 13:55:20 +00:00
|
|
|
return convertElementToValue(engine(), qAsConst(*(d()->container))[index]);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 21:57:11 +00:00
|
|
|
bool containerPutIndexed(uint index, const QV4::Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-04-05 18:23:20 +00:00
|
|
|
if (internalClass()->engine->hasException)
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-10-22 11:26:08 +00:00
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(engine(), QLatin1String("Index out of range during indexed set"));
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:49:18 +00:00
|
|
|
if (d()->isReadOnly) {
|
|
|
|
engine()->throwTypeError(QLatin1String("Cannot insert into a readonly container"));
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2019-12-02 15:49:18 +00:00
|
|
|
}
|
2018-04-18 08:18:48 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2017-04-18 13:55:20 +00:00
|
|
|
size_t count = size_t(d()->container->size());
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
typename Container::value_type element = convertValueToElement<typename Container::value_type>(value);
|
|
|
|
|
2017-04-18 13:55:20 +00:00
|
|
|
if (index == count) {
|
|
|
|
d()->container->push_back(element);
|
|
|
|
} else if (index < count) {
|
|
|
|
(*d()->container)[index] = element;
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* the value at the given index, increasing length to index+1. */
|
2017-04-18 13:55:20 +00:00
|
|
|
d()->container->reserve(index + 1);
|
|
|
|
while (index > count++) {
|
|
|
|
d()->container->push_back(typename Container::value_type());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2017-04-18 13:55:20 +00:00
|
|
|
d()->container->push_back(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference)
|
2013-05-21 14:31:05 +00:00
|
|
|
storeReference();
|
2017-02-08 21:57:11 +00:00
|
|
|
return true;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 14:29:53 +00:00
|
|
|
QV4::PropertyAttributes containerQueryIndexed(uint index) const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(engine(), QLatin1String("Index out of range during indexed query"));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
}
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2013-05-21 14:31:05 +00:00
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
loadReference();
|
|
|
|
}
|
2017-04-18 13:55:20 +00:00
|
|
|
return (index < size_t(d()->container->size())) ? QV4::Attr_Data : QV4::Attr_Invalid;
|
2013-06-10 15:11:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:44:12 +00:00
|
|
|
struct OwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator
|
|
|
|
{
|
|
|
|
~OwnPropertyKeyIterator() override = default;
|
|
|
|
PropertyKey next(const Object *o, Property *pd = nullptr, PropertyAttributes *attrs = nullptr) override
|
|
|
|
{
|
|
|
|
const QQmlSequence *s = static_cast<const QQmlSequence *>(o);
|
|
|
|
|
|
|
|
if (s->d()->isReference) {
|
|
|
|
if (!s->d()->object)
|
|
|
|
return ObjectOwnPropertyKeyIterator::next(o, pd, attrs);
|
|
|
|
s->loadReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arrayIndex < static_cast<uint>(s->d()->container->size())) {
|
|
|
|
uint index = arrayIndex;
|
|
|
|
++arrayIndex;
|
|
|
|
if (attrs)
|
|
|
|
*attrs = QV4::Attr_Data;
|
|
|
|
if (pd)
|
2019-12-06 17:49:39 +00:00
|
|
|
pd->value = convertElementToValue(s->engine(), (*s->d()->container)[index]);
|
2018-08-02 13:44:12 +00:00
|
|
|
return PropertyKey::fromArrayIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjectOwnPropertyKeyIterator::next(o, pd, attrs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-08 20:29:47 +00:00
|
|
|
static OwnPropertyKeyIterator *containerOwnPropertyKeys(const Object *m, Value *target)
|
2018-08-02 13:44:12 +00:00
|
|
|
{
|
2018-09-08 20:29:47 +00:00
|
|
|
*target = *m;
|
2018-08-02 13:44:12 +00:00
|
|
|
return new OwnPropertyKeyIterator;
|
|
|
|
}
|
|
|
|
|
2013-06-21 18:33:39 +00:00
|
|
|
bool containerDeleteIndexedProperty(uint index)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX)
|
|
|
|
return false;
|
2018-04-18 08:18:48 +00:00
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2013-05-21 14:31:05 +00:00
|
|
|
return false;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2017-04-18 13:55:20 +00:00
|
|
|
if (index >= size_t(d()->container->size()))
|
2013-05-21 14:31:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* according to ECMA262r3 it should be Undefined, */
|
|
|
|
/* but we cannot, so we insert a default-value instead. */
|
2017-04-18 13:55:20 +00:00
|
|
|
(*d()->container)[index] = typename Container::value_type();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference)
|
2013-05-21 14:31:05 +00:00
|
|
|
storeReference();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-07 20:09:28 +00:00
|
|
|
bool containerIsEqualTo(Managed *other)
|
|
|
|
{
|
2014-10-30 21:30:01 +00:00
|
|
|
if (!other)
|
|
|
|
return false;
|
2013-06-07 20:09:28 +00:00
|
|
|
QQmlSequence<Container> *otherSequence = other->as<QQmlSequence<Container> >();
|
|
|
|
if (!otherSequence)
|
|
|
|
return false;
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference && otherSequence->d()->isReference) {
|
|
|
|
return d()->object == otherSequence->d()->object && d()->propertyIndex == otherSequence->d()->propertyIndex;
|
|
|
|
} else if (!d()->isReference && !otherSequence->d()->isReference) {
|
2013-06-07 20:09:28 +00:00
|
|
|
return this == otherSequence;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-24 15:00:28 +00:00
|
|
|
struct DefaultCompareFunctor
|
|
|
|
{
|
|
|
|
bool operator()(typename Container::value_type lhs, typename Container::value_type rhs)
|
|
|
|
{
|
|
|
|
return convertElementToString(lhs) < convertElementToString(rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompareFunctor
|
|
|
|
{
|
2017-01-05 21:21:35 +00:00
|
|
|
CompareFunctor(QV4::ExecutionEngine *v4, const QV4::Value &compareFn)
|
|
|
|
: m_v4(v4), m_compareFn(&compareFn)
|
2013-06-24 15:00:28 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()(typename Container::value_type lhs, typename Container::value_type rhs)
|
|
|
|
{
|
2017-01-05 21:21:35 +00:00
|
|
|
QV4::Scope scope(m_v4);
|
2017-09-01 09:48:15 +00:00
|
|
|
ScopedFunctionObject compare(scope, m_compareFn);
|
|
|
|
if (!compare)
|
|
|
|
return m_v4->throwTypeError();
|
2017-12-05 09:45:14 +00:00
|
|
|
Value *argv = scope.alloc(2);
|
|
|
|
argv[0] = convertElementToValue(m_v4, lhs);
|
|
|
|
argv[1] = convertElementToValue(m_v4, rhs);
|
|
|
|
QV4::ScopedValue result(scope, compare->call(m_v4->globalObject, argv, 2));
|
2017-08-03 18:26:28 +00:00
|
|
|
return result->toNumber() < 0;
|
2013-06-24 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-05 21:21:35 +00:00
|
|
|
QV4::ExecutionEngine *m_v4;
|
2015-01-15 10:36:57 +00:00
|
|
|
const QV4::Value *m_compareFn;
|
2013-06-24 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
2018-04-18 08:18:48 +00:00
|
|
|
bool sort(const FunctionObject *f, const Value *, const Value *argv, int argc)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2018-04-18 08:18:48 +00:00
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
if (argc == 1 && argv[0].as<FunctionObject>()) {
|
|
|
|
CompareFunctor cf(f->engine(), argv[0]);
|
2016-09-09 08:06:31 +00:00
|
|
|
std::sort(d()->container->begin(), d()->container->end(), cf);
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
DefaultCompareFunctor cf;
|
2016-09-09 08:06:31 +00:00
|
|
|
std::sort(d()->container->begin(), d()->container->end(), cf);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference)
|
2013-05-21 14:31:05 +00:00
|
|
|
storeReference();
|
2018-04-18 08:18:48 +00:00
|
|
|
|
|
|
|
return true;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
static QV4::ReturnedValue method_get_length(const FunctionObject *b, const Value *thisObject, const Value *, int)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-08-04 16:53:51 +00:00
|
|
|
QV4::Scope scope(b);
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::Scoped<QQmlSequence<Container>> This(scope, thisObject->as<QQmlSequence<Container> >());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_RESULT(Encode(0));
|
2013-06-13 11:43:09 +00:00
|
|
|
This->loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2017-04-18 13:55:20 +00:00
|
|
|
RETURN_RESULT(Encode(qint32(This->d()->container->size())));
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::Scope scope(f);
|
|
|
|
QV4::Scoped<QQmlSequence<Container>> This(scope, thisObject->as<QQmlSequence<Container> >());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
quint32 newLength = argc ? argv[0].toUInt32() : 0;
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (newLength > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(scope.engine, QLatin1String("Index out of range during length set"));
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2018-04-18 08:18:48 +00:00
|
|
|
|
|
|
|
if (This->d()->isReadOnly)
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Read the sequence from the QObject property if we're a reference */
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-06-13 11:43:09 +00:00
|
|
|
This->loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
/* Determine whether we need to modify the sequence */
|
2017-04-18 13:55:20 +00:00
|
|
|
quint32 newCount = static_cast<quint32>(newLength);
|
|
|
|
quint32 count = static_cast<quint32>(This->d()->container->size());
|
2013-05-21 14:31:05 +00:00
|
|
|
if (newCount == count) {
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
} else if (newCount > count) {
|
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* undefined values increasing length to newLength. */
|
|
|
|
/* We cannot, so we insert default-values instead. */
|
2016-09-09 08:06:31 +00:00
|
|
|
This->d()->container->reserve(newCount);
|
2013-05-21 14:31:05 +00:00
|
|
|
while (newCount > count++) {
|
2017-04-18 13:55:20 +00:00
|
|
|
This->d()->container->push_back(typename Container::value_type());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to remove */
|
|
|
|
/* elements until the sequence is the required length. */
|
2017-04-18 13:55:20 +00:00
|
|
|
if (newCount < count) {
|
|
|
|
This->d()->container->erase(This->d()->container->begin() + newCount, This->d()->container->end());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write back if required. */
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
2013-05-21 14:31:05 +00:00
|
|
|
/* write back. already checked that object is non-null, so skip that check here. */
|
2013-06-13 11:43:09 +00:00
|
|
|
This->storeReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant toVariant() const
|
2016-09-09 08:06:31 +00:00
|
|
|
{ return QVariant::fromValue<Container>(*d()->container); }
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2014-05-07 13:36:38 +00:00
|
|
|
static QVariant toVariant(QV4::ArrayObject *array)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-19 07:10:42 +00:00
|
|
|
QV4::Scope scope(array->engine());
|
2013-05-21 14:31:05 +00:00
|
|
|
Container result;
|
2013-12-16 08:16:57 +00:00
|
|
|
quint32 length = array->getLength();
|
2013-09-19 07:10:42 +00:00
|
|
|
QV4::ScopedValue v(scope);
|
2013-08-20 11:38:15 +00:00
|
|
|
for (quint32 i = 0; i < length; ++i)
|
2018-06-22 20:59:43 +00:00
|
|
|
result.push_back(convertValueToElement<typename Container::value_type>((v = array->get(i))));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QVariant::fromValue(result);
|
|
|
|
}
|
|
|
|
|
2017-04-26 10:49:37 +00:00
|
|
|
void* getRawContainerPtr() const
|
|
|
|
{ return d()->container; }
|
|
|
|
|
2013-06-10 14:29:53 +00:00
|
|
|
void loadReference() const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-04-29 09:28:40 +00:00
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
2018-02-21 09:41:54 +00:00
|
|
|
void *a[] = { d()->container, nullptr };
|
2014-04-29 09:28:40 +00:00
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::ReadProperty, d()->propertyIndex, a);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storeReference()
|
|
|
|
{
|
2014-04-29 09:28:40 +00:00
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
2013-05-21 14:31:05 +00:00
|
|
|
int status = -1;
|
2016-07-14 10:09:23 +00:00
|
|
|
QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding;
|
2018-02-21 09:41:54 +00:00
|
|
|
void *a[] = { d()->container, nullptr, &status, &flags };
|
2014-04-29 09:28:40 +00:00
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:18:33 +00:00
|
|
|
static QV4::ReturnedValue virtualGet(const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty)
|
2018-06-22 20:59:43 +00:00
|
|
|
{
|
|
|
|
if (!id.isArrayIndex())
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualGet(that, id, receiver, hasProperty);
|
2018-06-22 20:59:43 +00:00
|
|
|
return static_cast<const QQmlSequence<Container> *>(that)->containerGetIndexed(id.asArrayIndex(), hasProperty);
|
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver)
|
2018-06-22 13:34:57 +00:00
|
|
|
{
|
|
|
|
if (id.isArrayIndex())
|
|
|
|
return static_cast<QQmlSequence<Container> *>(that)->containerPutIndexed(id.asArrayIndex(), value);
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualPut(that, id, value, receiver);
|
2018-06-22 13:34:57 +00:00
|
|
|
}
|
2013-06-10 14:29:53 +00:00
|
|
|
static QV4::PropertyAttributes queryIndexed(const QV4::Managed *that, uint index)
|
|
|
|
{ return static_cast<const QQmlSequence<Container> *>(that)->containerQueryIndexed(index); }
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id)
|
2018-06-19 19:29:01 +00:00
|
|
|
{
|
|
|
|
if (id.isArrayIndex()) {
|
|
|
|
uint index = id.asArrayIndex();
|
|
|
|
return static_cast<QQmlSequence<Container> *>(that)->containerDeleteIndexedProperty(index);
|
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualDeleteProperty(that, id);
|
2018-06-19 19:29:01 +00:00
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualIsEqualTo(Managed *that, Managed *other)
|
2013-06-07 20:09:28 +00:00
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerIsEqualTo(other); }
|
2018-09-08 20:29:47 +00:00
|
|
|
static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target)
|
|
|
|
{ return static_cast<const QQmlSequence<Container> *>(m)->containerOwnPropertyKeys(m, target);}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-07 01:06:42 +00:00
|
|
|
|
|
|
|
template <typename Container>
|
2016-09-09 13:37:57 +00:00
|
|
|
void Heap::QQmlSequence<Container>::init(const Container &container)
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Object::init();
|
|
|
|
this->container = new Container(container);
|
|
|
|
propertyIndex = -1;
|
|
|
|
isReference = false;
|
2018-04-18 08:18:48 +00:00
|
|
|
isReadOnly = false;
|
2016-09-07 11:31:14 +00:00
|
|
|
object.init();
|
|
|
|
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::Scope scope(internalClass->engine);
|
2014-11-07 01:06:42 +00:00
|
|
|
QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this);
|
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
|
|
|
o->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Container>
|
2018-04-18 08:18:48 +00:00
|
|
|
void Heap::QQmlSequence<Container>::init(QObject *object, int propertyIndex, bool readOnly)
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Object::init();
|
|
|
|
this->container = new Container;
|
|
|
|
this->propertyIndex = propertyIndex;
|
|
|
|
isReference = true;
|
2018-04-18 08:18:48 +00:00
|
|
|
this->isReadOnly = readOnly;
|
2016-09-07 11:31:14 +00:00
|
|
|
this->object.init(object);
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::Scope scope(internalClass->engine);
|
2014-11-07 01:06:42 +00:00
|
|
|
QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this);
|
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
|
|
|
o->loadReference();
|
|
|
|
o->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-09 20:55:23 +00:00
|
|
|
namespace QV4 {
|
|
|
|
|
2017-04-18 13:55:20 +00:00
|
|
|
typedef QQmlSequence<std::vector<int> > QQmlIntStdVectorList;
|
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlIntStdVectorList);
|
|
|
|
typedef QQmlSequence<std::vector<qreal> > QQmlRealStdVectorList;
|
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlRealStdVectorList);
|
|
|
|
typedef QQmlSequence<std::vector<bool> > QQmlBoolStdVectorList;
|
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlBoolStdVectorList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QStringList> QQmlQStringList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQStringList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QString> > QQmlStringList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlStringList);
|
2017-04-18 13:55:20 +00:00
|
|
|
typedef QQmlSequence<std::vector<QString> > QQmlStringStdVectorList;
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<int> > QQmlIntList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlIntList);
|
2019-10-31 08:48:21 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlStringStdVectorList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QUrl> > QQmlUrlList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlUrlList);
|
2017-04-18 13:55:20 +00:00
|
|
|
typedef QQmlSequence<std::vector<QUrl> > QQmlUrlStdVectorList;
|
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlUrlStdVectorList);
|
2019-07-04 21:54:38 +00:00
|
|
|
#if QT_CONFIG(qml_itemmodel)
|
2015-03-03 17:36:51 +00:00
|
|
|
typedef QQmlSequence<QModelIndexList> QQmlQModelIndexList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQModelIndexList);
|
2017-04-18 13:55:20 +00:00
|
|
|
typedef QQmlSequence<std::vector<QModelIndex> > QQmlQModelIndexStdVectorList;
|
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQModelIndexStdVectorList);
|
2015-03-06 18:27:22 +00:00
|
|
|
typedef QQmlSequence<QItemSelection> QQmlQItemSelectionRangeList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQItemSelectionRangeList);
|
2019-07-04 21:54:38 +00:00
|
|
|
#endif
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<bool> > QQmlBoolList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlBoolList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<qreal> > QQmlRealList;
|
2016-10-08 11:03:35 +00:00
|
|
|
DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlRealList);
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2015-03-09 20:55:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 11:14:47 +00:00
|
|
|
#define REGISTER_QML_SEQUENCE_METATYPE(unused, unused2, SequenceType, unused3) qRegisterMetaType<SequenceType>(#SequenceType);
|
2013-09-18 10:31:55 +00:00
|
|
|
void SequencePrototype::init()
|
2013-06-13 11:43:09 +00:00
|
|
|
{
|
2014-05-09 12:32:59 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(REGISTER_QML_SEQUENCE_METATYPE)
|
2013-09-18 10:31:55 +00:00
|
|
|
defineDefaultProperty(QStringLiteral("sort"), method_sort, 1);
|
2015-04-26 07:22:17 +00:00
|
|
|
defineDefaultProperty(engine()->id_valueOf(), method_valueOf, 0);
|
2013-06-13 11:43:09 +00:00
|
|
|
}
|
2014-05-09 12:32:59 +00:00
|
|
|
#undef REGISTER_QML_SEQUENCE_METATYPE
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
ReturnedValue SequencePrototype::method_valueOf(const FunctionObject *f, const Value *thisObject, const Value *, int)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-12-05 09:45:14 +00:00
|
|
|
return Encode(thisObject->toString(f->engine()));
|
2017-08-04 16:53:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
ReturnedValue SequencePrototype::method_sort(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
|
2017-08-04 16:53:51 +00:00
|
|
|
{
|
|
|
|
Scope scope(b);
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::ScopedObject o(scope, thisObject);
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!o || !o->isListType())
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
if (argc >= 2)
|
2017-08-04 16:53:51 +00:00
|
|
|
return o.asReturnedValue();
|
2013-05-31 11:05:37 +00:00
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
#define CALL_SORT(SequenceElementType, SequenceElementTypeName, SequenceType, DefaultValue) \
|
2013-05-31 11:05:37 +00:00
|
|
|
if (QQml##SequenceElementTypeName##List *s = o->as<QQml##SequenceElementTypeName##List>()) { \
|
2018-04-18 08:18:48 +00:00
|
|
|
if (!s->sort(b, thisObject, argv, argc)) \
|
|
|
|
THROW_TYPE_ERROR(); \
|
2013-05-31 11:05:37 +00:00
|
|
|
} else
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(CALL_SORT)
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
#undef CALL_SORT
|
2013-11-01 11:38:32 +00:00
|
|
|
{}
|
2017-08-04 16:53:51 +00:00
|
|
|
return o.asReturnedValue();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 04:53:45 +00:00
|
|
|
#define IS_SEQUENCE(unused1, unused2, SequenceType, unused3) \
|
|
|
|
if (sequenceTypeId == qMetaTypeId<SequenceType>()) { \
|
|
|
|
return true; \
|
|
|
|
} else
|
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
bool SequencePrototype::isSequenceType(int sequenceTypeId)
|
2012-08-08 04:53:45 +00:00
|
|
|
{
|
2013-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(IS_SEQUENCE) { /* else */ return false; }
|
2012-08-08 04:53:45 +00:00
|
|
|
}
|
|
|
|
#undef IS_SEQUENCE
|
|
|
|
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
#define NEW_REFERENCE_SEQUENCE(ElementType, ElementTypeName, SequenceType, unused) \
|
|
|
|
if (sequenceType == qMetaTypeId<SequenceType>()) { \
|
2018-04-18 08:18:48 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QQml##ElementTypeName##List>(object, propertyIndex, readOnly)); \
|
2013-09-12 20:37:41 +00:00
|
|
|
return obj.asReturnedValue(); \
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
} else
|
|
|
|
|
2018-04-18 08:18:48 +00:00
|
|
|
ReturnedValue SequencePrototype::newSequence(QV4::ExecutionEngine *engine, int sequenceType, QObject *object, int propertyIndex, bool readOnly, bool *succeeded)
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
{
|
2013-09-12 20:37:41 +00:00
|
|
|
QV4::Scope scope(engine);
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
// This function is called when the property is a QObject Q_PROPERTY of
|
|
|
|
// the given sequence type. Internally we store a typed-sequence
|
|
|
|
// (as well as object ptr + property index for updated-read and write-back)
|
|
|
|
// and so access/mutate avoids variant conversion.
|
|
|
|
*succeeded = true;
|
2013-09-12 20:37:41 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(NEW_REFERENCE_SEQUENCE) { /* else */ *succeeded = false; return QV4::Encode::undefined(); }
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
}
|
|
|
|
#undef NEW_REFERENCE_SEQUENCE
|
|
|
|
|
|
|
|
#define NEW_COPY_SEQUENCE(ElementType, ElementTypeName, SequenceType, unused) \
|
|
|
|
if (sequenceType == qMetaTypeId<SequenceType>()) { \
|
2018-04-10 20:45:27 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QQml##ElementTypeName##List>(v.value<SequenceType >())); \
|
2013-09-12 20:37:41 +00:00
|
|
|
return obj.asReturnedValue(); \
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
} else
|
|
|
|
|
2013-09-12 20:37:41 +00:00
|
|
|
ReturnedValue SequencePrototype::fromVariant(QV4::ExecutionEngine *engine, const QVariant& v, bool *succeeded)
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
{
|
2013-09-12 20:37:41 +00:00
|
|
|
QV4::Scope scope(engine);
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
// This function is called when assigning a sequence value to a normal JS var
|
|
|
|
// in a JS block. Internally, we store a sequence of the specified type.
|
|
|
|
// Access and mutation is extremely fast since it will not need to modify any
|
|
|
|
// QObject property.
|
|
|
|
int sequenceType = v.userType();
|
|
|
|
*succeeded = true;
|
2013-09-12 20:37:41 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(NEW_COPY_SEQUENCE) { /* else */ *succeeded = false; return QV4::Encode::undefined(); }
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
}
|
|
|
|
#undef NEW_COPY_SEQUENCE
|
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
#define SEQUENCE_TO_VARIANT(ElementType, ElementTypeName, SequenceType, unused) \
|
2013-05-31 11:05:37 +00:00
|
|
|
if (QQml##ElementTypeName##List *list = object->as<QQml##ElementTypeName##List>()) \
|
2013-05-14 16:58:34 +00:00
|
|
|
return list->toVariant(); \
|
|
|
|
else
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2014-05-07 14:14:08 +00:00
|
|
|
QVariant SequencePrototype::toVariant(Object *object)
|
2013-05-14 13:59:10 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(object->isListType());
|
2013-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_TO_VARIANT) { /* else */ return QVariant(); }
|
2013-05-14 13:59:10 +00:00
|
|
|
}
|
|
|
|
|
2013-10-01 08:14:54 +00:00
|
|
|
#undef SEQUENCE_TO_VARIANT
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
#define SEQUENCE_TO_VARIANT(ElementType, ElementTypeName, SequenceType, unused) \
|
|
|
|
if (typeHint == qMetaTypeId<SequenceType>()) { \
|
2013-05-14 16:58:34 +00:00
|
|
|
return QQml##ElementTypeName##List::toVariant(a); \
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
} else
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
QVariant SequencePrototype::toVariant(const QV4::Value &array, int typeHint, bool *succeeded)
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
{
|
|
|
|
*succeeded = true;
|
|
|
|
|
2015-02-13 09:42:01 +00:00
|
|
|
if (!array.as<ArrayObject>()) {
|
2013-05-14 16:58:34 +00:00
|
|
|
*succeeded = false;
|
|
|
|
return QVariant();
|
|
|
|
}
|
2015-02-13 12:39:20 +00:00
|
|
|
QV4::Scope scope(array.as<Object>()->engine());
|
2013-10-07 10:23:38 +00:00
|
|
|
QV4::ScopedArrayObject a(scope, array);
|
2013-09-24 13:25:10 +00:00
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_TO_VARIANT) { /* else */ *succeeded = false; return QVariant(); }
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
#undef SEQUENCE_TO_VARIANT
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2017-04-26 10:49:37 +00:00
|
|
|
#define SEQUENCE_GET_RAWCONTAINERPTR(ElementType, ElementTypeName, SequenceType, unused) \
|
|
|
|
if (const QQml##ElementTypeName##List *list = [&]() -> const QQml##ElementTypeName##List* \
|
|
|
|
{ if (typeHint == qMetaTypeId<SequenceType>()) return object->as<QQml##ElementTypeName##List>(); return nullptr;}()) \
|
|
|
|
return list->getRawContainerPtr(); \
|
|
|
|
else
|
|
|
|
|
|
|
|
void* SequencePrototype::getRawContainerPtr(const Object *object, int typeHint)
|
|
|
|
{
|
|
|
|
FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_GET_RAWCONTAINERPTR) { /* else */ return nullptr; }
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SEQUENCE_GET_RAWCONTAINERPTR
|
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
#define MAP_META_TYPE(ElementType, ElementTypeName, SequenceType, unused) \
|
2013-05-31 11:05:37 +00:00
|
|
|
if (object->as<QQml##ElementTypeName##List>()) { \
|
|
|
|
return qMetaTypeId<SequenceType>(); \
|
|
|
|
} else
|
2012-08-09 00:59:31 +00:00
|
|
|
|
2015-02-13 12:39:20 +00:00
|
|
|
int SequencePrototype::metaTypeForSequence(const QV4::Object *object)
|
2012-07-18 03:20:00 +00:00
|
|
|
{
|
2013-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(MAP_META_TYPE)
|
2013-05-31 11:05:37 +00:00
|
|
|
/*else*/ {
|
2013-05-14 16:58:34 +00:00
|
|
|
return -1;
|
2012-07-18 03:20:00 +00:00
|
|
|
}
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
#undef MAP_META_TYPE
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
|
|
|
QT_END_NAMESPACE
|