108QV4::ReturnedValue QQmlDelayedCallQueue::addUniquelyAndExecuteLater(QV4::ExecutionEngine *engine, QQmlV4FunctionPtr args)
110 QQmlDelayedCallQueue *self = engine->delayedCallQueue();
112 QV4::Scope scope(engine);
113 if (args->length() == 0)
114 THROW_GENERIC_ERROR(
"Qt.callLater: no arguments given");
116 QV4::ScopedValue firstArgument(scope, (*args)[0]);
117 QObject *explicitObject =
nullptr;
118 int functionIndex = 0;
119 if (!firstArgument->as<QV4::FunctionObject>()) {
121 const QV4::QObjectWrapper *wrapper = firstArgument->as<QV4::QObjectWrapper>();
122 explicitObject = wrapper ? wrapper->object() :
nullptr;
124 THROW_GENERIC_ERROR(
"Qt.callLater: first argument not a function, signal, or QObject");
128 QV4::ScopedValue function(scope, (*args)[functionIndex]);
129 const QV4::FunctionObject *func = function->as<QV4::FunctionObject>();
132 THROW_GENERIC_ERROR(
"Qt.callLater: second argument not a function or signal");
134 std::pair<QObject *,
int> functionData = QV4::QObjectMethod::extractQtMethod(func);
137 const auto matchesObjectAndArguments = [&](
const DelayedFunctionCall &dfc) {
138 return dfc.matchesExplicitObject(explicitObject)
139 && (!explicitObject || dfc.argumentsEqual(args, functionIndex + 1));
142 QList<DelayedFunctionCall>::Iterator iter;
143 if (functionData.second != -1) {
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) {
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()) {
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);
173 self->m_delayedFunctionCalls.append(
174 DelayedFunctionCall(QV4::PersistentValue(engine, function), explicitObject));
177 DelayedFunctionCall& dfc = self->m_delayedFunctionCalls.last();
178 if (dfc.m_objectGuard.isNull()) {
179 if (functionData.second != -1) {
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;
192 if (!functionAlreadyStored || !explicitObject)
193 self->storeAnyArguments(dfc, args, functionIndex + 1, engine);
195 if (!self->m_callbackOutstanding) {
196 self->m_tickedMethod.invoke(self, Qt::QueuedConnection);
197 self->m_callbackOutstanding =
true;
199 return QV4::Encode::undefined();