Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2012-02-16 04:43:03 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-20 05:21:40 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 11:55:39 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-19 09:38:36 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-20 05:21:40 +00:00
|
|
|
**
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-20 05:21:40 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-19 09:38:36 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
2016-01-19 09:38:36 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
#include <QtCore/qsequentialiterable.h>
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2013-05-21 15:06:36 +00:00
|
|
|
#include "qv4sequenceobject_p.h"
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
|
2013-05-08 05:32:45 +00:00
|
|
|
#include <private/qv4functionobject_p.h>
|
2013-05-21 14:31:05 +00:00
|
|
|
#include <private/qv4arrayobject_p.h>
|
|
|
|
#include <private/qqmlengine_p.h>
|
2013-09-05 11:22:23 +00:00
|
|
|
#include <private/qv4scopedvalue_p.h>
|
2017-09-01 08:09:45 +00:00
|
|
|
#include <private/qv4jscall_p.h>
|
2014-07-25 15:44:14 +00:00
|
|
|
#include "qv4runtime_p.h"
|
|
|
|
#include "qv4objectiterator_p.h"
|
2020-09-09 08:34:48 +00:00
|
|
|
#include <private/qqmlmetatype_p.h>
|
|
|
|
#include <private/qqmltype_p_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.
|
2014-11-13 11:14:09 +00:00
|
|
|
static void generateWarning(QV4::ExecutionEngine *v4, const QString& description)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-12-31 15:03:34 +00:00
|
|
|
QQmlEngine *engine = v4->qmlEngine();
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!engine)
|
|
|
|
return;
|
|
|
|
QQmlError retn;
|
|
|
|
retn.setDescription(description);
|
2013-06-07 18:49:28 +00:00
|
|
|
|
2017-08-09 15:14:11 +00:00
|
|
|
QV4::CppStackFrame *stackFrame = v4->currentStackFrame;
|
2013-06-07 18:49:28 +00:00
|
|
|
|
2017-08-21 08:59:53 +00:00
|
|
|
retn.setLine(stackFrame->lineNumber());
|
2017-08-03 11:41:13 +00:00
|
|
|
retn.setUrl(QUrl(stackFrame->source()));
|
2013-05-21 14:31:05 +00:00
|
|
|
QQmlEnginePrivate::warning(engine, retn);
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:06:42 +00:00
|
|
|
namespace QV4 {
|
|
|
|
|
|
|
|
namespace Heap {
|
|
|
|
|
2020-09-09 07:41:56 +00:00
|
|
|
struct QV4Sequence : Object {
|
2020-09-09 08:34:48 +00:00
|
|
|
void init(const QQmlType &qmlType, const void *container);
|
|
|
|
void init(QObject *object, int propertyIndex, const QQmlType &qmlType, bool readOnly);
|
2016-09-09 08:06:31 +00:00
|
|
|
void destroy() {
|
2020-09-09 08:34:48 +00:00
|
|
|
typePrivate->typeId.destroy(container);
|
|
|
|
QQmlType::derefHandle(typePrivate);
|
2016-09-09 08:06:31 +00:00
|
|
|
object.destroy();
|
2016-09-09 14:20:57 +00:00
|
|
|
Object::destroy();
|
2016-09-09 08:06:31 +00:00
|
|
|
}
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
mutable void *container;
|
|
|
|
const QQmlTypePrivate *typePrivate;
|
2020-09-09 07:44:14 +00:00
|
|
|
QV4QPointer<QObject> object;
|
2014-11-07 01:06:42 +00:00
|
|
|
int propertyIndex;
|
2018-04-18 08:18:48 +00:00
|
|
|
bool isReference : 1;
|
|
|
|
bool isReadOnly : 1;
|
2014-11-07 01:06:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:41:56 +00:00
|
|
|
struct QV4Sequence : public QV4::Object
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2020-09-09 08:34:48 +00:00
|
|
|
V4_OBJECT2(QV4Sequence, QV4::Object)
|
2020-09-09 07:41:56 +00:00
|
|
|
Q_MANAGED_TYPE(V4Sequence)
|
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
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
static const QMetaSequence *meta(const Heap::QV4Sequence *p)
|
|
|
|
{
|
|
|
|
return p->typePrivate->extraData.ld;
|
|
|
|
}
|
|
|
|
|
|
|
|
qsizetype size() const
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
return meta(p)->size(p->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant at(int index) const
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = meta(p);
|
|
|
|
QVariant result(m->valueMetaType());
|
|
|
|
m->valueAtIndex(p->container, index, result.data());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void append(const QVariant &item)
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = meta(p);
|
|
|
|
if (item.metaType() == m->valueMetaType()) {
|
|
|
|
m->addValueAtEnd(p->container, item.constData());
|
|
|
|
} else {
|
|
|
|
QVariant converted = item;
|
|
|
|
if (!converted.convert(m->valueMetaType()))
|
|
|
|
converted = QVariant(m->valueMetaType());
|
|
|
|
m->addValueAtEnd(p->container, converted.constData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void replace(int index, const QVariant &item)
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = meta(p);
|
|
|
|
if (item.metaType() == m->valueMetaType()) {
|
|
|
|
m->setValueAtIndex(p->container, index, item.constData());
|
|
|
|
} else {
|
|
|
|
QVariant converted = item;
|
|
|
|
if (!converted.convert(m->valueMetaType()))
|
|
|
|
converted = QVariant(m->valueMetaType());
|
|
|
|
m->setValueAtIndex(p->container, index, converted.constData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Compare>
|
|
|
|
void sort(const Compare &compare)
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = meta(p);
|
|
|
|
|
|
|
|
QSequentialIterable iterable(*m, p->typePrivate->typeId, p->container);
|
|
|
|
if (iterable.canRandomAccessIterate()) {
|
|
|
|
std::sort(QSequentialIterable::RandomAccessIterator(iterable.mutableBegin()),
|
|
|
|
QSequentialIterable::RandomAccessIterator(iterable.mutableEnd()),
|
|
|
|
compare);
|
|
|
|
} else if (iterable.canReverseIterate()) {
|
|
|
|
std::sort(QSequentialIterable::BidirectionalIterator(iterable.mutableBegin()),
|
|
|
|
QSequentialIterable::BidirectionalIterator(iterable.mutableEnd()),
|
|
|
|
compare);
|
|
|
|
} else {
|
|
|
|
qWarning() << "Container has no suitable iterator for sorting";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeLast(int num)
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = meta(p);
|
|
|
|
|
|
|
|
if (m->canEraseRangeAtIterator() && m->hasRandomAccessIterator() && num > 1) {
|
|
|
|
void *i = m->end(p->container);
|
|
|
|
m->advanceIterator(i, -num);
|
|
|
|
void *j = m->end(p->container);
|
|
|
|
m->eraseRangeAtIterator(p->container, i, j);
|
|
|
|
m->destroyIterator(i);
|
|
|
|
m->destroyIterator(j);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
m->removeValueAtEnd(p->container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant toVariant()
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
return QVariant(p->typePrivate->typeId, p->container);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
if (index < size()) {
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = true;
|
2020-09-09 08:34:48 +00:00
|
|
|
return engine()->fromVariant(at(index));
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
2013-09-26 20:07:27 +00:00
|
|
|
return Encode::undefined();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 21:57:11 +00:00
|
|
|
bool containerPutIndexed(uint index, const QV4::Value &value)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-04-05 18:23:20 +00:00
|
|
|
if (internalClass()->engine->hasException)
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-10-22 11:26:08 +00:00
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(engine(), QLatin1String("Index out of range during indexed set"));
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:49:18 +00:00
|
|
|
if (d()->isReadOnly) {
|
|
|
|
engine()->throwTypeError(QLatin1String("Cannot insert into a readonly container"));
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2019-12-02 15:49:18 +00:00
|
|
|
}
|
2018-04-18 08:18:48 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2017-02-08 21:57:11 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
qsizetype count = size();
|
|
|
|
const QMetaType valueMetaType = meta(d())->valueMetaType();
|
2021-03-24 14:36:37 +00:00
|
|
|
const QVariant element = engine()->toVariant(value, valueMetaType, false);
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2017-04-18 13:55:20 +00:00
|
|
|
if (index == count) {
|
2020-09-09 08:34:48 +00:00
|
|
|
append(element);
|
2017-04-18 13:55:20 +00:00
|
|
|
} else if (index < count) {
|
2020-09-09 08:34:48 +00:00
|
|
|
replace(index, element);
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* the value at the given index, increasing length to index+1. */
|
2020-09-09 08:34:48 +00:00
|
|
|
while (index > count++)
|
|
|
|
append(QVariant(valueMetaType));
|
|
|
|
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();
|
2017-02-08 21:57:11 +00:00
|
|
|
return true;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 14:29:53 +00:00
|
|
|
QV4::PropertyAttributes containerQueryIndexed(uint index) const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(engine(), QLatin1String("Index out of range during indexed query"));
|
2013-05-21 14:31:05 +00:00
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
}
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2013-05-21 14:31:05 +00:00
|
|
|
return QV4::Attr_Invalid;
|
|
|
|
loadReference();
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
return (index < size()) ? QV4::Attr_Data : QV4::Attr_Invalid;
|
2013-06-10 15:11:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:44:12 +00:00
|
|
|
struct OwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator
|
|
|
|
{
|
|
|
|
~OwnPropertyKeyIterator() override = default;
|
|
|
|
PropertyKey next(const Object *o, Property *pd = nullptr, PropertyAttributes *attrs = nullptr) override
|
|
|
|
{
|
2020-09-09 07:41:56 +00:00
|
|
|
const QV4Sequence *s = static_cast<const QV4Sequence *>(o);
|
2018-08-02 13:44:12 +00:00
|
|
|
|
|
|
|
if (s->d()->isReference) {
|
|
|
|
if (!s->d()->object)
|
|
|
|
return ObjectOwnPropertyKeyIterator::next(o, pd, attrs);
|
|
|
|
s->loadReference();
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
if (arrayIndex < s->size()) {
|
2018-08-02 13:44:12 +00:00
|
|
|
uint index = arrayIndex;
|
|
|
|
++arrayIndex;
|
|
|
|
if (attrs)
|
|
|
|
*attrs = QV4::Attr_Data;
|
|
|
|
if (pd)
|
2020-09-09 08:34:48 +00:00
|
|
|
pd->value = s->engine()->fromVariant(s->at(index));
|
2018-08-02 13:44:12 +00:00
|
|
|
return PropertyKey::fromArrayIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjectOwnPropertyKeyIterator::next(o, pd, attrs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-08 20:29:47 +00:00
|
|
|
static OwnPropertyKeyIterator *containerOwnPropertyKeys(const Object *m, Value *target)
|
2018-08-02 13:44:12 +00:00
|
|
|
{
|
2018-09-08 20:29:47 +00:00
|
|
|
*target = *m;
|
2018-08-02 13:44:12 +00:00
|
|
|
return new OwnPropertyKeyIterator;
|
|
|
|
}
|
|
|
|
|
2013-06-21 18:33:39 +00:00
|
|
|
bool containerDeleteIndexedProperty(uint index)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (index > INT_MAX)
|
|
|
|
return false;
|
2018-04-18 08:18:48 +00:00
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2013-05-21 14:31:05 +00:00
|
|
|
return false;
|
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
if (index >= size())
|
2013-05-21 14:31:05 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* according to ECMA262r3 it should be Undefined, */
|
|
|
|
/* but we cannot, so we insert a default-value instead. */
|
2020-09-09 08:34:48 +00:00
|
|
|
replace(index, QVariant());
|
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;
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4Sequence *otherSequence = other->as<QV4Sequence>();
|
2013-06-07 20:09:28 +00:00
|
|
|
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
|
|
|
|
{
|
2020-09-09 08:34:48 +00:00
|
|
|
bool operator()(const QVariant &lhs, const QVariant &rhs)
|
2013-06-24 15:00:28 +00:00
|
|
|
{
|
2020-09-09 08:34:48 +00:00
|
|
|
return lhs.toString() < rhs.toString();
|
2013-06-24 15:00:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompareFunctor
|
|
|
|
{
|
2017-01-05 21:21:35 +00:00
|
|
|
CompareFunctor(QV4::ExecutionEngine *v4, const QV4::Value &compareFn)
|
|
|
|
: m_v4(v4), m_compareFn(&compareFn)
|
2013-06-24 15:00:28 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
bool operator()(const QVariant &lhs, const QVariant &rhs)
|
2013-06-24 15:00:28 +00:00
|
|
|
{
|
2017-01-05 21:21:35 +00:00
|
|
|
QV4::Scope scope(m_v4);
|
2017-09-01 09:48:15 +00:00
|
|
|
ScopedFunctionObject compare(scope, m_compareFn);
|
|
|
|
if (!compare)
|
|
|
|
return m_v4->throwTypeError();
|
2017-12-05 09:45:14 +00:00
|
|
|
Value *argv = scope.alloc(2);
|
2020-09-09 08:34:48 +00:00
|
|
|
argv[0] = m_v4->fromVariant(lhs);
|
|
|
|
argv[1] = m_v4->fromVariant(rhs);
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::ScopedValue result(scope, compare->call(m_v4->globalObject, argv, 2));
|
2020-04-20 08:28:31 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return false;
|
2017-08-03 18:26:28 +00:00
|
|
|
return result->toNumber() < 0;
|
2013-06-24 15:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-01-05 21:21:35 +00:00
|
|
|
QV4::ExecutionEngine *m_v4;
|
2015-01-15 10:36:57 +00:00
|
|
|
const QV4::Value *m_compareFn;
|
2013-06-24 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
2018-04-18 08:18:48 +00:00
|
|
|
bool sort(const FunctionObject *f, const Value *, const Value *argv, int argc)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2018-04-18 08:18:48 +00:00
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
if (argc == 1 && argv[0].as<FunctionObject>())
|
|
|
|
sort(CompareFunctor(f->engine(), argv[0]));
|
|
|
|
else
|
|
|
|
sort(DefaultCompareFunctor());
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (d()->isReference)
|
2013-05-21 14:31:05 +00:00
|
|
|
storeReference();
|
2018-04-18 08:18:48 +00:00
|
|
|
|
|
|
|
return true;
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
static QV4::ReturnedValue method_get_length(const FunctionObject *b, const Value *thisObject, const Value *, int)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-08-04 16:53:51 +00:00
|
|
|
QV4::Scope scope(b);
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4::Scoped<QV4Sequence> This(scope, thisObject->as<QV4Sequence>());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_RESULT(Encode(0));
|
2013-06-13 11:43:09 +00:00
|
|
|
This->loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
RETURN_RESULT(Encode(qint32(This->size())));
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::Scope scope(f);
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4::Scoped<QV4Sequence> This(scope, thisObject->as<QV4Sequence>());
|
2013-06-13 11:43:09 +00:00
|
|
|
if (!This)
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
quint32 newLength = argc ? argv[0].toUInt32() : 0;
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Qt containers have int (rather than uint) allowable indexes. */
|
|
|
|
if (newLength > INT_MAX) {
|
2014-11-13 11:14:09 +00:00
|
|
|
generateWarning(scope.engine, QLatin1String("Index out of range during length set"));
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2018-04-18 08:18:48 +00:00
|
|
|
|
|
|
|
if (This->d()->isReadOnly)
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
|
2013-05-21 14:31:05 +00:00
|
|
|
/* Read the sequence from the QObject property if we're a reference */
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-06-13 11:43:09 +00:00
|
|
|
This->loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
/* Determine whether we need to modify the sequence */
|
2017-04-18 13:55:20 +00:00
|
|
|
quint32 newCount = static_cast<quint32>(newLength);
|
2020-09-09 08:34:48 +00:00
|
|
|
quint32 count = static_cast<quint32>(This->size());
|
2013-05-21 14:31:05 +00:00
|
|
|
if (newCount == count) {
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
} else if (newCount > count) {
|
2020-09-09 08:34:48 +00:00
|
|
|
const QMetaType valueMetaType = meta(This->d())->valueMetaType();
|
2013-05-21 14:31:05 +00:00
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* undefined values increasing length to newLength. */
|
|
|
|
/* We cannot, so we insert default-values instead. */
|
2020-09-09 08:34:48 +00:00
|
|
|
while (newCount > count++)
|
|
|
|
This->append(QVariant(valueMetaType));
|
2013-05-21 14:31:05 +00:00
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to remove */
|
|
|
|
/* elements until the sequence is the required length. */
|
2020-09-09 08:34:48 +00:00
|
|
|
if (newCount < count)
|
|
|
|
This->removeLast(count - newCount);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
/* write back if required. */
|
2014-04-29 09:28:40 +00:00
|
|
|
if (This->d()->isReference) {
|
2013-05-21 14:31:05 +00:00
|
|
|
/* write back. already checked that object is non-null, so skip that check here. */
|
2013-06-13 11:43:09 +00:00
|
|
|
This->storeReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2017-01-05 21:21:35 +00:00
|
|
|
RETURN_UNDEFINED();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 10:49:37 +00:00
|
|
|
void* getRawContainerPtr() const
|
|
|
|
{ return d()->container; }
|
|
|
|
|
2013-06-10 14:29:53 +00:00
|
|
|
void loadReference() const
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2014-04-29 09:28:40 +00:00
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
2018-02-21 09:41:54 +00:00
|
|
|
void *a[] = { d()->container, nullptr };
|
2014-04-29 09:28:40 +00:00
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::ReadProperty, d()->propertyIndex, a);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void storeReference()
|
|
|
|
{
|
2014-04-29 09:28:40 +00:00
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
2013-05-21 14:31:05 +00:00
|
|
|
int status = -1;
|
2016-07-14 10:09:23 +00:00
|
|
|
QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding;
|
2018-02-21 09:41:54 +00:00
|
|
|
void *a[] = { d()->container, nullptr, &status, &flags };
|
2014-04-29 09:28:40 +00:00
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 21:18:33 +00:00
|
|
|
static QV4::ReturnedValue virtualGet(const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty)
|
2018-06-22 20:59:43 +00:00
|
|
|
{
|
|
|
|
if (!id.isArrayIndex())
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualGet(that, id, receiver, hasProperty);
|
2020-09-09 08:34:48 +00:00
|
|
|
return static_cast<const QV4Sequence *>(that)->containerGetIndexed(id.asArrayIndex(), hasProperty);
|
2018-06-22 20:59:43 +00:00
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver)
|
2018-06-22 13:34:57 +00:00
|
|
|
{
|
|
|
|
if (id.isArrayIndex())
|
2020-09-09 08:34:48 +00:00
|
|
|
return static_cast<QV4Sequence *>(that)->containerPutIndexed(id.asArrayIndex(), value);
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualPut(that, id, value, receiver);
|
2018-06-22 13:34:57 +00:00
|
|
|
}
|
2013-06-10 14:29:53 +00:00
|
|
|
static QV4::PropertyAttributes queryIndexed(const QV4::Managed *that, uint index)
|
2020-09-09 08:34:48 +00:00
|
|
|
{ return static_cast<const QV4Sequence *>(that)->containerQueryIndexed(index); }
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id)
|
2018-06-19 19:29:01 +00:00
|
|
|
{
|
|
|
|
if (id.isArrayIndex()) {
|
|
|
|
uint index = id.asArrayIndex();
|
2020-09-09 08:34:48 +00:00
|
|
|
return static_cast<QV4Sequence *>(that)->containerDeleteIndexedProperty(index);
|
2018-06-19 19:29:01 +00:00
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
return Object::virtualDeleteProperty(that, id);
|
2018-06-19 19:29:01 +00:00
|
|
|
}
|
2018-06-25 21:18:33 +00:00
|
|
|
static bool virtualIsEqualTo(Managed *that, Managed *other)
|
2020-09-09 08:34:48 +00:00
|
|
|
{ return static_cast<QV4Sequence *>(that)->containerIsEqualTo(other); }
|
2018-09-08 20:29:47 +00:00
|
|
|
static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target)
|
2020-09-09 08:34:48 +00:00
|
|
|
{ return static_cast<const QV4Sequence *>(m)->containerOwnPropertyKeys(m, target);}
|
2013-05-21 14:31:05 +00:00
|
|
|
};
|
|
|
|
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
void Heap::QV4Sequence::init(const QQmlType &qmlType, const void *container)
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Object::init();
|
2020-09-09 08:34:48 +00:00
|
|
|
|
|
|
|
Q_ASSERT(qmlType.isSequentialContainer());
|
|
|
|
typePrivate = qmlType.priv();
|
|
|
|
QQmlType::refHandle(typePrivate);
|
|
|
|
|
|
|
|
this->container = QMetaType(typePrivate->typeId).create(container);
|
2016-09-09 13:37:57 +00:00
|
|
|
propertyIndex = -1;
|
|
|
|
isReference = false;
|
2018-04-18 08:18:48 +00:00
|
|
|
isReadOnly = false;
|
2016-09-07 11:31:14 +00:00
|
|
|
object.init();
|
|
|
|
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::Scope scope(internalClass->engine);
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4::Scoped<QV4::QV4Sequence> o(scope, this);
|
2014-11-07 01:06:42 +00:00
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
|
|
|
o->init();
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
void Heap::QV4Sequence::init(QObject *object, int propertyIndex, const QQmlType &qmlType,
|
|
|
|
bool readOnly)
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2016-09-09 13:37:57 +00:00
|
|
|
Object::init();
|
2020-09-09 08:34:48 +00:00
|
|
|
|
|
|
|
Q_ASSERT(qmlType.isSequentialContainer());
|
|
|
|
typePrivate = qmlType.priv();
|
|
|
|
QQmlType::refHandle(typePrivate);
|
|
|
|
container = QMetaType(typePrivate->typeId).create();
|
2016-09-09 13:37:57 +00:00
|
|
|
this->propertyIndex = propertyIndex;
|
|
|
|
isReference = true;
|
2018-04-18 08:18:48 +00:00
|
|
|
this->isReadOnly = readOnly;
|
2016-09-07 11:31:14 +00:00
|
|
|
this->object.init(object);
|
2015-08-28 11:33:10 +00:00
|
|
|
QV4::Scope scope(internalClass->engine);
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4::Scoped<QV4::QV4Sequence> o(scope, this);
|
2014-11-07 01:06:42 +00:00
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
|
|
|
o->loadReference();
|
|
|
|
o->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-09 20:55:23 +00:00
|
|
|
namespace QV4 {
|
2020-09-09 08:34:48 +00:00
|
|
|
DEFINE_OBJECT_VTABLE(QV4Sequence);
|
|
|
|
}
|
|
|
|
|
2013-09-18 10:31:55 +00:00
|
|
|
void SequencePrototype::init()
|
2013-06-13 11:43:09 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
ReturnedValue SequencePrototype::method_valueOf(const FunctionObject *f, const Value *thisObject, const Value *, int)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2017-12-05 09:45:14 +00:00
|
|
|
return Encode(thisObject->toString(f->engine()));
|
2017-08-04 16:53:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
ReturnedValue SequencePrototype::method_sort(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
|
2017-08-04 16:53:51 +00:00
|
|
|
{
|
|
|
|
Scope scope(b);
|
2017-12-05 09:45:14 +00:00
|
|
|
QV4::ScopedObject o(scope, thisObject);
|
2013-05-21 14:31:05 +00:00
|
|
|
if (!o || !o->isListType())
|
2017-01-05 21:21:35 +00:00
|
|
|
THROW_TYPE_ERROR();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2017-12-05 09:45:14 +00:00
|
|
|
if (argc >= 2)
|
2017-08-04 16:53:51 +00:00
|
|
|
return o.asReturnedValue();
|
2013-05-31 11:05:37 +00:00
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
if (auto *s = o->as<QV4Sequence>()) {
|
|
|
|
if (!s->sort(b, thisObject, argv, argc))
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2017-08-04 16:53:51 +00:00
|
|
|
return o.asReturnedValue();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
ReturnedValue SequencePrototype::newSequence(
|
|
|
|
QV4::ExecutionEngine *engine, QMetaType sequenceType, QObject *object,
|
|
|
|
int propertyIndex, bool readOnly, bool *succeeded)
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
{
|
2013-09-12 20:37:41 +00:00
|
|
|
QV4::Scope scope(engine);
|
Add support for more sequence types
This commit adds support for more sequence types by adding a sequence
wrapper. This class enables conversion between v8::Array and C++
sequences of various types (currently just QList<int>, QList<qreal>,
QList<bool>, QList<QString>, QList<QUrl> and QStringList), but more
types can be added later if required).
When a JavaScript object is created from such a sequence, its
prototype object is set to the v8::Array prototype object. The
indexed setter, indexed getter, length and toString methods are
implemented directly or in terms of the underlying sequence resource.
Note that currently, sequences of ValueTypes are NOT supported, due to
the fact that operations like:
someObj.someValueTypeSequence[i].x = 5;
would not behave as required.
Task-number: QTBUG-20826
Task-number: QTBUG-21770
Change-Id: I36deb448fb0e87a32084a900e70a2604ff369309
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
2011-10-03 00:52:38 +00:00
|
|
|
// This function is called when the property is a QObject Q_PROPERTY of
|
2020-09-09 08:34:48 +00:00
|
|
|
// the given sequence type. Internally we store a 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
|
|
|
// (as well as object ptr + property index for updated-read and write-back)
|
|
|
|
// and so access/mutate avoids variant conversion.
|
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
const QQmlType qmlType = QQmlMetaType::qmlType(sequenceType);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (qmlType.isSequentialContainer()) {
|
|
|
|
*succeeded = true;
|
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QV4Sequence>(
|
|
|
|
object, propertyIndex, qmlType, readOnly));
|
|
|
|
return obj.asReturnedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
*succeeded = false;
|
|
|
|
return Encode::undefined();
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:07:58 +00:00
|
|
|
ReturnedValue SequencePrototype::fromVariant(
|
|
|
|
QV4::ExecutionEngine *engine, const QVariant &v, bool *succeeded)
|
|
|
|
{
|
|
|
|
return fromData(engine, v.metaType(), v.constData(), succeeded);
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
ReturnedValue SequencePrototype::fromData(ExecutionEngine *engine, QMetaType type, const void *data, 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.
|
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
const QQmlType qmlType = QQmlMetaType::qmlType(type);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (qmlType.isSequentialContainer()) {
|
|
|
|
*succeeded = true;
|
2021-03-11 10:07:58 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QV4Sequence>(qmlType, data));
|
2020-09-09 08:34:48 +00:00
|
|
|
return obj.asReturnedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
*succeeded = false;
|
|
|
|
return 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
|
|
|
|
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());
|
2020-09-09 08:34:48 +00:00
|
|
|
return object->as<QV4Sequence>()->toVariant();
|
2013-05-14 13:59:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
QVariant SequencePrototype::toVariant(const QV4::Value &array, QMetaType 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
|
|
|
|
2021-06-07 13:28:54 +00:00
|
|
|
const QQmlType type = QQmlMetaType::qmlType(typeHint);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (type.isSequentialContainer()) {
|
|
|
|
const QMetaSequence *meta = type.priv()->extraData.ld;
|
|
|
|
const QMetaType containerMetaType(type.priv()->typeId);
|
|
|
|
QVariant result(containerMetaType);
|
|
|
|
quint32 length = a->getLength();
|
|
|
|
QV4::ScopedValue v(scope);
|
|
|
|
for (quint32 i = 0; i < length; ++i) {
|
|
|
|
const QMetaType valueMetaType = meta->valueMetaType();
|
2021-03-24 14:36:37 +00:00
|
|
|
QVariant variant = scope.engine->toVariant(a->get(i), valueMetaType, false);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (variant.metaType() != valueMetaType && !variant.convert(valueMetaType))
|
|
|
|
variant = QVariant(valueMetaType);
|
|
|
|
meta->addValueAtEnd(result.data(), variant.constData());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
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
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
*succeeded = false;
|
|
|
|
return QVariant();
|
|
|
|
}
|
2017-04-26 10:49:37 +00:00
|
|
|
|
|
|
|
void* SequencePrototype::getRawContainerPtr(const Object *object, int typeHint)
|
|
|
|
{
|
2020-09-09 08:34:48 +00:00
|
|
|
if (auto *s = object->as<QV4Sequence>()) {
|
|
|
|
if (s->d()->typePrivate->typeId.id() == typeHint)
|
|
|
|
return s->getRawContainerPtr();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2017-04-26 10:49:37 +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
|
|
|
{
|
2020-09-09 08:34:48 +00:00
|
|
|
if (auto *s = object->as<QV4Sequence>())
|
|
|
|
return s->d()->typePrivate->typeId.id();
|
|
|
|
return -1;
|
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
|