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 "qqmldelayedcallqueue_p.h"
41#include <private/qqmlengine_p.h>
42#include <private/qqmljavascriptexpression_p.h>
43#include <private/qv4value_p.h>
44#include <private/qv4jscall_p.h>
45#include <private/qv4qobjectwrapper_p.h>
46#include <private/qv4qmlcontext_p.h>
47
48#include <QQmlError>
49
50QT_BEGIN_NAMESPACE
51
52//
53// struct QQmlDelayedCallQueue::DelayedFunctionCall
54//
55
56void QQmlDelayedCallQueue::DelayedFunctionCall::execute(QV4::ExecutionEngine *engine) const
57{
58 if (!m_guarded ||
59 (!m_objectGuard.isNull() &&
60 !QQmlData::wasDeleted(m_objectGuard) &&
61 QQmlData::get(m_objectGuard) &&
62 !QQmlData::get(m_objectGuard)->isQueuedForDeletion)) {
63
64 QV4::Scope scope(engine);
65
66 QV4::ArrayObject *array = m_args.as<QV4::ArrayObject>();
67 const QV4::FunctionObject *callback = m_function.as<QV4::FunctionObject>();
68 Q_ASSERT(callback);
69 const int argCount = array ? array->getLength() : 0;
70 QV4::JSCallData jsCallData(scope, argCount);
71 *jsCallData->thisObject = QV4::Encode::undefined();
72
73 for (int i = 0; i < argCount; i++) {
74 jsCallData->args[i] = array->get(i);
75 }
76
77 callback->call(jsCallData);
78
79 if (scope.engine->hasException) {
80 QQmlError error = scope.engine->catchExceptionAsQmlError();
81 error.setDescription(error.description() + QLatin1String(" (exception occurred during delayed function evaluation)"));
82 QQmlEnginePrivate::warning(QQmlEnginePrivate::get(scope.engine->qmlEngine()), error);
83 }
84 }
85}
86
87//
88// class QQmlDelayedCallQueue
89//
90
91QQmlDelayedCallQueue::QQmlDelayedCallQueue()
92 : QObject(nullptr), m_engine(nullptr), m_callbackOutstanding(false)
93{
94}
95
96QQmlDelayedCallQueue::~QQmlDelayedCallQueue()
97{
98}
99
100void QQmlDelayedCallQueue::init(QV4::ExecutionEngine* engine)
101{
102 m_engine = engine;
103
104 const QMetaObject &metaObject = QQmlDelayedCallQueue::staticMetaObject;
105 int methodIndex = metaObject.indexOfSlot("ticked()");
106 m_tickedMethod = metaObject.method(methodIndex);
107}
108
109QV4::ReturnedValue QQmlDelayedCallQueue::addUniquelyAndExecuteLater(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc)
110{
111 QV4::Scope scope(b);
112 if (argc == 0)
113 THROW_GENERIC_ERROR("Qt.callLater: no arguments given");
114
115 const QV4::FunctionObject *func = argv[0].as<QV4::FunctionObject>();
116
117 if (!func)
118 THROW_GENERIC_ERROR("Qt.callLater: first argument not a function or signal");
119
120 QPair<QObject *, int> functionData = QV4::QObjectMethod::extractQtMethod(func);
121 QV4::ReturnedValue arg0 = argc ? argv[0].asReturnedValue() : QV4::Encode::undefined();
122
123 QVector<DelayedFunctionCall>::Iterator iter;
124 if (functionData.second != -1) {
125 // This is a QObject function wrapper
126 iter = m_delayedFunctionCalls.begin();
127 while (iter != m_delayedFunctionCalls.end()) {
128 DelayedFunctionCall& dfc = *iter;
129 QPair<QObject *, int> storedFunctionData = QV4::QObjectMethod::extractQtMethod(dfc.m_function.as<QV4::FunctionObject>());
130 if (storedFunctionData == functionData) {
131 break; // Already stored!
132 }
133 ++iter;
134 }
135 } else {
136 // This is a JavaScript function (dynamic slot on VMEMO)
137 iter = m_delayedFunctionCalls.begin();
138 while (iter != m_delayedFunctionCalls.end()) {
139 DelayedFunctionCall& dfc = *iter;
140 if (arg0 == dfc.m_function.value()) {
141 break; // Already stored!
142 }
143 ++iter;
144 }
145 }
146
147 const bool functionAlreadyStored = (iter != m_delayedFunctionCalls.end());
148 if (functionAlreadyStored) {
149 DelayedFunctionCall dfc = *iter;
150 m_delayedFunctionCalls.erase(iter);
151 m_delayedFunctionCalls.append(dfc);
152 } else {
153 m_delayedFunctionCalls.append(QV4::PersistentValue(m_engine, arg0));
154 }
155
156 DelayedFunctionCall& dfc = m_delayedFunctionCalls.last();
157 if (dfc.m_objectGuard.isNull()) {
158 if (functionData.second != -1) {
159 // if it's a qobject function wrapper, guard against qobject deletion
160 dfc.m_objectGuard = QQmlGuard<QObject>(functionData.first);
161 dfc.m_guarded = true;
162 } else if (func->scope()->type == QV4::Heap::ExecutionContext::Type_QmlContext) {
163 QV4::QmlContext::Data *g = static_cast<QV4::QmlContext::Data *>(func->scope());
164 Q_ASSERT(g->qml()->scopeObject);
165 dfc.m_objectGuard = QQmlGuard<QObject>(g->qml()->scopeObject);
166 dfc.m_guarded = true;
167 }
168 }
169 storeAnyArguments(dfc, argv, argc, 1, m_engine);
170
171 if (!m_callbackOutstanding) {
172 m_tickedMethod.invoke(this, Qt::QueuedConnection);
173 m_callbackOutstanding = true;
174 }
175 return QV4::Encode::undefined();
176}
177
178void QQmlDelayedCallQueue::storeAnyArguments(DelayedFunctionCall &dfc, const QV4::Value *argv, int argc, int offset, QV4::ExecutionEngine *engine)
179{
180 const int length = argc - offset;
181 if (length == 0) {
182 dfc.m_args.clear();
183 return;
184 }
185 QV4::Scope scope(engine);
186 QV4::ScopedArrayObject array(scope, engine->newArrayObject(length));
187 uint i = 0;
188 for (int j = offset, ej = argc; j < ej; ++i, ++j)
189 array->put(i, argv[j]);
190 dfc.m_args.set(engine, array);
191}
192
193void QQmlDelayedCallQueue::executeAllExpired_Later()
194{
195 // Make a local copy of the list and clear m_delayedFunctionCalls
196 // This ensures correct behavior in the case of recursive calls to Qt.callLater()
197 QVector<DelayedFunctionCall> delayedCalls = m_delayedFunctionCalls;
198 m_delayedFunctionCalls.clear();
199
200 QVector<DelayedFunctionCall>::Iterator iter = delayedCalls.begin();
201 while (iter != delayedCalls.end()) {
202 DelayedFunctionCall& dfc = *iter;
203 dfc.execute(m_engine);
204 ++iter;
205 }
206}
207
208void QQmlDelayedCallQueue::ticked()
209{
210 m_callbackOutstanding = false;
211 executeAllExpired_Later();
212}
213
214QT_END_NAMESPACE
215
216#include "moc_qqmldelayedcallqueue_p.cpp"
217