aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsapi
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-04-16 11:40:10 +0200
committerUlf Hermann <ulf.hermann@qt.io>2024-04-26 07:13:50 +0200
commit23fc22e16022e355f2a1aff8705c09b807fbe024 (patch)
tree1e30c3c7d5897fa1e48071a8d0238083baff82a9 /src/qml/jsapi
parent372db480cda5899148629d8edad76405bc1b98de (diff)
QmlCompiler: Consider lengths to be qsizetype
This is what we get as "source material" from all the containers. In JavaScript, arrays sizes are up to 32bit unsigned, but we need to represent -1 in a few places. Therefore, we cannot clamp the result to 32bit signed if the underlying container supports 64bit indices. We can change qjslist.h here because this header is only supposed to be used from generated code. Therefore, if you rebuild any users of this code, you will also re-generate them, adapting to the new API. Since we check for QJSNumberCoercion::isArrayIndex() before we use any number as an array index when generating code, this is actually safe. We just need to adapt isArrayIndex() to consider numbers greater than INT_MAX as possible values. Fixes: QTBUG-122582 Change-Id: I1147978a05bfedb63037c7f4437921f85a6aabb1 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r--src/qml/jsapi/qjslist.h80
1 files changed, 36 insertions, 44 deletions
diff --git a/src/qml/jsapi/qjslist.h b/src/qml/jsapi/qjslist.h
index f55d669a41..d604e266f2 100644
--- a/src/qml/jsapi/qjslist.h
+++ b/src/qml/jsapi/qjslist.h
@@ -28,7 +28,7 @@ QT_BEGIN_NAMESPACE
struct QJSListIndexClamp
{
- static qsizetype clamp(int start, qsizetype max, qsizetype min = 0)
+ static qsizetype clamp(qsizetype start, qsizetype max, qsizetype min = 0)
{
Q_ASSERT(min >= 0);
Q_ASSERT(min <= max);
@@ -43,18 +43,15 @@ struct QJSList : private QJSListIndexClamp
QJSList(List *list, QJSEngine *engine) : m_list(list), m_engine(engine) {}
- Value at(int index) const
+ Value at(qsizetype index) const
{
Q_ASSERT(index >= 0 && index < size());
return *(m_list->cbegin() + index);
}
- int size() const
- {
- return int(std::min(m_list->size(), qsizetype(std::numeric_limits<int>::max())));
- }
+ qsizetype size() const { return m_list->size(); }
- void resize(int size)
+ void resize(qsizetype size)
{
m_list->resize(size);
}
@@ -64,7 +61,7 @@ struct QJSList : private QJSListIndexClamp
return std::find(m_list->cbegin(), m_list->cend(), value) != m_list->cend();
}
- bool includes(const Value &value, int start) const
+ bool includes(const Value &value, qsizetype start) const
{
return std::find(m_list->cbegin() + clamp(start, m_list->size()), m_list->cend(), value)
!= m_list->cend();
@@ -88,14 +85,14 @@ struct QJSList : private QJSListIndexClamp
{
return *m_list;
}
- List slice(int start) const
+ List slice(qsizetype start) const
{
List result;
std::copy(m_list->cbegin() + clamp(start, m_list->size()), m_list->cend(),
std::back_inserter(result));
return result;
}
- List slice(int start, int end) const
+ List slice(qsizetype start, qsizetype end) const
{
const qsizetype size = m_list->size();
const qsizetype clampedStart = clamp(start, size);
@@ -107,7 +104,7 @@ struct QJSList : private QJSListIndexClamp
return result;
}
- int indexOf(const Value &value) const
+ qsizetype indexOf(const Value &value) const
{
const auto begin = m_list->cbegin();
const auto end = m_list->cend();
@@ -116,9 +113,9 @@ struct QJSList : private QJSListIndexClamp
return -1;
const qsizetype result = it - begin;
Q_ASSERT(result >= 0);
- return result > std::numeric_limits<int>::max() ? -1 : int(result);
+ return result;
}
- int indexOf(const Value &value, int start) const
+ qsizetype indexOf(const Value &value, qsizetype start) const
{
const auto begin = m_list->cbegin();
const auto end = m_list->cend();
@@ -127,18 +124,17 @@ struct QJSList : private QJSListIndexClamp
return -1;
const qsizetype result = it - begin;
Q_ASSERT(result >= 0);
- return result > std::numeric_limits<int>::max() ? -1 : int(result);
+ return result;
}
- int lastIndexOf(const Value &value) const
+ qsizetype lastIndexOf(const Value &value) const
{
const auto begin = std::make_reverse_iterator(m_list->cend());
const auto end = std::make_reverse_iterator(m_list->cbegin());
const auto it = std::find(begin, end, value);
- const qsizetype result = (end - it) - 1;
- return result > std::numeric_limits<int>::max() ? -1 : int(result);
+ return (end - it) - 1;
}
- int lastIndexOf(const Value &value, int start) const
+ qsizetype lastIndexOf(const Value &value, qsizetype start) const
{
const qsizetype size = m_list->size();
if (size == 0)
@@ -150,8 +146,7 @@ struct QJSList : private QJSListIndexClamp
const auto end = std::make_reverse_iterator(m_list->cbegin());
const auto it = std::find(begin, end, value);
- const qsizetype result = (end - it) - 1;
- return result > std::numeric_limits<int>::max() ? -1 : int(result);
+ return (end - it) - 1;
}
QString toString() const { return join(); }
@@ -168,18 +163,18 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
QJSList(QQmlListProperty<QObject> *list, QJSEngine *engine) : m_list(list), m_engine(engine) {}
- QObject *at(int index) const
+ QObject *at(qsizetype index) const
{
Q_ASSERT(index >= 0 && index < size());
return m_list->at(m_list, index);
}
- int size() const
+ qsizetype size() const
{
- return int(std::min(m_list->count(m_list), qsizetype(std::numeric_limits<int>::max())));
+ return m_list->count(m_list);
}
- void resize(int size)
+ void resize(qsizetype size)
{
qsizetype current = m_list->count(m_list);
if (current < size && m_list->append) {
@@ -206,7 +201,7 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
return false;
}
- bool includes(const QObject *value, int start) const
+ bool includes(const QObject *value, qsizetype start) const
{
if (!m_list->count || !m_list->at)
return false;
@@ -239,7 +234,7 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
{
return m_list->toList<QObjectList>();
}
- QObjectList slice(int start) const
+ QObjectList slice(qsizetype start) const
{
if (!m_list->count || !m_list->at)
return QObjectList();
@@ -252,7 +247,7 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
result.append(m_list->at(m_list, i));
return result;
}
- QObjectList slice(int start, int end) const
+ QObjectList slice(qsizetype start, qsizetype end) const
{
if (!m_list->count || !m_list->at)
return QObjectList();
@@ -267,46 +262,43 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
return result;
}
- int indexOf(const QObject *value) const
+ qsizetype indexOf(const QObject *value) const
{
if (!m_list->count || !m_list->at)
return -1;
- const qsizetype end
- = std::min(m_list->count(m_list), qsizetype(std::numeric_limits<int>::max()));
+ const qsizetype end = m_list->count(m_list);
for (qsizetype i = 0; i < end; ++i) {
if (m_list->at(m_list, i) == value)
- return int(i);
+ return i;
}
return -1;
}
- int indexOf(const QObject *value, int start) const
+ qsizetype indexOf(const QObject *value, qsizetype start) const
{
if (!m_list->count || !m_list->at)
return -1;
const qsizetype size = m_list->count(m_list);
- for (qsizetype i = clamp(start, size),
- end = std::min(size, qsizetype(std::numeric_limits<int>::max()));
- i < end; ++i) {
+ for (qsizetype i = clamp(start, size); i < size; ++i) {
if (m_list->at(m_list, i) == value)
- return int(i);
+ return i;
}
return -1;
}
- int lastIndexOf(const QObject *value) const
+ qsizetype lastIndexOf(const QObject *value) const
{
if (!m_list->count || !m_list->at)
return -1;
for (qsizetype i = m_list->count(m_list) - 1; i >= 0; --i) {
if (m_list->at(m_list, i) == value)
- return i > std::numeric_limits<int>::max() ? -1 : int(i);
+ return i;
}
return -1;
}
- int lastIndexOf(const QObject *value, int start) const
+ qsizetype lastIndexOf(const QObject *value, qsizetype start) const
{
if (!m_list->count || !m_list->at)
return -1;
@@ -318,7 +310,7 @@ struct QJSList<QQmlListProperty<QObject>, QObject *> : private QJSListIndexClam
qsizetype clampedStart = std::min(clamp(start, size), size - 1);
for (qsizetype i = clampedStart; i >= 0; --i) {
if (m_list->at(m_list, i) == value)
- return i > std::numeric_limits<int>::max() ? -1 : int(i);
+ return i;
}
return -1;
}
@@ -342,11 +334,11 @@ public:
}
bool hasNext() const { return m_index < m_size; }
- int next() { return m_index++; }
+ qsizetype next() { return m_index++; }
private:
- int m_index;
- int m_size;
+ qsizetype m_index;
+ qsizetype m_size;
};
// QJSListForInIterator must not require initialization so that we can jump over it with goto.
@@ -365,7 +357,7 @@ public:
Value next(const QJSList<List, Value> &list) { return list.at(m_index++); }
private:
- int m_index;
+ qsizetype m_index;
};
// QJSListForOfIterator must not require initialization so that we can jump over it with goto.