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
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://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
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
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
|
|
|
|
** and conditions see http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://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
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** 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
|
|
|
**
|
2015-01-28 11:55:39 +00:00
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company 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.
|
|
|
|
**
|
|
|
|
** $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>
|
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>
|
|
|
|
#include <private/qqmlmodelindexvaluetype_p.h>
|
|
|
|
#include <QtCore/qabstractitemmodel.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.
|
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
|
|
|
|
2014-11-13 11:14:09 +00:00
|
|
|
QV4::StackFrame frame = v4->currentStackFrame();
|
2013-06-07 18:49:28 +00:00
|
|
|
|
|
|
|
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()) \
|
2015-03-03 17:36:51 +00:00
|
|
|
F(QUrl, Url, QList<QUrl>, QUrl()) \
|
2015-03-06 18:27:22 +00:00
|
|
|
F(QModelIndex, QModelIndex, QModelIndexList, QModelIndex()) \
|
|
|
|
F(QItemSelectionRange, QItemSelectionRange, QItemSelection, QItemSelectionRange())
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-08-28 11:33:10 +00:00
|
|
|
QQmlSequence(const Container &container);
|
|
|
|
QQmlSequence(QObject *object, int propertyIndex);
|
2014-11-07 01:06:42 +00:00
|
|
|
|
|
|
|
mutable Container container;
|
|
|
|
QPointer<QObject> object;
|
|
|
|
int propertyIndex;
|
|
|
|
bool isReference;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
2014-04-29 09:28:40 +00:00
|
|
|
if (signedIdx < d()->container.count()) {
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2014-04-29 09:28:40 +00:00
|
|
|
return convertElementToValue(engine(), d()->container.at(signedIdx));
|
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
|
|
|
}
|
|
|
|
|
2015-01-15 10:36:57 +00:00
|
|
|
void 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)
|
2013-10-22 11:26:08 +00:00
|
|
|
return;
|
|
|
|
|
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"));
|
2013-05-21 14:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2013-05-21 14:31:05 +00:00
|
|
|
return;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
int count = d()->container.count();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
typename Container::value_type element = convertValueToElement<typename Container::value_type>(value);
|
|
|
|
|
|
|
|
if (signedIdx == count) {
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container.append(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
} else if (signedIdx < count) {
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container[signedIdx] = 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. */
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container.reserve(signedIdx + 1);
|
2013-05-21 14:31:05 +00:00
|
|
|
while (signedIdx > count++) {
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container.append(typename Container::value_type());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container.append(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();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
2014-04-29 09:28:40 +00:00
|
|
|
return (signedIdx < d()->container.count()) ? QV4::Attr_Data : QV4::Attr_Invalid;
|
2013-06-10 15:11:52 +00:00
|
|
|
}
|
|
|
|
|
Remove type punning from QV4::Value.
The union in QV4::Value is used to do type punning. In C++, this is
compiler-defined behavior. For example, Clang and GCC will try to detect
it and try to do the proper thing. However, it can play havoc with Alias
Analysis, and it is not guaranteed that some Undefined Behavior (or
Compiler depenedent behavior) might occur.
The really problematic part is the struct inside the union: depending on
the calling convention and the register size, it results in some
exciting code. For example, the AMD64 ABI specifies that a struct of two
values of INTEGER class can be passed in separate registers when doing a
function call. Now, if the AA in the compiler looses track of the fact
that the tag overlaps with the double, you might get:
ecx := someTag
... conditional jumps
double_case:
rdx := xorredDoubleValue
callq someWhere
If the someWhere function checks for the tag first, mayhem ensues: the
double value in rdx does not overwrite the tag that is passed in ecx.
Changing the code to do reinterpret_cast<>s might also give problems
on 32bit architectures, because there is a double, whose size is not the
same as the size of the tag, which could confuse AA.
So, to fix this, the following is changed:
- only have a quint64 field in the QV4::Value, which has the added
benefit that it's very clear for the compiler that it's a POD
- as memcpy is the only approved way to ensure bit-by-bit "conversion"
between types (esp. FP<->non-FP types), change all conversions to use
memcpy. Use bitops (shift/and/or) for anything else.
- only use accessor functions for non-quint64 values
As any modern compiler has memcpy as an intrinsic, the call will be
replaced with one or a few move instructions. The accessor functions
also get inlined, the bitops get optimized, so in all cases the compiler
can generate the most compact code possible.
This patch obsoletes f558bc48585c69de36151248c969a484a969ebb4 (which had
the exact aliassing problem of the double and the tag as described
above).
Change-Id: I60a39d8564be5ce6106403a56a8de90943217006
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
2015-07-08 08:52:59 +00:00
|
|
|
void containerAdvanceIterator(ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attrs)
|
2013-06-10 15:11:52 +00:00
|
|
|
{
|
Remove type punning from QV4::Value.
The union in QV4::Value is used to do type punning. In C++, this is
compiler-defined behavior. For example, Clang and GCC will try to detect
it and try to do the proper thing. However, it can play havoc with Alias
Analysis, and it is not guaranteed that some Undefined Behavior (or
Compiler depenedent behavior) might occur.
The really problematic part is the struct inside the union: depending on
the calling convention and the register size, it results in some
exciting code. For example, the AMD64 ABI specifies that a struct of two
values of INTEGER class can be passed in separate registers when doing a
function call. Now, if the AA in the compiler looses track of the fact
that the tag overlaps with the double, you might get:
ecx := someTag
... conditional jumps
double_case:
rdx := xorredDoubleValue
callq someWhere
If the someWhere function checks for the tag first, mayhem ensues: the
double value in rdx does not overwrite the tag that is passed in ecx.
Changing the code to do reinterpret_cast<>s might also give problems
on 32bit architectures, because there is a double, whose size is not the
same as the size of the tag, which could confuse AA.
So, to fix this, the following is changed:
- only have a quint64 field in the QV4::Value, which has the added
benefit that it's very clear for the compiler that it's a POD
- as memcpy is the only approved way to ensure bit-by-bit "conversion"
between types (esp. FP<->non-FP types), change all conversions to use
memcpy. Use bitops (shift/and/or) for anything else.
- only use accessor functions for non-quint64 values
As any modern compiler has memcpy as an intrinsic, the call will be
replaced with one or a few move instructions. The accessor functions
also get inlined, the bitops get optimized, so in all cases the compiler
can generate the most compact code possible.
This patch obsoletes f558bc48585c69de36151248c969a484a969ebb4 (which had
the exact aliassing problem of the double and the tag as described
above).
Change-Id: I60a39d8564be5ce6106403a56a8de90943217006
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
2015-07-08 08:52:59 +00:00
|
|
|
name->setM(0);
|
2013-06-10 15:11:52 +00:00
|
|
|
*index = UINT_MAX;
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object) {
|
2014-01-08 13:51:33 +00:00
|
|
|
QV4::Object::advanceIterator(this, it, name, index, p, attrs);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-10 15:11:52 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (it->arrayIndex < static_cast<uint>(d()->container.count())) {
|
2013-06-10 15:11:52 +00:00
|
|
|
*index = it->arrayIndex;
|
|
|
|
++it->arrayIndex;
|
2014-01-08 13:51:33 +00:00
|
|
|
*attrs = QV4::Attr_Data;
|
2014-04-29 09:28:40 +00:00
|
|
|
p->value = convertElementToValue(engine(), d()->container.at(*index));
|
2014-01-08 13:51:33 +00:00
|
|
|
return;
|
2013-06-10 15:11:52 +00:00
|
|
|
}
|
2014-01-08 13:51:33 +00:00
|
|
|
QV4::Object::advanceIterator(this, it, name, index, p, 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;
|
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();
|
|
|
|
}
|
|
|
|
qint32 signedIdx = static_cast<qint32>(index);
|
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (signedIdx >= d()->container.count())
|
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. */
|
2014-04-29 09:28:40 +00:00
|
|
|
d()->container.replace(signedIdx, 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
|
|
|
|
{
|
2015-01-15 10:36:57 +00:00
|
|
|
CompareFunctor(QV4::ExecutionContext *ctx, const QV4::Value &compareFn)
|
|
|
|
: m_ctx(ctx), m_compareFn(&compareFn)
|
2013-06-24 15:00:28 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
bool operator()(typename Container::value_type lhs, typename Container::value_type rhs)
|
|
|
|
{
|
2013-09-27 06:32:44 +00:00
|
|
|
QV4::Scope scope(m_ctx);
|
|
|
|
ScopedObject compare(scope, m_compareFn);
|
2013-09-11 12:36:01 +00:00
|
|
|
ScopedCallData callData(scope, 2);
|
2014-05-06 07:23:59 +00:00
|
|
|
callData->args[0] = convertElementToValue(this->m_ctx->d()->engine, lhs);
|
|
|
|
callData->args[1] = convertElementToValue(this->m_ctx->d()->engine, rhs);
|
2015-03-13 16:21:18 +00:00
|
|
|
callData->thisObject = this->m_ctx->d()->engine->globalObject;
|
2013-09-27 06:32:44 +00:00
|
|
|
QV4::ScopedValue result(scope, compare->call(callData));
|
2013-09-11 11:55:01 +00:00
|
|
|
return result->toNumber() < 0;
|
2013-06-24 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QV4::ExecutionContext *m_ctx;
|
2015-01-15 10:36:57 +00:00
|
|
|
const QV4::Value *m_compareFn;
|
2013-06-24 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
2013-11-03 14:23:05 +00:00
|
|
|
void sort(QV4::CallContext *ctx)
|
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
|
|
|
return;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2013-09-27 06:32:44 +00:00
|
|
|
QV4::Scope scope(ctx);
|
2015-02-13 12:56:05 +00:00
|
|
|
if (ctx->argc() == 1 && ctx->args()[0].as<FunctionObject>()) {
|
2015-01-15 20:54:12 +00:00
|
|
|
CompareFunctor cf(ctx, ctx->args()[0]);
|
2014-04-29 09:28:40 +00:00
|
|
|
std::sort(d()->container.begin(), d()->container.end(), cf);
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
DefaultCompareFunctor cf;
|
2014-04-29 09:28:40 +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();
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:23:05 +00:00
|
|
|
static QV4::ReturnedValue method_get_length(QV4::CallContext *ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-25 08:09:26 +00:00
|
|
|
QV4::Scope scope(ctx);
|
2015-01-15 20:54:12 +00:00
|
|
|
QV4::Scoped<QQmlSequence<Container> > This(scope, ctx->thisObject().as<QQmlSequence<Container> >());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2014-07-28 08:07:57 +00:00
|
|
|
return ctx->engine()->throwTypeError();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->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
|
|
|
}
|
2014-04-29 09:28:40 +00:00
|
|
|
return QV4::Encode(This->d()->container.count());
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-11-03 14:23:05 +00:00
|
|
|
static QV4::ReturnedValue method_set_length(QV4::CallContext* ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-09-25 08:09:26 +00:00
|
|
|
QV4::Scope scope(ctx);
|
2015-01-15 20:54:12 +00:00
|
|
|
QV4::Scoped<QQmlSequence<Container> > This(scope, ctx->thisObject().as<QQmlSequence<Container> >());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2014-07-28 08:07:57 +00:00
|
|
|
return ctx->engine()->throwTypeError();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2015-01-15 20:54:12 +00:00
|
|
|
quint32 newLength = ctx->args()[0].toUInt32();
|
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"));
|
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 */
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->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);
|
2014-04-29 09:28:40 +00:00
|
|
|
qint32 count = This->d()->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. */
|
2014-04-29 09:28:40 +00:00
|
|
|
This->d()->container.reserve(newCount);
|
2013-05-21 14:31:05 +00:00
|
|
|
while (newCount > count++) {
|
2014-04-29 09:28:40 +00:00
|
|
|
This->d()->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--;
|
2014-04-29 09:28:40 +00:00
|
|
|
This->d()->container.removeAt(count);
|
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
|
|
|
}
|
2013-09-12 09:13:03 +00:00
|
|
|
return QV4::Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant toVariant() const
|
2014-04-29 09:28:40 +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)
|
2013-09-19 07:10:42 +00:00
|
|
|
result << convertValueToElement<typename Container::value_type>((v = array->getIndexed(i)));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QVariant::fromValue(result);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
void *a[] = { &d()->container, 0 };
|
|
|
|
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;
|
|
|
|
QQmlPropertyPrivate::WriteFlags flags = QQmlPropertyPrivate::DontRemoveBinding;
|
2014-04-29 09:28:40 +00:00
|
|
|
void *a[] = { &d()->container, 0, &status, &flags };
|
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 09:42:01 +00:00
|
|
|
static QV4::ReturnedValue getIndexed(const QV4::Managed *that, uint index, bool *hasProperty)
|
|
|
|
{ return static_cast<const QQmlSequence<Container> *>(that)->containerGetIndexed(index, hasProperty); }
|
2015-01-15 10:36:57 +00:00
|
|
|
static void putIndexed(Managed *that, uint index, const QV4::Value &value)
|
2013-06-21 21:42:08 +00:00
|
|
|
{ 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); }
|
Remove type punning from QV4::Value.
The union in QV4::Value is used to do type punning. In C++, this is
compiler-defined behavior. For example, Clang and GCC will try to detect
it and try to do the proper thing. However, it can play havoc with Alias
Analysis, and it is not guaranteed that some Undefined Behavior (or
Compiler depenedent behavior) might occur.
The really problematic part is the struct inside the union: depending on
the calling convention and the register size, it results in some
exciting code. For example, the AMD64 ABI specifies that a struct of two
values of INTEGER class can be passed in separate registers when doing a
function call. Now, if the AA in the compiler looses track of the fact
that the tag overlaps with the double, you might get:
ecx := someTag
... conditional jumps
double_case:
rdx := xorredDoubleValue
callq someWhere
If the someWhere function checks for the tag first, mayhem ensues: the
double value in rdx does not overwrite the tag that is passed in ecx.
Changing the code to do reinterpret_cast<>s might also give problems
on 32bit architectures, because there is a double, whose size is not the
same as the size of the tag, which could confuse AA.
So, to fix this, the following is changed:
- only have a quint64 field in the QV4::Value, which has the added
benefit that it's very clear for the compiler that it's a POD
- as memcpy is the only approved way to ensure bit-by-bit "conversion"
between types (esp. FP<->non-FP types), change all conversions to use
memcpy. Use bitops (shift/and/or) for anything else.
- only use accessor functions for non-quint64 values
As any modern compiler has memcpy as an intrinsic, the call will be
replaced with one or a few move instructions. The accessor functions
also get inlined, the bitops get optimized, so in all cases the compiler
can generate the most compact code possible.
This patch obsoletes f558bc48585c69de36151248c969a484a969ebb4 (which had
the exact aliassing problem of the double and the tag as described
above).
Change-Id: I60a39d8564be5ce6106403a56a8de90943217006
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
2015-07-08 08:52:59 +00:00
|
|
|
static void advanceIterator(Managed *that, ObjectIterator *it, Value *name, uint *index, Property *p, PropertyAttributes *attrs)
|
2014-01-08 13:51:33 +00:00
|
|
|
{ return static_cast<QQmlSequence<Container> *>(that)->containerAdvanceIterator(it, name, index, p, attrs); }
|
2013-05-21 14:31:05 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-07 01:06:42 +00:00
|
|
|
|
|
|
|
template <typename Container>
|
2015-08-28 11:33:10 +00:00
|
|
|
Heap::QQmlSequence<Container>::QQmlSequence(const Container &container)
|
|
|
|
: container(container)
|
2014-11-07 01:06:42 +00:00
|
|
|
, propertyIndex(-1)
|
|
|
|
, isReference(false)
|
|
|
|
{
|
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>
|
2015-08-28 11:33:10 +00:00
|
|
|
Heap::QQmlSequence<Container>::QQmlSequence(QObject *object, int propertyIndex)
|
|
|
|
: object(object)
|
2014-11-07 01:06:42 +00:00
|
|
|
, propertyIndex(propertyIndex)
|
|
|
|
, isReference(true)
|
|
|
|
{
|
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 {
|
|
|
|
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QStringList> QQmlQStringList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QQmlQStringList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QString> > QQmlStringList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QQmlStringList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<int> > QQmlIntList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QQmlIntList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<QUrl> > QQmlUrlList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QQmlUrlList);
|
2015-03-03 17:36:51 +00:00
|
|
|
typedef QQmlSequence<QModelIndexList> QQmlQModelIndexList;
|
|
|
|
template<>
|
|
|
|
DEFINE_OBJECT_VTABLE(QQmlQModelIndexList);
|
2015-03-06 18:27:22 +00:00
|
|
|
typedef QQmlSequence<QItemSelection> QQmlQItemSelectionRangeList;
|
|
|
|
template<>
|
|
|
|
DEFINE_OBJECT_VTABLE(QQmlQItemSelectionRangeList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<bool> > QQmlBoolList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QQmlBoolList);
|
2013-05-31 11:05:37 +00:00
|
|
|
typedef QQmlSequence<QList<qreal> > QQmlRealList;
|
2013-05-21 14:31:05 +00:00
|
|
|
template<>
|
2014-01-20 12:51:00 +00:00
|
|
|
DEFINE_OBJECT_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
|
|
|
|
2013-11-03 14:23:05 +00:00
|
|
|
QV4::ReturnedValue SequencePrototype::method_sort(QV4::CallContext *ctx)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2013-10-07 10:23:38 +00:00
|
|
|
QV4::Scope scope(ctx);
|
2015-01-15 20:54:12 +00:00
|
|
|
QV4::ScopedObject o(scope, ctx->thisObject());
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!o || !o->isListType())
|
2014-07-28 08:07:57 +00:00
|
|
|
return ctx->engine()->throwTypeError();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2015-01-15 20:54:12 +00:00
|
|
|
if (ctx->argc() >= 2)
|
2013-10-07 10:23:38 +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>()) { \
|
|
|
|
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-11-01 11:38:32 +00:00
|
|
|
{}
|
2013-10-07 10:23:38 +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>()) { \
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocObject<QQml##ElementTypeName##List>(object, propertyIndex)); \
|
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::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
|
|
|
{
|
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>()) { \
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocObject<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
|
|
|
|
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
|