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
13#include <QQmlError>
14
16
17//
18// struct QQmlDelayedCallQueue::DelayedFunctionCall
19//
20
21void QQmlDelayedCallQueue::DelayedFunctionCall::execute(QV4::ExecutionEngine *engine) const
22{
23 if (!m_guarded ||
24 (!m_objectGuard.isNull() &&
25 !QQmlData::wasDeleted(m_objectGuard) &&
26 QQmlData::get(m_objectGuard) &&
27 !QQmlData::get(m_objectGuard)->isQueuedForDeletion)) {
28
29 QV4::Scope scope(engine);
30
31 QV4::ArrayObject *array = m_args.as<QV4::ArrayObject>();
32 const QV4::FunctionObject *callback = m_function.as<QV4::FunctionObject>();
33 Q_ASSERT(callback);
34 const int argCount = array ? array->getLength() : 0;
35 QV4::JSCallArguments jsCallData(scope, argCount);
36 *jsCallData.thisObject = QV4::Encode::undefined();
37
38 for (int i = 0; i < argCount; i++) {
39 jsCallData.args[i] = array->get(i);
40 }
41
42 callback->call(jsCallData);
43
44 if (scope.hasException()) {
45 QQmlError error = scope.engine->catchExceptionAsQmlError();
46 error.setDescription(error.description() + QLatin1String(" (exception occurred during delayed function evaluation)"));
47 QQmlEnginePrivate::warning(QQmlEnginePrivate::get(scope.engine->qmlEngine()), error);
48 }
49 }
50}
51
52//
53// class QQmlDelayedCallQueue
54//
55
56QQmlDelayedCallQueue::QQmlDelayedCallQueue()
57 : QObject(nullptr), m_engine(nullptr), m_callbackOutstanding(false)
58{
59}
60
61QQmlDelayedCallQueue::~QQmlDelayedCallQueue()
62{
63}
64
65void QQmlDelayedCallQueue::init(QV4::ExecutionEngine* engine)
66{
67 m_engine = engine;
68
69 const QMetaObject &metaObject = QQmlDelayedCallQueue::staticMetaObject;
70 int methodIndex = metaObject.indexOfSlot("ticked()");
71 m_tickedMethod = metaObject.method(methodIndex);
72}
73
74QV4::ReturnedValue QQmlDelayedCallQueue::addUniquelyAndExecuteLater(QV4::ExecutionEngine *engine, QQmlV4FunctionPtr args)
75{
76 QQmlDelayedCallQueue *self = engine->delayedCallQueue();
77
78 QV4::Scope scope(engine);
79 if (args->length() == 0)
80 THROW_GENERIC_ERROR("Qt.callLater: no arguments given");
81
82 QV4::ScopedValue firstArgument(scope, (*args)[0]);
83
84 const QV4::FunctionObject *func = firstArgument->as<QV4::FunctionObject>();
85
86 if (!func)
87 THROW_GENERIC_ERROR("Qt.callLater: first argument not a function or signal");
88
89 std::pair<QObject *, int> functionData = QV4::QObjectMethod::extractQtMethod(func);
90
91 QList<DelayedFunctionCall>::Iterator iter;
92 if (functionData.second != -1) {
93 // This is a QObject function wrapper
94 iter = self->m_delayedFunctionCalls.begin();
95 while (iter != self->m_delayedFunctionCalls.end()) {
96 DelayedFunctionCall& dfc = *iter;
97 std::pair<QObject *, int> storedFunctionData = QV4::QObjectMethod::extractQtMethod(dfc.m_function.as<QV4::FunctionObject>());
98 if (storedFunctionData == functionData) {
99 break; // Already stored!
100 }
101 ++iter;
102 }
103 } else {
104 // This is a JavaScript function (dynamic slot on VMEMO)
105 iter = self->m_delayedFunctionCalls.begin();
106 while (iter != self->m_delayedFunctionCalls.end()) {
107 DelayedFunctionCall& dfc = *iter;
108 if (firstArgument->asReturnedValue() == dfc.m_function.value()) {
109 break; // Already stored!
110 }
111 ++iter;
112 }
113 }
114
115 const bool functionAlreadyStored = (iter != self->m_delayedFunctionCalls.end());
116 if (functionAlreadyStored) {
117 DelayedFunctionCall dfc = *iter;
118 self->m_delayedFunctionCalls.erase(iter);
119 self->m_delayedFunctionCalls.append(dfc);
120 } else {
121 self->m_delayedFunctionCalls.append(QV4::PersistentValue(engine, firstArgument));
122 }
123
124 DelayedFunctionCall& dfc = self->m_delayedFunctionCalls.last();
125 if (dfc.m_objectGuard.isNull()) {
126 if (functionData.second != -1) {
127 // if it's a qobject function wrapper, guard against qobject deletion
128 dfc.m_objectGuard = QQmlGuard<QObject>(functionData.first);
129 dfc.m_guarded = true;
130 } else if (const auto *js = func->as<QV4::JavaScriptFunctionObject>();
131 js && js->scope()->type == QV4::Heap::ExecutionContext::Type_QmlContext) {
132 QV4::QmlContext::Data *g = static_cast<QV4::QmlContext::Data *>(js->scope());
133 Q_ASSERT(g->qml()->scopeObject);
134 dfc.m_objectGuard = QQmlGuard<QObject>(g->qml()->scopeObject);
135 dfc.m_guarded = true;
136 }
137 }
138 self->storeAnyArguments(dfc, args, 1, engine);
139
140 if (!self->m_callbackOutstanding) {
141 self->m_tickedMethod.invoke(self, Qt::QueuedConnection);
142 self->m_callbackOutstanding = true;
143 }
144 return QV4::Encode::undefined();
145}
146
147void QQmlDelayedCallQueue::storeAnyArguments(DelayedFunctionCall &dfc, QQmlV4FunctionPtr args, int offset, QV4::ExecutionEngine *engine)
148{
149 const int length = args->length() - offset;
150 if (length == 0) {
151 dfc.m_args.clear();
152 return;
153 }
154 QV4::Scope scope(engine);
155 QV4::ScopedArrayObject array(scope, engine->newArrayObject(length));
156 uint i = 0;
157 for (int j = offset, ej = args->length(); j < ej; ++i, ++j)
158 // fromReturnedValue is safe, args should already ensure marking
159 array->put(i, QV4::Value::fromReturnedValue((*args)[j]));
160 dfc.m_args.set(engine, array);
161}
162
163void QQmlDelayedCallQueue::executeAllExpired_Later()
164{
165 // Make a local copy of the list and clear m_delayedFunctionCalls
166 // This ensures correct behavior in the case of recursive calls to Qt.callLater()
167 QList<DelayedFunctionCall> delayedCalls = m_delayedFunctionCalls;
168 m_delayedFunctionCalls.clear();
169
170 QList<DelayedFunctionCall>::Iterator iter = delayedCalls.begin();
171 while (iter != delayedCalls.end()) {
172 DelayedFunctionCall& dfc = *iter;
173 dfc.execute(m_engine);
174 ++iter;
175 }
176}
177
178void QQmlDelayedCallQueue::ticked()
179{
180 m_callbackOutstanding = false;
181 executeAllExpired_Later();
182}
183
184QT_END_NAMESPACE
185
186#include "moc_qqmldelayedcallqueue_p.cpp"
Combined button and popup list for selecting options.