Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5#include <private/qqmlengine_p.h>
6#include <private/qqmljavascriptexpression_p.h>
7#include <private/qv4value_p.h>
8#include <private/qv4jscall_p.h>
9#include <private/qv4qobjectwrapper_p.h>
10#include <private/qv4qmlcontext_p.h>
11
12#include <QQmlError>
13
15
16//
17// struct QQmlDelayedCallQueue::DelayedFunctionCall
18//
19
20void QQmlDelayedCallQueue::DelayedFunctionCall::execute(QV4::ExecutionEngine *engine) const
21{
22 if (!m_guarded ||
23 (!m_objectGuard.isNull() &&
24 !QQmlData::wasDeleted(m_objectGuard) &&
25 QQmlData::get(m_objectGuard) &&
26 !QQmlData::get(m_objectGuard)->isQueuedForDeletion)) {
27
28 QV4::Scope scope(engine);
29
31 const QV4::FunctionObject *callback = m_function.as<QV4::FunctionObject>();
32 Q_ASSERT(callback);
33 const int argCount = array ? array->getLength() : 0;
34 QV4::JSCallArguments jsCallData(scope, argCount);
35 *jsCallData.thisObject = QV4::Encode::undefined();
36
37 for (int i = 0; i < argCount; i++) {
38 jsCallData.args[i] = array->get(i);
39 }
40
41 callback->call(jsCallData);
42
43 if (scope.hasException()) {
44 QQmlError error = scope.engine->catchExceptionAsQmlError();
45 error.setDescription(error.description() + QLatin1String(" (exception occurred during delayed function evaluation)"));
46 QQmlEnginePrivate::warning(QQmlEnginePrivate::get(scope.engine->qmlEngine()), error);
47 }
48 }
49}
50
51//
52// class QQmlDelayedCallQueue
53//
54
56 : QObject(nullptr), m_engine(nullptr), m_callbackOutstanding(false)
57{
58}
59
63
65{
66 m_engine = engine;
67
68 const QMetaObject &metaObject = QQmlDelayedCallQueue::staticMetaObject;
69 int methodIndex = metaObject.indexOfSlot("ticked()");
70 m_tickedMethod = metaObject.method(methodIndex);
71}
72
74{
75 QQmlDelayedCallQueue *self = engine->delayedCallQueue();
76
77 QV4::Scope scope(engine);
78 if (args->length() == 0)
79 THROW_GENERIC_ERROR("Qt.callLater: no arguments given");
80
81 QV4::ScopedValue firstArgument(scope, (*args)[0]);
82
83 const QV4::FunctionObject *func = firstArgument->as<QV4::FunctionObject>();
84
85 if (!func)
86 THROW_GENERIC_ERROR("Qt.callLater: first argument not a function or signal");
87
88 QPair<QObject *, int> functionData = QV4::QObjectMethod::extractQtMethod(func);
89
91 if (functionData.second != -1) {
92 // This is a QObject function wrapper
93 iter = self->m_delayedFunctionCalls.begin();
94 while (iter != self->m_delayedFunctionCalls.end()) {
95 DelayedFunctionCall& dfc = *iter;
96 QPair<QObject *, int> storedFunctionData = QV4::QObjectMethod::extractQtMethod(dfc.m_function.as<QV4::FunctionObject>());
97 if (storedFunctionData == functionData) {
98 break; // Already stored!
99 }
100 ++iter;
101 }
102 } else {
103 // This is a JavaScript function (dynamic slot on VMEMO)
104 iter = self->m_delayedFunctionCalls.begin();
105 while (iter != self->m_delayedFunctionCalls.end()) {
106 DelayedFunctionCall& dfc = *iter;
107 if (firstArgument->asReturnedValue() == dfc.m_function.value()) {
108 break; // Already stored!
109 }
110 ++iter;
111 }
112 }
113
114 const bool functionAlreadyStored = (iter != self->m_delayedFunctionCalls.end());
115 if (functionAlreadyStored) {
116 DelayedFunctionCall dfc = *iter;
117 self->m_delayedFunctionCalls.erase(iter);
118 self->m_delayedFunctionCalls.append(dfc);
119 } else {
120 self->m_delayedFunctionCalls.append(QV4::PersistentValue(engine, firstArgument));
121 }
122
123 DelayedFunctionCall& dfc = self->m_delayedFunctionCalls.last();
124 if (dfc.m_objectGuard.isNull()) {
125 if (functionData.second != -1) {
126 // if it's a qobject function wrapper, guard against qobject deletion
127 dfc.m_objectGuard = QQmlGuard<QObject>(functionData.first);
128 dfc.m_guarded = true;
129 } else if (const auto *js = func->as<QV4::JavaScriptFunctionObject>();
130 js && js->scope()->type == QV4::Heap::ExecutionContext::Type_QmlContext) {
131 QV4::QmlContext::Data *g = static_cast<QV4::QmlContext::Data *>(js->scope());
132 Q_ASSERT(g->qml()->scopeObject);
133 dfc.m_objectGuard = QQmlGuard<QObject>(g->qml()->scopeObject);
134 dfc.m_guarded = true;
135 }
136 }
137 self->storeAnyArguments(dfc, args, 1, engine);
138
139 if (!self->m_callbackOutstanding) {
140 self->m_tickedMethod.invoke(self, Qt::QueuedConnection);
141 self->m_callbackOutstanding = true;
142 }
143 return QV4::Encode::undefined();
144}
145
146void QQmlDelayedCallQueue::storeAnyArguments(DelayedFunctionCall &dfc, QQmlV4FunctionPtr args, int offset, QV4::ExecutionEngine *engine)
147{
148 const int length = args->length() - offset;
149 if (length == 0) {
150 dfc.m_args.clear();
151 return;
152 }
153 QV4::Scope scope(engine);
154 QV4::ScopedArrayObject array(scope, engine->newArrayObject(length));
155 uint i = 0;
156 for (int j = offset, ej = args->length(); j < ej; ++i, ++j)
157 array->put(i, (*args)[j]);
158 dfc.m_args.set(engine, array);
159}
160
161void QQmlDelayedCallQueue::executeAllExpired_Later()
162{
163 // Make a local copy of the list and clear m_delayedFunctionCalls
164 // This ensures correct behavior in the case of recursive calls to Qt.callLater()
165 QVector<DelayedFunctionCall> delayedCalls = m_delayedFunctionCalls;
166 m_delayedFunctionCalls.clear();
167
168 QVector<DelayedFunctionCall>::Iterator iter = delayedCalls.begin();
169 while (iter != delayedCalls.end()) {
170 DelayedFunctionCall& dfc = *iter;
171 dfc.execute(m_engine);
172 ++iter;
173 }
174}
175
177{
178 m_callbackOutstanding = false;
179 executeAllExpired_Later();
180}
181
183
184#include "moc_qqmldelayedcallqueue_p.cpp"
qsizetype length() const noexcept
Definition qlist.h:399
\inmodule QtCore
Definition qobject.h:103
static bool wasDeleted(const QObject *)
Definition qqmldata_p.h:312
static QQmlData * get(QObjectPrivate *priv, bool create)
Definition qqmldata_p.h:199
static QV4::ReturnedValue addUniquelyAndExecuteLater(QV4::ExecutionEngine *engine, QQmlV4FunctionPtr args)
void init(QV4::ExecutionEngine *)
void warning(const QQmlError &)
static QQmlEnginePrivate * get(QQmlEngine *e)
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
bool isNull() const noexcept
Definition qqmlguard_p.h:44
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first character in the string.
Definition qstring.h:1349
iterator end()
Returns an \l{STL-style iterators}{STL-style iterator} pointing just after the last character in the ...
Definition qstring.h:1357
QString & append(QChar c)
Definition qstring.cpp:3252
QString last(qsizetype n) const &
Definition qstring.h:392
iterator erase(const_iterator first, const_iterator last)
Definition qstring.cpp:9411
Combined button and popup list for selecting options.
quint64 ReturnedValue
@ QueuedConnection
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter * iter
DBusConnection const char DBusError * error
GLenum GLuint GLenum GLsizei length
GLenum GLuint GLintptr offset
GLboolean GLboolean g
GLenum func
Definition qopenglext.h:663
GLenum array
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int uint
Definition qtypes.h:34
#define THROW_GENERIC_ERROR(str)
obj metaObject() -> className()
QObject::connect nullptr
QJSValueList args
QJSEngine engine
[0]
\inmodule QtCore
int indexOfSlot(const char *slot) const
Finds slot and returns its index; otherwise returns -1.
static constexpr ReturnedValue undefined()
static QPair< QObject *, int > extractQtMethod(const QV4::FunctionObject *function)
const T * as() const
Definition qv4value_p.h:132