57 ListWrapperObject object(p);
58 Heap::ArrayData *arrayData = object.arrayData();
60 const uint length = arrayData->length();
61 if (Q_UNLIKELY(length == std::numeric_limits<uint>::max())) {
62 object.scope.engine->throwRangeError(QLatin1String(
"Too many elements."));
66 ArrayData::realloc(object.object, Heap::ArrayData::Simple, length + 1,
false);
67 QV4::Scope scope(object.scope.engine);
68 QV4::ScopedValue wrappedObject(scope, QV4::QObjectWrapper::wrap(object.scope.engine, o));
69 arrayData->vtable()->put(
70 object.object, length, wrappedObject);
167ReturnedValue QmlListWrapper::createOwned(ExecutionEngine *engine,
const QQmlProperty &prop)
169 Q_ASSERT(prop.propertyMetaType().flags() & QMetaType::IsQmlList);
170 QQmlPropertyPrivate *privProp = QQmlPropertyPrivate::get(prop);
171 QQmlListProperty<QObject> listProp;
172 privProp->core.readProperty(privProp->object, &listProp);
173 const qsizetype listSize = listProp.count(&listProp);
175 Scoped<QV4::QmlListWrapper> listWrapper(scope, QV4::QmlListWrapper::create(engine, privProp->propertyType()));
178 Heap::ArrayData *arrayData = listWrapper->arrayData();
179 ArrayData::realloc(listWrapper, Heap::ArrayData::Simple, listSize,
false);
180 for (qsizetype i = 0; i != listSize; ++i) {
181 QObject *o = listProp.at(&listProp, i);
182 ScopedObject scopedO(scope, QV4::QObjectWrapper::wrap(engine, o));
183 arrayData->vtable()->put(listWrapper, i, scopedO);
185 return listWrapper.asReturnedValue();
202ReturnedValue QmlListWrapper::virtualGet(
const Managed *m, PropertyKey id,
const Value *receiver,
bool *hasProperty)
204 Q_ASSERT(m->as<QmlListWrapper>());
205 const QmlListWrapper *w =
static_cast<
const QmlListWrapper *>(m);
206 QV4::ExecutionEngine *v4 = w->engine();
208 if (id.isArrayIndex()) {
209 const uint index = id.asArrayIndex();
210 const quint32 count = w->d()->property()->count
211 ? w->d()->property()->count(w->d()->property())
213 if (index < count && w->d()->property()->at) {
216 return QV4::QObjectWrapper::wrap(v4, w->d()->property()->at(w->d()->property(), index));
220 *hasProperty =
false;
221 return Value::undefinedValue().asReturnedValue();
224 return Object::virtualGet(m, id, receiver, hasProperty);
235bool QmlListWrapper::virtualPut(Managed *m, PropertyKey id,
const Value &value, Value *receiver)
237 Q_ASSERT(m->as<QmlListWrapper>());
239 const auto *w =
static_cast<
const QmlListWrapper *>(m);
240 QV4::ExecutionEngine *v4 = w->engine();
242 QQmlListProperty<QObject> *prop = w->d()->property();
244 if (id.isArrayIndex()) {
245 if (!prop->count || !prop->replace)
248 const uint index = id.asArrayIndex();
249 const int count = prop->count(prop);
250 if (count < 0 || index >= uint(count))
253 if (value.isNull()) {
254 prop->replace(prop, index,
nullptr);
258 QV4::Scope scope(v4);
259 QV4::ScopedObject so(scope, value.toObject(scope.engine));
260 if (
auto *wrapper = so->as<QV4::QObjectWrapper>()) {
261 QObject *object = wrapper->object();
263 prop->replace(prop, index, object);
267 const QMetaType elementType = w->d()->elementType();
268 const QMetaObject *elementMeta = elementType.metaObject();
269 if (Q_UNLIKELY(!elementMeta || !QQmlMetaObject::canConvert(object, elementMeta))) {
270 qCWarning(lcIncompatibleElement)
271 <<
"Cannot insert" << object <<
"into a QML list of" << elementType.name();
272 prop->replace(prop, index,
nullptr);
276 prop->replace(prop, index, object);
283 return Object::virtualPut(m, id, value, receiver);
325 defineDefaultProperty(QStringLiteral(
"pop"), method_pop, 0);
326 defineDefaultProperty(QStringLiteral(
"push"), method_push, 1);
327 defineDefaultProperty(QStringLiteral(
"shift"), method_shift, 0);
328 defineDefaultProperty(QStringLiteral(
"splice"), method_splice, 2);
329 defineDefaultProperty(QStringLiteral(
"unshift"), method_unshift, 1);
330 defineDefaultProperty(QStringLiteral(
"indexOf"), method_indexOf, 1);
331 defineDefaultProperty(QStringLiteral(
"lastIndexOf"), method_lastIndexOf, 1);
332 defineDefaultProperty(QStringLiteral(
"sort"), method_sort, 1);
333 defineAccessorProperty(QStringLiteral(
"length"), method_get_length, method_set_length);
336ReturnedValue
PropertyListPrototype::method_pop(
const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
339 ScopedObject instance(scope, thisObject->toObject(scope.engine));
343 QmlListWrapper *w = instance->as<QmlListWrapper>();
347 QQmlListProperty<QObject> *property = w->d()->property();
349 if (!property->count)
350 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
351 const qsizetype len = property->count(property);
356 return scope.engine->throwTypeError(u"List doesn't define an At function"_s);
358 scope, QV4::QObjectWrapper::wrap(scope.engine, property->at(property, len - 1)));
360 if (!property->removeLast)
361 return scope.engine->throwTypeError(u"List doesn't define a RemoveLast function"_s);
362 property->removeLast(property);
364 return result->asReturnedValue();
367ReturnedValue
PropertyListPrototype::method_push(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
370 ScopedObject instance(scope, thisObject->toObject(scope.engine));
373 QmlListWrapper *w = instance->as<QmlListWrapper>();
377 QQmlListProperty<QObject> *property = w->d()->property();
378 if (!property->append)
379 return scope.engine->throwTypeError(u"List doesn't define an Append function"_s);
380 if (!property->count)
381 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
383 for (
int i = 0; i < argc; ++i) {
384 const Value &arg = argv[i];
385 if (!arg.isNull() && !arg.as<QObjectWrapper>())
389 const qsizetype length = property->count(property);
390 if (!qIsAtMostUintLimit(length, std::numeric_limits<uint>::max() - argc))
391 return scope.engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
393 const QMetaType elementType = w->d()->elementType();
394 const QMetaObject *elementMeta = elementType.metaObject();
395 for (
int i = 0; i < argc; ++i) {
396 if (argv[i].isNull()) {
397 property->append(property,
nullptr);
401 QObject *object = argv[i].as<QV4::QObjectWrapper>()->object();
403 property->append(property,
nullptr);
407 if (Q_UNLIKELY(!elementMeta || !QQmlMetaObject::canConvert(object, elementMeta))) {
408 qCWarning(lcIncompatibleElement)
409 <<
"Cannot append" << object <<
"to a QML list of" << elementType.name();
410 property->append(property,
nullptr);
414 property->append(property, object);
417 const auto actualLength = property->count(property);
418 if (actualLength != length + argc)
419 qmlWarning(property->object) <<
"List didn't append all objects";
421 return Encode(uint(actualLength));
424ReturnedValue
PropertyListPrototype::method_shift(
const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
427 ScopedObject instance(scope, thisObject->toObject(scope.engine));
430 QmlListWrapper *w = instance->as<QmlListWrapper>();
434 QQmlListProperty<QObject> *property = w->d()->property();
436 if (!property->count)
437 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
438 const qsizetype len = property->count(property);
443 return scope.engine->throwTypeError(u"List doesn't define an At function"_s);
444 ScopedValue result(scope, QV4::QObjectWrapper::wrap(scope.engine, property->at(property, 0)));
446 if (!property->replace)
447 return scope.engine->throwTypeError(u"List doesn't define a Replace function"_s);
448 if (!property->removeLast)
449 return scope.engine->throwTypeError(u"List doesn't define a RemoveLast function"_s);
451 for (qsizetype i = 1; i < len; ++i)
452 property->replace(property, i - 1, property->at(property, i));
453 property->removeLast(property);
455 return result->asReturnedValue();
458ReturnedValue
PropertyListPrototype::method_splice(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
461 ScopedObject instance(scope, thisObject->toObject(scope.engine));
464 QmlListWrapper *w = instance->as<QmlListWrapper>();
468 QQmlListProperty<QObject> *property = w->d()->property();
470 if (!property->count)
471 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
472 const qsizetype len = property->count(property);
474 const double rs = (argc ? argv[0] : Value::undefinedValue()).toInteger();
477 start =
static_cast<qsizetype>(qMax(0., len + rs));
479 start =
static_cast<qsizetype>(qMin(rs,
static_cast<
double>(len)));
481 qsizetype deleteCount = 0;
482 qsizetype itemCount = 0;
484 deleteCount = len - start;
485 }
else if (argc > 1){
486 itemCount = argc - 2;
487 double dc = argv[1].toInteger();
488 deleteCount =
static_cast<qsizetype>(qMin(qMax(dc, 0.),
double(len - start)));
491 if (itemCount > deleteCount
492 && len > std::numeric_limits<qsizetype>::max() - itemCount + deleteCount) {
493 return scope.engine->throwTypeError();
496 if (!qIsAtMostUintLimit(deleteCount, std::numeric_limits<uint>::max() - 1))
497 return scope.engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
500 return scope.engine->throwTypeError(u"List doesn't define an At function"_s);
502 for (qsizetype i = 0; i < itemCount; ++i) {
503 const auto arg = argv[i + 2];
504 if (!arg.isNull() && !arg.as<QObjectWrapper>())
508 ScopedArrayObject newArray(scope, scope.engine->newArrayObject());
509 newArray->arrayReserve(deleteCount);
510 ScopedValue v(scope);
511 QV4::ScopedValue wrappedObject(scope);
512 for (qsizetype i = 0; i < deleteCount; ++i) {
513 wrappedObject = QObjectWrapper::wrap(scope.engine, property->at(property, start + i));
517 newArray->setArrayLengthUnchecked(deleteCount);
519 if (!property->replace)
520 return scope.engine->throwTypeError(u"List doesn't define a Replace function"_s);
521 if (!property->removeLast)
522 return scope.engine->throwTypeError(u"List doesn't define a RemoveLast function"_s);
523 if (!property->append)
524 return scope.engine->throwTypeError(u"List doesn't define an Append function"_s);
526 if (itemCount < deleteCount) {
527 for (qsizetype k = start; k < len - deleteCount; ++k)
528 property->replace(property, k + itemCount, property->at(property, k + deleteCount));
529 for (qsizetype k = len; k > len - deleteCount + itemCount; --k)
530 property->removeLast(property);
531 }
else if (itemCount > deleteCount) {
532 for (qsizetype k = 0; k < itemCount - deleteCount; ++k)
533 property->append(property,
nullptr);
534 for (qsizetype k = len - deleteCount; k > start; --k) {
536 property, k + itemCount - 1, property->at(property, k + deleteCount - 1));
540 const QMetaType elementType = w->d()->elementType();
541 const QMetaObject *elementMeta = elementType.metaObject();
542 for (qsizetype i = 0; i < itemCount; ++i) {
543 const auto arg = argv[i + 2];
545 property->replace(property, start + i,
nullptr);
549 QObject *object = arg.as<QObjectWrapper>()->object();
551 property->replace(property, start + i,
nullptr);
555 if (Q_UNLIKELY(!elementMeta || !QQmlMetaObject::canConvert(object, elementMeta))) {
556 qCWarning(lcIncompatibleElement)
557 <<
"Cannot splice" << object <<
"into a QML list of" << elementType.name();
558 property->replace(property, start + i,
nullptr);
562 property->replace(property, start + i, object);
565 return newArray->asReturnedValue();
568ReturnedValue
PropertyListPrototype::method_unshift(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
571 ScopedObject instance(scope, thisObject->toObject(scope.engine));
575 QmlListWrapper *w = instance->as<QmlListWrapper>();
579 QQmlListProperty<QObject> *property = w->d()->property();
581 if (!property->count)
582 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
583 const qsizetype len = property->count(property);
585 if (std::numeric_limits<qsizetype>::max() - len < argc || !qIsAtMostUintLimit(len + argc))
586 return scope.engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
588 if (!property->append)
589 return scope.engine->throwTypeError(u"List doesn't define an Append function"_s);
590 if (!property->replace)
591 return scope.engine->throwTypeError(u"List doesn't define a Replace function"_s);
593 for (
int i = 0; i < argc; ++i) {
594 const auto arg = argv[i];
595 if (!arg.isNull() && !arg.as<QObjectWrapper>())
599 for (
int i = 0; i < argc; ++i)
600 property->append(property,
nullptr);
601 if (property->count(property) != argc + len)
602 return scope.engine->throwTypeError(u"List doesn't append null objects"_s);
604 for (qsizetype k = len; k > 0; --k)
605 property->replace(property, k + argc - 1, property->at(property, k - 1));
607 const QMetaType elementType = w->d()->elementType();
608 const QMetaObject *elementMeta = elementType.metaObject();
609 for (
int i = 0; i < argc; ++i) {
610 const auto *wrapper = argv[i].as<QObjectWrapper>();
611 QObject *object = wrapper ? wrapper->object() :
nullptr;
613 property->replace(property, i, object);
617 if (Q_UNLIKELY(!elementMeta || !QQmlMetaObject::canConvert(object, elementMeta))) {
618 qCWarning(lcIncompatibleElement)
619 <<
"Cannot unshift" << object <<
"into a QML list of" << elementType.name();
620 property->replace(property, i,
nullptr);
624 property->replace(property, i, object);
627 return Encode(uint(len + argc));
639 QObject *searchValue;
640 if (argv[0].isNull()) {
641 searchValue =
nullptr;
643 Scoped<QObjectWrapper> wrapper(scope, argv[0]);
645 searchValue = wrapper->object();
650 ScopedObject instance(scope, thisObject->toObject(scope.engine));
654 QmlListWrapper *w = instance->as<QmlListWrapper>();
658 QQmlListProperty<QObject> *property = w->d()->property();
660 if (!property->count)
661 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
662 const qsizetype len = property->count(property);
667 return iterate(scope.engine, property, len, searchValue);
670ReturnedValue
PropertyListPrototype::method_indexOf(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
672 return firstOrLastIndexOf(
673 b, thisObject, argv, argc,
674 [argc, argv](ExecutionEngine *engine, QQmlListProperty<QObject> *property,
675 qsizetype len, QObject *searchValue) -> ReturnedValue {
676 qsizetype fromIndex = 0;
678 double f = argv[1].toInteger();
679 if (hasExceptionOrIsInterrupted(engine))
680 return Encode::undefined();
684 f = qMax(len + f, 0.);
685 fromIndex = qsizetype(f);
688 for (qsizetype i = fromIndex; i < len; ++i) {
689 if (property->at(property, i) == searchValue) {
690 if (qIsAtMostUintLimit(i))
691 return Encode(uint(i));
692 return engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
700ReturnedValue
PropertyListPrototype::method_lastIndexOf(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
702 return firstOrLastIndexOf(
703 b, thisObject, argv, argc,
704 [argc, argv](ExecutionEngine *engine, QQmlListProperty<QObject> *property,
705 qsizetype len, QObject *searchValue) -> ReturnedValue {
706 qsizetype fromIndex = len - 1;
708 double f = argv[1].toInteger();
709 if (hasExceptionOrIsInterrupted(engine))
710 return Encode::undefined();
712 f = qMin(f, (
double)(len - 1));
718 fromIndex = qsizetype(f);
721 for (qsizetype i = fromIndex; i >= 0; --i) {
722 if (property->at(property, i) == searchValue) {
723 if (qIsAtMostUintLimit(i))
724 return Encode(uint(i));
725 return engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
733ReturnedValue
PropertyListPrototype::method_sort(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
736 ScopedObject instance(scope, thisObject->toObject(scope.engine));
740 QmlListWrapper *w = instance->as<QmlListWrapper>();
744 QQmlListProperty<QObject> *property = w->d()->property();
746 if (!property->count)
747 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
748 if (property->count(property) == 0)
749 return thisObject->asReturnedValue();
751 return scope.engine->throwTypeError(u"List doesn't define an At function"_s);
752 if (!property->replace)
753 return scope.engine->throwTypeError(u"List doesn't define a Replace function"_s);
755 ScopedValue comparefn(scope, argc ? argv[0] : Value::undefinedValue());
756 if (!comparefn->isUndefined() && !comparefn->isFunctionObject())
759 const ArrayElementLessThan lessThan(scope.engine, comparefn);
760 sortHelper(begin(*property), end(*property), [&](QObject *a, QObject *b) {
761 Scoped<QObjectWrapper> o1(scope, QObjectWrapper::wrap(scope.engine, a));
762 Scoped<QObjectWrapper> o2(scope, QObjectWrapper::wrap(scope.engine, b));
763 return lessThan(o1, o2);
766 return thisObject->asReturnedValue();
791ReturnedValue
PropertyListPrototype::method_set_length(
const FunctionObject *b,
const Value *thisObject,
const Value *argv,
int argc)
794 ScopedObject instance(scope, thisObject->toObject(scope.engine));
798 const QmlListWrapper *w = instance->as<QmlListWrapper>();
802 QQmlListProperty<QObject> *property = w->d()->property();
805 const uint newLength = argc ? argv[0].asArrayLength(&ok) : 0;
807 return scope.engine->throwRangeError(QString::fromLatin1(
"Invalid list length."));
809 if (newLength == 0 && property->clear) {
810 property->clear(property);
814 if (!property->count)
815 return scope.engine->throwTypeError(u"List doesn't define a Count function"_s);
817 qsizetype count = property->count(property);
818 if (!qIsAtMostUintLimit(count))
819 return scope.engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
821 if (newLength < uint(count)) {
822 if (!property->removeLast)
823 return scope.engine->throwTypeError(u"List doesn't define a RemoveLast function"_s);
825 for (uint i = count; i > newLength; --i)
826 property->removeLast(property);
831 if (!property->append)
832 return scope.engine->throwTypeError(u"List doesn't define an Append function"_s);
834 for (uint i = count; i < newLength; ++i)
835 property->append(property,
nullptr);
837 count = property->count(property);
838 if (!qIsAtMostUintLimit(count))
839 return scope.engine->throwRangeError(QString::fromLatin1(
"List length out of range."));
841 if (uint(count) != newLength)
842 return scope.engine->throwTypeError(u"List doesn't append null objects"_s);