36QQmlBinding *QQmlBinding::create(
const QQmlPropertyData *property,
const QQmlScriptString &script, QObject *obj, QQmlContext *ctxt)
38 QQmlBinding *b = newBinding(property);
40 if (ctxt && !ctxt->isValid())
43 const QQmlScriptStringPrivate *scriptPrivate = script.d.data();
44 if (!ctxt && (!scriptPrivate->context || !scriptPrivate->context->isValid()))
48 QV4::Function *runtimeFunction =
nullptr;
50 QQmlRefPointer<QQmlContextData> ctxtdata = QQmlContextData::get(scriptPrivate->context);
51 QQmlEnginePrivate *engine = QQmlEnginePrivate::get(scriptPrivate->context->engine());
52 if (engine && ctxtdata && !ctxtdata->urlString().isEmpty() && ctxtdata->typeCompilationUnit()) {
53 url = ctxtdata->urlString();
54 if (scriptPrivate->bindingId != QQmlBinding::Invalid)
55 runtimeFunction = ctxtdata->typeCompilationUnit()->runtimeFunctions.at(scriptPrivate->bindingId);
58 b->setNotifyOnValueChanged(
true);
59 b->QQmlJavaScriptExpression::setContext(QQmlContextData::get(ctxt ? ctxt : scriptPrivate->context));
60 b->setScopeObject(obj ? obj : scriptPrivate->scope);
62 QV4::ExecutionEngine *v4 = b->engine()->handle();
63 if (runtimeFunction) {
65 QV4::Scoped<QV4::QmlContext> qmlContext(scope, QV4::QmlContext::create(v4->rootContext(), ctxtdata, b->scopeObject()));
66 b->setupFunction(qmlContext, runtimeFunction);
68 QString code = scriptPrivate->script;
69 b->createQmlBinding(b->context(), b->scopeObject(), code, url, scriptPrivate->lineNumber);
90QQmlBinding *QQmlBinding::create(
91 const QQmlPropertyData *property,
const QString &str, QObject *obj,
92 const QQmlRefPointer<QQmlContextData> &ctxt,
const QString &url, quint16 lineNumber)
94 QQmlBinding *b = newBinding(property);
96 b->setNotifyOnValueChanged(
true);
97 b->QQmlJavaScriptExpression::setContext(ctxt);
98 b->setScopeObject(obj);
100 b->createQmlBinding(ctxt, obj, str, url, lineNumber);
112QQmlBinding *QQmlBinding::create(QMetaType propertyType, QV4::Function *function, QObject *obj,
113 const QQmlRefPointer<QQmlContextData> &ctxt,
114 QV4::ExecutionContext *scope)
116 QQmlBinding *b = newBinding(propertyType);
118 b->setNotifyOnValueChanged(
true);
119 b->QQmlJavaScriptExpression::setContext(ctxt);
120 b->setScopeObject(obj);
123 b->setupFunction(scope, function);
133void QQmlBinding::update(QQmlPropertyData::WriteFlags flags)
135 if (!enabledFlag() || !hasValidContext())
139 if (QQmlData::wasDeleted(targetObject()))
143 if (Q_UNLIKELY(updatingFlag())) {
144 const QQmlPropertyData *d =
nullptr;
145 QQmlPropertyData vtd;
146 getPropertyData(&d, &vtd);
148 QQmlProperty p = QQmlPropertyPrivate::restore(targetObject(), *d, &vtd,
nullptr);
149 printBindingLoopError(p);
152 setUpdatingFlag(
true);
154 DeleteWatcher watcher(
this);
156 QQmlEngine *qmlEngine = engine();
157 QV4::Scope scope(qmlEngine->handle());
159 if (canUseAccessor())
160 flags.setFlag(QQmlPropertyData::BypassInterceptor);
162 Q_TRACE_SCOPE(QQmlBinding, qmlEngine, function() ? function()->name()->toQString() : QString(),
163 sourceLocation().sourceFile, sourceLocation().line, sourceLocation().column);
164 QQmlBindingProfiler prof(QQmlEnginePrivate::get(qmlEngine)->profiler, function());
165 doUpdate(watcher, flags, scope);
167 if (!watcher.wasDeleted())
168 setUpdatingFlag(
false);
177QV4::ReturnedValue QQmlBinding::evaluate(
bool *isUndefined)
179 QV4::ExecutionEngine *v4 = engine()->handle();
181 const QV4::Value *argv =
nullptr;
182 const QV4::Value *thisObject =
nullptr;
183 QV4::BoundFunction *b =
nullptr;
184 if ((b =
static_cast<QV4::BoundFunction *>(m_boundFunction.valueRef()))) {
185 QV4::Heap::MemberData *args = b->boundArgs();
187 argc = args->values.size;
188 argv = args->values.data();
190 thisObject = &b->d()->boundThis;
192 QV4::Scope scope(v4);
193 QV4::JSCallData jsCall(thisObject, argv, argc);
195 return QQmlJavaScriptExpression::evaluate(jsCall.callData(scope), isUndefined);
380QQmlBinding *QQmlBinding::createTranslationBinding(
381 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
382 const QV4::CompiledData::Binding *binding, QObject *obj,
383 const QQmlRefPointer<QQmlContextData> &ctxt)
385 QQmlTranslationBinding *b =
new QQmlTranslationBindingFromBinding(unit, binding);
387 b->setNotifyOnValueChanged(
true);
388 b->QQmlJavaScriptExpression::setContext(ctxt);
389 b->setScopeObject(obj);
390#if QT_CONFIG(translation) && QT_CONFIG(qml_debug)
391 if (QQmlDebugTranslationService *service
392 = QQmlDebugConnector::service<QQmlDebugTranslationService>()) {
393 service->foundTranslationBinding(
394 TranslationBindingInformation::create(unit, binding, b->scopeObject(), ctxt));
400QQmlBinding *QQmlBinding::createTranslationBinding(
401 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
402 const QQmlRefPointer<QQmlContextData> &ctxt,
const QString &propertyName,
403 const QQmlTranslation &translationData,
const QQmlSourceLocation &location, QObject *obj)
405 QQmlTranslationBinding *b =
new QQmlTranslationBindingFromTranslationInfo(
406 unit, translationData, location.column, location.line);
408 b->setNotifyOnValueChanged(
true);
409 b->QQmlJavaScriptExpression::setContext(ctxt);
410 b->setScopeObject(obj);
412#if QT_CONFIG(translation) && QT_CONFIG(qml_debug)
413 QString originString;
414 if (QQmlDebugTranslationService *service =
415 QQmlDebugConnector::service<QQmlDebugTranslationService>()) {
416 service->foundTranslationBinding({ unit, b->scopeObject(), ctxt,
418 propertyName, translationData,
420 location.line, location.column });
423 Q_UNUSED(propertyName)
428bool QQmlBinding::slowWrite(
429 const QQmlPropertyData &core,
const QQmlPropertyData &valueTypeData,
const void *result,
430 QMetaType resultType,
bool isUndefined, QQmlPropertyData::WriteFlags flags)
439 QQmlEngine *qmlEngine = engine();
440 const QMetaType metaType = valueTypeData.isValid() ? valueTypeData.propType() : core.propType();
441 QQmlJavaScriptExpression::DeleteWatcher watcher(
this);
443 if (core.isVarProperty()) {
444 QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(m_target.data());
446 QV4::Scope scope(qmlEngine->handle());
447 QV4::ScopedValue value(scope, qmlEngine->handle()->metaTypeToJS(resultType, result));
448 vmemo->setVMEProperty(core.coreIndex(),
450 }
else if (isUndefined && core.isResettable()) {
451 void *args[] = {
nullptr };
452 QMetaObject::metacall(m_target.data(), QMetaObject::ResetProperty, core.coreIndex(), args);
453 }
else if (isUndefined && metaType == QMetaType::fromType<QVariant>()) {
454 QQmlPropertyPrivate::writeValueProperty(
455 m_target.data(), core, valueTypeData, QVariant(), context(), flags);
456 }
else if (metaType == QMetaType::fromType<QJSValue>()) {
457 QQmlPropertyPrivate::writeValueProperty(
458 m_target.data(), core, valueTypeData,
459 QVariant(resultType, result), context(), flags);
460 }
else if (isUndefined) {
461 const char *name = metaType.name();
462 const QString typeName = name
463 ? QString::fromUtf8(name)
464 : QStringLiteral(
"[unknown property type]");
465 delayedError()->setErrorDescription(
466 QStringLiteral(
"Unable to assign [undefined] to ") + typeName);
468 }
else if (!QQmlPropertyPrivate::writeValueProperty(
469 m_target.data(), core, valueTypeData, QVariant(resultType, result),
471 if (watcher.wasDeleted())
473 handleWriteError(result, resultType, metaType);
480Q_NEVER_INLINE
bool QQmlBinding::slowWrite(
const QQmlPropertyData &core,
481 const QQmlPropertyData &valueTypeData,
482 const QV4::Value &result,
483 bool isUndefined, QQmlPropertyData::WriteFlags flags)
485 const QMetaType metaType = valueTypeData.isValid() ? valueTypeData.propType() : core.propType();
486 const int type = metaType.id();
488 QQmlJavaScriptExpression::DeleteWatcher watcher(
this);
491 bool isVarProperty = core.isVarProperty();
494 }
else if (core.isQList()) {
495 if (core.propType().flags() & QMetaType::IsQmlList)
496 value = QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QObject*>>());
498 value = QV4::ExecutionEngine::toVariant(result, core.propType());
499 }
else if (result.isNull() && core.isQObject()) {
500 value = QVariant::fromValue((QObject *)
nullptr);
501 }
else if (core.propType() == QMetaType::fromType<QList<QUrl>>()) {
502 const QVariant resultVariant
503 = QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QUrl>>());
504 value = QVariant::fromValue(QQmlPropertyPrivate::resolveUrlsOnAssignment()
505 ? QQmlPropertyPrivate::urlSequence(resultVariant, context())
506 : QQmlPropertyPrivate::urlSequence(resultVariant));
507 }
else if (!isVarProperty && metaType != QMetaType::fromType<QJSValue>()) {
508 value = QV4::ExecutionEngine::toVariant(result, metaType);
513 }
else if (isVarProperty) {
514 const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
515 if (f && f->isBinding()) {
518 delayedError()->setErrorDescription(QLatin1String(
"Invalid use of Qt.binding() in a binding declaration."));
522 QQmlVMEMetaObject *vmemo = QQmlVMEMetaObject::get(m_target.data());
524 vmemo->setVMEProperty(core.coreIndex(), result);
525 }
else if (isUndefined
526 && (valueTypeData.isValid() ? valueTypeData.isResettable() : core.isResettable())) {
527 QQmlPropertyPrivate::resetValueProperty(
528 m_target.data(), core, valueTypeData, context(), flags);
529 }
else if (isUndefined && type == QMetaType::QVariant) {
530 QQmlPropertyPrivate::writeValueProperty(m_target.data(), core, valueTypeData, QVariant(), context(), flags);
531 }
else if (metaType == QMetaType::fromType<QJSValue>()) {
532 const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
533 if (f && f->isBinding()) {
534 delayedError()->setErrorDescription(QLatin1String(
"Invalid use of Qt.binding() in a binding declaration."));
537 QQmlPropertyPrivate::writeValueProperty(
538 m_target.data(), core, valueTypeData,
539 QVariant::fromValue(QJSValuePrivate::fromReturnedValue(result.asReturnedValue())),
541 }
else if (isUndefined) {
542 const char *name = QMetaType(type).name();
543 const QLatin1String typeName(name ? name :
"[unknown property type]");
544 delayedError()->setErrorDescription(QLatin1String(
"Unable to assign [undefined] to ")
547 }
else if (
const QV4::FunctionObject *f = result.as<QV4::FunctionObject>();
548 f && !f->as<QV4::QQmlTypeWrapper>()) {
550 delayedError()->setErrorDescription(QLatin1String(
"Invalid use of Qt.binding() in a binding declaration."));
552 delayedError()->setErrorDescription(QLatin1String(
"Unable to assign a function to a property of any type other than var."));
554 }
else if (!QQmlPropertyPrivate::writeValueProperty(m_target.data(), core, valueTypeData, value, context(), flags)) {
556 if (watcher.wasDeleted())
558 handleWriteError(value.constData(), value.metaType(), metaType);
599QVariant QQmlBinding::evaluate()
601 QQmlEngine *qmlEngine = engine();
602 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(qmlEngine);
603 ep->referenceScarceResources();
605 bool isUndefined =
false;
607 QV4::Scope scope(qmlEngine->handle());
608 QV4::ScopedValue result(scope, QQmlJavaScriptExpression::evaluate(&isUndefined));
610 ep->dereferenceScarceResources();
612 return QV4::ExecutionEngine::toVariant(result, QMetaType::fromType<QList<QObject*> >());
641QVector<QQmlProperty> QQmlBinding::dependencies()
const
643 QVector<QQmlProperty> dependencies;
644 if (!m_target.data())
647 for (QQmlJavaScriptExpressionGuard *guard = activeGuards.first(); guard; guard = activeGuards.next(guard)) {
648 if (guard->signalIndex() == -1)
651 QObject *senderObject = guard->senderAsObject();
655 const QMetaObject *senderMeta = senderObject->metaObject();
659 for (
int i = 0; i < senderMeta->propertyCount(); i++) {
660 QMetaProperty property = senderMeta->property(i);
661 if (property.notifySignalIndex() == QMetaObjectPrivate::signal(senderMeta, guard->signalIndex()).methodIndex()) {
662 dependencies.push_back(QQmlProperty(senderObject, QString::fromUtf8(property.name())));
667 for (
auto trigger = qpropertyChangeTriggers; trigger; trigger = trigger->next) {
668 QMetaProperty prop = trigger->property();
670 dependencies.push_back(QQmlProperty(trigger->target, QString::fromUtf8(prop.name())));
681void QQmlBinding::doUpdate(
const DeleteWatcher &watcher, QQmlPropertyData::WriteFlags flags, QV4::Scope &scope)
683 auto ep = QQmlEnginePrivate::get(scope.engine);
684 ep->referenceScarceResources();
687 auto canWrite = [&]() {
return !watcher.wasDeleted() && isAddedToObject() && !hasError(); };
688 const QV4::Function *v4Function = function();
689 if (v4Function && v4Function->kind == QV4::Function::AotCompiled && !hasBoundFunction()) {
690 const auto returnType = v4Function->aotCompiledFunction.types[0];
691 if (returnType == QMetaType::fromType<QVariant>()) {
693 const bool isUndefined = !evaluate(&result, returnType);
695 error = !write(result.data(), result.metaType(), isUndefined, flags);
697 const auto size = returnType.sizeOf();
698 if (Q_LIKELY(size > 0)) {
699 Q_ALLOCA_VAR(
void, result, size);
700 if (returnType.flags() & QMetaType::NeedsConstruction)
701 returnType.construct(result);
702 const bool isUndefined = !evaluate(result, returnType);
704 error = !write(result, returnType, isUndefined, flags);
705 if (returnType.flags() & QMetaType::NeedsDestruction)
706 returnType.destruct(result);
707 }
else if (canWrite()) {
708 error = !write(QV4::Value::undefined(),
true, flags);
712 bool isUndefined =
false;
713 QV4::ScopedValue result(scope, evaluate(&isUndefined));
715 error = !write(result, isUndefined, flags);
718 if (!watcher.wasDeleted()) {
721 delayedError()->setErrorLocation(sourceLocation());
722 delayedError()->setErrorObject(m_target.data());
726 if (!delayedError()->addError(ep)) ep->warning(
this->error(engine()));
732 ep->dereferenceScarceResources();
737 QBiPointer<
const QtPrivate::QMetaTypeInterface,
const QMetaObject> targetMeta;
739 const QMetaObject *targetMetaObject() {
744 if (targetMeta.isT2())
745 return targetMeta.asT2();
747 const QMetaObject *metaObject = QQmlPropertyPrivate::rawMetaObjectForType(
748 QMetaType(targetMeta.asT1())).metaObject();
751 targetMeta = metaObject;
761 QQmlPropertyData::WriteFlags flags)
override final
763 const QQmlPropertyData *pd;
764 QQmlPropertyData vtpd;
765 getPropertyData(&pd, &vtpd);
766 if (Q_UNLIKELY(isUndefined || vtpd.isValid()))
767 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
770 QObject *resultObject =
nullptr;
771 QQmlMetaObject resultMo;
772 const auto typeFlags = type.flags();
773 if (!result || ((typeFlags & QMetaType::IsPointer) && !*
static_cast<
void **>(result))) {
775 return pd->writeProperty(targetObject(), &resultObject, flags);
776 }
else if (typeFlags & QMetaType::PointerToQObject) {
777 resultObject = *
static_cast<QObject **>(result);
779 return pd->writeProperty(targetObject(), &resultObject, flags);
780 if (QQmlData *ddata = QQmlData::get(resultObject,
false))
781 resultMo = ddata->propertyCache;
782 if (resultMo.isNull())
783 resultMo = resultObject->metaObject();
784 }
else if (type == QMetaType::fromType<QVariant>()) {
785 const QVariant value = *
static_cast<QVariant *>(result);
786 resultMo = QQmlPropertyPrivate::rawMetaObjectForType(value.metaType());
787 if (resultMo.isNull())
788 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
789 resultObject = *
static_cast<QObject *
const *>(value.constData());
791 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
794 return compareAndSet(resultMo, resultObject, pd, flags, [&]() {
795 return slowWrite(*pd, vtpd, result, type, isUndefined, flags);
800 QQmlPropertyData::WriteFlags flags)
override final
802 const QQmlPropertyData *pd;
803 QQmlPropertyData vtpd;
804 getPropertyData(&pd, &vtpd);
805 if (Q_UNLIKELY(isUndefined || vtpd.isValid()))
806 return slowWrite(*pd, vtpd, result, isUndefined, flags);
809 QObject *resultObject =
nullptr;
810 QQmlMetaObject resultMo;
811 if (result.isNull()) {
813 return pd->writeProperty(targetObject(), &resultObject, flags);
814 }
else if (
auto wrapper = result.as<QV4::QObjectWrapper>()) {
815 resultObject = wrapper->object();
817 return pd->writeProperty(targetObject(), &resultObject, flags);
818 if (QQmlData *ddata = QQmlData::get(resultObject,
false))
819 resultMo = ddata->propertyCache;
820 if (resultMo.isNull()) {
821 resultMo = resultObject->metaObject();
823 }
else if (
auto variant = result.as<QV4::VariantObject>()) {
824 const QVariant value = variant->d()->data();
825 resultMo = QQmlPropertyPrivate::rawMetaObjectForType(value.metaType());
826 if (resultMo.isNull())
827 return slowWrite(*pd, vtpd, result, isUndefined, flags);
828 resultObject = *
static_cast<QObject *
const *>(value.constData());
830 return slowWrite(*pd, vtpd, result, isUndefined, flags);
833 return compareAndSet(resultMo, resultObject, pd, flags, [&]() {
834 return slowWrite(*pd, vtpd, result, isUndefined, flags);
841 template<
typename SlowWrite>
842 bool compareAndSet(
const QQmlMetaObject &resultMo, QObject *resultObject,
const QQmlPropertyData *pd,
843 QQmlPropertyData::WriteFlags flags,
const SlowWrite &slowWrite)
845 const QMetaObject *propertyMo = targetMetaObject();
849 if (QQmlMetaObject::canConvert(resultMo, propertyMo)) {
850 return pd->writeProperty(targetObject(), &resultObject, flags);
851 }
else if (!resultObject && QQmlMetaObject::canConvert(propertyMo, resultMo)) {
855 return pd->writeProperty(targetObject(), &resultObject, flags);