7#include <private/qjsvalue_p.h>
8#include <private/qqmlanybinding_p.h>
9#include <private/qqmlbinding_p.h>
10#include <private/qqmlboundsignal_p.h>
11#include <private/qqmlcomponent_p.h>
12#include <private/qqmlcomponentattached_p.h>
13#include <private/qqmlcustomparser_p.h>
14#include <private/qqmldebugconnector_p.h>
15#include <private/qqmldebugserviceinterfaces_p.h>
16#include <private/qqmlengine_p.h>
17#include <private/qqmlpropertybinding_p.h>
18#include <private/qqmlpropertyvalueinterceptor_p.h>
19#include <private/qqmlscriptdata_p.h>
20#include <private/qqmlscriptstring_p.h>
21#include <private/qqmlsourcecoordinate_p.h>
22#include <private/qqmlstringconverters_p.h>
23#include <private/qqmlvaluetypeproxybinding_p.h>
24#include <private/qqmlvme_p.h>
25#include <private/qqmlvmemetaobject_p.h>
26#include <private/qv4function_p.h>
27#include <private/qv4functionobject_p.h>
28#include <private/qv4generatorobject_p.h>
29#include <private/qv4qobjectwrapper_p.h>
30#include <private/qv4referenceobject_p.h>
31#include <private/qv4resolvedtypereference_p.h>
33#include <qtqml_tracepoints_p.h>
35#include <QtCore/qscopedvaluerollback.h>
36#include <QtCore/qloggingcategory.h>
44"struct ExecutionEngine;"\
45"class ExecutableCompilationUnit;"\
46"namespace CompiledData {"\
47"struct Object;"\
48"}}"\
49"class QQmlEngine;"
52Q_TRACE_POINT(qtqml, QQmlObjectCreator_createInstance_entry,
const QV4::ExecutableCompilationUnit *compilationUnit,
const QV4::CompiledData::Object *object,
const QUrl &url)
53Q_TRACE_POINT(qtqml, QQmlObjectCreator_createInstance_exit,
const QString &typeName)
55QQmlObjectCreator::QQmlObjectCreator(
56 const QQmlRefPointer<QQmlContextData> &parentContext,
57 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
58 const QQmlRefPointer<QQmlContextData> &creationContext,
59 const QString &inlineComponentName,
60 QQmlIncubatorPrivate *incubator)
62 , m_inlineComponentName(inlineComponentName)
63 , compilationUnit(compilationUnit)
64 , propertyCaches(compilationUnit->propertyCachesPtr())
65 , sharedState(
new QQmlObjectCreatorSharedState, QQmlRefPointer<QQmlObjectCreatorSharedState>::Adopt)
66 , topLevelCreator(
true)
67 , isContextObject(
true)
68 , incubator(incubator)
72 sharedState->componentAttached =
nullptr;
73 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList();
74 sharedState->creationContext = creationContext;
75 sharedState->rootContext.reset();
76 sharedState->hadTopLevelRequiredProperties =
false;
78 if (
auto profiler = QQmlEnginePrivate::get(engine)->profiler) {
79 Q_QML_PROFILE_IF_ENABLED(QQmlProfilerDefinitions::ProfileCreating, profiler,
80 sharedState->profiler.init(profiler));
86QQmlObjectCreator::QQmlObjectCreator(
const QQmlRefPointer<QQmlContextData> &parentContext,
87 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit,
const QString inlineComponentName,
88 QQmlObjectCreatorSharedState *inheritedSharedState,
bool isContextObject)
90 , m_inlineComponentName(inlineComponentName)
91 , compilationUnit(compilationUnit)
92 , propertyCaches(compilationUnit->propertyCachesPtr())
93 , sharedState(inheritedSharedState)
94 , topLevelCreator(
false)
95 , isContextObject(isContextObject)
101void QQmlObjectCreator::init(
const QQmlRefPointer<QQmlContextData> &providedParentContext)
103 parentContext = providedParentContext;
104 engine = parentContext->engine();
105 v4 = engine->handle();
107 Q_ASSERT(compilationUnit);
108 Q_ASSERT(compilationUnit->engine == v4);
109 if (!compilationUnit->runtimeStrings)
110 compilationUnit->populate();
112 qmlUnit = compilationUnit->unitData();
114 _scopeObject =
nullptr;
115 _bindingTarget =
nullptr;
116 _valueTypeProperty =
nullptr;
117 _compiledObject =
nullptr;
118 _compiledObjectIndex = -1;
120 _vmeMetaObject =
nullptr;
121 _qmlContext =
nullptr;
124QQmlObjectCreator::~QQmlObjectCreator()
126 if (topLevelCreator) {
130 QQmlObjectCreatorRecursionWatcher watcher(
this);
132 while (sharedState->componentAttached) {
133 QQmlComponentAttached *a = sharedState->componentAttached;
139QObject *QQmlObjectCreator::create(
int subComponentIndex, QObject *parent, QQmlInstantiationInterrupt *interrupt,
int flags)
141 if (phase == CreatingObjectsPhase2) {
142 phase = ObjectsCreated;
143 return context->contextObject();
145 Q_ASSERT(phase == Startup);
146 phase = CreatingObjects;
149 bool isComponentRoot =
false;
151 if (subComponentIndex == -1) {
153 isComponentRoot =
true;
155 Q_ASSERT(subComponentIndex >= 0);
156 if (flags & CreationFlags::InlineComponent) {
157 if (compilationUnit->componentsAreBound()
158 && compilationUnit != parentContext->typeCompilationUnit()) {
159 recordError({}, tr(
"Cannot instantiate bound inline component in different file"));
160 phase = ObjectsCreated;
163 objectToCreate = subComponentIndex;
164 isComponentRoot =
true;
166 Q_ASSERT(flags & CreationFlags::NormalObject);
167 if (compilationUnit->componentsAreBound()
168 && sharedState->creationContext != parentContext) {
169 recordError({}, tr(
"Cannot instantiate bound component "
170 "outside its creation context"));
171 phase = ObjectsCreated;
174 if (subComponentIndex < compilationUnit->objectCount()) {
175 const QV4::CompiledData::Object *compObj
176 = compilationUnit->objectAt(subComponentIndex);
177 objectToCreate = compObj->bindingTable()->value.objectIndex;
181 objectToCreate = compilationUnit->resolvedIndex(subComponentIndex);
186 QQmlEnginePrivate *enginePriv = QQmlEnginePrivate::get(engine);
187 context = isComponentRoot
188 ? enginePriv->createComponentRootContext(compilationUnit, parentContext,
190 : enginePriv->createBareContext(compilationUnit, parentContext, subComponentIndex);
192 if (!sharedState->rootContext) {
193 sharedState->rootContext = context;
194 sharedState->rootContext->setIncubator(incubator);
195 sharedState->rootContext->setRootObjectInCreation(
true);
198 QV4::Scope scope(v4);
200 Q_ASSERT(sharedState->allJavaScriptObjects.canTrack() || topLevelCreator);
202 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(scope);
204 if (!isComponentRoot && sharedState->creationContext) {
206 QV4::ScopedValue scripts(scope, sharedState->creationContext->importedScripts());
207 context->setImportedScripts(v4, scripts);
210 QObject *instance = createInstance(objectToCreate, parent,
true);
212 QQmlData *ddata = QQmlData::get(instance);
214 ddata->compilationUnit = compilationUnit;
218 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList();
220 phase = CreatingObjectsPhase2;
222 if (interrupt && interrupt->shouldInterrupt())
225 phase = ObjectsCreated;
228 if (QQmlEngineDebugService *service
229 = QQmlDebugConnector::service<QQmlEngineDebugService>()) {
230 if (!parentContext->isInternal())
231 parentContext->asQQmlContextPrivate()->appendInstance(instance);
232 service->objectCreated(engine, instance);
233 }
else if (!parentContext->isInternal() && QQmlDebugConnector::service<QV4DebugService>()) {
234 parentContext->asQQmlContextPrivate()->appendInstance(instance);
241void QQmlObjectCreator::beginPopulateDeferred(
const QQmlRefPointer<QQmlContextData> &newContext)
243 context = newContext;
244 sharedState->rootContext = newContext;
246 Q_ASSERT(topLevelCreator);
247 Q_ASSERT(!sharedState->allJavaScriptObjects.canTrack());
250 QV4::Scope valueScope(v4);
251 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(valueScope);
254void QQmlObjectCreator::populateDeferred(QObject *instance,
int deferredIndex,
255 const QQmlPropertyPrivate *qmlProperty,
256 const QV4::CompiledData::Binding *binding)
258 doPopulateDeferred(instance, deferredIndex, [
this, qmlProperty, binding]() {
259 Q_ASSERT(qmlProperty);
261 QQmlListProperty<QObject> savedList;
262 qSwap(_currentList, savedList);
264 const QQmlPropertyData &property = qmlProperty->core;
266 if (property.propType().flags().testFlag(QMetaType::IsQmlList)) {
267 void *argv[1] = { (
void*)&_currentList };
268 QMetaObject::metacall(_qobject, QMetaObject::ReadProperty, property.coreIndex(), argv);
269 }
else if (_currentList.object) {
270 _currentList = QQmlListProperty<QObject>();
273 setPropertyBinding(&property, binding);
275 qSwap(_currentList, savedList);
279void QQmlObjectCreator::populateDeferred(QObject *instance,
int deferredIndex)
281 doPopulateDeferred(instance, deferredIndex, [
this]() { setupBindings(ApplyDeferred); });
284bool QQmlObjectCreator::populateDeferredProperties(QObject *instance,
285 const QQmlData::DeferredData *deferredData)
287 beginPopulateDeferred(deferredData->context);
288 populateDeferred(instance, deferredData->deferredIdx);
289 finalizePopulateDeferred();
290 return errors.isEmpty();
293void QQmlObjectCreator::populateDeferredBinding(
const QQmlProperty &qmlProperty,
int deferredIndex,
294 const QV4::CompiledData::Binding *binding)
297 populateDeferred(qmlProperty.object(), deferredIndex, QQmlPropertyPrivate::get(qmlProperty),
300 populateDeferred(qmlProperty.object(), deferredIndex);
304void QQmlObjectCreator::populateDeferredInstance(
305 QObject *outerObject,
int deferredIndex,
int index, QObject *instance,
306 QObject *bindingTarget,
const QQmlPropertyData *valueTypeProperty,
307 const QV4::CompiledData::Binding *binding)
309 doPopulateDeferred(outerObject, deferredIndex, [&]() {
310 populateInstance(index, instance, bindingTarget, valueTypeProperty, binding);
314void QQmlObjectCreator::finalizePopulateDeferred()
316 phase = ObjectsCreated;
319void QQmlObjectCreator::setPropertyValue(
const QQmlPropertyData *property,
const QV4::CompiledData::Binding *binding,
320 std::optional<
int> foldedNumber)
322 QQmlPropertyData::WriteFlags propertyWriteFlags = QQmlPropertyData::BypassInterceptor | QQmlPropertyData::RemoveBindingOnAliasWrite;
323 QV4::Scope scope(v4);
325 QMetaType propertyType = property->propType();
327 const QV4::CompiledData::Binding::Type effectiveType
328 = foldedNumber ? QV4::CompiledData::Binding::Type_Number : binding->type();
329 const auto effectiveNumber = [&]() ->
double {
330 return foldedNumber ?
double(*foldedNumber) : compilationUnit->bindingValueAsNumber(binding);
333 if (property->isEnum()) {
334 if (foldedNumber.has_value() ||
337 (property->isAlias() && binding->isNumberBinding())) {
338 propertyType = property->propType().underlyingType();
341 QVariant value = compilationUnit->bindingValueAsString(binding);
342 bool ok = QQmlPropertyPrivate::write(_qobject, *property, value, context);
349 auto assertOrNull = [&](
bool ok)
351 Q_ASSERT(ok || effectiveType == QV4::CompiledData::Binding::Type_Null);
355 auto assertType = [&](QV4::CompiledData::Binding::Type type)
357 Q_ASSERT(effectiveType == type || effectiveType == QV4::CompiledData::Binding::Type_Null);
361 if (property->isQObject()) {
362 if (effectiveType == QV4::CompiledData::Binding::Type_Null) {
363 QObject *value =
nullptr;
364 const bool ok = property->writeProperty(_qobject, &value, propertyWriteFlags);
371 switch (propertyType.id()) {
372 case QMetaType::QVariant: {
373 if (effectiveType == QV4::CompiledData::Binding::Type_Number) {
374 double n = effectiveNumber();
375 if (
double(
int(n)) == n) {
376 if (property->isVarProperty()) {
377 _vmeMetaObject->setVMEProperty(property->coreIndex(), QV4::Value::fromInt32(
int(n)));
381 property->writeProperty(_qobject, &value, propertyWriteFlags);
384 if (property->isVarProperty()) {
385 _vmeMetaObject->setVMEProperty(property->coreIndex(), QV4::Value::fromDouble(n));
388 property->writeProperty(_qobject, &value, propertyWriteFlags);
391 }
else if (effectiveType == QV4::CompiledData::Binding::Type_Boolean) {
392 if (property->isVarProperty()) {
393 _vmeMetaObject->setVMEProperty(property->coreIndex(), QV4::Value::fromBoolean(binding->valueAsBoolean()));
395 QVariant value(binding->valueAsBoolean());
396 property->writeProperty(_qobject, &value, propertyWriteFlags);
398 }
else if (effectiveType == QV4::CompiledData::Binding::Type_Null) {
399 if (property->isVarProperty()) {
400 _vmeMetaObject->setVMEProperty(property->coreIndex(), QV4::Value::nullValue());
402 QVariant nullValue = QVariant::fromValue(
nullptr);
403 property->writeProperty(_qobject, &nullValue, propertyWriteFlags);
406 QString stringValue = compilationUnit->bindingValueAsString(binding);
407 if (property->isVarProperty()) {
408 QV4::ScopedString s(scope, v4->newString(stringValue));
409 _vmeMetaObject->setVMEProperty(property->coreIndex(), s);
411 QVariant value = stringValue;
412 property->writeProperty(_qobject, &value, propertyWriteFlags);
417 case QMetaType::QString: {
418 assertOrNull(binding->evaluatesToString());
419 QString value = compilationUnit->bindingValueAsString(binding);
420 property->writeProperty(_qobject, &value, propertyWriteFlags);
423 case QMetaType::QStringList: {
424 assertOrNull(binding->evaluatesToString());
425 QStringList value(compilationUnit->bindingValueAsString(binding));
426 property->writeProperty(_qobject, &value, propertyWriteFlags);
429 case QMetaType::QByteArray: {
430 assertType(QV4::CompiledData::Binding::Type_String);
431 QByteArray value(compilationUnit->bindingValueAsString(binding).toUtf8());
432 property->writeProperty(_qobject, &value, propertyWriteFlags);
435 case QMetaType::QUrl: {
436 assertType(QV4::CompiledData::Binding::Type_String);
437 const QString string = compilationUnit->bindingValueAsString(binding);
438 QUrl value = (!string.isEmpty() && QQmlPropertyPrivate::resolveUrlsOnAssignment())
439 ? compilationUnit->finalUrl().resolved(QUrl(string))
441 property->writeProperty(_qobject, &value, propertyWriteFlags);
444 case QMetaType::UInt: {
445 assertType(QV4::CompiledData::Binding::Type_Number);
446 double d = effectiveNumber();
447 uint value = uint(d);
448 property->writeProperty(_qobject, &value, propertyWriteFlags);
452 case QMetaType::Int: {
453 assertType(QV4::CompiledData::Binding::Type_Number);
454 double d = effectiveNumber();
456 property->writeProperty(_qobject, &value, propertyWriteFlags);
460 case QMetaType::SChar: {
461 assertType(QV4::CompiledData::Binding::Type_Number);
462 double d = effectiveNumber();
463 qint8 value = qint8(d);
464 property->writeProperty(_qobject, &value, propertyWriteFlags);
467 case QMetaType::UChar: {
468 assertType(QV4::CompiledData::Binding::Type_Number);
469 double d = effectiveNumber();
470 quint8 value = quint8(d);
471 property->writeProperty(_qobject, &value, propertyWriteFlags);
474 case QMetaType::Short: {
475 assertType(QV4::CompiledData::Binding::Type_Number);
476 double d = effectiveNumber();
477 qint16 value = qint16(d);
478 property->writeProperty(_qobject, &value, propertyWriteFlags);
481 case QMetaType::UShort: {
482 assertType(QV4::CompiledData::Binding::Type_Number);
483 double d = effectiveNumber();
484 quint16 value = quint16(d);
485 property->writeProperty(_qobject, &value, propertyWriteFlags);
488 case QMetaType::LongLong: {
489 assertType(QV4::CompiledData::Binding::Type_Number);
490 double d = effectiveNumber();
491 qint64 value = qint64(d);
492 property->writeProperty(_qobject, &value, propertyWriteFlags);
495 case QMetaType::ULongLong: {
496 assertType(QV4::CompiledData::Binding::Type_Number);
497 double d = effectiveNumber();
498 quint64 value = quint64(d);
499 property->writeProperty(_qobject, &value, propertyWriteFlags);
503 case QMetaType::Float: {
504 assertType(QV4::CompiledData::Binding::Type_Number);
505 float value =
float(effectiveNumber());
506 property->writeProperty(_qobject, &value, propertyWriteFlags);
509 case QMetaType::Double: {
510 assertType(QV4::CompiledData::Binding::Type_Number);
511 double value = effectiveNumber();
512 property->writeProperty(_qobject, &value, propertyWriteFlags);
515 case QMetaType::QColor: {
517 QVariant data = QQmlStringConverters::colorFromString(
518 compilationUnit->bindingValueAsString(binding), &ok);
520 property->writeProperty(_qobject, data.data(), propertyWriteFlags);
523#if QT_CONFIG(datestring)
524 case QMetaType::QDate: {
526 QDate value = QQmlStringConverters::dateFromString(compilationUnit->bindingValueAsString(binding), &ok);
528 property->writeProperty(_qobject, &value, propertyWriteFlags);
531 case QMetaType::QTime: {
533 QTime value = QQmlStringConverters::timeFromString(compilationUnit->bindingValueAsString(binding), &ok);
535 property->writeProperty(_qobject, &value, propertyWriteFlags);
538 case QMetaType::QDateTime: {
540 QDateTime value = QQmlStringConverters::dateTimeFromString(
541 compilationUnit->bindingValueAsString(binding), &ok);
543 property->writeProperty(_qobject, &value, propertyWriteFlags);
547 case QMetaType::QPoint: {
549 QPoint value = QQmlStringConverters::pointFFromString(compilationUnit->bindingValueAsString(binding), &ok).toPoint();
551 property->writeProperty(_qobject, &value, propertyWriteFlags);
554 case QMetaType::QPointF: {
556 QPointF value = QQmlStringConverters::pointFFromString(compilationUnit->bindingValueAsString(binding), &ok);
558 property->writeProperty(_qobject, &value, propertyWriteFlags);
561 case QMetaType::QSize: {
563 QSize value = QQmlStringConverters::sizeFFromString(compilationUnit->bindingValueAsString(binding), &ok).toSize();
565 property->writeProperty(_qobject, &value, propertyWriteFlags);
568 case QMetaType::QSizeF: {
570 QSizeF value = QQmlStringConverters::sizeFFromString(compilationUnit->bindingValueAsString(binding), &ok);
572 property->writeProperty(_qobject, &value, propertyWriteFlags);
575 case QMetaType::QRect: {
577 QRect value = QQmlStringConverters::rectFFromString(compilationUnit->bindingValueAsString(binding), &ok).toRect();
579 property->writeProperty(_qobject, &value, propertyWriteFlags);
582 case QMetaType::QRectF: {
584 QRectF value = QQmlStringConverters::rectFFromString(compilationUnit->bindingValueAsString(binding), &ok);
586 property->writeProperty(_qobject, &value, propertyWriteFlags);
589 case QMetaType::Bool: {
590 assertType(QV4::CompiledData::Binding::Type_Boolean);
591 bool value = binding->valueAsBoolean();
592 property->writeProperty(_qobject, &value, propertyWriteFlags);
595 case QMetaType::QVector2D:
596 case QMetaType::QVector3D:
597 case QMetaType::QVector4D:
598 case QMetaType::QQuaternion: {
599 QVariant result = QQmlValueTypeProvider::createValueType(
600 compilationUnit->bindingValueAsString(binding), propertyType);
601 assertOrNull(result.isValid());
602 property->writeProperty(_qobject, result.data(), propertyWriteFlags);
607 if (propertyType == QMetaType::fromType<QList<
double>>()) {
608 assertType(QV4::CompiledData::Binding::Type_Number);
610 value.append(compilationUnit->bindingValueAsNumber(binding));
611 property->writeProperty(_qobject, &value, propertyWriteFlags);
613 }
else if (propertyType == QMetaType::fromType<QList<
float>>()) {
614 assertType(QV4::CompiledData::Binding::Type_Number);
616 value.append(
float(compilationUnit->bindingValueAsNumber(binding)));
617 property->writeProperty(_qobject, &value, propertyWriteFlags);
619 }
else if (propertyType == QMetaType::fromType<QList<
int>>()) {
620 assertType(QV4::CompiledData::Binding::Type_Number);
621 double n = compilationUnit->bindingValueAsNumber(binding);
623 value.append(
int(n));
624 property->writeProperty(_qobject, &value, propertyWriteFlags);
626 }
else if (propertyType == QMetaType::fromType<QList<
bool>>()) {
627 assertType(QV4::CompiledData::Binding::Type_Boolean);
629 value.append(binding->valueAsBoolean());
630 property->writeProperty(_qobject, &value, propertyWriteFlags);
632 }
else if (propertyType == QMetaType::fromType<QList<QUrl>>()) {
633 assertType(QV4::CompiledData::Binding::Type_String);
634 const QUrl url(compilationUnit->bindingValueAsString(binding));
636 QQmlPropertyPrivate::resolveUrlsOnAssignment()
637 ? compilationUnit->finalUrl().resolved(url)
640 property->writeProperty(_qobject, &value, propertyWriteFlags);
642 }
else if (propertyType == QMetaType::fromType<QList<QString>>()) {
643 assertOrNull(binding->evaluatesToString());
644 QList<QString> value;
645 value.append(compilationUnit->bindingValueAsString(binding));
646 property->writeProperty(_qobject, &value, propertyWriteFlags);
648 }
else if (propertyType == QMetaType::fromType<QJSValue>()) {
650 switch (binding->type()) {
651 case QV4::CompiledData::Binding::Type_Boolean:
652 value = QJSValue(binding->valueAsBoolean());
654 case QV4::CompiledData::Binding::Type_Number: {
655 const double n = compilationUnit->bindingValueAsNumber(binding);
656 if (
double(
int(n)) == n)
657 value = QJSValue(
int(n));
662 case QV4::CompiledData::Binding::Type_Null:
663 value = QJSValue::NullValue;
666 value = QJSValue(compilationUnit->bindingValueAsString(binding));
669 property->writeProperty(_qobject, &value, propertyWriteFlags);
673 switch (binding->type()) {
674 case QV4::CompiledData::Binding::Type_Boolean:
675 source = binding->valueAsBoolean();
677 case QV4::CompiledData::Binding::Type_Number: {
678 const double n = compilationUnit->bindingValueAsNumber(binding);
679 if (
double(
int(n)) == n)
685 case QV4::CompiledData::Binding::Type_Null:
686 source = QVariant::fromValue<std::nullptr_t>(
nullptr);
688 case QV4::CompiledData::Binding::Type_Invalid:
691 source = compilationUnit->bindingValueAsString(binding);
695 QVariant target = QQmlValueTypeProvider::createValueType(
696 source, propertyType, engine->handle());
697 if (target.isValid()) {
698 property->writeProperty(_qobject, target.data(), propertyWriteFlags);
704 QString stringValue = compilationUnit->bindingValueAsString(binding);
705 QMetaProperty metaProperty = _qobject->metaObject()->property(property->coreIndex());
706 recordError(binding->location, tr(
"Cannot assign value %1 to property"
707" %2").arg(stringValue, QString::fromUtf8(metaProperty.name())));
716 const QMetaObject *mo = object->metaObject();
717 while (mo && !type.isValid()) {
718 type = QQmlMetaType::qmlType(mo);
719 mo = mo->superClass();
724void QQmlObjectCreator::setupBindings(BindingSetupFlags mode)
726 QQmlListProperty<QObject> savedList;
727 qSwap(_currentList, savedList);
729 const QV4::CompiledData::BindingPropertyData *propertyData
730 = compilationUnit->bindingPropertyDataPerObjectAt(_compiledObjectIndex);
732 if (_compiledObject->idNameIndex) {
733 const QQmlPropertyData *idProperty = propertyData->last();
734 Q_ASSERT(!idProperty || !idProperty->isValid() || idProperty->name(_qobject) == QLatin1String(
"id"));
735 if (idProperty && idProperty->isValid() && idProperty->isWritable() && idProperty->propType().id() == QMetaType::QString) {
736 QV4::CompiledData::Binding idBinding;
737 idBinding.propertyNameIndex = 0;
738 idBinding.clearFlags();
739 idBinding.setType(QV4::CompiledData::Binding::Type_String);
740 idBinding.stringIndex = _compiledObject->idNameIndex;
741 idBinding.location = _compiledObject->location;
742 idBinding.value.nullMarker = 0;
743 setPropertyValue(idProperty, &idBinding);
748 if (_valueTypeProperty) {
749 QQmlAbstractBinding *binding = QQmlPropertyPrivate::binding(_bindingTarget, QQmlPropertyIndex(_valueTypeProperty->coreIndex()));
751 if (binding && binding->kind() != QQmlAbstractBinding::ValueTypeProxy) {
752 QQmlPropertyPrivate::removeBinding(
753 _bindingTarget, QQmlPropertyIndex(_valueTypeProperty->coreIndex()),
754 QQmlPropertyPrivate::OverrideSticky);
755 }
else if (binding) {
756 QQmlValueTypeProxyBinding *proxy =
static_cast<QQmlValueTypeProxyBinding *>(binding);
758 if (qmlTypeForObject(_bindingTarget).isValid()) {
759 quint32 bindingSkipList = 0;
761 const QQmlPropertyData *defaultProperty = _compiledObject->indexOfDefaultPropertyOrAlias != -1 ? _propertyCache->parent()->defaultProperty() : _propertyCache->defaultProperty();
763 const QV4::CompiledData::Binding *binding = _compiledObject->bindingTable();
764 for (quint32 i = 0; i < _compiledObject->nBindings; ++i, ++binding) {
765 const QQmlPropertyData *property = binding->propertyNameIndex != 0
766 ? _propertyCache->property(stringAt(binding->propertyNameIndex),
770 bindingSkipList |= (1 << property->coreIndex());
773 proxy->removeBindings(bindingSkipList);
778 const QQmlPropertyData *defaultProperty = _compiledObject->indexOfDefaultPropertyOrAlias != -1
779 ? _propertyCache->parent()->defaultProperty()
780 : _propertyCache->defaultProperty();
782 int currentListPropertyIndex = -1;
785 const auto applyBinding = [&](
const QQmlPropertyData *property,
786 const QV4::CompiledData::Binding *binding) ->
bool {
787 if (property && property->propType().flags().testFlag(QMetaType::IsQmlList)) {
788 if (property->coreIndex() != currentListPropertyIndex) {
789 void *argv[1] = { (
void*)&_currentList };
790 QMetaObject::metacall(_qobject, QMetaObject::ReadProperty, property->coreIndex(), argv);
791 currentListPropertyIndex = property->coreIndex();
794 const QMetaObject *
const metaobject = _qobject->metaObject();
795 const int qmlListBehavorClassInfoIndex = metaobject->indexOfClassInfo(
"QML.ListPropertyAssignBehavior");
796 if (qmlListBehavorClassInfoIndex != -1) {
797 const char *overrideBehavior =
798 metaobject->classInfo(qmlListBehavorClassInfoIndex).value();
799 if (!strcmp(overrideBehavior,
801 if (_currentList.clear) {
802 _currentList.clear(&_currentList);
805 bool isDefaultProperty =
806 (property->name(_qobject)
807 == QString::fromUtf8(
809 ->classInfo(metaobject->indexOfClassInfo(
812 if (!isDefaultProperty
813 && (!strcmp(overrideBehavior,
814 "ReplaceIfNotDefault"))) {
815 if (_currentList.clear) {
816 _currentList.clear(&_currentList);
822 }
else if (_currentList.object) {
823 _currentList = QQmlListProperty<QObject>();
824 currentListPropertyIndex = -1;
827 return setPropertyBinding(property, binding);
833 QVarLengthArray<
const QV4::CompiledData::Binding *, 8> explicitDefaultPropertyBindings;
835 const auto applyExplicitDefaultPropertyBindings = [&]() ->
bool {
836 for (
const QV4::CompiledData::Binding *b : std::as_const(explicitDefaultPropertyBindings)) {
837 if (!applyBinding(defaultProperty, b))
840 explicitDefaultPropertyBindings.clear();
844 const QV4::CompiledData::RuntimeBindingIndex *bindingOrder =
845 compilationUnit->bindingApplicationOrder(_compiledObjectIndex);
846 for (quint32 k = 0; k < _compiledObject->nBindings; ++k) {
847 const quint32 i = bindingOrder[k];
848 const QV4::CompiledData::Binding *binding = _compiledObject->bindingTable() + i;
849 const QQmlPropertyData *
const property = propertyData->at(i);
851 const QQmlPropertyData *targetProperty = property;
852 if (targetProperty->isAlias()) {
854 QQmlPropertyIndex originalIndex(targetProperty->coreIndex(),
855 _valueTypeProperty ? _valueTypeProperty->coreIndex()
857 auto [targetObject, targetIndex] =
858 QQmlPropertyPrivate::findAliasTarget(_bindingTarget, originalIndex);
859 QQmlData *data = QQmlData::get(targetObject);
860 Q_ASSERT(data && data->propertyCache);
861 targetProperty = data->propertyCache->property(targetIndex.coreIndex());
862 sharedState->requiredProperties.remove({ targetObject, targetProperty });
864 sharedState->requiredProperties.remove({ _bindingTarget, property });
867 if (compilationUnit->bindingIsCustomParser(_compiledObjectIndex, i))
870 if (compilationUnit->bindingIsDeferred(_compiledObjectIndex, i)) {
871 if (!(mode & ApplyDeferred))
873 }
else if (!(mode & ApplyImmediate)) {
879 if (binding->propertyNameIndex != quint32(0)
881 && property == defaultProperty) {
882 explicitDefaultPropertyBindings.append(binding);
888 if (binding->propertyNameIndex == quint32(0)
889 && !explicitDefaultPropertyBindings.isEmpty()
890 && !(binding->location < explicitDefaultPropertyBindings.first()->location)) {
891 if (!applyExplicitDefaultPropertyBindings())
895 if (!applyBinding(property, binding))
900 if (!applyExplicitDefaultPropertyBindings())
902 qSwap(_currentList, savedList);
905bool QQmlObjectCreator::setPropertyBinding(
const QQmlPropertyData *bindingProperty,
const QV4::CompiledData::Binding *binding)
907 const int bindingIndex =
int(binding - _compiledObject->bindingTable());
908 const std::optional<
int> foldedNumber
909 = compilationUnit->bindingFoldedEnum(_compiledObjectIndex, bindingIndex);
910 const QV4::CompiledData::Binding::Type bindingType = binding->type();
911 if (bindingType == QV4::CompiledData::Binding::Type_AttachedProperty) {
912 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(binding->value.objectIndex);
913 QQmlObjectCreationProfiler profiler(sharedState->profiler.profiler, obj);
915 Q_ASSERT(stringAt(obj->inheritedTypeNameIndex).isEmpty());
916 QV4::ResolvedTypeReference *tr = resolvedType(binding->propertyNameIndex);
918 QQmlType attachedType = tr->type();
919 QQmlTypeLoader *typeLoader = QQmlTypeLoader::get(engine);
920 if (!attachedType.isValid()) {
921 QQmlTypeNameCache::Result res
922 = context->imports()->query(stringAt(binding->propertyNameIndex), typeLoader);
924 attachedType = res.type;
930 sharedState->profiler,
931 profiler.update(compilationUnit.data(), obj, attachedType.qmlTypeName(),
934 QObject *qmlObject = qmlAttachedPropertiesObject(
935 _qobject, attachedType.attachedPropertiesFunction(typeLoader));
937 recordError(binding->location,
938 QStringLiteral(
"Could not create attached properties object '%1'")
939 .arg(QString::fromUtf8(attachedType.typeName())));
943 const size_t objectIndex = sharedState->allCreatedObjects.size();
944 sharedState->allCreatedObjects.push_back(qmlObject);
945 const QQmlType attachedObjectType
946 = QQmlMetaType::qmlType(attachedType.attachedPropertiesType(typeLoader));
947 const int parserStatusCast = attachedObjectType.parserStatusCast();
948 QQmlParserStatus *parserStatus =
nullptr;
949 if (parserStatusCast != -1)
950 parserStatus =
reinterpret_cast<QQmlParserStatus*>(
reinterpret_cast<
char *>(qmlObject) + parserStatusCast);
952 parserStatus->classBegin();
954 sharedState->allParserStatusCallbacks.push_back({objectIndex, parserStatusCast});
957 if (!populateInstance(binding->value.objectIndex, qmlObject, qmlObject,
964 if (bindingProperty && bindingProperty->propType() == QMetaType::fromType<QQmlScriptString>()) {
965 QQmlScriptString ss(compilationUnit->bindingValueAsScriptString(binding),
966 context->asQQmlContext(), _scopeObject);
967 ss.d.data()->bindingId = bindingType == QV4::CompiledData::Binding::Type_Script ? binding->value.compiledScriptIndex : (quint32)QQmlBinding::Invalid;
968 ss.d.data()->lineNumber = binding->location.line();
969 ss.d.data()->columnNumber = binding->location.column();
970 ss.d.data()->isStringLiteral = bindingType == QV4::CompiledData::Binding::Type_String;
971 ss.d.data()->isNumberLiteral = bindingType == QV4::CompiledData::Binding::Type_Number;
972 ss.d.data()->numberValue = compilationUnit->bindingValueAsNumber(binding);
974 QQmlPropertyData::WriteFlags propertyWriteFlags = QQmlPropertyData::BypassInterceptor |
975 QQmlPropertyData::RemoveBindingOnAliasWrite;
976 int propertyWriteStatus = -1;
977 void *argv[] = { &ss,
nullptr, &propertyWriteStatus, &propertyWriteFlags };
978 QMetaObject::metacall(_qobject, QMetaObject::WriteProperty, bindingProperty->coreIndex(), argv);
982 QObject *createdSubObject =
nullptr;
983 if (bindingType == QV4::CompiledData::Binding::Type_Object) {
986 QScopedValueRollback topLevelRequired(sharedState->hadTopLevelRequiredProperties);
987 int objectIndex = binding->value.objectIndex;
988 if (
const int wrapperIdx = compilationUnit->implicitComponentForObject(objectIndex);
991 objectIndex = wrapperIdx;
993 createdSubObject = createInstance(objectIndex, _bindingTarget);
994 if (!createdSubObject)
998 if (bindingType == QV4::CompiledData::Binding::Type_GroupedProperty) {
999 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(binding->value.objectIndex);
1000 if (stringAt(obj->inheritedTypeNameIndex).isEmpty()) {
1002 QObject *groupObject =
nullptr;
1003 QQmlGadgetPtrWrapper *valueType =
nullptr;
1004 const QQmlPropertyData *valueTypeProperty =
nullptr;
1005 QObject *bindingTarget = _bindingTarget;
1006 int groupObjectIndex = binding->value.objectIndex;
1008 if (!bindingProperty) {
1009 for (
int i = 0, end = compilationUnit->objectCount(); i != end; ++i) {
1010 const QV4::CompiledData::Object *external = compilationUnit->objectAt(i);
1011 if (external->idNameIndex == binding->propertyNameIndex) {
1012 bindingTarget = groupObject = context->idValue(compilationUnit->objectId(i));
1018 }
else if (QQmlMetaType::isValueType(bindingProperty->propType())) {
1019 valueType = QQmlGadgetPtrWrapper::instance(engine, bindingProperty->propType());
1021 recordError(binding->location, tr(
"Cannot set properties on %1 as it is null").arg(stringAt(binding->propertyNameIndex)));
1025 valueType->read(_qobject, bindingProperty->coreIndex());
1027 groupObject = valueType;
1028 valueTypeProperty = bindingProperty;
1029 }
else if (bindingProperty->propType().flags() & QMetaType::PointerToQObject) {
1030 void *argv[1] = { &groupObject };
1031 QMetaObject::metacall(_qobject, QMetaObject::ReadProperty, bindingProperty->coreIndex(), argv);
1033 QQmlPropertyIndex index(bindingProperty->coreIndex());
1034 auto anyBinding = QQmlAnyBinding::ofProperty(_qobject, index);
1038 anyBinding.refresh();
1039 QMetaObject::metacall(_qobject, QMetaObject::ReadProperty, bindingProperty->coreIndex(), argv);
1042 recordError(binding->location, tr(
"Cannot set properties on %1 as it is null").arg(stringAt(binding->propertyNameIndex)));
1047 bindingTarget = groupObject;
1051 tr(
"Using grouped property syntax on %1 which has no properties")
1052 .arg(stringAt(binding->propertyNameIndex)));
1056 if (!populateInstance(groupObjectIndex, groupObject, bindingTarget, valueTypeProperty,
1063 _qobject, bindingProperty->coreIndex(),
1064 QQmlPropertyData::BypassInterceptor,
1065 QV4::ReferenceObject::AllProperties);
1072 if (!bindingProperty)
1075 const QV4::CompiledData::Binding::Flags bindingFlags = binding->flags();
1076 const bool allowedToRemoveBinding
1077 = !compilationUnit->bindingIsSignalHandlerExpression(_compiledObjectIndex, bindingIndex)
1078 && !(bindingFlags & QV4::CompiledData::Binding::IsOnAssignment)
1079 && !compilationUnit->bindingIsPropertyObserver(_compiledObjectIndex, bindingIndex)
1080 && !_valueTypeProperty;
1082 if (allowedToRemoveBinding) {
1083 if (bindingProperty->acceptsQBinding()) {
1084 removePendingBinding(_bindingTarget, bindingProperty->coreIndex());
1086 QQmlPropertyPrivate::removeBinding(
1087 _bindingTarget, QQmlPropertyIndex(bindingProperty->coreIndex()),
1088 QQmlPropertyPrivate::OverrideSticky);
1093 && (bindingType == QV4::CompiledData::Binding::Type_Script || binding->isTranslationBinding())) {
1094 if (compilationUnit->bindingIsSignalHandlerExpression(_compiledObjectIndex, bindingIndex)
1095 || compilationUnit->bindingIsPropertyObserver(_compiledObjectIndex, bindingIndex)) {
1096 QV4::Function *runtimeFunction = compilationUnit->runtimeFunctions[binding->value.compiledScriptIndex];
1097 int signalIndex = _propertyCache->methodIndexToSignalIndex(bindingProperty->coreIndex());
1098 QQmlBoundSignalExpression *expr =
new QQmlBoundSignalExpression(
1099 _bindingTarget, signalIndex, context,
1100 _scopeObject, runtimeFunction, currentQmlContext());
1102 if (bindingProperty->notifiesViaBindable()) {
1103 auto target = _bindingTarget;
1104 if (bindingProperty->isAlias()) {
1109 QQmlPropertyIndex originalIndex(bindingProperty->coreIndex(), _valueTypeProperty ? _valueTypeProperty->coreIndex() : -1);
1110 auto [aliasTargetObject, aliasTargetIndex] = QQmlPropertyPrivate::findAliasTarget(target, originalIndex);
1111 target = aliasTargetObject;
1112 QQmlData *data = QQmlData::get(target);
1113 Q_ASSERT(data && data->propertyCache);
1114 bindingProperty = data->propertyCache->property(aliasTargetIndex.coreIndex());
1116 auto &observer = QQmlData::get(_scopeObject)->propertyObservers.emplace_back(expr);
1117 QUntypedBindable bindable;
1118 void *argv[] = { &bindable };
1119 target->qt_metacall(QMetaObject::BindableProperty, bindingProperty->coreIndex(), argv);
1120 Q_ASSERT(bindable.isValid());
1121 bindable.observe(&observer);
1123 QQmlBoundSignal *bs =
new QQmlBoundSignal(_bindingTarget, signalIndex, _scopeObject, engine);
1124 bs->takeExpression(expr);
1126 }
else if (bindingProperty->acceptsQBinding()) {
1127 QUntypedPropertyBinding qmlBinding;
1128 if (binding->isTranslationBinding()) {
1129 qmlBinding = QQmlTranslationPropertyBinding::create(bindingProperty, compilationUnit, binding);
1131 QV4::Function *runtimeFunction = compilationUnit->runtimeFunctions[binding->value.compiledScriptIndex];
1132 QQmlPropertyIndex index(bindingProperty->coreIndex(), -1);
1133 qmlBinding = QQmlPropertyBinding::create(bindingProperty, runtimeFunction, _scopeObject, context, currentQmlContext(), _bindingTarget, index);
1135 sharedState.data()->allQPropertyBindings.push_back(DeferredQPropertyBinding {_bindingTarget, bindingProperty->coreIndex(), qmlBinding });
1137 QQmlData *data = QQmlData::get(_bindingTarget,
true);
1138 data->setBindingBit(_bindingTarget, bindingProperty->coreIndex());
1145 QQmlBinding::Ptr qmlBinding;
1146 const QQmlPropertyData *targetProperty = bindingProperty;
1147 const QQmlPropertyData *subprop =
nullptr;
1148 if (_valueTypeProperty) {
1149 targetProperty = _valueTypeProperty;
1150 subprop = bindingProperty;
1152 if (binding->isTranslationBinding()) {
1153 qmlBinding = QQmlBinding::createTranslationBinding(
1154 compilationUnit, binding, _scopeObject, context);
1156 QV4::Function *runtimeFunction = compilationUnit->runtimeFunctions[binding->value.compiledScriptIndex];
1157 qmlBinding = QQmlBinding::create(targetProperty, runtimeFunction, _scopeObject,
1158 context, currentQmlContext());
1161 auto bindingTarget = _bindingTarget;
1162 auto valueTypeProperty = _valueTypeProperty;
1163 auto assignBinding = [qmlBinding, bindingTarget, targetProperty, subprop, bindingProperty, valueTypeProperty](QQmlObjectCreatorSharedState *sharedState)
mutable ->
bool {
1164 if (!qmlBinding->setTarget(bindingTarget, *targetProperty, subprop) && targetProperty->isAlias())
1167 sharedState->allCreatedBindings.push_back(qmlBinding);
1169 if (bindingProperty->isAlias()) {
1170 QQmlPropertyPrivate::setBinding(qmlBinding.data(), QQmlPropertyPrivate::DontEnable);
1172 qmlBinding->addToObject();
1174 if (!valueTypeProperty) {
1175 QQmlData *targetDeclarativeData = QQmlData::get(bindingTarget);
1176 Q_ASSERT(targetDeclarativeData);
1177 targetDeclarativeData->setPendingBindingBit(bindingTarget, bindingProperty->coreIndex());
1183 if (!assignBinding(sharedState.data()))
1184 pendingAliasBindings.push_back(assignBinding);
1189 if (bindingType == QV4::CompiledData::Binding::Type_Object) {
1190 if (bindingFlags & QV4::CompiledData::Binding::IsOnAssignment) {
1192 QQmlType type = qmlTypeForObject(createdSubObject);
1193 Q_ASSERT(type.isValid());
1195 int valueSourceCast = type.propertyValueSourceCast();
1196 if (valueSourceCast != -1) {
1197 QQmlPropertyValueSource *vs =
reinterpret_cast<QQmlPropertyValueSource *>(
reinterpret_cast<
char *>(createdSubObject) + valueSourceCast);
1198 QObject *target = createdSubObject->parent();
1200 if (_valueTypeProperty) {
1201 prop = QQmlPropertyPrivate::restore(target, *_valueTypeProperty,
1202 bindingProperty, context);
1204 prop = QQmlPropertyPrivate::restore(target, *bindingProperty,
nullptr, context);
1206 vs->setTarget(prop);
1209 int valueInterceptorCast = type.propertyValueInterceptorCast();
1210 if (valueInterceptorCast != -1) {
1211 QQmlPropertyValueInterceptor *vi =
reinterpret_cast<QQmlPropertyValueInterceptor *>(
reinterpret_cast<
char *>(createdSubObject) + valueInterceptorCast);
1212 QObject *target = createdSubObject->parent();
1214 QQmlPropertyIndex propertyIndex;
1215 if (bindingProperty->isAlias()) {
1216 QQmlPropertyIndex originalIndex(bindingProperty->coreIndex(), _valueTypeProperty ? _valueTypeProperty->coreIndex() : -1);
1217 auto aliasTarget = QQmlPropertyPrivate::findAliasTarget(target, originalIndex);
1218 target = aliasTarget.targetObject;
1219 QQmlData *data = QQmlData::get(target);
1220 if (!data || !data->propertyCache) {
1221 qWarning() <<
"can't resolve property alias for 'on' assignment";
1226 QQmlPropertyData targetPropertyData = *data->propertyCache->property(aliasTarget.targetIndex.coreIndex());
1227 auto prop = QQmlPropertyPrivate::restore(
1228 target, targetPropertyData,
nullptr, context);
1229 vi->setTarget(prop);
1230 propertyIndex = QQmlPropertyPrivate::propertyIndex(prop);
1233 if (_valueTypeProperty) {
1234 prop = QQmlPropertyPrivate::restore(
1235 target, *_valueTypeProperty, bindingProperty, context);
1237 prop = QQmlPropertyPrivate::restore(
1238 target, *bindingProperty,
nullptr, context);
1240 vi->setTarget(prop);
1241 propertyIndex = QQmlPropertyPrivate::propertyIndex(prop);
1244 QQmlInterceptorMetaObject *mo = QQmlInterceptorMetaObject::get(target);
1246 mo =
new QQmlInterceptorMetaObject(target, QQmlData::get(target)->propertyCache);
1247 mo->registerInterceptor(propertyIndex, vi);
1254 if (compilationUnit->bindingIsSignalHandlerObject(_compiledObjectIndex, bindingIndex)) {
1255 if (!bindingProperty->isFunction()) {
1256 recordError(binding->valueLocation, tr(
"Cannot assign an object to signal property %1").arg(bindingProperty->name(_qobject)));
1259 QMetaMethod method = QQmlMetaType::defaultMethod(createdSubObject);
1260 if (!method.isValid()) {
1261 recordError(binding->valueLocation, tr(
"Cannot assign object type %1 with no default method").arg(QString::fromLatin1(createdSubObject->metaObject()->className())));
1264 qCWarning(lcQmlDefaultMethod) <<
"Assigning an object to a signal handler is deprecated. "
1265 "Instead, create the object, give it an id, and call the desired slot "
1266 "from the signal handler. The object is:" << createdSubObject;
1268 QMetaMethod signalMethod = _qobject->metaObject()->method(bindingProperty->coreIndex());
1269 if (!QMetaObject::checkConnectArgs(signalMethod, method)) {
1270 recordError(binding->valueLocation,
1271 tr(
"Cannot connect mismatched signal/slot %1 vs %2")
1272 .arg(QString::fromUtf8(method.methodSignature()),
1273 QString::fromUtf8(signalMethod.methodSignature())));
1277 QQmlPropertyPrivate::connect(_qobject, bindingProperty->coreIndex(), createdSubObject, method.methodIndex());
1281 QQmlPropertyData::WriteFlags propertyWriteFlags = QQmlPropertyData::BypassInterceptor |
1282 QQmlPropertyData::RemoveBindingOnAliasWrite;
1283 int propertyWriteStatus = -1;
1284 void *argv[] = {
nullptr,
nullptr, &propertyWriteStatus, &propertyWriteFlags };
1286 if (
const char *iid = QQmlMetaType::interfaceIId(bindingProperty->propType())) {
1287 void *ptr = createdSubObject->qt_metacast(iid);
1290 QMetaObject::metacall(_qobject, QMetaObject::WriteProperty, bindingProperty->coreIndex(), argv);
1292 recordError(binding->location, tr(
"Cannot assign object to interface property"));
1295 }
else if (bindingProperty->propType() == QMetaType::fromType<QVariant>()) {
1296 if (bindingProperty->isVarProperty()) {
1297 QV4::Scope scope(v4);
1298 QV4::ScopedValue wrappedObject(scope, QV4::QObjectWrapper::wrap(engine->handle(), createdSubObject));
1299 _vmeMetaObject->setVMEProperty(bindingProperty->coreIndex(), wrappedObject);
1301 QVariant value = QVariant::fromValue(createdSubObject);
1303 QMetaObject::metacall(_qobject, QMetaObject::WriteProperty, bindingProperty->coreIndex(), argv);
1305 }
else if (bindingProperty->propType() == QMetaType::fromType<QJSValue>()) {
1306 QV4::Scope scope(v4);
1307 QV4::ScopedValue wrappedObject(scope, QV4::QObjectWrapper::wrap(engine->handle(), createdSubObject));
1308 if (bindingProperty->isVarProperty()) {
1309 _vmeMetaObject->setVMEProperty(bindingProperty->coreIndex(), wrappedObject);
1312 QJSValuePrivate::setValue(&value, wrappedObject);
1314 QMetaObject::metacall(_qobject, QMetaObject::WriteProperty, bindingProperty->coreIndex(), argv);
1316 }
else if (bindingProperty->propType().flags().testFlag(QMetaType::IsQmlList)) {
1317 Q_ASSERT(_currentList.object);
1319 QObject *itemToAdd = createdSubObject;
1321 QMetaType listItemType = QQmlMetaType::listValueType(bindingProperty->propType());
1322 if (listItemType.isValid()) {
1323 const char *iid = QQmlMetaType::interfaceIId(listItemType);
1325 itemToAdd =
static_cast<QObject *>(createdSubObject->qt_metacast(iid));
1328 if (_currentList.append)
1329 _currentList.append(&_currentList, itemToAdd);
1331 recordError(binding->location, tr(
"Cannot assign object to read only list"));
1334 }
else if (bindingProperty->propType().flags().testFlag(QMetaType::PointerToQObject)) {
1336 argv[0] = &createdSubObject;
1337 QMetaObject::metacall(_qobject, QMetaObject::WriteProperty, bindingProperty->coreIndex(), argv);
1339 QVariant target = QQmlValueTypeProvider::createValueType(
1340 QVariant::fromValue(createdSubObject), bindingProperty->propType(), v4);
1341 if (target.isValid())
1342 bindingProperty->writeProperty(_qobject, target.data(), propertyWriteFlags);
1344 recordError(binding->location, tr(
"Cannot construct value type from given object"));
1349 if (bindingProperty->isQList()) {
1350 recordError(binding->location, tr(
"Cannot assign primitives to lists"));
1354 setPropertyValue(bindingProperty, binding, foldedNumber);
1358void QQmlObjectCreator::setupFunctions()
1360 QV4::Scope scope(v4);
1361 QV4::ScopedValue function(scope);
1362 QV4::ScopedContext qmlContext(scope, currentQmlContext());
1364 const quint32_le *functionIdx = _compiledObject->functionOffsetTable();
1365 for (quint32 i = 0; i < _compiledObject->nFunctions; ++i, ++functionIdx) {
1366 QV4::Function *runtimeFunction = compilationUnit->runtimeFunctions[*functionIdx];
1367 const QString name = runtimeFunction->name()->toQString();
1369 const QQmlPropertyData *property = _propertyCache->property(name, _qobject, context);
1370 if (!property->isVMEFunction())
1373 if (runtimeFunction->isGenerator())
1374 function = QV4::GeneratorFunction::create(qmlContext, runtimeFunction);
1376 function = QV4::FunctionObject::createScriptFunction(qmlContext, runtimeFunction);
1377 _vmeMetaObject->setVmeMethod(property->coreIndex(), function);
1381void QQmlObjectCreator::recordError(
const QV4::CompiledData::Location &location,
const QString &description)
1384 error.setUrl(compilationUnit->url());
1385 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(location.line()));
1386 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(location.column()));
1387 error.setDescription(description);
1391void QQmlObjectCreator::registerObjectWithContextById(
int objectIndex, QObject *instance)
const
1393 if (
const int id = compilationUnit->objectId(objectIndex); id >= 0)
1394 context->setIdValue(id, instance);
1397QObject *QQmlObjectCreator::createInstance(
int index, QObject *parent,
bool isContextObject)
1399 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(index);
1400 Q_TRACE(QQmlObjectCreator_createInstance_entry, compilationUnit.data(), obj, context->url());
1401 QQmlObjectCreationProfiler profiler(sharedState->profiler.profiler, obj);
1403 const bool isImplicitComponent = index >= compilationUnit->objectCount();
1404 const InitFlags flags = (isContextObject ? InitFlag::IsContextObject : InitFlag::None)
1405 | (index == 0 ? InitFlag::IsDocumentRoot : InitFlag::None)
1406 | (isImplicitComponent ? InitFlag::IsImplicitComponent : InitFlag::None);
1408 if (compilationUnit->objectIsComponent(index) || isImplicitComponent) {
1409 Q_TRACE_EXIT(QQmlObjectCreator_createInstance_exit, QStringLiteral(
"<component>"));
1411 sharedState->profiler,
1413 compilationUnit.data(), obj, QStringLiteral(
"<component>"),
1415 return initializeComponent(
1417 createComponent(engine, compilationUnit.data(), index, parent, context), flags);
1420 const QV4::ResolvedTypeReference *typeRef = resolvedType(obj->inheritedTypeNameIndex);
1422 const QQmlType type = typeRef->type();
1423 Q_ASSERT(type.isValid());
1425 Q_TRACE_EXIT(QQmlObjectCreator_createInstance_exit, type.qmlTypeName());
1427 sharedState->profiler,
1428 profiler.update(compilationUnit.data(), obj, type.qmlTypeName(), context->url()));
1430 if (type.isCompositeSingleton()) {
1433 tr(
"Composite Singleton Type %1 is not creatable")
1434 .arg(stringAt(obj->inheritedTypeNameIndex)));
1438 if (!type.isComposite()) {
1439 if (QObject *instance = type.createWithQQmlData())
1440 return initializeNonComposite(index, obj, typeRef, instance, parent, flags);
1443 tr(
"Unable to create object of type %1").arg(
1444 stringAt(obj->inheritedTypeNameIndex)));
1448 QQmlRefPointer<QV4::ExecutableCompilationUnit> executableCu = typeRef->isSelfReference()
1450 : engine->handle()->executableCompilationUnit(typeRef->compilationUnit());
1451 Q_ASSERT(executableCu);
1453 const bool isInlineComponent = type.isInlineComponent();
1454 const QString inlineComponentName = isInlineComponent ? type.elementName() : QString();
1455 QQmlObjectCreator subCreator(
1456 context, executableCu, inlineComponentName, sharedState.data(),
1459 if (QObject *instance = isInlineComponent
1460 ? subCreator.create(
1461 executableCu->inlineComponentId(inlineComponentName),
nullptr,
nullptr,
1462 CreationFlags::InlineComponent)
1463 : subCreator.create()) {
1464 return initializeComposite(index, obj, typeRef, instance, parent, flags);
1467 errors += subCreator.errors;
1471void QQmlObjectCreator::initializeDData(
1472 const QV4::CompiledData::Object *obj, QObject *instance, QQmlData *ddata, InitFlags flags)
1474 ddata->lineNumber = obj->location.line();
1475 ddata->columnNumber = obj->location.column();
1476 ddata->setImplicitDestructible();
1480 const bool documentRoot = (flags & InitFlag::IsDocumentRoot)
1481 || ddata->rootObjectInCreation
1482 || obj->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot);
1484 context->installContext(
1485 ddata, documentRoot ? QQmlContextData::DocumentRoot : QQmlContextData::OrdinaryObject);
1489 if ((flags & (InitFlag::IsContextObject | InitFlag::IsImplicitComponent))
1490 == InitFlag::IsContextObject) {
1491 context->setContextObject(instance);
1495void QQmlObjectCreator::initializePropertyCache(
1496 int index, QQmlData *ddata,
const QV4::ResolvedTypeReference *typeRef)
1498 if (!typeRef->isFullyDynamicType()) {
1499 QQmlPropertyCache::ConstPtr cache = propertyCaches->at(index);
1500 Q_ASSERT(!cache.isNull());
1501 ddata->propertyCache = std::move(cache);
1505void QQmlObjectCreator::initializeParent(QObject *instance, QObject *parent)
1507 if (instance->isWidgetType()) {
1508 if (parent && parent->isWidgetType()) {
1509 QAbstractDeclarativeData::setWidgetParent(instance, parent);
1514 }
else if (parent) {
1515 QQml_setParent_noEvent(instance, parent);
1519QObject *QQmlObjectCreator::populateInstanceAndAliasBindings(
1520 int index, QObject *instance, InitFlags flags)
1522 const auto doPopulate = [&]() {
1523 if (!populateInstance(
1524 index, instance, instance,
nullptr)) {
1526 pendingAliasBindings.clear();
1530 if (!flags.testFlag(InitFlag::IsContextObject))
1533 while (!pendingAliasBindings.empty()) {
1534 for (std::vector<PendingAliasBinding>::iterator it = pendingAliasBindings.begin();
1535 it != pendingAliasBindings.end(); ) {
1536 if ((*it)(sharedState.data()))
1537 it = pendingAliasBindings.erase(it);
1546 QObject *scopeObject = instance;
1547 qSwap(_scopeObject, scopeObject);
1549 Q_ASSERT(sharedState->allJavaScriptObjects.canTrack());
1550 sharedState->allJavaScriptObjects.trackObject(v4, instance);
1552 QV4::Scope valueScope(v4);
1553 QV4::QmlContext *qmlContext =
static_cast<QV4::QmlContext *>(valueScope.constructUndefined(1));
1555 qSwap(_qmlContext, qmlContext);
1557 const bool ok = doPopulate();
1559 qSwap(_qmlContext, qmlContext);
1560 qSwap(_scopeObject, scopeObject);
1562 return ok ? instance :
nullptr;
1565QObject *QQmlObjectCreator::initializeComponent(
1566 int index,
const QV4::CompiledData::Object *obj, QObject *instance, InitFlags flags)
1571 QScopedValueRollback<QQmlObjectCreator*> ocRestore(
1572 QQmlEnginePrivate::get(engine)->activeObjectCreator,
this);
1575 initializeDData(obj, instance, QQmlData::get(instance), flags);
1577 if (!flags.testFlag(InitFlag::IsImplicitComponent))
1578 registerObjectWithContextById(index, instance);
1582QObject *QQmlObjectCreator::initializeComposite(
1583 int index,
const QV4::CompiledData::Object *obj,
const QV4::ResolvedTypeReference *typeRef,
1584 QObject *instance, QObject *parent, InitFlags flags)
1589 QScopedValueRollback<QQmlObjectCreator*> ocRestore(
1590 QQmlEnginePrivate::get(engine)->activeObjectCreator,
this);
1592 initializeParent(instance, parent);
1593 QQmlData *ddata = QQmlData::get(instance,
true);
1594 initializeDData(obj, instance, ddata, flags);
1596 initializePropertyCache(index, ddata, typeRef);
1598 return populateInstanceAndAliasBindings(index, instance, flags);
1601QObject *QQmlObjectCreator::initializeNonComposite(
1602 int index,
const QV4::CompiledData::Object *obj,
const QV4::ResolvedTypeReference *typeRef,
1603 QObject *instance, QObject *parent, InitFlags flags)
1607 Q_ASSERT(!compilationUnit->objectIsComponent(index));
1610 const QQmlType type = typeRef->type();
1611 Q_ASSERT(type.isValid());
1612 Q_ASSERT(!type.isComposite());
1614 QScopedValueRollback<QQmlObjectCreator*> ocRestore(QQmlEnginePrivate::get(engine)->activeObjectCreator,
this);
1616 if (
const int finalizerCast = type.finalizerCast(); finalizerCast != -1) {
1617 const auto hook =
reinterpret_cast<QQmlFinalizerHook *>(
1618 reinterpret_cast<
char *>(instance) + finalizerCast);
1619 sharedState->finalizeHooks.push_back(hook);
1622 QQmlData *ddata = QQmlData::get(instance,
true);
1623 if (sharedState->rootContext && sharedState->rootContext->isRootObjectInCreation()) {
1624 ddata->rootObjectInCreation =
true;
1625 sharedState->rootContext->setRootObjectInCreation(
false);
1628 const size_t instanceIndex = sharedState->allCreatedObjects.size();
1629 sharedState->allCreatedObjects.push_back(instance);
1631 initializeParent(instance, parent);
1632 initializeDData(obj, instance, ddata, flags);
1634 if (
const int parserStatusCast = type.parserStatusCast(); parserStatusCast != -1) {
1635 if (QQmlParserStatus *parserStatus =
reinterpret_cast<QQmlParserStatus*>(
1636 reinterpret_cast<
char *>(instance) + parserStatusCast)) {
1637 parserStatus->classBegin();
1640 Q_QML_OC_PROFILE(sharedState->profiler, sharedState->profiler.push(obj));
1641 sharedState->allParserStatusCallbacks.push_back({ instanceIndex, parserStatusCast });
1645 if (QQmlCustomParser *customParser = type.customParser();
1646 customParser && compilationUnit->objectHasCustomParserBindings(index)) {
1647 customParser->engine = QQmlEnginePrivate::get(engine);
1648 customParser->imports = compilationUnit->typeNameCache().data();
1650 QList<
const QV4::CompiledData::Binding *> bindings;
1651 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(index);
1652 const QV4::CompiledData::RuntimeBindingIndex *bindingOrder =
1653 compilationUnit->bindingApplicationOrder(index);
1654 for (quint32 k = 0; k < obj->nBindings; ++k) {
1655 const quint32 i = bindingOrder[k];
1656 if (compilationUnit->bindingIsCustomParser(index, i))
1657 bindings << obj->bindingTable() + i;
1659 customParser->applyBindings(instance, compilationUnit, bindings);
1661 customParser->engine =
nullptr;
1662 customParser->imports = (QQmlTypeNameCache*)
nullptr;
1665 initializePropertyCache(index, ddata, typeRef);
1666 return populateInstanceAndAliasBindings(index, instance, flags);
1670
1671
1672
1673
1674
1675QObject *QQmlObjectCreator::createObjectInContext(
1676 int objectIndex, QObject *parent,
1677 const QQmlRefPointer<QQmlContextData> &existingContext)
1679 Q_ASSERT(phase == Startup);
1680 phase = CreatingObjects;
1682 context = existingContext;
1683 if (!sharedState->rootContext)
1684 sharedState->rootContext = context;
1686 QV4::Scope scope(v4);
1687 if (topLevelCreator)
1688 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(scope);
1690 QObject *rv = createInstance(objectIndex, parent,
false);
1691 phase = ObjectsCreated;
1695bool QQmlObjectCreator::finalize(QQmlInstantiationInterrupt &interrupt)
1697 Q_ASSERT(phase == ObjectsCreated || phase == Finalizing);
1700 QQmlObjectCreatorRecursionWatcher watcher(
this);
1701 QScopedValueRollback<QQmlObjectCreator*> ocRestore(QQmlEnginePrivate::get(engine)->activeObjectCreator,
this);
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716 const bool removeConstantBindings = !QV4::ExecutionEngine::isPreviewing();
1718 while (!sharedState->allCreatedBindings.empty()) {
1719 QQmlAbstractBinding::Ptr b = sharedState->allCreatedBindings.back();
1720 sharedState->allCreatedBindings.pop_back();
1723 if (!b->isAddedToObject())
1725 QQmlData *data = QQmlData::get(b->targetObject());
1727 data->clearPendingBindingBit(b->targetPropertyIndex().coreIndex());
1728 b->setEnabled(
true, QQmlPropertyData::BypassInterceptor |
1729 QQmlPropertyData::DontRemoveBinding);
1730 if (removeConstantBindings && b->kind() == QQmlAbstractBinding::QmlBinding) {
1731 QQmlBinding *binding =
static_cast<QQmlBinding*>(b.data());
1732 if (!binding->hasError() && !binding->hasDependencies()
1733 && !binding->hasUnresolvedNames()) {
1734 b->removeFromObject();
1738 if (watcher.hasRecursed() || interrupt.shouldInterrupt())
1742 while (!sharedState->allQPropertyBindings.isEmpty()) {
1743 auto& [target, index, qmlBinding] = sharedState->allQPropertyBindings.first();
1745 QQmlData *data = QQmlData::get(target);
1746 if (!data || !data->hasBindingBit(index)) {
1748 sharedState->allQPropertyBindings.pop_front();
1752 QUntypedBindable bindable;
1753 void *argv[] = { &bindable };
1755 target->metaObject()->metacall(target, QMetaObject::BindableProperty, index, argv);
1756 const bool success = bindable.setBinding(qmlBinding);
1758 const auto bindingPrivateRefCount = QPropertyBindingPrivate::get(qmlBinding)->refCount();
1761 sharedState->allQPropertyBindings.pop_front();
1764 if (removeConstantBindings && success && bindingPrivateRefCount > 1) {
1765 if (
auto priv = QPropertyBindingPrivate::get(qmlBinding); priv->isQmlBinding()) {
1766 const auto qmlBindingBase =
static_cast<
const QQmlPropertyBindingBase *>(priv);
1767 if (qmlBindingBase->bindingKind()
1768 == QQmlPropertyBindingBase::BindingKind::JavaScript) {
1769 const auto qmlBindingPriv =
1770 static_cast<
const QQmlPropertyBinding *>(qmlBindingBase);
1771 const auto jsExpression = qmlBindingPriv->jsExpression();
1772 const bool canRemove = !qmlBinding.error().hasError()
1773 && !qmlBindingPriv->hasDependencies()
1774 && !jsExpression->hasUnresolvedNames();
1776 bindable.takeBinding();
1781 if (watcher.hasRecursed() || interrupt.shouldInterrupt())
1785 if (QQmlVME::componentCompleteEnabled()) {
1786 while (!sharedState->allParserStatusCallbacks.empty()) {
1787 QQmlObjectCompletionProfiler profiler(&sharedState->profiler);
1788 const ParserStatus status = sharedState->allParserStatusCallbacks.back();
1789 sharedState->allParserStatusCallbacks.pop_back();
1791 const QQmlGuard<QObject> &instance = sharedState->allCreatedObjects[status.objectIndex];
1795 QQmlParserStatus *parserStatus
1796 =
reinterpret_cast<QQmlParserStatus *>(
1797 reinterpret_cast<
char *>(instance.data()) + status.parserStatusCast);
1798 parserStatus->componentComplete();
1800 if (watcher.hasRecursed() || interrupt.shouldInterrupt())
1805 for (QQmlFinalizerHook *hook: std::as_const(sharedState->finalizeHooks)) {
1806 hook->componentFinalized();
1807 if (watcher.hasRecursed())
1810 sharedState->finalizeHooks.clear();
1812 while (sharedState->componentAttached) {
1813 QQmlComponentAttached *a = sharedState->componentAttached;
1814 a->removeFromList();
1815 QQmlData *d = QQmlData::get(a->parent());
1817 Q_ASSERT(d->context);
1818 d->context->addComponentAttached(a);
1819 if (QQmlVME::componentCompleteEnabled())
1820 emit a->completed();
1822 if (watcher.hasRecursed() || interrupt.shouldInterrupt())
1831void QQmlObjectCreator::clear()
1833 if (phase == Done || phase == Finalizing || phase == Startup)
1835 Q_ASSERT(phase != Startup);
1837 while (!sharedState->allCreatedObjects.empty()) {
1838 auto object = sharedState->allCreatedObjects.back();
1839 sharedState->allCreatedObjects.pop_back();
1840 if (engine->objectOwnership(object) != QQmlEngine::CppOwnership) {
1845 while (sharedState->componentAttached) {
1846 QQmlComponentAttached *a = sharedState->componentAttached;
1847 a->removeFromList();
1853void QQmlObjectCreator::registerPostHocRequiredProperties(
const QV4::CompiledData::Binding *binding)
1855 const qsizetype oldRequiredPropertiesCount = sharedState->requiredProperties.size();
1856 QSet<QString> postHocRequired;
1857 for (
auto it = _compiledObject->requiredPropertyExtraDataBegin(); it != _compiledObject->requiredPropertyExtraDataEnd(); ++it)
1858 postHocRequired.insert(stringAt(it->nameIndex));
1859 bool hadInheritedRequiredProperties = !postHocRequired.empty();
1861 for (
int propertyIndex = 0; propertyIndex != _compiledObject->propertyCount(); ++propertyIndex) {
1862 const QV4::CompiledData::Property* property = _compiledObject->propertiesBegin() + propertyIndex;
1863 const QQmlPropertyData *propertyData = _propertyCache->property(_propertyCache->propertyOffset() + propertyIndex);
1865 auto postHocIt = postHocRequired.isEmpty() ? postHocRequired.end() : postHocRequired.find(stringAt(property->nameIndex()));
1866 if (!property->isRequired() && postHocRequired.end() == postHocIt)
1868 if (postHocIt != postHocRequired.end())
1869 postHocRequired.erase(postHocIt);
1870 if (isContextObject)
1871 sharedState->hadTopLevelRequiredProperties =
true;
1872 sharedState->requiredProperties.insert({_qobject, propertyData},
1873 RequiredPropertyInfo {compilationUnit->stringAt(property->nameIndex()), compilationUnit->finalUrl(), property->location, {}});
1877 const auto getPropertyCacheRange = [&]() -> std::pair<
int,
int> {
1895 QV4::ResolvedTypeReference *typeRef = resolvedType(_compiledObject->inheritedTypeNameIndex);
1898 if (binding->isAttachedProperty())
1899 return { 0, _propertyCache->propertyCount() };
1900 Q_ASSERT(binding->isGroupedProperty());
1901 return { 0, _propertyCache->propertyOffset() + 1 };
1903 Q_ASSERT(!compilationUnit->objectIsComponent(_compiledObjectIndex));
1904 QQmlType type = typeRef->type();
1905 if (type.isComposite())
1906 return { _propertyCache->propertyOffset(), _propertyCache->propertyCount() };
1907 return { 0, _propertyCache->propertyCount() };
1909 const auto [offset, count] = getPropertyCacheRange();
1910 for (
int i = offset; i < count; ++i) {
1911 const QQmlPropertyData *propertyData = _propertyCache->maybeUnresolvedProperty(i);
1917 if (!propertyData->isRequired() && postHocRequired.isEmpty())
1919 QString name = propertyData->name(_qobject);
1920 const auto postHocIt = postHocRequired.constFind(name);
1921 if (postHocIt == postHocRequired.constEnd()) {
1922 if (!propertyData->isRequired())
1925 postHocRequired.erase(postHocIt);
1928 if (isContextObject)
1929 sharedState->hadTopLevelRequiredProperties =
true;
1930 sharedState->requiredProperties.insert(
1931 {_qobject, propertyData},
1932 RequiredPropertyInfo {
1933 std::move(name), compilationUnit->finalUrl(), _compiledObject->location, {}
1937 if (binding && binding->isAttachedProperty()
1938 && sharedState->requiredProperties.size() != oldRequiredPropertiesCount) {
1941 QLatin1String(
"Attached property has required properties. This is not supported"));
1948 if (!postHocRequired.isEmpty()) {
1950 for (
int i = 0; i < offset; ++i) {
1951 const QQmlPropertyData *propertyData = _propertyCache->maybeUnresolvedProperty(i);
1954 QString name = propertyData->name(_qobject);
1955 const auto postHocIt = postHocRequired.constFind(name);
1956 if (postHocIt == postHocRequired.constEnd())
1958 postHocRequired.erase(postHocIt);
1960 if (isContextObject)
1961 sharedState->hadTopLevelRequiredProperties =
true;
1962 sharedState->requiredProperties.insert(
1963 {_qobject, propertyData},
1964 RequiredPropertyInfo {
1965 std::move(name), compilationUnit->finalUrl(), _compiledObject->location, {}
1970 if (!postHocRequired.isEmpty() && hadInheritedRequiredProperties)
1971 recordError({}, QLatin1String(
"Property %1 was marked as required but does not exist").arg(*postHocRequired.begin()));
1974bool QQmlObjectCreator::populateInstance(
int index, QObject *instance, QObject *bindingTarget,
1975 const QQmlPropertyData *valueTypeProperty,
1976 const QV4::CompiledData::Binding *binding)
1979 QQmlData *declarativeData = QQmlData::get(instance,
true);
1981 qSwap(_qobject, instance);
1982 qSwap(_valueTypeProperty, valueTypeProperty);
1983 qSwap(_compiledObjectIndex, index);
1984 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(_compiledObjectIndex);
1985 qSwap(_compiledObject, obj);
1986 qSwap(_ddata, declarativeData);
1987 qSwap(_bindingTarget, bindingTarget);
1989 QV4::Scope valueScope(v4);
1990 QV4::ScopedValue scopeObjectProtector(valueScope);
1992 QQmlPropertyCache::ConstPtr cache = propertyCaches->at(_compiledObjectIndex);
1994 QQmlVMEMetaObject *vmeMetaObject =
nullptr;
1995 if (propertyCaches->needsVMEMetaObject(_compiledObjectIndex)) {
1996 Q_ASSERT(!cache.isNull());
1998 vmeMetaObject =
new QQmlVMEMetaObject(v4, _qobject, cache, compilationUnit, _compiledObjectIndex);
1999 _ddata->propertyCache = cache;
2000 scopeObjectProtector = _ddata->jsWrapper.value();
2002 vmeMetaObject = QQmlVMEMetaObject::get(_qobject, v4);
2005 registerObjectWithContextById(_compiledObjectIndex, _qobject);
2007 qSwap(_propertyCache, cache);
2008 qSwap(_vmeMetaObject, vmeMetaObject);
2013 if (!stringAt(_compiledObject->inheritedTypeNameIndex).isEmpty()) {
2014 _ddata->compilationUnit = compilationUnit;
2015 _ddata->cuObjectIndex =
int(_compiledObjectIndex);
2021 const bool applyingDeferred =
2022 compilationUnit->objectIsPopulatedByDeferredBinding(_compiledObjectIndex);
2023 if (!applyingDeferred
2024 && compilationUnit->objectHasDeferredBindings(_compiledObjectIndex)) {
2025 _ddata->deferData(_compiledObjectIndex, compilationUnit, context, m_inlineComponentName);
2028 registerPostHocRequiredProperties(binding);
2030 if (_compiledObject->nFunctions > 0)
2032 setupBindings(applyingDeferred ? BindingMode::ApplyAll : BindingMode::ApplyImmediate);
2034 for (
int aliasIndex = 0; aliasIndex != _compiledObject->aliasCount(); ++aliasIndex) {
2039 _vmeMetaObject->connectAlias(_compiledObject, aliasIndex);
2040 if (!context->isIdValueSet(0))
2042 const QQmlPropertyData *aliasProperty =
2043 _propertyCache->property(_vmeMetaObject->aliasOffset() + aliasIndex);
2045 if (!aliasProperty || (!aliasProperty->isWritable() && !aliasProperty->isQList()))
2047 QObject *target = context->idValue(aliasProperty->aliasTargetObjectId());
2050 QQmlData *targetDData = QQmlData::get(target,
false);
2051 if (targetDData ==
nullptr || targetDData->propertyCache.isNull())
2054 const int targetPropertyIndex = aliasProperty->aliasTarget();
2055 int coreIndex = QQmlPropertyIndex::fromEncoded(targetPropertyIndex).coreIndex();
2057 const QQmlPropertyData *
const targetProperty = targetDData->propertyCache->property(coreIndex);
2058 if (!targetProperty)
2060 auto it = sharedState->requiredProperties.find({target, targetProperty});
2061 if (it != sharedState->requiredProperties.end()) {
2062 const QV4::CompiledData::Alias* alias = _compiledObject->aliasesBegin() + aliasIndex;
2063 it->aliasesToRequired.push_back(
2064 AliasToRequiredInfo {
2065 compilationUnit->stringAt(alias->nameIndex()),
2066 compilationUnit->finalUrl()
2071 qSwap(_vmeMetaObject, vmeMetaObject);
2072 qSwap(_bindingTarget, bindingTarget);
2073 qSwap(_ddata, declarativeData);
2074 qSwap(_compiledObject, obj);
2075 qSwap(_compiledObjectIndex, index);
2076 qSwap(_valueTypeProperty, valueTypeProperty);
2077 qSwap(_qobject, instance);
2078 qSwap(_propertyCache, cache);
2080 return errors.isEmpty();
2084
2085
2086QQmlComponent *QQmlObjectCreator::createComponent(
2087 QQmlEngine *engine, QV4::ExecutableCompilationUnit *compilationUnit,
int index,
2088 QObject *parent,
const QQmlRefPointer<QQmlContextData> &context)
2090 QQmlComponent *component =
new QQmlComponent(engine, compilationUnit, index, parent);
2091 QQmlComponentPrivate::get(component)->setCreationContext(context);
2092 QQmlData *ddata = QQmlData::get(component,
true);
2094 ddata->compilationUnit = compilationUnit;
2095 ddata->cuObjectIndex = index;
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112void QQmlObjectCreator::repopulateBindings(
2113 int index, QObject *instance,
2114 const QQmlRefPointer<QQmlContextData> &instanceContext, InitFlags flags)
2116 phase = QQmlObjectCreator::ObjectsCreated;
2119 if (compilationUnit->objectCount() <= index)
2122 const auto oldContext = std::exchange(context, instanceContext);
2123 const auto contextGuard = qScopeGuard([&]() {
2124 context = oldContext;
2127 auto rootContextGuard = qScopeGuard([&]() {
2128 sharedState->rootContext.reset();
2130 if (!sharedState->rootContext)
2131 sharedState->rootContext = context;
2133 rootContextGuard.dismiss();
2135 QV4::Scope scope(engine->handle());
2136 auto jsObjectsGuard = qScopeGuard([&]() {
2137 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList();
2139 if (!sharedState->allJavaScriptObjects.canTrack())
2140 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(scope);
2142 jsObjectsGuard.dismiss();
2144 QQmlData *ddata = QQmlData::get(instance,
true);
2147 QQmlVMEMetaObject *vmeMetaObject = QQmlVMEMetaObject::get(instance, v4);
2149 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(index);
2152 qSwap(_qobject, instance);
2153 qSwap(_compiledObjectIndex, index);
2154 qSwap(_compiledObject, obj);
2155 qSwap(_ddata, ddata);
2156 QObject *bindingTarget = _qobject;
2157 qSwap(_bindingTarget, bindingTarget);
2158 QObject *scopeObject = _qobject;
2159 qSwap(_scopeObject, scopeObject);
2160 const QQmlPropertyData *valueTypeProperty =
nullptr;
2161 qSwap(_valueTypeProperty, valueTypeProperty);
2163 Q_ASSERT(sharedState->allJavaScriptObjects.canTrack());
2164 sharedState->allJavaScriptObjects.trackObject(v4, _qobject);
2166 QV4::QmlContext *qmlContext =
static_cast<QV4::QmlContext *>(scope.constructUndefined(1));
2167 qSwap(_qmlContext, qmlContext);
2169 QV4::ScopedValue scopeObjectProtector(scope);
2171 QQmlPropertyCache::ConstPtr cache = propertyCaches->at(_compiledObjectIndex);
2172 _ddata->propertyCache = cache;
2173 qSwap(_propertyCache, cache);
2174 qSwap(_vmeMetaObject, vmeMetaObject);
2176 if (compilationUnit->objectId(_compiledObjectIndex) < context->numIdValues())
2177 registerObjectWithContextById(_compiledObjectIndex, _qobject);
2179 _ddata->compilationUnit = compilationUnit;
2180 _ddata->cuObjectIndex =
int(_compiledObjectIndex);
2182 if (_compiledObject->nFunctions > 0)
2184 setupBindings(BindingMode::ApplyImmediate);
2186 if (flags.testFlag(InitFlag::IsContextObject)) {
2187 while (!pendingAliasBindings.empty()) {
2188 for (
auto it = pendingAliasBindings.begin(); it != pendingAliasBindings.end();) {
2189 if ((*it)(sharedState.data()))
2190 it = pendingAliasBindings.erase(it);
2198 qSwap(_vmeMetaObject, vmeMetaObject);
2199 qSwap(_propertyCache, cache);
2200 qSwap(_qmlContext, qmlContext);
2201 qSwap(_scopeObject, scopeObject);
2202 qSwap(_valueTypeProperty, valueTypeProperty);
2203 qSwap(_bindingTarget, bindingTarget);
2204 qSwap(_ddata, ddata);
2205 qSwap(_compiledObject, obj);
2206 qSwap(_compiledObjectIndex, index);
2207 qSwap(_qobject, instance);
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223bool QQmlObjectCreator::applyLateBinding(
int index, QObject *instance,
2224 const QQmlRefPointer<QQmlContextData> &instanceContext,
2227 phase = QQmlObjectCreator::ObjectsCreated;
2229 if (compilationUnit->objectCount() <= index)
2232 const auto oldContext = std::exchange(context, instanceContext);
2233 const auto contextGuard = qScopeGuard([&]() { context = oldContext; });
2235 auto rootContextGuard = qScopeGuard([&]() { sharedState->rootContext.reset(); });
2236 if (!sharedState->rootContext)
2237 sharedState->rootContext = context;
2239 rootContextGuard.dismiss();
2241 QV4::Scope scope(engine->handle());
2242 auto jsObjectsGuard = qScopeGuard(
2243 [&]() { sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(); });
2244 if (!sharedState->allJavaScriptObjects.canTrack())
2245 sharedState->allJavaScriptObjects = ObjectInCreationGCAnchorList(scope);
2247 jsObjectsGuard.dismiss();
2249 QQmlData *ddata = QQmlData::get(instance,
true);
2255 const QQmlRefPointer<QV4::ExecutableCompilationUnit> savedUnit = ddata->compilationUnit;
2256 const QQmlPropertyCache::ConstPtr savedCache = ddata->propertyCache;
2257 const int savedCuObjectIndex = ddata->cuObjectIndex;
2258 const auto ddataGuard = qScopeGuard([&]() {
2259 ddata->compilationUnit = savedUnit;
2260 ddata->propertyCache = savedCache;
2261 ddata->cuObjectIndex = savedCuObjectIndex;
2264 QQmlVMEMetaObject *vmeMetaObject = QQmlVMEMetaObject::get(instance, engine->handle());
2265 const QV4::CompiledData::Object *obj = compilationUnit->objectAt(index);
2266 if (bindingIndex < 0 || uint(bindingIndex) >= obj->nBindings)
2269 qSwap(_qobject, instance);
2270 qSwap(_compiledObjectIndex, index);
2271 qSwap(_compiledObject, obj);
2272 qSwap(_ddata, ddata);
2273 QObject *bindingTarget = _qobject;
2274 qSwap(_bindingTarget, bindingTarget);
2275 QObject *scopeObject = _qobject;
2276 qSwap(_scopeObject, scopeObject);
2277 const QQmlPropertyData *valueTypeProperty =
nullptr;
2278 qSwap(_valueTypeProperty, valueTypeProperty);
2280 Q_ASSERT(sharedState->allJavaScriptObjects.canTrack());
2281 sharedState->allJavaScriptObjects.trackObject(v4, _qobject);
2283 QV4::QmlContext *qmlContext =
static_cast<QV4::QmlContext *>(scope.constructUndefined(1));
2284 qSwap(_qmlContext, qmlContext);
2286 QQmlPropertyCache::ConstPtr cache = propertyCaches->at(_compiledObjectIndex);
2287 qSwap(_propertyCache, cache);
2288 qSwap(_vmeMetaObject, vmeMetaObject);
2290 const QV4::CompiledData::Binding *binding = _compiledObject->bindingTable() + bindingIndex;
2291 const QQmlPropertyData *defaultProperty = _compiledObject->indexOfDefaultPropertyOrAlias != -1
2292 ? _propertyCache->parent()->defaultProperty()
2293 : _propertyCache->defaultProperty();
2294 const QQmlPropertyData *property = binding->propertyNameIndex != 0
2295 ? _propertyCache->property(stringAt(binding->propertyNameIndex), _qobject, context)
2298 const bool installed = property && setPropertyBinding(property, binding);
2300 qSwap(_vmeMetaObject, vmeMetaObject);
2301 qSwap(_propertyCache, cache);
2302 qSwap(_qmlContext, qmlContext);
2303 qSwap(_valueTypeProperty, valueTypeProperty);
2304 qSwap(_scopeObject, scopeObject);
2305 qSwap(_bindingTarget, bindingTarget);
2306 qSwap(_ddata, ddata);
2307 qSwap(_compiledObject, obj);
2308 qSwap(_compiledObjectIndex, index);
2309 qSwap(_qobject, instance);
2320void ObjectInCreationGCAnchorList::trackObject(QV4::ExecutionEngine *engine, QObject *instance)
2322 QV4::Value *wrapper = allocationScope->construct(1, QV4::QObjectWrapper::wrap(engine, instance));
2326 QV4::WriteBarrier::markCustom(engine, [wrapper](QV4::MarkStack *ms) {
2327 wrapper->heapObject()->mark(ms);
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static QQmlType qmlTypeForObject(QObject *object)
QQmlObjectCreatorRecursionWatcher(QQmlObjectCreator *creator)