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-01-02 11:17:46 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-09-20 05:21:40 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
**
|
|
|
|
** $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
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
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
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.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
|
|
|
**
|
2012-09-20 05:21:40 +00:00
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
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
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
2012-09-20 05:21:40 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.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
|
|
|
**
|
2012-01-24 03:37:23 +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
|
|
|
** $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>
|
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.
|
|
|
|
static void generateWarning(QV4::ExecutionContext *ctx, const QString& description)
|
|
|
|
{
|
2013-06-04 14:00:13 +00:00
|
|
|
QQmlEngine *engine = ctx->engine->v8Engine->engine();
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!engine)
|
|
|
|
return;
|
|
|
|
QQmlError retn;
|
|
|
|
retn.setDescription(description);
|
2013-06-07 18:49:28 +00:00
|
|
|
|
|
|
|
QV4::ExecutionEngine::StackFrame frame = ctx->engine->currentStackFrame();
|
|
|
|
|
|
|
|
retn.setLine(frame.line);
|
|
|
|
retn.setUrl(QUrl(frame.source));
|
2013-05-21 14:31:05 +00:00
|
|
|
QQmlEnginePrivate::warning(engine, retn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// F(elementType, elementTypeName, sequenceType, defaultValue)
|
|
|
|
#define FOREACH_QML_SEQUENCE_TYPE(F) \
|
|
|
|
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()) \
|
|
|
|
F(QUrl, Url, QList<QUrl>, QUrl())
|
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
static QV4::Value convertElementToValue(QV4::ExecutionEngine *engine, const QString &element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-06-10 15:11:52 +00:00
|
|
|
return QV4::Value::fromString(engine, element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
static QV4::Value convertElementToValue(QV4::ExecutionEngine *, int element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
return QV4::Value::fromInt32(element);
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
static QV4::Value convertElementToValue(QV4::ExecutionEngine *engine, const QUrl &element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-06-10 15:11:52 +00:00
|
|
|
return QV4::Value::fromString(engine, element.toString());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
static QV4::Value convertElementToValue(QV4::ExecutionEngine *, qreal element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
return QV4::Value::fromDouble(element);
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:11:52 +00:00
|
|
|
static QV4::Value convertElementToValue(QV4::ExecutionEngine *, bool element)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
return QV4::Value::fromBoolean(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(qreal element)
|
|
|
|
{
|
|
|
|
QString qstr;
|
|
|
|
__qmljs_numberToString(&qstr, element, 10);
|
|
|
|
return qstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString convertElementToString(bool element)
|
|
|
|
{
|
|
|
|
if (element)
|
|
|
|
return QStringLiteral("true");
|
|
|
|
else
|
|
|
|
return QStringLiteral("false");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ElementType> ElementType convertValueToElement(const QV4::Value &value);
|
|
|
|
|
|
|
|
template <> QString convertValueToElement(const QV4::Value &value)
|
|
|
|
{
|
2013-09-09 12:33:28 +00:00
|
|
|
return value.toQStringNoThrow();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <> int convertValueToElement(const QV4::Value &value)
|
|
|
|
{
|
|
|
|
return value.toInt32();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> QUrl convertValueToElement(const QV4::Value &value)
|
|
|
|
{
|
2013-09-09 12:33:28 +00:00
|
|
|
return QUrl(value.toQStringNoThrow());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <> qreal convertValueToElement(const QV4::Value &value)
|
|
|
|
{
|
|
|
|
return value.toNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> bool convertValueToElement(const QV4::Value &value)
|
|
|
|
{
|
|
|
|
return value.toBoolean();
|
|
|
|
}
|
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
template <typename Container>
|
2013-06-13 11:43:09 +00:00
|
|
|
class QQmlSequence : public QV4::Object
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-05-31 11:05:37 +00:00
|
|
|
Q_MANAGED
|
2013-05-21 14:31:05 +00:00
|
|
|
public:
|
|
|
|
QQmlSequence(QV4::ExecutionEngine *engine, const Container &container)
|
2013-08-30 09:22:00 +00:00
|
|
|
: QV4::Object(engine->sequenceClass)
|
2013-05-21 14:31:05 +00:00
|
|
|
, m_container(container)
|
|
|
|
, m_object(0)
|
|
|
|
, m_propertyIndex(-1)
|
|
|
|
, m_isReference(false)
|
|
|
|
{
|
2013-05-31 11:05:37 +00:00
|
|
|
type = Type_QmlSequence;
|
2013-05-21 14:31:05 +00:00
|
|
|
vtbl = &static_vtbl;
|
2013-06-13 11:43:09 +00:00
|
|
|
init(engine);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QQmlSequence(QV4::ExecutionEngine *engine, QObject *object, int propertyIndex)
|
2013-08-30 09:22:00 +00:00
|
|
|
: QV4::Object(engine->sequenceClass)
|
2013-05-21 14:31:05 +00:00
|
|
|
, m_object(object)
|
|
|
|
, m_propertyIndex(propertyIndex)
|
|
|
|
, m_isReference(true)
|
|
|
|
{
|
2013-05-31 11:05:37 +00:00
|
|
|
type = Type_QmlSequence;
|
2013-05-21 14:31:05 +00:00
|
|
|
vtbl = &static_vtbl;
|
|
|
|
loadReference();
|
2013-06-13 11:43:09 +00:00
|
|
|
init(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(ExecutionEngine *engine)
|
|
|
|
{
|
|
|
|
defineAccessorProperty(engine, QStringLiteral("length"), method_get_length, method_set_length);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:55:13 +00:00
|
|
|
QV4::Value containerGetIndexed(uint index, bool *hasProperty)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2013-06-21 20:55:13 +00:00
|
|
|
generateWarning(engine()->current, QLatin1String("Index out of range during indexed get"));
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return QV4::Value::undefinedValue();
|
|
|
|
}
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return QV4::Value::undefinedValue();
|
|
|
|
}
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
|
|
|
if (signedIdx < m_container.count()) {
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2013-06-21 20:55:13 +00:00
|
|
|
return convertElementToValue(engine(), m_container.at(signedIdx));
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return QV4::Value::undefinedValue();
|
|
|
|
}
|
|
|
|
|
2013-06-21 21:42:08 +00:00
|
|
|
void containerPutIndexed(uint index, const QV4::Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2013-06-21 21:42:08 +00:00
|
|
|
generateWarning(engine()->current, QLatin1String("Index out of range during indexed set"));
|
2013-05-21 14:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object)
|
|
|
|
return;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
|
|
|
|
|
|
|
int count = m_container.count();
|
|
|
|
|
|
|
|
typename Container::value_type element = convertValueToElement<typename Container::value_type>(value);
|
|
|
|
|
|
|
|
if (signedIdx == count) {
|
|
|
|
m_container.append(element);
|
|
|
|
} else if (signedIdx < count) {
|
|
|
|
m_container[signedIdx] = element;
|
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* the value at the given index, increasing length to index+1. */
|
|
|
|
m_container.reserve(signedIdx + 1);
|
|
|
|
while (signedIdx > count++) {
|
|
|
|
m_container.append(typename Container::value_type());
|
|
|
|
}
|
|
|
|
m_container.append(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_isReference)
|
|
|
|
storeReference();
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-06-10 14:29:53 +00:00
|
|
|
generateWarning(engine()->current, QLatin1String("Index out of range during indexed query"));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
}
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object)
|
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
2013-06-10 15:11:52 +00:00
|
|
|
return (signedIdx < m_container.count()) ? QV4::Attr_Data : QV4::Attr_Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Property *containerAdvanceIterator(ObjectIterator *it, String **name, uint *index, PropertyAttributes *attrs)
|
|
|
|
{
|
|
|
|
*name = 0;
|
|
|
|
*index = UINT_MAX;
|
|
|
|
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object)
|
2013-06-13 11:43:09 +00:00
|
|
|
return QV4::Object::advanceIterator(this, it, name, index, attrs);
|
2013-06-10 15:11:52 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it->arrayIndex < m_container.count()) {
|
|
|
|
if (attrs)
|
|
|
|
*attrs = QV4::Attr_Data;
|
|
|
|
*index = it->arrayIndex;
|
|
|
|
++it->arrayIndex;
|
|
|
|
it->tmpDynamicProperty.value = convertElementToValue(engine(), m_container.at(*index));
|
|
|
|
return &it->tmpDynamicProperty;
|
|
|
|
}
|
2013-06-13 11:43:09 +00:00
|
|
|
return QV4::Object::advanceIterator(this, it, name, index, attrs);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object)
|
|
|
|
return false;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
|
|
|
|
|
|
|
if (signedIdx >= m_container.count())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* according to ECMA262r3 it should be Undefined, */
|
|
|
|
/* but we cannot, so we insert a default-value instead. */
|
|
|
|
m_container.replace(signedIdx, typename Container::value_type());
|
|
|
|
|
|
|
|
if (m_isReference)
|
|
|
|
storeReference();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-07 20:09:28 +00:00
|
|
|
bool containerIsEqualTo(Managed *other)
|
|
|
|
{
|
|
|
|
QQmlSequence<Container> *otherSequence = other->as<QQmlSequence<Container> >();
|
|
|
|
if (!otherSequence)
|
|
|
|
return false;
|
|
|
|
if (m_isReference && otherSequence->m_isReference) {
|
|
|
|
return m_object == otherSequence->m_object && m_propertyIndex == otherSequence->m_propertyIndex;
|
|
|
|
} else if (!m_isReference && !otherSequence->m_isReference) {
|
|
|
|
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
|
|
|
|
{
|
|
|
|
CompareFunctor(QV4::ExecutionContext *ctx, const QV4::Value &compareFn)
|
|
|
|
: m_ctx(ctx), m_compareFn(compareFn)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()(typename Container::value_type lhs, typename Container::value_type rhs)
|
|
|
|
{
|
|
|
|
QV4::Managed *fun = this->m_compareFn.asManaged();
|
2013-09-11 12:47:34 +00:00
|
|
|
Scope scope(fun->engine());
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 2);
|
2013-09-05 11:22:23 +00:00
|
|
|
callData->args[0] = convertElementToValue(this->m_ctx->engine, lhs);
|
|
|
|
callData->args[1] = convertElementToValue(this->m_ctx->engine, rhs);
|
|
|
|
callData->thisObject = QV4::Value::fromObject(this->m_ctx->engine->globalObject);
|
2013-09-11 11:55:01 +00:00
|
|
|
QV4::ScopedValue result(scope, fun->call(callData));
|
|
|
|
return result->toNumber() < 0;
|
2013-06-24 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QV4::ExecutionContext *m_ctx;
|
|
|
|
QV4::Value m_compareFn;
|
|
|
|
};
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
void sort(QV4::SimpleCallContext *ctx)
|
|
|
|
{
|
|
|
|
if (m_isReference) {
|
|
|
|
if (!m_object)
|
|
|
|
return;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->argumentCount == 1 && ctx->arguments[0].asFunctionObject()) {
|
|
|
|
QV4::Value compareFn = ctx->arguments[0];
|
|
|
|
CompareFunctor cf(ctx, compareFn);
|
2013-09-12 09:06:59 +00:00
|
|
|
std::sort(m_container.begin(), m_container.end(), cf);
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
DefaultCompareFunctor cf;
|
2013-09-12 09:06:59 +00:00
|
|
|
std::sort(m_container.begin(), m_container.end(), cf);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_isReference)
|
|
|
|
storeReference();
|
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
static QV4::ReturnedValue method_get_length(QV4::SimpleCallContext *ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-06-13 11:43:09 +00:00
|
|
|
QQmlSequence<Container> *This = ctx->thisObject.as<QQmlSequence<Container> >();
|
|
|
|
if (!This)
|
|
|
|
ctx->throwTypeError();
|
|
|
|
|
|
|
|
if (This->m_isReference) {
|
|
|
|
if (!This->m_object)
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode(0);
|
2013-06-13 11:43:09 +00:00
|
|
|
This->loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode(This->m_container.count());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
static QV4::ReturnedValue method_set_length(QV4::SimpleCallContext* ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-06-13 11:43:09 +00:00
|
|
|
QQmlSequence<Container> *This = ctx->thisObject.as<QQmlSequence<Container> >();
|
|
|
|
if (!This)
|
|
|
|
ctx->throwTypeError();
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
quint32 newLength = ctx->arguments[0].toUInt32();
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (newLength > INT_MAX) {
|
|
|
|
generateWarning(ctx, QLatin1String("Index out of range during length set"));
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
/* Read the sequence from the QObject property if we're a reference */
|
2013-06-13 11:43:09 +00:00
|
|
|
if (This->m_isReference) {
|
|
|
|
if (!This->m_object)
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode::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 */
|
|
|
|
qint32 newCount = static_cast<qint32>(newLength);
|
2013-06-13 11:43:09 +00:00
|
|
|
qint32 count = This->m_container.count();
|
2013-05-21 14:31:05 +00:00
|
|
|
if (newCount == count) {
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode::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. */
|
2013-06-13 11:43:09 +00:00
|
|
|
This->m_container.reserve(newCount);
|
2013-05-21 14:31:05 +00:00
|
|
|
while (newCount > count++) {
|
2013-06-13 11:43:09 +00:00
|
|
|
This->m_container.append(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. */
|
|
|
|
while (newCount < count) {
|
|
|
|
count--;
|
2013-06-13 11:43:09 +00:00
|
|
|
This->m_container.removeAt(count);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write back if required. */
|
2013-06-13 11:43:09 +00:00
|
|
|
if (This->m_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
|
|
|
}
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant toVariant() const
|
|
|
|
{ return QVariant::fromValue<Container>(m_container); }
|
|
|
|
|
|
|
|
static QVariant toVariant(QV4::ArrayObject *array)
|
|
|
|
{
|
|
|
|
Container result;
|
2013-08-20 11:38:15 +00:00
|
|
|
quint32 length = array->arrayLength();
|
|
|
|
for (quint32 i = 0; i < length; ++i)
|
2013-09-11 19:48:23 +00:00
|
|
|
result << convertValueToElement<typename Container::value_type>(QV4::Value::fromReturnedValue(array->getIndexed(i)));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QVariant::fromValue(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-10 14:29:53 +00:00
|
|
|
void loadReference() const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(m_object);
|
|
|
|
Q_ASSERT(m_isReference);
|
|
|
|
void *a[] = { &m_container, 0 };
|
|
|
|
QMetaObject::metacall(m_object, QMetaObject::ReadProperty, m_propertyIndex, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void storeReference()
|
|
|
|
{
|
|
|
|
Q_ASSERT(m_object);
|
|
|
|
Q_ASSERT(m_isReference);
|
|
|
|
int status = -1;
|
|
|
|
QQmlPropertyPrivate::WriteFlags flags = QQmlPropertyPrivate::DontRemoveBinding;
|
|
|
|
void *a[] = { &m_container, 0, &status, &flags };
|
|
|
|
QMetaObject::metacall(m_object, QMetaObject::WriteProperty, m_propertyIndex, a);
|
|
|
|
}
|
|
|
|
|
2013-06-10 14:29:53 +00:00
|
|
|
mutable Container m_container;
|
2013-07-05 11:29:57 +00:00
|
|
|
QPointer<QObject> m_object;
|
2013-05-21 14:31:05 +00:00
|
|
|
int m_propertyIndex;
|
|
|
|
bool m_isReference;
|
|
|
|
|
2013-09-11 19:48:23 +00:00
|
|
|
static QV4::ReturnedValue getIndexed(QV4::Managed *that, uint index, bool *hasProperty)
|
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerGetIndexed(index, hasProperty).asReturnedValue(); }
|
2013-06-21 21:42:08 +00:00
|
|
|
static void putIndexed(Managed *that, uint index, const QV4::Value &value)
|
|
|
|
{ static_cast<QQmlSequence<Container> *>(that)->containerPutIndexed(index, value); }
|
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); }
|
2013-06-21 18:33:39 +00:00
|
|
|
static bool deleteIndexedProperty(QV4::Managed *that, uint index)
|
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerDeleteIndexedProperty(index); }
|
2013-06-07 20:09:28 +00:00
|
|
|
static bool isEqualTo(Managed *that, Managed *other)
|
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerIsEqualTo(other); }
|
2013-06-10 15:11:52 +00:00
|
|
|
static Property *advanceIterator(Managed *that, ObjectIterator *it, String **name, uint *index, PropertyAttributes *attrs)
|
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerAdvanceIterator(it, name, index, attrs); }
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
static void destroy(Managed *that)
|
|
|
|
{
|
2013-05-31 11:05:37 +00:00
|
|
|
static_cast<QQmlSequence<Container> *>(that)->~QQmlSequence<Container>();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QStringList> QQmlQStringList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlQStringList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QString> > QQmlStringList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlStringList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<int> > QQmlIntList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlIntList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QUrl> > QQmlUrlList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlUrlList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<bool> > QQmlBoolList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlBoolList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<qreal> > QQmlRealList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
|
|
|
DEFINE_MANAGED_VTABLE(QQmlRealList);
|
|
|
|
|
2013-07-26 11:14:47 +00:00
|
|
|
#define REGISTER_QML_SEQUENCE_METATYPE(unused, unused2, SequenceType, unused3) qRegisterMetaType<SequenceType>(#SequenceType);
|
2013-08-29 12:31:32 +00:00
|
|
|
SequencePrototype::SequencePrototype(InternalClass *ic)
|
|
|
|
: QV4::Object(ic)
|
2013-05-21 15:06:36 +00:00
|
|
|
{
|
|
|
|
FOREACH_QML_SEQUENCE_TYPE(REGISTER_QML_SEQUENCE_METATYPE)
|
|
|
|
}
|
|
|
|
#undef REGISTER_QML_SEQUENCE_METATYPE
|
|
|
|
|
2013-06-13 11:43:09 +00:00
|
|
|
void SequencePrototype::init(QV4::ExecutionEngine *engine)
|
|
|
|
{
|
|
|
|
defineDefaultProperty(engine, QStringLiteral("sort"), method_sort, 1);
|
|
|
|
defineDefaultProperty(engine, QStringLiteral("valueOf"), method_valueOf, 0);
|
|
|
|
}
|
|
|
|
|
2013-09-12 09:13:03 +00:00
|
|
|
QV4::ReturnedValue SequencePrototype::method_sort(QV4::SimpleCallContext *ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
QV4::Object *o = ctx->thisObject.asObject();
|
|
|
|
if (!o || !o->isListType())
|
|
|
|
ctx->throwTypeError();
|
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
if (ctx->argumentCount >= 2)
|
2013-09-12 09:13:03 +00:00
|
|
|
return ctx->thisObject.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>()) { \
|
|
|
|
s->sort(ctx); \
|
|
|
|
} 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-09-12 09:13:03 +00:00
|
|
|
return ctx->thisObject.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>()) { \
|
2013-05-21 15:06:36 +00:00
|
|
|
QV4::Object *obj = new (engine->memoryManager) QQml##ElementTypeName##List(engine, object, propertyIndex); \
|
2013-05-14 16:58:34 +00:00
|
|
|
return QV4::Value::fromObject(obj); \
|
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-05-21 15:06:36 +00:00
|
|
|
QV4::Value SequencePrototype::newSequence(QV4::ExecutionEngine *engine, int sequenceType, QObject *object, int propertyIndex, 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
|
|
|
{
|
|
|
|
// 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-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(NEW_REFERENCE_SEQUENCE) { /* else */ *succeeded = false; return QV4::Value::undefinedValue(); }
|
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>()) { \
|
2013-05-21 15:06:36 +00:00
|
|
|
QV4::Object *obj = new (engine->memoryManager) QQml##ElementTypeName##List(engine, v.value<SequenceType >()); \
|
2013-05-14 16:58:34 +00:00
|
|
|
return QV4::Value::fromObject(obj); \
|
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-05-21 15:06:36 +00:00
|
|
|
QV4::Value 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
|
|
|
{
|
|
|
|
// 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-05-14 16:58:34 +00:00
|
|
|
FOREACH_QML_SEQUENCE_TYPE(NEW_COPY_SEQUENCE) { /* else */ *succeeded = false; return QV4::Value::undefinedValue(); }
|
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
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
QVariant SequencePrototype::toVariant(QV4::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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2013-05-21 15:06:36 +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;
|
|
|
|
|
2013-05-14 16:58:34 +00:00
|
|
|
QV4::ArrayObject *a = array.asArrayObject();
|
|
|
|
if (!a) {
|
|
|
|
*succeeded = false;
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
int SequencePrototype::metaTypeForSequence(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
|