Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmldelayedcallqueue.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6#include <private/qqmlengine_p.h>
7#include <private/qqmljavascriptexpression_p.h>
8#include <private/qv4value_p.h>
9#include <private/qv4jscall_p.h>
10#include <private/qv4qobjectwrapper_p.h>
11#include <private/qv4qmlcontext_p.h>
12#include <private/qv4runtime_p.h>
13#include <private/qobject_p.h>
14
15#include <QQmlError>
16
18
19//
20// struct QQmlDelayedCallQueue::DelayedFunctionCall
21//
22
23static bool isAlive(const QObject *object)
24{
25 return !QQmlData::wasDeleted(object) && QQmlData::get(object);
26}
27
28// deleteLater(), and so destroy(), doesn't set isQueuedForDeletion, and an object's context
29// can be invalidated while the object stays alive.
30static bool isAliveInValidContext(const QObject *object)
31{
32 if (QQmlData::wasDeleted(object) || QObjectPrivate::get(object)->deleteLaterCalled)
33 return false;
34 const QQmlData *ddata = QQmlData::get(object);
35 return ddata && (!ddata->context || ddata->context->isValid());
36}
37
38void QQmlDelayedCallQueue::DelayedFunctionCall::execute(QV4::ExecutionEngine *engine) const
39{
40 if ((!m_guarded || isAlive(m_objectGuard))
41 && (!m_hasExplicitObject || isAliveInValidContext(m_explicitObject))) {
42
43 QV4::Scope scope(engine);
44
45 QV4::ArrayObject *array = m_args.as<QV4::ArrayObject>();
46 const QV4::FunctionObject *callback = m_function.as<QV4::FunctionObject>();
47 Q_ASSERT(callback);
48 const int argCount = array ? array->getLength() : 0;
49 QV4::JSCallArguments jsCallData(scope, argCount);
50 *jsCallData.thisObject = QV4::Encode::undefined();
51
52 for (int i = 0; i < argCount; i++) {
53 jsCallData.args[i] = array->get(i);
54 }
55
56 callback->call(jsCallData);
57
58 if (scope.hasException()) {
59 QQmlError error = scope.engine->catchExceptionAsQmlError();
60 error.setDescription(error.description() + QLatin1String(" (exception occurred during delayed function evaluation)"));
61 QQmlEnginePrivate::warning(QQmlEnginePrivate::get(scope.engine->qmlEngine()), error);
62 }
63 }
64}
65
66bool QQmlDelayedCallQueue::DelayedFunctionCall::argumentsEqual(
67 QQmlV4FunctionPtr args, int offset) const
68{
69 QV4::ArrayObject *array = m_args.as<QV4::ArrayObject>();
70 const int argCount = array ? array->getLength() : 0;
71 if (argCount != args->length() - offset)
72 return false;
73
74 QV4::Scope scope(args->v4engine());
75 QV4::ScopedValue storedArgument(scope);
76 QV4::ScopedValue argument(scope);
77 for (int i = 0; i < argCount; ++i) {
78 storedArgument = array->get(i);
79 argument = (*args)[offset + i];
80 if (!QV4::RuntimeHelpers::strictEqual(storedArgument, argument))
81 return false;
82 }
83 return true;
84}
85
86//
87// class QQmlDelayedCallQueue
88//
89
90QQmlDelayedCallQueue::QQmlDelayedCallQueue()
91 : QObject(nullptr), m_engine(nullptr), m_callbackOutstanding(false)
92{
93}
94
95QQmlDelayedCallQueue::~QQmlDelayedCallQueue()
96{
97}
98
99void QQmlDelayedCallQueue::init(QV4::ExecutionEngine* engine)
100{
101 m_engine = engine;
102
103 const QMetaObject &metaObject = QQmlDelayedCallQueue::staticMetaObject;
104 int methodIndex = metaObject.indexOfSlot("ticked()");
105 m_tickedMethod = metaObject.method(methodIndex);
106}
107
108QV4::ReturnedValue QQmlDelayedCallQueue::addUniquelyAndExecuteLater(QV4::ExecutionEngine *engine, QQmlV4FunctionPtr args)
109{
110 QQmlDelayedCallQueue *self = engine->delayedCallQueue();
111
112 QV4::Scope scope(engine);
113 if (args->length() == 0)
114 THROW_GENERIC_ERROR("Qt.callLater: no arguments given");
115
116 QV4::ScopedValue firstArgument(scope, (*args)[0]);
117 QObject *explicitObject = nullptr;
118 int functionIndex = 0;
119 if (!firstArgument->as<QV4::FunctionObject>()) {
120 // Qt.callLater(object, function, ...)
121 const QV4::QObjectWrapper *wrapper = firstArgument->as<QV4::QObjectWrapper>();
122 explicitObject = wrapper ? wrapper->object() : nullptr;
123 if (!explicitObject)
124 THROW_GENERIC_ERROR("Qt.callLater: first argument not a function, signal, or QObject");
125 functionIndex = 1;
126 }
127
128 QV4::ScopedValue function(scope, (*args)[functionIndex]);
129 const QV4::FunctionObject *func = function->as<QV4::FunctionObject>();
130
131 if (!func)
132 THROW_GENERIC_ERROR("Qt.callLater: second argument not a function or signal");
133
134 std::pair<QObject *, int> functionData = QV4::QObjectMethod::extractQtMethod(func);
135
136 // Calls with an object merge only if their arguments are strictly equal, too
137 const auto matchesObjectAndArguments = [&](const DelayedFunctionCall &dfc) {
138 return dfc.matchesExplicitObject(explicitObject)
139 && (!explicitObject || dfc.argumentsEqual(args, functionIndex + 1));
140 };
141
142 QList<DelayedFunctionCall>::Iterator iter;
143 if (functionData.second != -1) {
144 // This is a QObject function wrapper
145 iter = self->m_delayedFunctionCalls.begin();
146 while (iter != self->m_delayedFunctionCalls.end()) {
147 DelayedFunctionCall& dfc = *iter;
148 std::pair<QObject *, int> storedFunctionData = QV4::QObjectMethod::extractQtMethod(dfc.m_function.as<QV4::FunctionObject>());
149 if (matchesObjectAndArguments(dfc) && storedFunctionData == functionData) {
150 break; // Already stored!
151 }
152 ++iter;
153 }
154 } else {
155 // This is a JavaScript function (dynamic slot on VMEMO)
156 iter = self->m_delayedFunctionCalls.begin();
157 while (iter != self->m_delayedFunctionCalls.end()) {
158 DelayedFunctionCall& dfc = *iter;
159 if (matchesObjectAndArguments(dfc)
160 && function->asReturnedValue() == dfc.m_function.value()) {
161 break; // Already stored!
162 }
163 ++iter;
164 }
165 }
166
167 const bool functionAlreadyStored = (iter != self->m_delayedFunctionCalls.end());
168 if (functionAlreadyStored) {
169 DelayedFunctionCall dfc = *iter;
170 self->m_delayedFunctionCalls.erase(iter);
171 self->m_delayedFunctionCalls.append(dfc);
172 } else {
173 self->m_delayedFunctionCalls.append(
174 DelayedFunctionCall(QV4::PersistentValue(engine, function), explicitObject));
175 }
176
177 DelayedFunctionCall& dfc = self->m_delayedFunctionCalls.last();
178 if (dfc.m_objectGuard.isNull()) {
179 if (functionData.second != -1) {
180 // if it's a qobject function wrapper, guard against qobject deletion
181 dfc.m_objectGuard = QQmlGuard<QObject>(functionData.first);
182 dfc.m_guarded = true;
183 } else if (const auto *js = func->as<QV4::JavaScriptFunctionObject>();
184 js && js->scope()->type == QV4::Heap::ExecutionContext::Type_QmlContext) {
185 QV4::QmlContext::Data *g = static_cast<QV4::QmlContext::Data *>(js->scope());
186 Q_ASSERT(g->qml()->scopeObject);
187 dfc.m_objectGuard = QQmlGuard<QObject>(g->qml()->scopeObject);
188 dfc.m_guarded = true;
189 }
190 }
191 // A merged call with an object already has these arguments
192 if (!functionAlreadyStored || !explicitObject)
193 self->storeAnyArguments(dfc, args, functionIndex + 1, engine);
194
195 if (!self->m_callbackOutstanding) {
196 self->m_tickedMethod.invoke(self, Qt::QueuedConnection);
197 self->m_callbackOutstanding = true;
198 }
199 return QV4::Encode::undefined();
200}
201
202void QQmlDelayedCallQueue::storeAnyArguments(DelayedFunctionCall &dfc, QQmlV4FunctionPtr args, int offset, QV4::ExecutionEngine *engine)
203{
204 const int length = args->length() - offset;
205 if (length == 0) {
206 dfc.m_args.clear();
207 return;
208 }
209 QV4::Scope scope(engine);
210 QV4::ScopedArrayObject array(scope, engine->newArrayObject(length));
211 uint i = 0;
212 for (int j = offset, ej = args->length(); j < ej; ++i, ++j)
213 // fromReturnedValue is safe, args should already ensure marking
214 array->put(i, QV4::Value::fromReturnedValue((*args)[j]));
215 dfc.m_args.set(engine, array);
216}
217
218void QQmlDelayedCallQueue::executeAllExpired_Later()
219{
220 // Make a local copy of the list and clear m_delayedFunctionCalls
221 // This ensures correct behavior in the case of recursive calls to Qt.callLater()
222 QList<DelayedFunctionCall> delayedCalls = m_delayedFunctionCalls;
223 m_delayedFunctionCalls.clear();
224
225 QList<DelayedFunctionCall>::Iterator iter = delayedCalls.begin();
226 while (iter != delayedCalls.end()) {
227 DelayedFunctionCall& dfc = *iter;
228 dfc.execute(m_engine);
229 ++iter;
230 }
231}
232
233void QQmlDelayedCallQueue::ticked()
234{
235 m_callbackOutstanding = false;
236 executeAllExpired_Later();
237}
238
239QT_END_NAMESPACE
240
241#include "moc_qqmldelayedcallqueue_p.cpp"
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE bool isAlive(const QObject *object)
static bool isAliveInValidContext(const QObject *object)