2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
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
|
|
|
#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>
|
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
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
namespace QV4 {
|
|
|
|
|
|
|
|
DEFINE_OBJECT_VTABLE(Sequence);
|
|
|
|
|
|
|
|
static const QMetaSequence *metaSequence(const Heap::Sequence *p)
|
|
|
|
{
|
|
|
|
return p->typePrivate->extraData.ld;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Compare>
|
|
|
|
void sortSequence(Sequence *sequence, const Compare &compare)
|
|
|
|
{
|
|
|
|
const auto *p = sequence->d();
|
|
|
|
const auto *m = metaSequence(p);
|
|
|
|
|
|
|
|
QSequentialIterable iterable(*m, p->typePrivate->listId, 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";
|
|
|
|
}
|
|
|
|
}
|
2013-05-21 15:06:36 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
struct SequenceOwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator
|
|
|
|
{
|
|
|
|
~SequenceOwnPropertyKeyIterator() override = default;
|
|
|
|
PropertyKey next(const Object *o, Property *pd = nullptr, PropertyAttributes *attrs = nullptr) override
|
|
|
|
{
|
|
|
|
const Sequence *s = static_cast<const Sequence *>(o);
|
|
|
|
|
|
|
|
if (s->d()->isReference) {
|
|
|
|
if (!s->d()->object)
|
|
|
|
return ObjectOwnPropertyKeyIterator::next(o, pd, attrs);
|
|
|
|
s->loadReference();
|
|
|
|
}
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
const qsizetype size = s->size();
|
|
|
|
if (size > 0 && qIsAtMostSizetypeLimit(arrayIndex, size - 1)) {
|
|
|
|
const uint index = arrayIndex;
|
2022-05-17 13:50:06 +00:00
|
|
|
++arrayIndex;
|
|
|
|
if (attrs)
|
|
|
|
*attrs = QV4::Attr_Data;
|
|
|
|
if (pd)
|
2022-07-12 14:08:17 +00:00
|
|
|
pd->value = s->engine()->fromVariant(s->at(qsizetype(index)));
|
2022-05-17 13:50:06 +00:00
|
|
|
return PropertyKey::fromArrayIndex(index);
|
|
|
|
}
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
if (memberIndex == 0) {
|
|
|
|
++memberIndex;
|
|
|
|
return o->engine()->id_length()->propertyKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
// You cannot add any own properties via the regular JavaScript interfaces.
|
|
|
|
return PropertyKey::invalid();
|
2016-09-09 08:06:31 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SequenceCompareFunctor
|
|
|
|
{
|
|
|
|
SequenceCompareFunctor(QV4::ExecutionEngine *v4, const QV4::Value &compareFn)
|
|
|
|
: m_v4(v4), m_compareFn(&compareFn)
|
|
|
|
{}
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool operator()(const QVariant &lhs, const QVariant &rhs)
|
|
|
|
{
|
|
|
|
QV4::Scope scope(m_v4);
|
|
|
|
ScopedFunctionObject compare(scope, m_compareFn);
|
|
|
|
if (!compare)
|
|
|
|
return m_v4->throwTypeError();
|
|
|
|
Value *argv = scope.alloc(2);
|
|
|
|
argv[0] = m_v4->fromVariant(lhs);
|
|
|
|
argv[1] = m_v4->fromVariant(rhs);
|
|
|
|
QV4::ScopedValue result(scope, compare->call(m_v4->globalObject, argv, 2));
|
|
|
|
if (scope.hasException())
|
|
|
|
return false;
|
|
|
|
return result->toNumber() < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QV4::ExecutionEngine *m_v4;
|
|
|
|
const QV4::Value *m_compareFn;
|
2014-11-07 01:06:42 +00:00
|
|
|
};
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
struct SequenceDefaultCompareFunctor
|
|
|
|
{
|
|
|
|
bool operator()(const QVariant &lhs, const QVariant &rhs)
|
|
|
|
{
|
|
|
|
return lhs.toString() < rhs.toString();
|
|
|
|
}
|
|
|
|
};
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void Heap::Sequence::init(const QQmlType &qmlType, const void *container)
|
2022-01-05 16:45:08 +00:00
|
|
|
{
|
2022-05-17 13:50:06 +00:00
|
|
|
Object::init();
|
|
|
|
|
|
|
|
Q_ASSERT(qmlType.isSequentialContainer());
|
|
|
|
typePrivate = qmlType.priv();
|
|
|
|
QQmlType::refHandle(typePrivate);
|
|
|
|
|
|
|
|
this->container = typePrivate->listId.create(container);
|
|
|
|
propertyIndex = -1;
|
|
|
|
isReference = false;
|
|
|
|
isReadOnly = false;
|
|
|
|
object.init();
|
|
|
|
|
|
|
|
QV4::Scope scope(internalClass->engine);
|
|
|
|
QV4::Scoped<QV4::Sequence> o(scope, this);
|
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
2022-01-05 16:45:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void Heap::Sequence::init(
|
|
|
|
QObject *object, int propertyIndex, const QQmlType &qmlType, bool readOnly)
|
2013-05-21 14:31:05 +00:00
|
|
|
{
|
2022-05-17 13:50:06 +00:00
|
|
|
Object::init();
|
2013-06-13 11:43:09 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
Q_ASSERT(qmlType.isSequentialContainer());
|
|
|
|
typePrivate = qmlType.priv();
|
|
|
|
QQmlType::refHandle(typePrivate);
|
|
|
|
container = QMetaType(typePrivate->listId).create();
|
|
|
|
this->propertyIndex = propertyIndex;
|
|
|
|
isReference = true;
|
|
|
|
this->isReadOnly = readOnly;
|
|
|
|
this->object.init(object);
|
|
|
|
QV4::Scope scope(internalClass->engine);
|
|
|
|
QV4::Scoped<QV4::Sequence> o(scope, this);
|
|
|
|
o->setArrayType(Heap::ArrayData::Custom);
|
|
|
|
o->loadReference();
|
|
|
|
}
|
2022-01-07 10:01:56 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void Heap::Sequence::destroy()
|
|
|
|
{
|
|
|
|
typePrivate->listId.destroy(container);
|
|
|
|
QQmlType::derefHandle(typePrivate);
|
|
|
|
object.destroy();
|
|
|
|
Object::destroy();
|
|
|
|
}
|
2022-01-07 10:01:56 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
const QMetaType Sequence::valueMetaType(const Heap::Sequence *p)
|
|
|
|
{
|
|
|
|
return p->typePrivate->typeId;
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
qsizetype Sequence::size() const
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
return metaSequence(p)->size(p->container);
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
QVariant Sequence::at(qsizetype index) const
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const QMetaType v = valueMetaType(p);
|
|
|
|
QVariant result;
|
|
|
|
if (v == QMetaType::fromType<QVariant>()) {
|
|
|
|
metaSequence(p)->valueAtIndex(p->container, index, &result);
|
|
|
|
} else {
|
|
|
|
result = QVariant(v);
|
|
|
|
metaSequence(p)->valueAtIndex(p->container, index, result.data());
|
2020-09-09 08:34:48 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
|
|
|
|
template<typename Action>
|
|
|
|
void convertAndDo(const QVariant &item, const QMetaType v, Action action)
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
|
|
|
if (item.metaType() == v) {
|
2022-07-12 14:08:17 +00:00
|
|
|
action(item.constData());
|
2022-05-17 13:50:06 +00:00
|
|
|
} else if (v == QMetaType::fromType<QVariant>()) {
|
2022-07-12 14:08:17 +00:00
|
|
|
action(&item);
|
2022-05-17 13:50:06 +00:00
|
|
|
} else {
|
|
|
|
QVariant converted = item;
|
|
|
|
if (!converted.convert(v))
|
|
|
|
converted = QVariant(v);
|
2022-07-12 14:08:17 +00:00
|
|
|
action(converted.constData());
|
2020-09-09 08:34:48 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
void Sequence::append(const QVariant &item)
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
2022-07-12 14:08:17 +00:00
|
|
|
const Heap::Sequence *p = d();
|
|
|
|
convertAndDo(item, valueMetaType(p), [p](const void *data) {
|
|
|
|
metaSequence(p)->addValueAtEnd(p->container, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequence::append(qsizetype num, const QVariant &item)
|
|
|
|
{
|
|
|
|
const Heap::Sequence *p = d();
|
|
|
|
convertAndDo(item, valueMetaType(p), [p, num](const void *data) {
|
|
|
|
const QMetaSequence *m = metaSequence(p);
|
|
|
|
void *container = p->container;
|
|
|
|
for (qsizetype i = 0; i < num; ++i)
|
|
|
|
m->addValueAtEnd(container, data);
|
|
|
|
});
|
2022-05-17 13:50:06 +00:00
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
void Sequence::replace(qsizetype index, const QVariant &item)
|
|
|
|
{
|
|
|
|
const Heap::Sequence *p = d();
|
|
|
|
convertAndDo(item, valueMetaType(p), [p, index](const void *data) {
|
|
|
|
metaSequence(p)->setValueAtIndex(p->container, index, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sequence::removeLast(qsizetype num)
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
const auto *m = metaSequence(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);
|
2020-09-09 08:34:48 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
QVariant Sequence::toVariant() const
|
|
|
|
{
|
|
|
|
const auto *p = d();
|
|
|
|
return QVariant(p->typePrivate->listId, p->container);
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
ReturnedValue Sequence::containerGetIndexed(qsizetype index, bool *hasProperty) const
|
2022-05-17 13:50:06 +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
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
2022-07-12 14:08:17 +00:00
|
|
|
if (index >= 0 && index < size()) {
|
2013-05-21 14:31:05 +00:00
|
|
|
if (hasProperty)
|
2022-05-17 13:50:06 +00:00
|
|
|
*hasProperty = true;
|
|
|
|
return engine()->fromVariant(at(index));
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
if (hasProperty)
|
|
|
|
*hasProperty = false;
|
|
|
|
return Encode::undefined();
|
|
|
|
}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
bool Sequence::containerPutIndexed(qsizetype index, const Value &value)
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
|
|
|
if (internalClass()->engine->hasException)
|
|
|
|
return false;
|
2013-10-22 11:26:08 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (d()->isReadOnly) {
|
|
|
|
engine()->throwTypeError(QLatin1String("Cannot insert into a readonly container"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2022-05-17 13:50:06 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
2018-04-18 08:18:48 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
const qsizetype count = size();
|
2022-05-17 13:50:06 +00:00
|
|
|
const QMetaType valueType = valueMetaType(d());
|
2022-09-13 12:57:47 +00:00
|
|
|
const QVariant element = ExecutionEngine::toVariant(value, valueType, false);
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
if (index < 0)
|
|
|
|
return false;
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (index == count) {
|
|
|
|
append(element);
|
|
|
|
} else if (index < count) {
|
|
|
|
replace(index, element);
|
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* the value at the given index, increasing length to index+1. */
|
2022-07-12 14:08:17 +00:00
|
|
|
append(index - count,
|
|
|
|
valueType == QMetaType::fromType<QVariant>() ? QVariant() : QVariant(valueType));
|
2022-05-17 13:50:06 +00:00
|
|
|
append(element);
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (d()->isReference)
|
|
|
|
storeReference();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SequenceOwnPropertyKeyIterator *containerOwnPropertyKeys(const Object *m, Value *target)
|
|
|
|
{
|
|
|
|
*target = *m;
|
|
|
|
return new SequenceOwnPropertyKeyIterator;
|
|
|
|
}
|
2018-08-02 13:44:12 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
bool Sequence::containerDeleteIndexedProperty(qsizetype index)
|
2022-05-17 13:50:06 +00:00
|
|
|
{
|
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2022-05-17 13:50:06 +00:00
|
|
|
loadReference();
|
|
|
|
}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
if (index < 0 || index >= size())
|
2022-05-17 13:50:06 +00:00
|
|
|
return false;
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
/* according to ECMA262r3 it should be Undefined, */
|
|
|
|
/* but we cannot, so we insert a default-value instead. */
|
|
|
|
replace(index, QVariant());
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (d()->isReference)
|
|
|
|
storeReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool Sequence::containerIsEqualTo(Managed *other)
|
|
|
|
{
|
|
|
|
if (!other)
|
|
|
|
return false;
|
|
|
|
Sequence *otherSequence = other->as<Sequence>();
|
|
|
|
if (!otherSequence)
|
2013-06-07 20:09:28 +00:00
|
|
|
return false;
|
2022-05-17 13:50:06 +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) {
|
|
|
|
return this == otherSequence;
|
2013-06-07 20:09:28 +00:00
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-07 20:09:28 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool Sequence::sort(const FunctionObject *f, const Value *, const Value *argv, int argc)
|
|
|
|
{
|
|
|
|
if (d()->isReadOnly)
|
|
|
|
return false;
|
|
|
|
if (d()->isReference) {
|
|
|
|
if (!d()->object)
|
2018-04-18 08:18:48 +00:00
|
|
|
return false;
|
2022-05-17 13:50:06 +00:00
|
|
|
loadReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (argc == 1 && argv[0].as<FunctionObject>())
|
|
|
|
sortSequence(this, SequenceCompareFunctor(f->engine(), argv[0]));
|
|
|
|
else
|
|
|
|
sortSequence(this, SequenceDefaultCompareFunctor());
|
2017-04-26 10:49:37 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (d()->isReference)
|
|
|
|
storeReference();
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-05-21 14:31:05 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void *Sequence::getRawContainerPtr() const
|
|
|
|
{ return d()->container; }
|
2014-11-07 01:06:42 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void Sequence::loadReference() const
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2022-05-17 13:50:06 +00:00
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
|
|
|
void *a[] = { d()->container, nullptr };
|
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::ReadProperty, d()->propertyIndex, a);
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void Sequence::storeReference()
|
|
|
|
{
|
|
|
|
Q_ASSERT(d()->object);
|
|
|
|
Q_ASSERT(d()->isReference);
|
|
|
|
int status = -1;
|
|
|
|
QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding;
|
|
|
|
void *a[] = { d()->container, nullptr, &status, &flags };
|
|
|
|
QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a);
|
|
|
|
}
|
2016-09-07 11:31:14 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
ReturnedValue Sequence::virtualGet(const Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty)
|
|
|
|
{
|
2022-07-12 14:08:17 +00:00
|
|
|
if (id.isArrayIndex()) {
|
|
|
|
const uint index = id.asArrayIndex();
|
|
|
|
if (qIsAtMostSizetypeLimit(index))
|
|
|
|
return static_cast<const Sequence *>(that)->containerGetIndexed(qsizetype(index), hasProperty);
|
|
|
|
|
|
|
|
generateWarning(that->engine(), QLatin1String("Index out of range during indexed get"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object::virtualGet(that, id, receiver, hasProperty);
|
2014-11-07 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool Sequence::virtualPut(Managed *that, PropertyKey id, const Value &value, Value *receiver)
|
2014-11-07 01:06:42 +00:00
|
|
|
{
|
2022-07-12 14:08:17 +00:00
|
|
|
if (id.isArrayIndex()) {
|
|
|
|
const uint index = id.asArrayIndex();
|
|
|
|
if (qIsAtMostSizetypeLimit(index))
|
|
|
|
return static_cast<Sequence *>(that)->containerPutIndexed(qsizetype(index), value);
|
|
|
|
|
|
|
|
generateWarning(that->engine(), QLatin1String("Index out of range during indexed set"));
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-17 13:50:06 +00:00
|
|
|
return Object::virtualPut(that, id, value, receiver);
|
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool Sequence::virtualDeleteProperty(Managed *that, PropertyKey id)
|
|
|
|
{
|
|
|
|
if (id.isArrayIndex()) {
|
2022-07-12 14:08:17 +00:00
|
|
|
const uint index = id.asArrayIndex();
|
|
|
|
if (qIsAtMostSizetypeLimit(index))
|
|
|
|
return static_cast<Sequence *>(that)->containerDeleteIndexedProperty(qsizetype(index));
|
|
|
|
|
|
|
|
generateWarning(that->engine(), QLatin1String("Index out of range during indexed delete"));
|
|
|
|
return false;
|
2022-05-17 13:50:06 +00:00
|
|
|
}
|
|
|
|
return Object::virtualDeleteProperty(that, id);
|
2014-11-07 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
bool Sequence::virtualIsEqualTo(Managed *that, Managed *other)
|
|
|
|
{
|
|
|
|
return static_cast<Sequence *>(that)->containerIsEqualTo(other);
|
2014-11-07 01:06:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
OwnPropertyKeyIterator *Sequence::virtualOwnPropertyKeys(const Object *m, Value *target)
|
|
|
|
{
|
|
|
|
return containerOwnPropertyKeys(m, target);
|
2020-09-09 08:34:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 16:45:08 +00:00
|
|
|
static QV4::ReturnedValue method_get_length(const FunctionObject *b, const Value *thisObject, const Value *, int)
|
|
|
|
{
|
|
|
|
QV4::Scope scope(b);
|
2022-05-17 13:50:06 +00:00
|
|
|
QV4::Scoped<Sequence> This(scope, thisObject->as<Sequence>());
|
2022-01-05 16:45:08 +00:00
|
|
|
if (!This)
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
|
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
|
|
|
RETURN_RESULT(Encode(0));
|
|
|
|
This->loadReference();
|
|
|
|
}
|
2022-07-12 14:08:17 +00:00
|
|
|
|
|
|
|
const qsizetype size = This->size();
|
|
|
|
if (qIsAtMostUintLimit(size))
|
|
|
|
RETURN_RESULT(Encode(uint(size)));
|
|
|
|
|
|
|
|
return scope.engine->throwRangeError(QLatin1String("Sequence length out of range"));
|
2022-01-05 16:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc)
|
|
|
|
{
|
|
|
|
QV4::Scope scope(f);
|
2022-05-17 13:50:06 +00:00
|
|
|
QV4::Scoped<Sequence> This(scope, thisObject->as<Sequence>());
|
2022-01-05 16:45:08 +00:00
|
|
|
if (!This)
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
bool ok = false;
|
|
|
|
const quint32 argv0 = argc ? argv[0].asArrayLength(&ok) : 0;
|
|
|
|
if (!ok || !qIsAtMostSizetypeLimit(argv0)) {
|
2022-01-05 16:45:08 +00:00
|
|
|
generateWarning(scope.engine, QLatin1String("Index out of range during length set"));
|
|
|
|
RETURN_UNDEFINED();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (This->d()->isReadOnly)
|
|
|
|
THROW_TYPE_ERROR();
|
|
|
|
|
2022-07-12 14:08:17 +00:00
|
|
|
const qsizetype newCount = qsizetype(argv0);
|
|
|
|
|
2022-01-05 16:45:08 +00:00
|
|
|
/* Read the sequence from the QObject property if we're a reference */
|
|
|
|
if (This->d()->isReference) {
|
|
|
|
if (!This->d()->object)
|
|
|
|
RETURN_UNDEFINED();
|
|
|
|
This->loadReference();
|
|
|
|
}
|
2022-07-12 14:08:17 +00:00
|
|
|
|
2022-01-05 16:45:08 +00:00
|
|
|
/* Determine whether we need to modify the sequence */
|
2022-07-12 14:08:17 +00:00
|
|
|
const qsizetype count = This->size();
|
2022-01-05 16:45:08 +00:00
|
|
|
if (newCount == count) {
|
|
|
|
RETURN_UNDEFINED();
|
|
|
|
} else if (newCount > count) {
|
2022-05-17 13:50:06 +00:00
|
|
|
const QMetaType valueMetaType = metaSequence(This->d())->valueMetaType();
|
2022-01-05 16:45:08 +00:00
|
|
|
/* according to ECMA262r3 we need to insert */
|
|
|
|
/* undefined values increasing length to newLength. */
|
|
|
|
/* We cannot, so we insert default-values instead. */
|
2022-07-12 14:08:17 +00:00
|
|
|
This->append(newCount - count, QVariant(valueMetaType));
|
2022-01-05 16:45:08 +00:00
|
|
|
} else {
|
|
|
|
/* according to ECMA262r3 we need to remove */
|
|
|
|
/* elements until the sequence is the required length. */
|
2022-07-12 14:08:17 +00:00
|
|
|
Q_ASSERT(newCount < count);
|
|
|
|
This->removeLast(count - newCount);
|
2022-01-05 16:45:08 +00:00
|
|
|
}
|
2022-07-12 14:08:17 +00:00
|
|
|
|
2022-01-05 16:45:08 +00:00
|
|
|
/* write back if required. */
|
|
|
|
if (This->d()->isReference) {
|
|
|
|
/* write back. already checked that object is non-null, so skip that check here. */
|
|
|
|
This->storeReference();
|
|
|
|
}
|
2022-07-12 14:08:17 +00:00
|
|
|
|
2022-01-05 16:45:08 +00:00
|
|
|
RETURN_UNDEFINED();
|
|
|
|
}
|
|
|
|
|
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);
|
2022-01-05 16:45:08 +00:00
|
|
|
defineAccessorProperty(QStringLiteral("length"), method_get_length, method_set_length);
|
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);
|
2022-07-05 14:24:05 +00:00
|
|
|
if (!o || !o->isV4SequenceType())
|
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
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
if (auto *s = o->as<Sequence>()) {
|
2020-09-09 08:34:48 +00:00
|
|
|
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.
|
|
|
|
|
2022-01-07 10:01:56 +00:00
|
|
|
const QQmlType qmlType = QQmlMetaType::qmlListType(sequenceType);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (qmlType.isSequentialContainer()) {
|
|
|
|
*succeeded = true;
|
2022-05-17 13:50:06 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<Sequence>(
|
2020-09-09 08:34:48 +00:00
|
|
|
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.
|
|
|
|
|
2022-01-07 10:01:56 +00:00
|
|
|
const QQmlType qmlType = QQmlMetaType::qmlListType(type);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (qmlType.isSequentialContainer()) {
|
|
|
|
*succeeded = true;
|
2022-05-17 13:50:06 +00:00
|
|
|
QV4::ScopedObject obj(scope, engine->memoryManager->allocate<Sequence>(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
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
QVariant SequencePrototype::toVariant(const Sequence *object)
|
2013-05-14 13:59:10 +00:00
|
|
|
{
|
2022-07-05 14:24:05 +00:00
|
|
|
Q_ASSERT(object->isV4SequenceType());
|
2022-05-17 13:50:06 +00:00
|
|
|
return object->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
|
|
|
|
2022-01-07 10:01:56 +00:00
|
|
|
const QQmlType type = QQmlMetaType::qmlListType(typeHint);
|
2020-09-09 08:34:48 +00:00
|
|
|
if (type.isSequentialContainer()) {
|
2022-01-07 10:01:56 +00:00
|
|
|
const QQmlTypePrivate *priv = type.priv();
|
|
|
|
const QMetaSequence *meta = priv->extraData.ld;
|
|
|
|
const QMetaType containerMetaType(priv->listId);
|
2020-09-09 08:34:48 +00:00
|
|
|
QVariant result(containerMetaType);
|
2022-07-12 14:08:17 +00:00
|
|
|
qint64 length = a->getLength();
|
|
|
|
Q_ASSERT(length >= 0);
|
|
|
|
Q_ASSERT(length <= qint64(std::numeric_limits<quint32>::max()));
|
|
|
|
|
2020-09-09 08:34:48 +00:00
|
|
|
QV4::ScopedValue v(scope);
|
2022-07-12 14:08:17 +00:00
|
|
|
for (quint32 i = 0; i < quint32(length); ++i) {
|
2022-01-07 10:01:56 +00:00
|
|
|
const QMetaType valueMetaType = priv->typeId;
|
2022-09-13 12:57:47 +00:00
|
|
|
QVariant variant = ExecutionEngine::toVariant(a->get(i), valueMetaType, false);
|
2022-02-03 13:43:47 +00:00
|
|
|
if (valueMetaType == QMetaType::fromType<QVariant>()) {
|
|
|
|
meta->addValueAtEnd(result.data(), &variant);
|
|
|
|
} else {
|
|
|
|
const QMetaType originalType = variant.metaType();
|
2022-09-08 12:41:42 +00:00
|
|
|
if (originalType != valueMetaType) {
|
|
|
|
QVariant converted(valueMetaType);
|
|
|
|
if (QQmlValueTypeProvider::createValueType(
|
|
|
|
variant, valueMetaType, converted.data())) {
|
|
|
|
variant = converted;
|
|
|
|
} else if (!variant.convert(valueMetaType)) {
|
|
|
|
qWarning().noquote()
|
|
|
|
<< QLatin1String("Could not convert array value "
|
|
|
|
"at position %1 from %2 to %3")
|
|
|
|
.arg(QString::number(i),
|
|
|
|
QString::fromUtf8(originalType.name()),
|
|
|
|
QString::fromUtf8(valueMetaType.name()));
|
|
|
|
variant = converted;
|
|
|
|
}
|
2022-02-03 13:43:47 +00:00
|
|
|
}
|
|
|
|
meta->addValueAtEnd(result.data(), variant.constData());
|
2022-01-07 10:01:56 +00:00
|
|
|
}
|
2020-09-09 08:34:48 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
void *SequencePrototype::getRawContainerPtr(const Sequence *object, QMetaType typeHint)
|
2017-04-26 10:49:37 +00:00
|
|
|
{
|
2022-05-17 13:50:06 +00:00
|
|
|
if (object->d()->typePrivate->listId == typeHint)
|
|
|
|
return object->getRawContainerPtr();
|
2020-09-09 08:34:48 +00:00
|
|
|
return nullptr;
|
2017-04-26 10:49:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
QMetaType SequencePrototype::metaTypeForSequence(const Sequence *object)
|
2012-07-18 03:20:00 +00:00
|
|
|
{
|
2022-05-17 13:50:06 +00:00
|
|
|
return object->d()->typePrivate->listId;
|
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
|
|
|
}
|
|
|
|
|
2022-05-17 13:50:06 +00:00
|
|
|
} // namespace QV4
|
|
|
|
|
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
|
2022-04-28 15:43:38 +00:00
|
|
|
|
|
|
|
#include "moc_qv4sequenceobject_p.cpp"
|