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
qqmlbinding.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
7#include "qqmlcontext.h"
8#include "qqmldata_p.h"
9#include "qqmlinfo.h"
10
11#include <private/qqmldebugserviceinterfaces_p.h>
12#include <private/qqmldebugconnector_p.h>
13
14#include <private/qqmlprofiler_p.h>
15#include <private/qqmlexpression_p.h>
16#include <private/qqmlscriptstring_p.h>
17#include <private/qqmlbuiltinfunctions_p.h>
18#include <private/qqmlvmemetaobject_p.h>
19#include <private/qqmlvaluetypewrapper_p.h>
20#include <private/qv4qmlcontext_p.h>
21#include <private/qv4qobjectwrapper_p.h>
22#include <private/qv4variantobject_p.h>
23#include <private/qv4jscall_p.h>
24#include <private/qjsvalue_p.h>
25
26#include <qtqml_tracepoints_p.h>
27
28#include <QVariant>
29#include <QtCore/qdebug.h>
30#include <QList>
31
33
34Q_TRACE_POINT(qtqml, QQmlBinding_entry, const QQmlEngine *engine, const QString &function, const QString &fileName, int line, int column)
35Q_TRACE_POINT(qtqml, QQmlBinding_exit)
36
37QQmlBinding *QQmlBinding::create(const QQmlPropertyData *property, const QQmlScriptString &script, QObject *obj, QQmlContext *ctxt)
38{
39 QQmlBinding *b = newBinding(property);
40
41 if (ctxt && !ctxt->isValid())
42 return b;
43
44 const QQmlScriptStringPrivate *scriptPrivate = script.d.data();
45 if (!ctxt && (!scriptPrivate->context || !scriptPrivate->context->isValid()))
46 return b;
47
48 QString url;
49 QV4::Function *runtimeFunction = nullptr;
50
51 QQmlRefPointer<QQmlContextData> ctxtdata = QQmlContextData::get(scriptPrivate->context);
52 QQmlEnginePrivate *engine = QQmlEnginePrivate::get(scriptPrivate->context->engine());
53 if (engine && ctxtdata && !ctxtdata->urlString().isEmpty() && ctxtdata->typeCompilationUnit()) {
54 url = ctxtdata->urlString();
55 if (scriptPrivate->bindingId != QQmlBinding::Invalid)
56 runtimeFunction = ctxtdata->typeCompilationUnit()->runtimeFunctions.at(scriptPrivate->bindingId);
57 }
58
59 b->setNotifyOnValueChanged(true);
60 b->QQmlJavaScriptExpression::setContext(QQmlContextData::get(ctxt ? ctxt : scriptPrivate->context));
61 b->setScopeObject(obj ? obj : scriptPrivate->scope);
62
63 QV4::ExecutionEngine *v4 = b->engine()->handle();
64 if (runtimeFunction) {
65 QV4::Scope scope(v4);
66 QV4::Scoped<QV4::QmlContext> qmlContext(scope, QV4::QmlContext::create(v4->rootContext(), ctxtdata, b->scopeObject()));
67 b->setupFunction(qmlContext, runtimeFunction);
68 } else {
69 QString code = scriptPrivate->script;
70 b->createQmlBinding(b->context(), b->scopeObject(), code, url, scriptPrivate->lineNumber);
71 }
72
73 return b;
74}
75
76QQmlSourceLocation QQmlBinding::sourceLocation() const
77{
78 if (m_sourceLocation)
79 return *m_sourceLocation;
80 return QQmlJavaScriptExpression::sourceLocation();
81}
82
83void QQmlBinding::setSourceLocation(const QQmlSourceLocation &location)
84{
85 if (m_sourceLocation)
86 delete m_sourceLocation;
87 m_sourceLocation = new QQmlSourceLocation(location);
88}
89
90
91QQmlBinding *QQmlBinding::create(
92 const QQmlPropertyData *property, const QString &str, QObject *obj,
93 const QQmlRefPointer<QQmlContextData> &ctxt, const QString &url, quint16 lineNumber)
94{
95 QQmlBinding *b = newBinding(property);
96
97 b->setNotifyOnValueChanged(true);
98 b->QQmlJavaScriptExpression::setContext(ctxt);
99 b->setScopeObject(obj);
100
101 b->createQmlBinding(ctxt, obj, str, url, lineNumber);
102
103 return b;
104}
105
106QQmlBinding *QQmlBinding::create(
107 const QQmlPropertyData *property, QV4::Function *function, QObject *obj,
108 const QQmlRefPointer<QQmlContextData> &ctxt, QV4::ExecutionContext *scope)
109{
110 return create(property ? property->propType() : QMetaType(), function, obj, ctxt, scope);
111}
112
113QQmlBinding *QQmlBinding::create(QMetaType propertyType, QV4::Function *function, QObject *obj,
114 const QQmlRefPointer<QQmlContextData> &ctxt,
115 QV4::ExecutionContext *scope)
116{
117 QQmlBinding *b = newBinding(propertyType);
118
119 b->setNotifyOnValueChanged(true);
120 b->QQmlJavaScriptExpression::setContext(ctxt);
121 b->setScopeObject(obj);
122
123 Q_ASSERT(scope);
124 b->setupFunction(scope, function);
125
126 return b;
127}
128
129QQmlBinding::~QQmlBinding()
130{
131 delete m_sourceLocation;
132}
133
134void QQmlBinding::update(QQmlPropertyData::WriteFlags flags)
135{
136 if (!enabledFlag() || !hasValidContext())
137 return;
138
139 // Check that the target has not been deleted
140 if (QQmlData::wasDeleted(targetObject()))
141 return;
142
143 // Check for a binding update loop
144 if (Q_UNLIKELY(updatingFlag())) {
145 const QQmlPropertyData *d = nullptr;
146 QQmlPropertyData vtd;
147 getPropertyData(&d, &vtd);
148 Q_ASSERT(d);
149 QQmlProperty p = QQmlPropertyPrivate::restore(targetObject(), *d, &vtd, nullptr);
150 printBindingLoopError(p);
151 return;
152 }
153 setUpdatingFlag(true);
154
155 DeleteWatcher watcher(this);
156
157 QQmlEngine *qmlEngine = engine();
158 QV4::Scope scope(qmlEngine->handle());
159
160 if (canUseAccessor())
161 flags.setFlag(QQmlPropertyData::BypassInterceptor);
162
163 Q_TRACE_SCOPE(QQmlBinding, qmlEngine, function() ? function()->name()->toQString() : QString(),
164 sourceLocation().sourceFile, sourceLocation().line, sourceLocation().column);
165 QQmlBindingProfiler prof(QQmlEnginePrivate::get(qmlEngine)->profiler, function());
166 doUpdate(watcher, flags, scope);
167
168 if (!watcher.wasDeleted())
169 setUpdatingFlag(false);
170}
171
172void QQmlBinding::printBindingLoopError(const QQmlProperty &prop)
173{
174 qmlWarning(prop.object()) << QString(QLatin1String("Binding loop detected for property \"%1\":\n%2"))
175 .arg(prop.name(), expressionIdentifier());
176}
177
178QV4::ReturnedValue QQmlBinding::evaluate(bool *isUndefined)
179{
180 QV4::ExecutionEngine *v4 = engine()->handle();
181 int argc = 0;
182 const QV4::Value *argv = nullptr;
183 const QV4::Value *thisObject = nullptr;
184 QV4::BoundFunction *b = nullptr;
185 if ((b = static_cast<QV4::BoundFunction *>(m_boundFunction.valueRef()))) {
186 QV4::Heap::MemberData *args = b->boundArgs();
187 if (args) {
188 argc = args->values.size;
189 argv = args->values.data();
190 }
191 thisObject = &b->d()->boundThis;
192 }
193 QV4::Scope scope(v4);
194 QV4::JSCallData jsCall(thisObject, argv, argc);
195
196 return QQmlJavaScriptExpression::evaluate(jsCall.callData(scope), isUndefined);
197}
198
199template<int StaticPropType>
201{
202protected:
203 // Returns true if successful, false if an error description was set on expression
204 Q_ALWAYS_INLINE bool write(void *result, QMetaType type, bool isUndefined,
205 QQmlPropertyData::WriteFlags flags) override final
206 {
207 const QQmlPropertyData *pd;
208 QQmlPropertyData vpd;
209 getPropertyData(&pd, &vpd);
210 Q_ASSERT(pd);
211
212 if (isUndefined || vpd.isValid())
213 return slowWrite(*pd, vpd, result, type, isUndefined, flags);
214
215 if ((StaticPropType == QMetaType::UnknownType && pd->propType() == type)
216 || StaticPropType == type.id()) {
217 Q_ASSERT(targetObject());
218 return pd->writeProperty(targetObject(), result, flags);
219 }
220
221 // If the type didn't match, we need to do JavaScript conversion. This should be rare.
222 QV4::Scope scope(engine()->handle());
223 QV4::ScopedValue value(scope, engine()->handle()->metaTypeToJS(type, result));
224 return write(value, isUndefined, flags);
225 }
226
227 // Returns true if successful, false if an error description was set on expression
228 Q_ALWAYS_INLINE bool write(const QV4::Value &result, bool isUndefined,
229 QQmlPropertyData::WriteFlags flags) override final
230 {
231 Q_ASSERT(targetObject());
232
233 const QQmlPropertyData *pd;
234 QQmlPropertyData vpd;
235 getPropertyData(&pd, &vpd);
236 Q_ASSERT(pd);
237
238 int propertyType = StaticPropType; // If the binding is specialized to a type, the if and switch below will be constant-folded.
239 if (propertyType == QMetaType::UnknownType)
240 propertyType = pd->propType().id();
241
242 if (Q_LIKELY(!isUndefined && !vpd.isValid())) {
243 switch (propertyType) {
244 case QMetaType::Bool:
245 if (result.isBoolean())
246 return doStore<bool>(result.booleanValue(), pd, flags);
247 else
248 return doStore<bool>(result.toBoolean(), pd, flags);
249 case QMetaType::Int:
250 if (result.isInteger())
251 return doStore<int>(result.integerValue(), pd, flags);
252 else if (result.isNumber()) {
253 return doStore<int>(result.toInt32(), pd, flags);
254 }
255 break;
256 case QMetaType::Double:
257 if (result.isNumber())
258 return doStore<double>(result.asDouble(), pd, flags);
259 break;
260 case QMetaType::Float:
261 if (result.isNumber())
262 return doStore<float>(result.asDouble(), pd, flags);
263 break;
264 case QMetaType::QString:
265 if (result.isString())
266 return doStore<QString>(result.toQStringNoThrow(), pd, flags);
267 break;
268 default:
269 if (const QV4::QQmlValueTypeWrapper *vtw = result.as<const QV4::QQmlValueTypeWrapper>()) {
270 if (vtw->d()->metaType() == pd->propType()) {
271 return vtw->write(m_target.data(), pd->coreIndex());
272 }
273 }
274 break;
275 }
276 }
277
278 return slowWrite(*pd, vpd, result, isUndefined, flags);
279 }
280
281 template <typename T>
282 Q_ALWAYS_INLINE bool doStore(T value, const QQmlPropertyData *pd, QQmlPropertyData::WriteFlags flags) const
283 {
284 void *o = &value;
285 return pd->writeProperty(targetObject(), o, flags);
286 }
287};
288
290public:
291 QQmlTranslationBinding(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit)
293 {
294 setCompilationUnit(compilationUnit);
295 setSource(QQmlEnginePrivate::get(compilationUnit->engine)->translationLanguage);
296 }
297
298 virtual QString bindingValue() const = 0;
299
300 static void onLanguageChange(QPropertyObserver *observer, QUntypedPropertyData *)
301 { static_cast<QQmlTranslationBinding *>(observer)->update(); }
302
303 void doUpdate(const DeleteWatcher &watcher,
304 QQmlPropertyData::WriteFlags flags, QV4::Scope &scope) override final
305 {
306 if (watcher.wasDeleted())
307 return;
308
309 if (!isAddedToObject() || hasError())
310 return;
311
312 const QString result = this->bindingValue();
313
314 Q_ASSERT(targetObject());
315
316 const QQmlPropertyData *pd;
317 QQmlPropertyData vpd;
318 getPropertyData(&pd, &vpd);
319 Q_ASSERT(pd);
320 if (pd->propType().id() == QMetaType::QString) {
321 doStore(result, pd, flags);
322 } else {
323 QV4::ScopedString value(scope, scope.engine->newString(result));
324 slowWrite(*pd, vpd, value, /*isUndefined*/false, flags);
325 }
326 }
327
328 bool hasDependencies() const override final { return true; }
329};
330
332{
333 const QV4::CompiledData::Binding *m_binding;
334
335public:
337 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
338 const QV4::CompiledData::Binding *binding)
339 : QQmlTranslationBinding(compilationUnit), m_binding(binding)
340 {
341 }
342
343 QString bindingValue() const override
344 {
345 return this->m_compilationUnit->bindingValueAsString(m_binding);
346 }
347
348 QQmlSourceLocation sourceLocation() const override final
349 {
350 return QQmlSourceLocation(m_compilationUnit->fileName(), m_binding->valueLocation.line(),
351 m_binding->valueLocation.column());
352 }
353};
354
356{
357 QQmlTranslation m_translationData;
358
359 quint16 m_line;
360 quint16 m_column;
361
362public:
364 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
365 const QQmlTranslation &translationData, quint16 line, quint16 column)
366 : QQmlTranslationBinding(compilationUnit),
368 m_line(line),
370 {
371 }
372
373 virtual QString bindingValue() const override { return m_translationData.translate(); }
374
375 QQmlSourceLocation sourceLocation() const override final
376 {
377 return QQmlSourceLocation(m_compilationUnit->fileName(), m_line, m_column);
378 }
379};
380
381QQmlBinding *QQmlBinding::createTranslationBinding(
382 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
383 const QV4::CompiledData::Binding *binding, QObject *obj,
384 const QQmlRefPointer<QQmlContextData> &ctxt)
385{
386 QQmlTranslationBinding *b = new QQmlTranslationBindingFromBinding(unit, binding);
387
388 b->setNotifyOnValueChanged(true);
389 b->QQmlJavaScriptExpression::setContext(ctxt);
390 b->setScopeObject(obj);
391#if QT_CONFIG(translation) && QT_CONFIG(qml_debug)
392 if (QQmlDebugTranslationService *service
393 = QQmlDebugConnector::service<QQmlDebugTranslationService>()) {
394 service->foundTranslationBinding(
395 TranslationBindingInformation::create(unit, binding, b->scopeObject(), ctxt));
396 }
397#endif
398 return b;
399}
400
401QQmlBinding *QQmlBinding::createTranslationBinding(
402 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
403 const QQmlRefPointer<QQmlContextData> &ctxt, const QString &propertyName,
404 const QQmlTranslation &translationData, const QQmlSourceLocation &location, QObject *obj)
405{
406 QQmlTranslationBinding *b = new QQmlTranslationBindingFromTranslationInfo(
407 unit, translationData, location.column, location.line);
408
409 b->setNotifyOnValueChanged(true);
410 b->QQmlJavaScriptExpression::setContext(ctxt);
411 b->setScopeObject(obj);
412
413#if QT_CONFIG(translation) && QT_CONFIG(qml_debug)
414 QString originString;
415 if (QQmlDebugTranslationService *service =
416 QQmlDebugConnector::service<QQmlDebugTranslationService>()) {
417 service->foundTranslationBinding({ unit, b->scopeObject(), ctxt,
418
419 propertyName, translationData,
420
421 location.line, location.column });
422 }
423#else
424 Q_UNUSED(propertyName)
425#endif
426 return b;
427}
428
429bool QQmlBinding::slowWrite(
430 const QQmlPropertyData &core, const QQmlPropertyData &valueTypeData, const void *result,
431 QMetaType resultType, bool isUndefined, QQmlPropertyData::WriteFlags flags)
432{
433 // The logic in this method is obscure. It follows what the other slowWrite does, minus the type
434 // conversions and the checking for binding functions. If you're writing a C++ type, and
435 // you're passing a binding function wrapped into QJSValue, you probably want it to be assigned.
436
437 if (hasError())
438 return false;
439
440 QQmlEngine *qmlEngine = engine();
441 const QMetaType metaType = valueTypeData.isValid() ? valueTypeData.propType() : core.propType();
442 QQmlJavaScriptExpression::DeleteWatcher watcher(this);
443
444 if (core.isVarProperty()) {
445 QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(m_target.data());
446 Q_ASSERT(vmemo);
447 QV4::Scope scope(qmlEngine->handle());
448 QV4::ScopedValue value(scope, qmlEngine->handle()->metaTypeToJS(resultType, result));
449 vmemo->setVMEProperty(core.coreIndex(),
450 value);
451 } else if (isUndefined && core.isResettable()) {
452 void *args[] = { nullptr };
453 QMetaObject::metacall(m_target.data(), QMetaObject::ResetProperty, core.coreIndex(), args);
454 } else if (isUndefined && metaType == QMetaType::fromType<QVariant>()) {
455 QQmlPropertyPrivate::writeValueProperty(
456 m_target.data(), core, valueTypeData, QVariant(), context(), flags);
457 } else if (metaType == QMetaType::fromType<QJSValue>()) {
458 QQmlPropertyPrivate::writeValueProperty(
459 m_target.data(), core, valueTypeData,
460 QVariant(resultType, result), context(), flags);
461 } else if (isUndefined) {
462 const char *name = metaType.name();
463 const QString typeName = name
464 ? QString::fromUtf8(name)
465 : QStringLiteral("[unknown property type]");
466 delayedError()->setErrorDescription(
467 QStringLiteral("Unable to assign [undefined] to ") + typeName);
468 return false;
469 } else if (!QQmlPropertyPrivate::writeValueProperty(
470 m_target.data(), core, valueTypeData, QVariant(resultType, result),
471 context(), flags)) {
472 if (watcher.wasDeleted())
473 return true;
474 handleWriteError(result, resultType, metaType);
475 return false;
476 }
477
478 return true;
479}
480
481Q_NEVER_INLINE bool QQmlBinding::slowWrite(const QQmlPropertyData &core,
482 const QQmlPropertyData &valueTypeData,
483 const QV4::Value &result,
484 bool isUndefined, QQmlPropertyData::WriteFlags flags)
485{
486 const QMetaType metaType = valueTypeData.isValid() ? valueTypeData.propType() : core.propType();
487 const int type = metaType.id();
488
489 QQmlJavaScriptExpression::DeleteWatcher watcher(this);
490
491 QVariant value;
492 bool isVarProperty = core.isVarProperty();
493
494 if (isUndefined) {
495 } else if (core.isQList()) {
496 if (core.propType().flags() & QMetaType::IsQmlList)
497 value = QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QObject*>>());
498 else
499 value = QV4::ExecutionEngine::toVariant(result, core.propType());
500 } else if (result.isNull() && core.isQObject()) {
501 value = QVariant::fromValue((QObject *)nullptr);
502 } else if (core.propType() == QMetaType::fromType<QList<QUrl>>()) {
503 const QVariant resultVariant
504 = QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QUrl>>());
505 value = QVariant::fromValue(QQmlPropertyPrivate::resolveUrlsOnAssignment()
506 ? QQmlPropertyPrivate::urlSequence(resultVariant, context())
507 : QQmlPropertyPrivate::urlSequence(resultVariant));
508 } else if (!isVarProperty && metaType != QMetaType::fromType<QJSValue>()) {
509 value = QV4::ExecutionEngine::toVariant(result, metaType);
510 }
511
512 if (hasError()) {
513 return false;
514 } else if (isVarProperty) {
515 const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
516 if (f && f->isBinding()) {
517 // we explicitly disallow this case to avoid confusion. Users can still store one
518 // in an array in a var property if they need to, but the common case is user error.
519 delayedError()->setErrorDescription(QLatin1String("Invalid use of Qt.binding() in a binding declaration."));
520 return false;
521 }
522
523 QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(m_target.data());
524 Q_ASSERT(vmemo);
525 vmemo->setVMEProperty(core.coreIndex(), result);
526 } else if (isUndefined
527 && (valueTypeData.isValid() ? valueTypeData.isResettable() : core.isResettable())) {
528 QQmlPropertyPrivate::resetValueProperty(
529 m_target.data(), core, valueTypeData, context(), flags);
530 } else if (isUndefined && type == QMetaType::QVariant) {
531 QQmlPropertyPrivate::writeValueProperty(m_target.data(), core, valueTypeData, QVariant(), context(), flags);
532 } else if (metaType == QMetaType::fromType<QJSValue>()) {
533 const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
534 if (f && f->isBinding()) {
535 delayedError()->setErrorDescription(QLatin1String("Invalid use of Qt.binding() in a binding declaration."));
536 return false;
537 }
538 QQmlPropertyPrivate::writeValueProperty(
539 m_target.data(), core, valueTypeData,
540 QVariant::fromValue(QJSValuePrivate::fromReturnedValue(result.asReturnedValue())),
541 context(), flags);
542 } else if (isUndefined) {
543 const char *name = QMetaType(type).name();
544 const QLatin1String typeName(name ? name : "[unknown property type]");
545 delayedError()->setErrorDescription(QLatin1String("Unable to assign [undefined] to ")
546 + typeName);
547 return false;
548 } else if (const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
549 f && !f->as<QV4::QQmlTypeWrapper>()) {
550 if (f->isBinding())
551 delayedError()->setErrorDescription(QLatin1String("Invalid use of Qt.binding() in a binding declaration."));
552 else
553 delayedError()->setErrorDescription(QLatin1String("Unable to assign a function to a property of any type other than var."));
554 return false;
555 } else if (!QQmlPropertyPrivate::writeValueProperty(m_target.data(), core, valueTypeData, value, context(), flags)) {
556
557 if (watcher.wasDeleted())
558 return true;
559 handleWriteError(value.constData(), value.metaType(), metaType);
560 return false;
561 }
562
563 return true;
564}
565
566void QQmlBinding::handleWriteError(const void *result, QMetaType resultType, QMetaType metaType)
567{
568 const char *valueType = nullptr;
569 const char *propertyType = nullptr;
570
571 if (resultType.flags() & QMetaType::PointerToQObject) {
572 if (QObject *o = *(QObject *const *)result) {
573 valueType = o->metaObject()->className();
574 QQmlMetaObject propertyMetaObject = QQmlPropertyPrivate::rawMetaObjectForType(metaType);
575 if (!propertyMetaObject.isNull())
576 propertyType = propertyMetaObject.className();
577 }
578 } else if (resultType.isValid()) {
579 if (resultType == QMetaType::fromType<std::nullptr_t>()
580 || resultType == QMetaType::fromType<void *>()) {
581 valueType = "null";
582 } else {
583 valueType = resultType.name();
584 }
585 }
586
587 if (!valueType)
588 valueType = "undefined";
589 if (!propertyType)
590 propertyType = metaType.name();
591 if (!propertyType)
592 propertyType = "[unknown property type]";
593
594 delayedError()->setErrorDescription(QStringLiteral("Unable to assign ")
595 + QString::fromUtf8(valueType)
596 + QStringLiteral(" to ")
597 + QString::fromUtf8(propertyType));
598}
599
600QVariant QQmlBinding::evaluate()
601{
602 QQmlEngine *qmlEngine = engine();
603 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(qmlEngine);
604 ep->referenceScarceResources();
605
606 bool isUndefined = false;
607
608 QV4::Scope scope(qmlEngine->handle());
609 QV4::ScopedValue result(scope, QQmlJavaScriptExpression::evaluate(&isUndefined));
610
611 ep->dereferenceScarceResources();
612
613 return QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QObject*> >());
614}
615
616void QQmlBinding::expressionChanged()
617{
618 update();
619}
620
621void QQmlBinding::refresh()
622{
623 update();
624}
625
626void QQmlBinding::setEnabled(bool e, QQmlPropertyData::WriteFlags flags)
627{
628 const bool wasEnabled = enabledFlag();
629 setEnabledFlag(e);
630 setNotifyOnValueChanged(e);
631 updateCanUseAccessor();
632
633 if (e && !wasEnabled)
634 update(flags);
635}
636
637QString QQmlBinding::expression() const
638{
639 return QStringLiteral("function() { [native code] }");
640}
641
642QList<QQmlProperty> QQmlBinding::dependencies() const
643{
644 QList<QQmlProperty> dependencies;
645 if (!m_target.data())
646 return dependencies;
647
648 for (QQmlJavaScriptExpressionGuard *guard = activeGuards.first(); guard; guard = activeGuards.next(guard)) {
649 if (guard->signalIndex() == -1) // guard's sender is a QQmlNotifier, not a QObject*.
650 continue;
651
652 QObject *senderObject = guard->senderAsObject();
653 if (!senderObject)
654 continue;
655
656 const QMetaObject *senderMeta = senderObject->metaObject();
657 if (!senderMeta)
658 continue;
659
660 for (int i = 0; i < senderMeta->propertyCount(); i++) {
661 QMetaProperty property = senderMeta->property(i);
662 if (property.notifySignalIndex() == QMetaObjectPrivate::signal(senderMeta, guard->signalIndex()).methodIndex()) {
663 dependencies.push_back(QQmlProperty(senderObject, QString::fromUtf8(property.name())));
664 }
665 }
666 }
667
668 for (auto trigger = qpropertyChangeTriggers; trigger; trigger = trigger->next) {
669 QMetaProperty prop = trigger->property();
670 if (prop.isValid())
671 dependencies.push_back(QQmlProperty(trigger->target, QString::fromUtf8(prop.name())));
672 }
673
674 return dependencies;
675}
676
677bool QQmlBinding::hasDependencies() const
678{
679 return !activeGuards.isEmpty() || qpropertyChangeTriggers;
680}
681
682void QQmlBinding::doUpdate(const DeleteWatcher &watcher, QQmlPropertyData::WriteFlags flags, QV4::Scope &scope)
683{
684 auto ep = QQmlEnginePrivate::get(scope.engine);
685 ep->referenceScarceResources();
686
687 bool error = false;
688 auto canWrite = [&]() { return !watcher.wasDeleted() && isAddedToObject() && !hasError(); };
689 const QV4::Function *v4Function = function();
690 if (v4Function && v4Function->kind == QV4::Function::AotCompiled && !hasBoundFunction()) {
691 const auto returnType = v4Function->aotCompiledFunction.types[0];
692 if (returnType == QMetaType::fromType<QVariant>()) {
693 QVariant result;
694 const bool isUndefined = !evaluate(&result, returnType);
695 if (canWrite())
696 error = !write(result.data(), result.metaType(), isUndefined, flags);
697 } else {
698 const auto size = returnType.sizeOf();
699 if (Q_LIKELY(size > 0)) {
700 Q_ALLOCA_VAR(void, result, size);
701 if (returnType.flags() & QMetaType::NeedsConstruction)
702 returnType.construct(result);
703 const bool isUndefined = !evaluate(result, returnType);
704 if (canWrite())
705 error = !write(result, returnType, isUndefined, flags);
706 if (returnType.flags() & QMetaType::NeedsDestruction)
707 returnType.destruct(result);
708 } else if (canWrite()) {
709 error = !write(QV4::Value::undefined(), true, flags);
710 }
711 }
712 } else {
713 bool isUndefined = false;
714 QV4::ScopedValue result(scope, evaluate(&isUndefined));
715 if (canWrite())
716 error = !write(result, isUndefined, flags);
717 }
718
719 if (!watcher.wasDeleted()) {
720
721 if (error) {
722 delayedError()->setErrorLocation(sourceLocation());
723 delayedError()->setErrorObject(m_target.data());
724 }
725
726 if (hasError()) {
727 if (!delayedError()->addError(ep)) ep->warning(this->error(engine()));
728 } else {
729 clearError();
730 }
731 }
732
733 ep->dereferenceScarceResources();
734}
735
737{
738 QBiPointer<const QtPrivate::QMetaTypeInterface, const QMetaObject> targetMeta;
739
740 const QMetaObject *targetMetaObject() {
741 // lazily produce the targetMetaObject as needed. At the point when we want to set
742 // the property, we either have the right type. Then the metaobject has to exist. Or we
743 // don't. Then the object given can't be of the right type.
744
745 if (targetMeta.isT2())
746 return targetMeta.asT2();
747
748 const QMetaObject *metaObject = QQmlPropertyPrivate::rawMetaObjectForType(
749 QMetaType(targetMeta.asT1())).metaObject();
750
751 if (metaObject)
752 targetMeta = metaObject;
753
754 return metaObject;
755 }
756
757public:
758 QObjectPointerBinding(QMetaType propertyType) : targetMeta(propertyType.iface()) {}
759
760protected:
761 Q_ALWAYS_INLINE bool write(void *result, QMetaType type, bool isUndefined,
762 QQmlPropertyData::WriteFlags flags) override final
763 {
764 const QQmlPropertyData *pd;
765 QQmlPropertyData vtpd;
766 getPropertyData(&pd, &vtpd);
767 if (Q_UNLIKELY(isUndefined || vtpd.isValid()))
768 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
769
770 // Check if the result is a QObject:
771 QObject *resultObject = nullptr;
772 QQmlMetaObject resultMo;
773 const auto typeFlags = type.flags();
774 if (!result || ((typeFlags & QMetaType::IsPointer) && !*static_cast<void **>(result))) {
775 // Special case: we can always write a nullptr. Don't bother checking anything else.
776 return pd->writeProperty(targetObject(), &resultObject, flags);
777 } else if (typeFlags & QMetaType::PointerToQObject) {
778 resultObject = *static_cast<QObject **>(result);
779 if (!resultObject)
780 return pd->writeProperty(targetObject(), &resultObject, flags);
781 if (QQmlData *ddata = QQmlData::get(resultObject, false))
782 resultMo = ddata->propertyCache;
783 if (resultMo.isNull())
784 resultMo = resultObject->metaObject();
785 } else if (type == QMetaType::fromType<QVariant>()) {
786 const QVariant value = *static_cast<QVariant *>(result);
787 resultMo = QQmlPropertyPrivate::rawMetaObjectForType(value.metaType());
788 if (resultMo.isNull())
789 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
790 resultObject = *static_cast<QObject *const *>(value.constData());
791 } else {
792 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
793 }
794
795 return compareAndSet(resultMo, resultObject, pd, flags, [&]() {
796 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
797 });
798 }
799
800 Q_ALWAYS_INLINE bool write(const QV4::Value &result, bool isUndefined,
801 QQmlPropertyData::WriteFlags flags) override final
802 {
803 const QQmlPropertyData *pd;
804 QQmlPropertyData vtpd;
805 getPropertyData(&pd, &vtpd);
806 if (Q_UNLIKELY(isUndefined || vtpd.isValid()))
807 return slowWrite(*pd, vtpd, result, isUndefined, flags);
808
809 // Check if the result is a QObject:
810 QObject *resultObject = nullptr;
811 QQmlMetaObject resultMo;
812 if (result.isNull()) {
813 // Special case: we can always write a nullptr. Don't bother checking anything else.
814 return pd->writeProperty(targetObject(), &resultObject, flags);
815 } else if (auto wrapper = result.as<QV4::QObjectWrapper>()) {
816 resultObject = wrapper->object();
817 if (!resultObject)
818 return pd->writeProperty(targetObject(), &resultObject, flags);
819 if (QQmlData *ddata = QQmlData::get(resultObject, false))
820 resultMo = ddata->propertyCache;
821 if (resultMo.isNull()) {
822 resultMo = resultObject->metaObject();
823 }
824 } else if (auto variant = result.as<QV4::VariantObject>()) {
825 const QVariant value = variant->d()->data();
826 resultMo = QQmlPropertyPrivate::rawMetaObjectForType(value.metaType());
827 if (resultMo.isNull())
828 return slowWrite(*pd, vtpd, result, isUndefined, flags);
829 resultObject = *static_cast<QObject *const *>(value.constData());
830 } else {
831 return slowWrite(*pd, vtpd, result, isUndefined, flags);
832 }
833
834 return compareAndSet(resultMo, resultObject, pd, flags, [&]() {
835 return slowWrite(*pd, vtpd, result, isUndefined, flags);
836 });
837 }
838
839private:
840 using QQmlBinding::slowWrite;
841
842 template<typename SlowWrite>
843 bool compareAndSet(const QQmlMetaObject &resultMo, QObject *resultObject, const QQmlPropertyData *pd,
844 QQmlPropertyData::WriteFlags flags, const SlowWrite &slowWrite)
845 {
846 const QMetaObject *propertyMo = targetMetaObject();
847 if (!propertyMo)
848 return slowWrite();
849
850 if (QQmlMetaObject::canConvert(resultMo, propertyMo)) {
851 return pd->writeProperty(targetObject(), &resultObject, flags);
852 } else if (!resultObject && QQmlMetaObject::canConvert(propertyMo, resultMo)) {
853 // In the case of a null QObject, we assign the null if there is
854 // any change that the null variant type could be up or down cast to
855 // the property type.
856 return pd->writeProperty(targetObject(), &resultObject, flags);
857 } else {
858 return slowWrite();
859 }
860 }
861};
862
863QQmlBinding *QQmlBinding::newBinding(const QQmlPropertyData *property)
864{
865 return newBinding(property ? property->propType() : QMetaType());
866}
867
868QQmlBinding *QQmlBinding::newBinding(QMetaType propertyType)
869{
870 if (propertyType.flags() & QMetaType::PointerToQObject)
871 return new QObjectPointerBinding(propertyType);
872
873 switch (propertyType.id()) {
874 case QMetaType::Bool:
875 return new GenericBinding<QMetaType::Bool>;
876 case QMetaType::Int:
877 return new GenericBinding<QMetaType::Int>;
878 case QMetaType::Double:
879 return new GenericBinding<QMetaType::Double>;
880 case QMetaType::Float:
881 return new GenericBinding<QMetaType::Float>;
882 case QMetaType::QString:
883 return new GenericBinding<QMetaType::QString>;
884 default:
885 return new GenericBinding<QMetaType::UnknownType>;
886 }
887}
888
889QT_END_NAMESPACE
Q_ALWAYS_INLINE bool write(const QV4::Value &result, bool isUndefined, QQmlPropertyData::WriteFlags flags) override final
Q_ALWAYS_INLINE bool write(void *result, QMetaType type, bool isUndefined, QQmlPropertyData::WriteFlags flags) override final
Q_ALWAYS_INLINE bool doStore(T value, const QQmlPropertyData *pd, QQmlPropertyData::WriteFlags flags) const
QObjectPointerBinding(QMetaType propertyType)
Q_ALWAYS_INLINE bool write(const QV4::Value &result, bool isUndefined, QQmlPropertyData::WriteFlags flags) override final
Q_ALWAYS_INLINE bool write(void *result, QMetaType type, bool isUndefined, QQmlPropertyData::WriteFlags flags) override final
QString bindingValue() const override
QQmlSourceLocation sourceLocation() const override final
QQmlTranslationBindingFromBinding(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit, const QV4::CompiledData::Binding *binding)
QQmlSourceLocation sourceLocation() const override final
QQmlTranslationBindingFromTranslationInfo(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit, const QQmlTranslation &translationData, quint16 line, quint16 column)
virtual QString bindingValue() const override
QQmlTranslationBinding(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit)
void doUpdate(const DeleteWatcher &watcher, QQmlPropertyData::WriteFlags flags, QV4::Scope &scope) override final
static void onLanguageChange(QPropertyObserver *observer, QUntypedPropertyData *)
virtual QString bindingValue() const =0
bool hasDependencies() const override final
Combined button and popup list for selecting options.
Definition qjsvalue.h:24