| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include <QtQml/qqml.h> |
| 41 | |
| 42 | #include "qv4sequenceobject_p.h" |
| 43 | |
| 44 | #include <private/qv4functionobject_p.h> |
| 45 | #include <private/qv4arrayobject_p.h> |
| 46 | #include <private/qqmlengine_p.h> |
| 47 | #include <private/qv4scopedvalue_p.h> |
| 48 | #include <private/qv4jscall_p.h> |
| 49 | #include "qv4runtime_p.h" |
| 50 | #include "qv4objectiterator_p.h" |
| 51 | #include <private/qqmlvaluetypewrapper_p.h> |
| 52 | #if QT_CONFIG(qml_itemmodel) |
| 53 | #include <private/qqmlmodelindexvaluetype_p.h> |
| 54 | #include <QtCore/qabstractitemmodel.h> |
| 55 | #endif |
| 56 | |
| 57 | #include <algorithm> |
| 58 | |
| 59 | QT_BEGIN_NAMESPACE |
| 60 | |
| 61 | using namespace QV4; |
| 62 | |
| 63 | // helper function to generate valid warnings if errors occur during sequence operations. |
| 64 | static void generateWarning(QV4::ExecutionEngine *v4, const QString& description) |
| 65 | { |
| 66 | QQmlEngine *engine = v4->qmlEngine(); |
| 67 | if (!engine) |
| 68 | return; |
| 69 | QQmlError retn; |
| 70 | retn.setDescription(description); |
| 71 | |
| 72 | QV4::CppStackFrame *stackFrame = v4->currentStackFrame; |
| 73 | |
| 74 | retn.setLine(stackFrame->lineNumber()); |
| 75 | retn.setUrl(QUrl(stackFrame->source())); |
| 76 | QQmlEnginePrivate::warning(engine, retn); |
| 77 | } |
| 78 | |
| 79 | // F(elementType, elementTypeName, sequenceType, defaultValue) |
| 80 | #if QT_CONFIG(qml_itemmodel) |
| 81 | #define FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F) \ |
| 82 | F(QModelIndex, QModelIndex, QModelIndexList, QModelIndex()) \ |
| 83 | F(QModelIndex, QModelIndexVector, QVector<QModelIndex>, QModelIndex()) \ |
| 84 | F(QModelIndex, QModelIndexStdVector, std::vector<QModelIndex>, QModelIndex()) \ |
| 85 | F(QItemSelectionRange, QItemSelectionRange, QItemSelection, QItemSelectionRange()) |
| 86 | #else |
| 87 | #define FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F) |
| 88 | #endif |
| 89 | |
| 90 | #define FOREACH_QML_SEQUENCE_TYPE(F) \ |
| 91 | F(int, IntVector, QVector<int>, 0) \ |
| 92 | F(qreal, RealVector, QVector<qreal>, 0.0) \ |
| 93 | F(bool, BoolVector, QVector<bool>, false) \ |
| 94 | F(int, IntStdVector, std::vector<int>, 0) \ |
| 95 | F(qreal, RealStdVector, std::vector<qreal>, 0.0) \ |
| 96 | F(bool, BoolStdVector, std::vector<bool>, false) \ |
| 97 | F(int, Int, QList<int>, 0) \ |
| 98 | F(qreal, Real, QList<qreal>, 0.0) \ |
| 99 | F(bool, Bool, QList<bool>, false) \ |
| 100 | F(QString, String, QList<QString>, QString()) \ |
| 101 | F(QString, QString, QStringList, QString()) \ |
| 102 | F(QString, StringVector, QVector<QString>, QString()) \ |
| 103 | F(QString, StringStdVector, std::vector<QString>, QString()) \ |
| 104 | F(QUrl, Url, QList<QUrl>, QUrl()) \ |
| 105 | F(QUrl, UrlVector, QVector<QUrl>, QUrl()) \ |
| 106 | F(QUrl, UrlStdVector, std::vector<QUrl>, QUrl()) \ |
| 107 | FOREACH_QML_SEQUENCE_TYPE_FOR_ITEMMODEL(F) |
| 108 | |
| 109 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QString &element) |
| 110 | { |
| 111 | return engine->newString(s: element)->asReturnedValue(); |
| 112 | } |
| 113 | |
| 114 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, int element) |
| 115 | { |
| 116 | return QV4::Encode(element); |
| 117 | } |
| 118 | |
| 119 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QUrl &element) |
| 120 | { |
| 121 | return engine->newString(s: element.toString())->asReturnedValue(); |
| 122 | } |
| 123 | |
| 124 | #if QT_CONFIG(qml_itemmodel) |
| 125 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QModelIndex &element) |
| 126 | { |
| 127 | const QMetaObject *vtmo = QQmlValueTypeFactory::metaObjectForMetaType(type: QMetaType::QModelIndex); |
| 128 | return QV4::QQmlValueTypeWrapper::create(engine, QVariant(element), metaObject: vtmo, typeId: QMetaType::QModelIndex); |
| 129 | } |
| 130 | |
| 131 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *engine, const QItemSelectionRange &element) |
| 132 | { |
| 133 | int metaTypeId = qMetaTypeId<QItemSelectionRange>(); |
| 134 | const QMetaObject *vtmo = QQmlValueTypeFactory::metaObjectForMetaType(type: metaTypeId); |
| 135 | return QV4::QQmlValueTypeWrapper::create(engine, QVariant::fromValue(value: element), metaObject: vtmo, typeId: metaTypeId); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, qreal element) |
| 140 | { |
| 141 | return QV4::Encode(element); |
| 142 | } |
| 143 | |
| 144 | static QV4::ReturnedValue convertElementToValue(QV4::ExecutionEngine *, bool element) |
| 145 | { |
| 146 | return QV4::Encode(element); |
| 147 | } |
| 148 | |
| 149 | static QString convertElementToString(const QString &element) |
| 150 | { |
| 151 | return element; |
| 152 | } |
| 153 | |
| 154 | static QString convertElementToString(int element) |
| 155 | { |
| 156 | return QString::number(element); |
| 157 | } |
| 158 | |
| 159 | static QString convertElementToString(const QUrl &element) |
| 160 | { |
| 161 | return element.toString(); |
| 162 | } |
| 163 | |
| 164 | #if QT_CONFIG(qml_itemmodel) |
| 165 | static QString convertElementToString(const QModelIndex &element) |
| 166 | { |
| 167 | return reinterpret_cast<const QQmlModelIndexValueType *>(&element)->toString(); |
| 168 | } |
| 169 | |
| 170 | static QString convertElementToString(const QItemSelectionRange &element) |
| 171 | { |
| 172 | return reinterpret_cast<const QQmlItemSelectionRangeValueType *>(&element)->toString(); |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | static QString convertElementToString(qreal element) |
| 177 | { |
| 178 | QString qstr; |
| 179 | RuntimeHelpers::numberToString(result: &qstr, num: element, radix: 10); |
| 180 | return qstr; |
| 181 | } |
| 182 | |
| 183 | static QString convertElementToString(bool element) |
| 184 | { |
| 185 | if (element) |
| 186 | return QStringLiteral("true" ); |
| 187 | else |
| 188 | return QStringLiteral("false" ); |
| 189 | } |
| 190 | |
| 191 | template <typename ElementType> ElementType convertValueToElement(const Value &value); |
| 192 | |
| 193 | template <> QString convertValueToElement(const Value &value) |
| 194 | { |
| 195 | return value.toQString(); |
| 196 | } |
| 197 | |
| 198 | template <> int convertValueToElement(const Value &value) |
| 199 | { |
| 200 | return value.toInt32(); |
| 201 | } |
| 202 | |
| 203 | template <> QUrl convertValueToElement(const Value &value) |
| 204 | { |
| 205 | return QUrl(value.toQString()); |
| 206 | } |
| 207 | |
| 208 | #if QT_CONFIG(qml_itemmodel) |
| 209 | template <> QModelIndex convertValueToElement(const Value &value) |
| 210 | { |
| 211 | const QQmlValueTypeWrapper *v = value.as<QQmlValueTypeWrapper>(); |
| 212 | if (v) |
| 213 | return v->toVariant().toModelIndex(); |
| 214 | return QModelIndex(); |
| 215 | } |
| 216 | |
| 217 | template <> QItemSelectionRange convertValueToElement(const Value &value) |
| 218 | { |
| 219 | const QQmlValueTypeWrapper *v = value.as<QQmlValueTypeWrapper>(); |
| 220 | if (v) |
| 221 | return v->toVariant().value<QItemSelectionRange>(); |
| 222 | return QItemSelectionRange(); |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | template <> qreal convertValueToElement(const Value &value) |
| 227 | { |
| 228 | return value.toNumber(); |
| 229 | } |
| 230 | |
| 231 | template <> bool convertValueToElement(const Value &value) |
| 232 | { |
| 233 | return value.toBoolean(); |
| 234 | } |
| 235 | |
| 236 | namespace QV4 { |
| 237 | |
| 238 | template <typename Container> struct QQmlSequence; |
| 239 | |
| 240 | namespace Heap { |
| 241 | |
| 242 | template <typename Container> |
| 243 | struct QQmlSequence : Object { |
| 244 | void init(const Container &container); |
| 245 | void init(QObject *object, int propertyIndex, bool readOnly); |
| 246 | void destroy() { |
| 247 | delete container; |
| 248 | object.destroy(); |
| 249 | Object::destroy(); |
| 250 | } |
| 251 | |
| 252 | mutable Container *container; |
| 253 | QQmlQPointer<QObject> object; |
| 254 | int propertyIndex; |
| 255 | bool isReference : 1; |
| 256 | bool isReadOnly : 1; |
| 257 | }; |
| 258 | |
| 259 | } |
| 260 | |
| 261 | template <typename Container> |
| 262 | struct QQmlSequence : public QV4::Object |
| 263 | { |
| 264 | V4_OBJECT2(QQmlSequence<Container>, QV4::Object) |
| 265 | Q_MANAGED_TYPE(QmlSequence) |
| 266 | V4_PROTOTYPE(sequencePrototype) |
| 267 | V4_NEEDS_DESTROY |
| 268 | public: |
| 269 | |
| 270 | void init() |
| 271 | { |
| 272 | defineAccessorProperty(QStringLiteral("length" ), method_get_length, method_set_length); |
| 273 | } |
| 274 | |
| 275 | QV4::ReturnedValue containerGetIndexed(uint index, bool *hasProperty) const |
| 276 | { |
| 277 | /* Qt containers have int (rather than uint) allowable indexes. */ |
| 278 | if (index > INT_MAX) { |
| 279 | generateWarning(engine(), QLatin1String("Index out of range during indexed get" )); |
| 280 | if (hasProperty) |
| 281 | *hasProperty = false; |
| 282 | return Encode::undefined(); |
| 283 | } |
| 284 | if (d()->isReference) { |
| 285 | if (!d()->object) { |
| 286 | if (hasProperty) |
| 287 | *hasProperty = false; |
| 288 | return Encode::undefined(); |
| 289 | } |
| 290 | loadReference(); |
| 291 | } |
| 292 | if (index < size_t(d()->container->size())) { |
| 293 | if (hasProperty) |
| 294 | *hasProperty = true; |
| 295 | return convertElementToValue(engine(), qAsConst(*(d()->container))[index]); |
| 296 | } |
| 297 | if (hasProperty) |
| 298 | *hasProperty = false; |
| 299 | return Encode::undefined(); |
| 300 | } |
| 301 | |
| 302 | bool containerPutIndexed(uint index, const QV4::Value &value) |
| 303 | { |
| 304 | if (internalClass()->engine->hasException) |
| 305 | return false; |
| 306 | |
| 307 | /* Qt containers have int (rather than uint) allowable indexes. */ |
| 308 | if (index > INT_MAX) { |
| 309 | generateWarning(engine(), QLatin1String("Index out of range during indexed set" )); |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | if (d()->isReadOnly) { |
| 314 | engine()->throwTypeError(QLatin1String("Cannot insert into a readonly container" )); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (d()->isReference) { |
| 319 | if (!d()->object) |
| 320 | return false; |
| 321 | loadReference(); |
| 322 | } |
| 323 | |
| 324 | size_t count = size_t(d()->container->size()); |
| 325 | |
| 326 | typename Container::value_type element = convertValueToElement<typename Container::value_type>(value); |
| 327 | |
| 328 | if (index == count) { |
| 329 | d()->container->push_back(element); |
| 330 | } else if (index < count) { |
| 331 | (*d()->container)[index] = element; |
| 332 | } else { |
| 333 | /* according to ECMA262r3 we need to insert */ |
| 334 | /* the value at the given index, increasing length to index+1. */ |
| 335 | d()->container->reserve(index + 1); |
| 336 | while (index > count++) { |
| 337 | d()->container->push_back(typename Container::value_type()); |
| 338 | } |
| 339 | d()->container->push_back(element); |
| 340 | } |
| 341 | |
| 342 | if (d()->isReference) |
| 343 | storeReference(); |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | QV4::PropertyAttributes containerQueryIndexed(uint index) const |
| 348 | { |
| 349 | /* Qt containers have int (rather than uint) allowable indexes. */ |
| 350 | if (index > INT_MAX) { |
| 351 | generateWarning(engine(), QLatin1String("Index out of range during indexed query" )); |
| 352 | return QV4::Attr_Invalid; |
| 353 | } |
| 354 | if (d()->isReference) { |
| 355 | if (!d()->object) |
| 356 | return QV4::Attr_Invalid; |
| 357 | loadReference(); |
| 358 | } |
| 359 | return (index < size_t(d()->container->size())) ? QV4::Attr_Data : QV4::Attr_Invalid; |
| 360 | } |
| 361 | |
| 362 | struct OwnPropertyKeyIterator : ObjectOwnPropertyKeyIterator |
| 363 | { |
| 364 | ~OwnPropertyKeyIterator() override = default; |
| 365 | PropertyKey next(const Object *o, Property *pd = nullptr, PropertyAttributes *attrs = nullptr) override |
| 366 | { |
| 367 | const QQmlSequence *s = static_cast<const QQmlSequence *>(o); |
| 368 | |
| 369 | if (s->d()->isReference) { |
| 370 | if (!s->d()->object) |
| 371 | return ObjectOwnPropertyKeyIterator::next(o, pd, attrs); |
| 372 | s->loadReference(); |
| 373 | } |
| 374 | |
| 375 | if (arrayIndex < static_cast<uint>(s->d()->container->size())) { |
| 376 | uint index = arrayIndex; |
| 377 | ++arrayIndex; |
| 378 | if (attrs) |
| 379 | *attrs = QV4::Attr_Data; |
| 380 | |
| 381 | // TODO: Replace the container->at() below with operator[] in Qt6! |
| 382 | // TODO: But _not_ in Qt5! |
| 383 | // |
| 384 | // gcc 5.3.1 as shipped on RHEL 7.6 includes a copy of basic_string<char> |
| 385 | // into QtQml, when it sees a std::vector::at(). The basic_string symbols |
| 386 | // are publicly visible and preferred over the ones from libstdc++ when |
| 387 | // building user code. Therefore, removing this at() breaks binary |
| 388 | // compatibility. We _do_ want to remove it in Qt6, though. |
| 389 | // |
| 390 | // The exact mechanism is that at() checks its argument and can throw an |
| 391 | // out_of_range exception. The construction of this exception then triggers |
| 392 | // some string manipulation that uses the std::basic_string symbols. Clearly, |
| 393 | // this is a compiler bug. And clearly, we don't want the check as we can't |
| 394 | // catch the exception anyway. |
| 395 | |
| 396 | if (pd) |
| 397 | pd->value = convertElementToValue(s->engine(), s->d()->container->at(index)); |
| 398 | return PropertyKey::fromArrayIndex(idx: index); |
| 399 | } |
| 400 | |
| 401 | return ObjectOwnPropertyKeyIterator::next(o, pd, attrs); |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | static OwnPropertyKeyIterator *containerOwnPropertyKeys(const Object *m, Value *target) |
| 406 | { |
| 407 | *target = *m; |
| 408 | return new OwnPropertyKeyIterator; |
| 409 | } |
| 410 | |
| 411 | bool containerDeleteIndexedProperty(uint index) |
| 412 | { |
| 413 | /* Qt containers have int (rather than uint) allowable indexes. */ |
| 414 | if (index > INT_MAX) |
| 415 | return false; |
| 416 | if (d()->isReadOnly) |
| 417 | return false; |
| 418 | if (d()->isReference) { |
| 419 | if (!d()->object) |
| 420 | return false; |
| 421 | loadReference(); |
| 422 | } |
| 423 | |
| 424 | if (index >= size_t(d()->container->size())) |
| 425 | return false; |
| 426 | |
| 427 | /* according to ECMA262r3 it should be Undefined, */ |
| 428 | /* but we cannot, so we insert a default-value instead. */ |
| 429 | (*d()->container)[index] = typename Container::value_type(); |
| 430 | |
| 431 | if (d()->isReference) |
| 432 | storeReference(); |
| 433 | |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | bool containerIsEqualTo(Managed *other) |
| 438 | { |
| 439 | if (!other) |
| 440 | return false; |
| 441 | QQmlSequence<Container> *otherSequence = other->as<QQmlSequence<Container> >(); |
| 442 | if (!otherSequence) |
| 443 | return false; |
| 444 | if (d()->isReference && otherSequence->d()->isReference) { |
| 445 | return d()->object == otherSequence->d()->object && d()->propertyIndex == otherSequence->d()->propertyIndex; |
| 446 | } else if (!d()->isReference && !otherSequence->d()->isReference) { |
| 447 | return this == otherSequence; |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | struct DefaultCompareFunctor |
| 453 | { |
| 454 | bool operator()(typename Container::value_type lhs, typename Container::value_type rhs) |
| 455 | { |
| 456 | return convertElementToString(lhs) < convertElementToString(rhs); |
| 457 | } |
| 458 | }; |
| 459 | |
| 460 | struct CompareFunctor |
| 461 | { |
| 462 | CompareFunctor(QV4::ExecutionEngine *v4, const QV4::Value &compareFn) |
| 463 | : m_v4(v4), m_compareFn(&compareFn) |
| 464 | {} |
| 465 | |
| 466 | bool operator()(typename Container::value_type lhs, typename Container::value_type rhs) |
| 467 | { |
| 468 | QV4::Scope scope(m_v4); |
| 469 | ScopedFunctionObject compare(scope, m_compareFn); |
| 470 | if (!compare) |
| 471 | return m_v4->throwTypeError(); |
| 472 | Value *argv = scope.alloc(nValues: 2); |
| 473 | argv[0] = convertElementToValue(m_v4, lhs); |
| 474 | argv[1] = convertElementToValue(m_v4, rhs); |
| 475 | QV4::ScopedValue result(scope, compare->call(thisObject: m_v4->globalObject, argv, argc: 2)); |
| 476 | if (scope.engine->hasException) |
| 477 | return false; |
| 478 | return result->toNumber() < 0; |
| 479 | } |
| 480 | |
| 481 | private: |
| 482 | QV4::ExecutionEngine *m_v4; |
| 483 | const QV4::Value *m_compareFn; |
| 484 | }; |
| 485 | |
| 486 | bool sort(const FunctionObject *f, const Value *, const Value *argv, int argc) |
| 487 | { |
| 488 | if (d()->isReadOnly) |
| 489 | return false; |
| 490 | if (d()->isReference) { |
| 491 | if (!d()->object) |
| 492 | return false; |
| 493 | loadReference(); |
| 494 | } |
| 495 | |
| 496 | if (argc == 1 && argv[0].as<FunctionObject>()) { |
| 497 | CompareFunctor cf(f->engine(), argv[0]); |
| 498 | std::sort(d()->container->begin(), d()->container->end(), cf); |
| 499 | } else { |
| 500 | DefaultCompareFunctor cf; |
| 501 | std::sort(d()->container->begin(), d()->container->end(), cf); |
| 502 | } |
| 503 | |
| 504 | if (d()->isReference) |
| 505 | storeReference(); |
| 506 | |
| 507 | return true; |
| 508 | } |
| 509 | |
| 510 | static QV4::ReturnedValue method_get_length(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 511 | { |
| 512 | QV4::Scope scope(b); |
| 513 | QV4::Scoped<QQmlSequence<Container>> This(scope, thisObject->as<QQmlSequence<Container> >()); |
| 514 | if (!This) |
| 515 | THROW_TYPE_ERROR(); |
| 516 | |
| 517 | if (This->d()->isReference) { |
| 518 | if (!This->d()->object) |
| 519 | RETURN_RESULT(Encode(0)); |
| 520 | This->loadReference(); |
| 521 | } |
| 522 | RETURN_RESULT(Encode(qint32(This->d()->container->size()))); |
| 523 | } |
| 524 | |
| 525 | static QV4::ReturnedValue method_set_length(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc) |
| 526 | { |
| 527 | QV4::Scope scope(f); |
| 528 | QV4::Scoped<QQmlSequence<Container>> This(scope, thisObject->as<QQmlSequence<Container> >()); |
| 529 | if (!This) |
| 530 | THROW_TYPE_ERROR(); |
| 531 | |
| 532 | quint32 newLength = argc ? argv[0].toUInt32() : 0; |
| 533 | /* Qt containers have int (rather than uint) allowable indexes. */ |
| 534 | if (newLength > INT_MAX) { |
| 535 | generateWarning(v4: scope.engine, description: QLatin1String("Index out of range during length set" )); |
| 536 | RETURN_UNDEFINED(); |
| 537 | } |
| 538 | |
| 539 | if (This->d()->isReadOnly) |
| 540 | THROW_TYPE_ERROR(); |
| 541 | |
| 542 | /* Read the sequence from the QObject property if we're a reference */ |
| 543 | if (This->d()->isReference) { |
| 544 | if (!This->d()->object) |
| 545 | RETURN_UNDEFINED(); |
| 546 | This->loadReference(); |
| 547 | } |
| 548 | /* Determine whether we need to modify the sequence */ |
| 549 | quint32 newCount = static_cast<quint32>(newLength); |
| 550 | quint32 count = static_cast<quint32>(This->d()->container->size()); |
| 551 | if (newCount == count) { |
| 552 | RETURN_UNDEFINED(); |
| 553 | } else if (newCount > count) { |
| 554 | /* according to ECMA262r3 we need to insert */ |
| 555 | /* undefined values increasing length to newLength. */ |
| 556 | /* We cannot, so we insert default-values instead. */ |
| 557 | This->d()->container->reserve(newCount); |
| 558 | while (newCount > count++) { |
| 559 | This->d()->container->push_back(typename Container::value_type()); |
| 560 | } |
| 561 | } else { |
| 562 | /* according to ECMA262r3 we need to remove */ |
| 563 | /* elements until the sequence is the required length. */ |
| 564 | if (newCount < count) { |
| 565 | This->d()->container->erase(This->d()->container->begin() + newCount, This->d()->container->end()); |
| 566 | } |
| 567 | } |
| 568 | /* write back if required. */ |
| 569 | if (This->d()->isReference) { |
| 570 | /* write back. already checked that object is non-null, so skip that check here. */ |
| 571 | This->storeReference(); |
| 572 | } |
| 573 | RETURN_UNDEFINED(); |
| 574 | } |
| 575 | |
| 576 | QVariant toVariant() const |
| 577 | { return QVariant::fromValue<Container>(*d()->container); } |
| 578 | |
| 579 | static QVariant toVariant(QV4::ArrayObject *array) |
| 580 | { |
| 581 | QV4::Scope scope(array->engine()); |
| 582 | Container result; |
| 583 | quint32 length = array->getLength(); |
| 584 | QV4::ScopedValue v(scope); |
| 585 | for (quint32 i = 0; i < length; ++i) |
| 586 | result.push_back(convertValueToElement<typename Container::value_type>((v = array->get(idx: i)))); |
| 587 | return QVariant::fromValue(result); |
| 588 | } |
| 589 | |
| 590 | void* getRawContainerPtr() const |
| 591 | { return d()->container; } |
| 592 | |
| 593 | void loadReference() const |
| 594 | { |
| 595 | Q_ASSERT(d()->object); |
| 596 | Q_ASSERT(d()->isReference); |
| 597 | void *a[] = { d()->container, nullptr }; |
| 598 | QMetaObject::metacall(d()->object, QMetaObject::ReadProperty, d()->propertyIndex, a); |
| 599 | } |
| 600 | |
| 601 | void storeReference() |
| 602 | { |
| 603 | Q_ASSERT(d()->object); |
| 604 | Q_ASSERT(d()->isReference); |
| 605 | int status = -1; |
| 606 | QQmlPropertyData::WriteFlags flags = QQmlPropertyData::DontRemoveBinding; |
| 607 | void *a[] = { d()->container, nullptr, &status, &flags }; |
| 608 | QMetaObject::metacall(d()->object, QMetaObject::WriteProperty, d()->propertyIndex, a); |
| 609 | } |
| 610 | |
| 611 | static QV4::ReturnedValue virtualGet(const QV4::Managed *that, PropertyKey id, const Value *receiver, bool *hasProperty) |
| 612 | { |
| 613 | if (!id.isArrayIndex()) |
| 614 | return Object::virtualGet(m: that, id, receiver, hasProperty); |
| 615 | return static_cast<const QQmlSequence<Container> *>(that)->containerGetIndexed(id.asArrayIndex(), hasProperty); |
| 616 | } |
| 617 | static bool virtualPut(Managed *that, PropertyKey id, const QV4::Value &value, Value *receiver) |
| 618 | { |
| 619 | if (id.isArrayIndex()) |
| 620 | return static_cast<QQmlSequence<Container> *>(that)->containerPutIndexed(id.asArrayIndex(), value); |
| 621 | return Object::virtualPut(m: that, id, value, receiver); |
| 622 | } |
| 623 | static QV4::PropertyAttributes queryIndexed(const QV4::Managed *that, uint index) |
| 624 | { return static_cast<const QQmlSequence<Container> *>(that)->containerQueryIndexed(index); } |
| 625 | static bool virtualDeleteProperty(QV4::Managed *that, PropertyKey id) |
| 626 | { |
| 627 | if (id.isArrayIndex()) { |
| 628 | uint index = id.asArrayIndex(); |
| 629 | return static_cast<QQmlSequence<Container> *>(that)->containerDeleteIndexedProperty(index); |
| 630 | } |
| 631 | return Object::virtualDeleteProperty(m: that, id); |
| 632 | } |
| 633 | static bool virtualIsEqualTo(Managed *that, Managed *other) |
| 634 | { return static_cast<QQmlSequence<Container> *>(that)->containerIsEqualTo(other); } |
| 635 | static QV4::OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target) |
| 636 | { return static_cast<const QQmlSequence<Container> *>(m)->containerOwnPropertyKeys(m, target);} |
| 637 | |
| 638 | }; |
| 639 | |
| 640 | |
| 641 | template <typename Container> |
| 642 | void Heap::QQmlSequence<Container>::init(const Container &container) |
| 643 | { |
| 644 | Object::init(); |
| 645 | this->container = new Container(container); |
| 646 | propertyIndex = -1; |
| 647 | isReference = false; |
| 648 | isReadOnly = false; |
| 649 | object.init(); |
| 650 | |
| 651 | QV4::Scope scope(internalClass->engine); |
| 652 | QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this); |
| 653 | o->setArrayType(Heap::ArrayData::Custom); |
| 654 | o->init(); |
| 655 | } |
| 656 | |
| 657 | template <typename Container> |
| 658 | void Heap::QQmlSequence<Container>::init(QObject *object, int propertyIndex, bool readOnly) |
| 659 | { |
| 660 | Object::init(); |
| 661 | this->container = new Container; |
| 662 | this->propertyIndex = propertyIndex; |
| 663 | isReference = true; |
| 664 | this->isReadOnly = readOnly; |
| 665 | this->object.init(object); |
| 666 | QV4::Scope scope(internalClass->engine); |
| 667 | QV4::Scoped<QV4::QQmlSequence<Container> > o(scope, this); |
| 668 | o->setArrayType(Heap::ArrayData::Custom); |
| 669 | o->loadReference(); |
| 670 | o->init(); |
| 671 | } |
| 672 | |
| 673 | } |
| 674 | |
| 675 | namespace QV4 { |
| 676 | |
| 677 | typedef QQmlSequence<QVector<int> > QQmlIntVectorList; |
| 678 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlIntVectorList); |
| 679 | typedef QQmlSequence<QVector<qreal> > QQmlRealVectorList; |
| 680 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlRealVectorList); |
| 681 | typedef QQmlSequence<QVector<bool> > QQmlBoolVectorList; |
| 682 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlBoolVectorList); |
| 683 | typedef QQmlSequence<std::vector<int> > QQmlIntStdVectorList; |
| 684 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlIntStdVectorList); |
| 685 | typedef QQmlSequence<std::vector<qreal> > QQmlRealStdVectorList; |
| 686 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlRealStdVectorList); |
| 687 | typedef QQmlSequence<std::vector<bool> > QQmlBoolStdVectorList; |
| 688 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlBoolStdVectorList); |
| 689 | typedef QQmlSequence<QStringList> QQmlQStringList; |
| 690 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQStringList); |
| 691 | typedef QQmlSequence<QList<QString> > QQmlStringList; |
| 692 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlStringList); |
| 693 | typedef QQmlSequence<QVector<QString> > QQmlStringVectorList; |
| 694 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlStringVectorList); |
| 695 | typedef QQmlSequence<std::vector<QString> > QQmlStringStdVectorList; |
| 696 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlStringStdVectorList); |
| 697 | typedef QQmlSequence<QList<int> > QQmlIntList; |
| 698 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlIntList); |
| 699 | typedef QQmlSequence<QList<QUrl> > QQmlUrlList; |
| 700 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlUrlList); |
| 701 | typedef QQmlSequence<QVector<QUrl> > QQmlUrlVectorList; |
| 702 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlUrlVectorList); |
| 703 | typedef QQmlSequence<std::vector<QUrl> > QQmlUrlStdVectorList; |
| 704 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlUrlStdVectorList); |
| 705 | #if QT_CONFIG(qml_itemmodel) |
| 706 | typedef QQmlSequence<QModelIndexList> QQmlQModelIndexList; |
| 707 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQModelIndexList); |
| 708 | typedef QQmlSequence<QVector<QModelIndex> > QQmlQModelIndexVectorList; |
| 709 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQModelIndexVectorList); |
| 710 | typedef QQmlSequence<std::vector<QModelIndex> > QQmlQModelIndexStdVectorList; |
| 711 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQModelIndexStdVectorList); |
| 712 | typedef QQmlSequence<QItemSelection> QQmlQItemSelectionRangeList; |
| 713 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlQItemSelectionRangeList); |
| 714 | #endif |
| 715 | typedef QQmlSequence<QList<bool> > QQmlBoolList; |
| 716 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlBoolList); |
| 717 | typedef QQmlSequence<QList<qreal> > QQmlRealList; |
| 718 | DEFINE_OBJECT_TEMPLATE_VTABLE(QQmlRealList); |
| 719 | |
| 720 | } |
| 721 | |
| 722 | #define REGISTER_QML_SEQUENCE_METATYPE(unused, unused2, SequenceType, unused3) qRegisterMetaType<SequenceType>(#SequenceType); |
| 723 | static bool registerAllSequenceTypes() |
| 724 | { |
| 725 | FOREACH_QML_SEQUENCE_TYPE(REGISTER_QML_SEQUENCE_METATYPE) |
| 726 | return true; |
| 727 | } |
| 728 | #undef REGISTER_QML_SEQUENCE_METATYPE |
| 729 | |
| 730 | void SequencePrototype::init() |
| 731 | { |
| 732 | static const bool registered = registerAllSequenceTypes(); |
| 733 | Q_UNUSED(registered); |
| 734 | defineDefaultProperty(QStringLiteral("sort" ), code: method_sort, argumentCount: 1); |
| 735 | defineDefaultProperty(name: engine()->id_valueOf(), code: method_valueOf, argumentCount: 0); |
| 736 | } |
| 737 | |
| 738 | ReturnedValue SequencePrototype::method_valueOf(const FunctionObject *f, const Value *thisObject, const Value *, int) |
| 739 | { |
| 740 | return Encode(thisObject->toString(e: f->engine())); |
| 741 | } |
| 742 | |
| 743 | ReturnedValue SequencePrototype::method_sort(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 744 | { |
| 745 | Scope scope(b); |
| 746 | QV4::ScopedObject o(scope, thisObject); |
| 747 | if (!o || !o->isListType()) |
| 748 | THROW_TYPE_ERROR(); |
| 749 | |
| 750 | if (argc >= 2) |
| 751 | return o.asReturnedValue(); |
| 752 | |
| 753 | #define CALL_SORT(SequenceElementType, SequenceElementTypeName, SequenceType, DefaultValue) \ |
| 754 | if (QQml##SequenceElementTypeName##List *s = o->as<QQml##SequenceElementTypeName##List>()) { \ |
| 755 | if (!s->sort(b, thisObject, argv, argc)) \ |
| 756 | THROW_TYPE_ERROR(); \ |
| 757 | } else |
| 758 | |
| 759 | FOREACH_QML_SEQUENCE_TYPE(CALL_SORT) |
| 760 | |
| 761 | #undef CALL_SORT |
| 762 | {} |
| 763 | return o.asReturnedValue(); |
| 764 | } |
| 765 | |
| 766 | #define IS_SEQUENCE(unused1, unused2, SequenceType, unused3) \ |
| 767 | if (sequenceTypeId == qMetaTypeId<SequenceType>()) { \ |
| 768 | return true; \ |
| 769 | } else |
| 770 | |
| 771 | bool SequencePrototype::isSequenceType(int sequenceTypeId) |
| 772 | { |
| 773 | FOREACH_QML_SEQUENCE_TYPE(IS_SEQUENCE) { /* else */ return false; } |
| 774 | } |
| 775 | #undef IS_SEQUENCE |
| 776 | |
| 777 | #define NEW_REFERENCE_SEQUENCE(ElementType, ElementTypeName, SequenceType, unused) \ |
| 778 | if (sequenceType == qMetaTypeId<SequenceType>()) { \ |
| 779 | QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QQml##ElementTypeName##List>(object, propertyIndex, readOnly)); \ |
| 780 | return obj.asReturnedValue(); \ |
| 781 | } else |
| 782 | |
| 783 | ReturnedValue SequencePrototype::newSequence(QV4::ExecutionEngine *engine, int sequenceType, QObject *object, int propertyIndex, bool readOnly, bool *succeeded) |
| 784 | { |
| 785 | QV4::Scope scope(engine); |
| 786 | // This function is called when the property is a QObject Q_PROPERTY of |
| 787 | // the given sequence type. Internally we store a typed-sequence |
| 788 | // (as well as object ptr + property index for updated-read and write-back) |
| 789 | // and so access/mutate avoids variant conversion. |
| 790 | *succeeded = true; |
| 791 | FOREACH_QML_SEQUENCE_TYPE(NEW_REFERENCE_SEQUENCE) { /* else */ *succeeded = false; return QV4::Encode::undefined(); } |
| 792 | } |
| 793 | #undef NEW_REFERENCE_SEQUENCE |
| 794 | |
| 795 | #define NEW_COPY_SEQUENCE(ElementType, ElementTypeName, SequenceType, unused) \ |
| 796 | if (sequenceType == qMetaTypeId<SequenceType>()) { \ |
| 797 | QV4::ScopedObject obj(scope, engine->memoryManager->allocate<QQml##ElementTypeName##List>(v.value<SequenceType >())); \ |
| 798 | return obj.asReturnedValue(); \ |
| 799 | } else |
| 800 | |
| 801 | ReturnedValue SequencePrototype::fromVariant(QV4::ExecutionEngine *engine, const QVariant& v, bool *succeeded) |
| 802 | { |
| 803 | QV4::Scope scope(engine); |
| 804 | // This function is called when assigning a sequence value to a normal JS var |
| 805 | // in a JS block. Internally, we store a sequence of the specified type. |
| 806 | // Access and mutation is extremely fast since it will not need to modify any |
| 807 | // QObject property. |
| 808 | int sequenceType = v.userType(); |
| 809 | *succeeded = true; |
| 810 | FOREACH_QML_SEQUENCE_TYPE(NEW_COPY_SEQUENCE) { /* else */ *succeeded = false; return QV4::Encode::undefined(); } |
| 811 | } |
| 812 | #undef NEW_COPY_SEQUENCE |
| 813 | |
| 814 | #define SEQUENCE_TO_VARIANT(ElementType, ElementTypeName, SequenceType, unused) \ |
| 815 | if (QQml##ElementTypeName##List *list = object->as<QQml##ElementTypeName##List>()) \ |
| 816 | return list->toVariant(); \ |
| 817 | else |
| 818 | |
| 819 | QVariant SequencePrototype::toVariant(Object *object) |
| 820 | { |
| 821 | Q_ASSERT(object->isListType()); |
| 822 | FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_TO_VARIANT) { /* else */ return QVariant(); } |
| 823 | } |
| 824 | |
| 825 | #undef SEQUENCE_TO_VARIANT |
| 826 | #define SEQUENCE_TO_VARIANT(ElementType, ElementTypeName, SequenceType, unused) \ |
| 827 | if (typeHint == qMetaTypeId<SequenceType>()) { \ |
| 828 | return QQml##ElementTypeName##List::toVariant(a); \ |
| 829 | } else |
| 830 | |
| 831 | QVariant SequencePrototype::toVariant(const QV4::Value &array, int typeHint, bool *succeeded) |
| 832 | { |
| 833 | *succeeded = true; |
| 834 | |
| 835 | if (!array.as<ArrayObject>()) { |
| 836 | *succeeded = false; |
| 837 | return QVariant(); |
| 838 | } |
| 839 | QV4::Scope scope(array.as<Object>()->engine()); |
| 840 | QV4::ScopedArrayObject a(scope, array); |
| 841 | |
| 842 | FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_TO_VARIANT) { /* else */ *succeeded = false; return QVariant(); } |
| 843 | } |
| 844 | |
| 845 | #undef SEQUENCE_TO_VARIANT |
| 846 | |
| 847 | #define SEQUENCE_GET_RAWCONTAINERPTR(ElementType, ElementTypeName, SequenceType, unused) \ |
| 848 | if (const QQml##ElementTypeName##List *list = [&]() -> const QQml##ElementTypeName##List* \ |
| 849 | { if (typeHint == qMetaTypeId<SequenceType>()) return object->as<QQml##ElementTypeName##List>(); return nullptr;}()) \ |
| 850 | return list->getRawContainerPtr(); \ |
| 851 | else |
| 852 | |
| 853 | void* SequencePrototype::getRawContainerPtr(const Object *object, int typeHint) |
| 854 | { |
| 855 | FOREACH_QML_SEQUENCE_TYPE(SEQUENCE_GET_RAWCONTAINERPTR) { /* else */ return nullptr; } |
| 856 | } |
| 857 | |
| 858 | #undef SEQUENCE_GET_RAWCONTAINERPTR |
| 859 | |
| 860 | #define MAP_META_TYPE(ElementType, ElementTypeName, SequenceType, unused) \ |
| 861 | if (object->as<QQml##ElementTypeName##List>()) { \ |
| 862 | return qMetaTypeId<SequenceType>(); \ |
| 863 | } else |
| 864 | |
| 865 | int SequencePrototype::metaTypeForSequence(const QV4::Object *object) |
| 866 | { |
| 867 | FOREACH_QML_SEQUENCE_TYPE(MAP_META_TYPE) |
| 868 | /*else*/ { |
| 869 | return -1; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | #undef MAP_META_TYPE |
| 874 | |
| 875 | QT_END_NAMESPACE |
| 876 | |