Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlpreviewobjectpatch.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <private/qqmlcomponent_p.h>
8#include <private/qqmlcontextdata_p.h>
9#include <private/qqmldata_p.h>
10#include <private/qqmljavascriptexpression_p.h>
11#include <private/qqmlobjectcreator_p.h>
12#include <private/qqmlpreviewbindingpatchcontext_p.h>
13#include <private/qqmlpreviewdiff_p.h>
14#include <private/qqmlproperty_p.h>
15#include <private/qqmlpropertyresolver_p.h>
16#include <private/qqmlscriptdata_p.h>
17#include <private/qqmlsignalnames_p.h>
18#include <private/qqmlvme_p.h>
19#include <private/qqmlvmemetaobject_p.h>
20#include <private/qv4functionobject_p.h>
21#include <private/qv4generatorobject_p.h>
22#include <private/qv4qmlcontext_p.h>
23#include <private/qv4resolvedtypereference_p.h>
24
25#include <QtQml/qqmlcomponent.h>
26#include <QtQml/qqmlproperty.h>
27
28#include <QtCore/qset.h>
29
31
32namespace QQmlPreview {
33
34// The indices at which object appears in oldUnit: its own (ddata) index plus any indices it
35// occupies through composite base levels (the VME meta-object chain).
36static QVarLengthArray<int, 4>
37objectIndices(QObject *object, const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit)
38{
39 QVarLengthArray<int, 4> objectIndices;
40
41 QQmlData *ddata = QQmlData::get(object);
42 if (!ddata)
43 return objectIndices;
44
45 if (ddata->compilationUnit == oldUnit)
46 objectIndices.push_back(ddata->cuObjectIndex);
47 if (ddata->hasVMEMetaObject) {
48 for (QQmlVMEMetaObject *vme =
49 static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
50 vme; vme = vme->parentVMEMetaObject()) {
51 if (vme->compilationUnit() == oldUnit)
52 objectIndices.push_back(vme->qmlObjectId());
53 }
54 }
55
56 return objectIndices;
57}
58
60nonCompositeBaseType(const QQmlPropertyCache::ConstPtr &propertyCache)
61{
62 for (QQmlPropertyCache::ConstPtr parent = propertyCache; parent; parent = parent->parent()) {
63 if (!parent->isComposite())
64 return parent;
65 }
66
67 return QQmlPropertyCache::ConstPtr();
68}
69
70static bool
71hasChangedNonCompositeBaseType(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
72 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit,
73 int objectIndex)
74{
75 const QV4::CompiledData::Object *oldObj = oldUnit->objectAt(objectIndex);
76 const auto *oldTypeRef = oldUnit->resolvedType(oldObj->inheritedTypeNameIndex);
77 if (!oldTypeRef)
78 return false; // Grouped property sub-objects have no inherited type.
79
80 const QV4::CompiledData::Object *newObj = newUnit->objectAt(objectIndex);
81 const auto *newTypeRef = newUnit->resolvedType(newObj->inheritedTypeNameIndex);
82 if (!newTypeRef)
83 return true; // Type disappeared — definitely changed.
84
85 return nonCompositeBaseType(oldTypeRef->typePropertyCache())
86 != nonCompositeBaseType(newTypeRef->typePropertyCache());
87}
88
89// Whether the object at objectIndex is the root of a component: The document, an inline component,
90// or an explicit or implicit Component.
91static bool isComponentRoot(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
92 int objectIndex)
93{
94 if (objectIndex == 0)
95 return true;
96
97 if (objectIndex >= unit->objectCount())
98 return false;
99
100 if (unit->objectAt(objectIndex)->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot))
101 return true;
102
103 if (unit->baseCompilationUnit()->implicitComponentForObject(objectIndex) >= 0)
104 return true;
105
106 for (int i = 0, end = unit->objectCount(); i < end; ++i) {
107 if (unit->objectIsComponent(i)
108 && int(unit->objectAt(i)->bindingTable()->value.objectIndex) == objectIndex) {
109 return true;
110 }
111 }
112 return false;
113}
114
115// reset() + repopulateBindings() cannot faithfully reproduce some components in place,
116// because either:
117// - Its index doesn't denote a component root anymore in the new unit, because objects were
118// added or removed in front of it.
119// - Its non-composite (C++) base type changed: the reused QObject is still an instance of the old
120// class (see hasChangedNonCompositeBaseType).
121// - It carries deferred bindings. We cannot re-install those because their handling is in the
122// type's discretion.
123// Fore those return false here. Otherwise return true.
124static bool
125canRebuildComponentRootInPlace(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
126 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit,
127 int objectIndex)
128{
129 // An index out of range in the new CU is obsolete: rebuildObject() skips it and applyDiff()
130 // retires it afterwards. There is nothing to recreate from an enclosing root.
131 if (objectIndex >= newUnit->objectCount())
132 return true;
133
134 if (!isComponentRoot(newUnit, objectIndex))
135 return false;
136
137 if (newUnit->objectHasDeferredBindings(objectIndex))
138 return false;
139
140 return !hasChangedNonCompositeBaseType(oldUnit, newUnit, objectIndex);
141}
142
143// Re-resolve the object's precomputed binding-target table against its relinked property cache.
144static void refreshBindingPropertyData(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &cu,
145 int objectIndex, const QQmlPropertyCache::ConstPtr &cache)
146{
147 QList<QV4::CompiledData::BindingPropertyData> &table =
148 cu->baseCompilationUnit()->bindingPropertyDataPerObject;
149 if (objectIndex >= table.size())
150 return;
151
152 QV4::CompiledData::BindingPropertyData &bindingData = table[objectIndex];
153
154 const QV4::CompiledData::Object *obj = cu->objectAt(objectIndex);
155 const QQmlPropertyResolver resolver(cache);
156
157 const QV4::CompiledData::Binding *binding = obj->bindingTable();
158 for (qsizetype i = 0, end = bindingData.size(); i < end; ++i, ++binding) {
159 if (!bindingData.at(i))
160 continue;
161
162 Q_ASSERT(i < obj->nBindings);
163 const QString name = BindingPatchContext::targetPropertyName(cu, objectIndex, binding);
164 if (name.isEmpty())
165 continue;
166
167 if (cu->bindingIsSignalHandlerExpression(objectIndex, i)
168 || cu->bindingIsSignalHandlerObject(objectIndex, i)) {
169 bindingData[i] = resolver.signal(
170 *QQmlSignalNames::anyHandlerNameToSignalName(name), nullptr,
171 QQmlPropertyResolver::IgnoreRevision);
172 } else if (cu->bindingIsPropertyObserver(objectIndex, i)) {
173 bindingData[i] = resolver.property(
174 *QQmlSignalNames::changedHandlerNameToPropertyName(name), nullptr,
175 QQmlPropertyResolver::IgnoreRevision);
176 } else {
177 bindingData[i] = resolver.property(
178 name, nullptr, QQmlPropertyResolver::IgnoreRevision);
179 }
180 }
181}
182
183// Ensure the property cache at objectIndex in cu is derived from actualParent, the cache we just
184// used for the level below it in the VME chain.
186relinkCache(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &cu, int objectIndex,
187 const QQmlPropertyCache::ConstPtr &actualParent)
188{
189 QQmlPropertyCacheVector *caches = cu->propertyCachesPtr();
190 QQmlPropertyCache::ConstPtr cache = caches->at(objectIndex);
191
192 // The bottom-most composite level's own base is a non-composite (C++) type. That base never
193 // changes across a reload since a changed non-composite base is rejected by
194 // hasChangedNonCompositeBaseType.
195 if (!actualParent)
196 return cache;
197
198 // A type that needs no VME meta-object of its own reuses its base type's property cache.
199 if (!caches->needsVMEMetaObject(objectIndex)) {
200 if (cache != actualParent) {
201 caches->set(objectIndex, actualParent);
202 refreshBindingPropertyData(cu, objectIndex, actualParent);
203 }
204 return actualParent;
205 }
206
207 // A type with its own cache derived from the base: re-derive it from the relinked base so its
208 // inherited offsets match the (possibly changed) base layout.
209 if (cache->parent() != actualParent) {
210 cache = cache->rebased(actualParent);
211 caches->set(objectIndex, cache);
212 refreshBindingPropertyData(cu, objectIndex, cache);
213 }
214
215 return cache;
216}
217
218// Re-link every property cache in cu that (transitively) derives oldBaseCache so it derives
219// newBaseCache instead.
220static void relinkDerivedCaches(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &cu,
221 const QQmlPropertyCache::ConstPtr &oldBaseCache,
222 const QQmlPropertyCache::ConstPtr &newBaseCache)
223{
224 QQmlPropertyCacheVector *caches = cu->propertyCachesPtr();
225 Q_ASSERT(caches);
226
227 QHash<const QQmlPropertyCache *, QQmlPropertyCache::ConstPtr> replacements;
228 replacements.insert(oldBaseCache.data(), newBaseCache);
229
230 // An instance of the type that needs no VME meta-object of its own shares the type's property
231 // cache. Such an entry is the old base cache itself, not derived from it, so the loop below
232 // would miss it. Objects created later from this unit would then get the stale cache as their
233 // QQmlData::propertyCache while their VME meta-objects use the new layout.
234 for (int i = 0, end = caches->count(); i < end; ++i) {
235 if (caches->at(i) != oldBaseCache)
236 continue;
237 caches->set(i, newBaseCache);
238 refreshBindingPropertyData(cu, i, newBaseCache);
239 }
240
241 for (bool changed = true; changed;) {
242 changed = false;
243 for (int i = 0, end = caches->count(); i < end; ++i) {
244 const QQmlPropertyCache::ConstPtr cache = caches->at(i);
245 if (!cache)
246 continue;
247
248 const auto it = replacements.constFind(cache->parent().data());
249 if (it == replacements.constEnd())
250 continue;
251
252 const QQmlPropertyCache::ConstPtr rebased = cache->rebased(*it);
253 replacements.insert(cache.data(), rebased);
254 caches->set(i, rebased);
255 refreshBindingPropertyData(cu, i, rebased);
256 changed = true;
257 }
258 }
259}
260
268
269// Walk the type resolution chain starting from the object at cuIndex in unit,
270// collecting all composite (QML-defined) base type levels. Returns them ordered
271// deepest-first (e.g., grandparent before parent).
273collectCompositeLevels(const CompositeLevel &instanceLevel,
274 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
275 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
276{
277 std::vector<CompositeLevel> levels;
278
279 auto currentUnit = instanceLevel.newCu;
280 const QV4::CompiledData::Object *obj = currentUnit->objectAt(instanceLevel.objectIndex);
281 const QV4::ResolvedTypeReference *typeRef =
282 currentUnit->resolvedType(obj->inheritedTypeNameIndex);
283
284 while (typeRef && typeRef->type().isComposite()) {
285 QQmlRefPointer<QV4::ExecutableCompilationUnit> cu = typeRef->isSelfReference()
286 ? currentUnit
287 : currentUnit->engine->executableCompilationUnit(typeRef->compilationUnit());
288 Q_ASSERT(cu);
289
290 // Replace the old unit with the new one wherever it occurs
291 const auto oldCu = cu;
292 if (cu == oldUnit)
293 cu = newUnit;
294
295 int rootIndex;
296 QString icName;
297 if (typeRef->type().isInlineComponent()) {
298 icName = typeRef->type().elementName();
299 rootIndex = cu->inlineComponentId(icName);
300 } else {
301 rootIndex = 0;
302 }
303
304 levels.push_back({ oldCu, cu, rootIndex, icName, nullptr });
305
306 if (rootIndex < 0 || rootIndex >= cu->objectCount())
307 break;
308
309 // Walk deeper into the base type
310 const QV4::CompiledData::Object *rootObj = cu->objectAt(rootIndex);
311 typeRef = cu->resolvedType(rootObj->inheritedTypeNameIndex);
312 currentUnit = cu;
313 }
314
315 return levels;
316}
317
318// Rebuilding a component root replaces the contexts it is the context object of. Instances of
319// inner components created in such a context still have it as parent context. When rebuilding
320// those, we have to parent their new contexts to the replacements, or they keep resolving ids
321// against the old context, whose objects have been retired.
328
330currentContext(const QQmlRefPointer<QQmlContextData> &context,
331 const ReplacedContexts &replacedContexts)
332{
333 const auto it = replacedContexts.constFind(context.data());
334 return it == replacedContexts.constEnd() ? context : it->newContext;
335}
336
337// The contexts of the composite levels of object, in the order of collectCompositeLevels().
339levelContexts(QObject *object, const QQmlRefPointer<QQmlContextData> &outerContext)
340{
341 QVarLengthArray<QQmlRefPointer<QQmlContextData>, 4> contexts;
342 for (QQmlRefPointer<QQmlContextData> ctx(QQmlData::get(object)->context); ctx;
343 ctx = ctx->linkedContext()) {
344 if (ctx != outerContext && ctx->contextObject() == object)
345 contexts.append(ctx);
346 }
347
348 // The linked contexts are ordered from the deepest base type to the most derived one.
349 std::reverse(contexts.begin(), contexts.end());
350
351 return contexts;
352}
353
354// cuIndex is not necessarily the "outermost" index. There may be levels above oldUnit.
355// Those have to be preserved/rebuilt.
356static void rebuildObject(QObject *object, int cuIndex,
357 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
358 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit,
359 ReplacedContexts *replacedContexts)
360{
361 // If the object's index doesn't exist in the new CU, it's obsolete.
362 if (cuIndex >= newUnit->objectCount())
363 return;
364
365 QQmlData *ddata = QQmlData::get(object);
366 Q_ASSERT(ddata);
367
368 QQmlRefPointer<QQmlContextData> outerContext =
369 QQmlRefPointer<QQmlContextData>(ddata->outerContext);
370
371 // If the object has no context, or is scheduled for deletion, it's half-dead already.
372 if (!ddata->context || !outerContext || !outerContext->isValid() || ddata->isQueuedForDeletion)
373 return;
374
375 CompositeLevel instanceLevel{ ddata->compilationUnit,
376 ddata->compilationUnit == oldUnit ? newUnit
377 : ddata->compilationUnit,
378 ddata->cuObjectIndex, QString(), outerContext };
379
380 // If the object doesn't exist anymore in the new CU it will be deleted via GC.
381 // Nothing to do here.
382 if (instanceLevel.objectIndex >= instanceLevel.newCu->objectCount())
383 return;
384
385 std::vector<CompositeLevel> levels = collectCompositeLevels(instanceLevel, oldUnit, newUnit);
386
387 // Build the set of compilation units that participate in this rebuild.
388 // Bindings from these CUs are "internal" and will be re-created by repopulateBindings.
389 std::vector<CompositeLevel> internalUnits;
390 internalUnits.push_back(instanceLevel);
391 for (const auto &level : levels)
392 internalUnits.push_back(level);
393
394 BindingPatchContext patchCtx(object, ddata->compilationUnit, ddata->cuObjectIndex);
395 QDuplicateTracker<QObject *> seenChildren;
396 patchCtx.stashExternalState(internalUnits, &seenChildren);
397 // Collect the CUs whose children will be recreated by repopulateBindings.
398 // This is oldUnit (being replaced) and the composite-level CUs.
399 // NOT newUnit (already-rebuilt objects pointing here must be preserved)
400 // and NOT instanceLevel.cu (its repopulateBindings only sets bindings, not children).
401 std::vector<QQmlRefPointer<QV4::ExecutableCompilationUnit>> unitsToUnparent;
402 unitsToUnparent.push_back(oldUnit);
403 for (const auto &level : levels)
404 unitsToUnparent.push_back(level.oldCu);
405 patchCtx.reset(unitsToUnparent, internalUnits);
406
407 QV4::ExecutionEngine *v4 = newUnit->engine;
408 Q_ASSERT(v4);
409 QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(v4);
410 Q_ASSERT(enginePrivate);
411
412 const QVarLengthArray<QQmlRefPointer<QQmlContextData>, 4> oldLevelContexts =
413 levelContexts(object, outerContext);
414
415 if (outerContext->contextObject() == object) {
416 const QQmlRefPointer<QQmlContextData> oldOuterContext = outerContext;
417 oldOuterContext->setContextObject(nullptr);
418 outerContext = enginePrivate->createComponentRootContext(
419 instanceLevel.newCu, currentContext(oldOuterContext->parent(), *replacedContexts),
420 instanceLevel.objectIndex);
421 outerContext->setContextObject(object);
422 instanceLevel.context = outerContext;
423 replacedContexts->insert(oldOuterContext.data(), { oldOuterContext, outerContext });
424 }
425
426 for (QQmlRefPointer<QQmlContextData> ctx(ddata->context); ctx; ctx = ctx->linkedContext()) {
427 if (ctx->contextObject() == object)
428 ctx->setContextObject(nullptr);
429 }
430
431 ddata->clear();
432
433 // clear() forgets where the object is declared, which only creating an object sets. The
434 // engine debug service finds objects by it and reports it: take it from the declaration in
435 // the unit the object is rebuilt from.
436 if (const QV4::CompiledData::Object *declaration =
437 instanceLevel.newCu->objectAt(instanceLevel.objectIndex)) {
438 ddata->lineNumber = declaration->location.line();
439 ddata->columnNumber = declaration->location.column();
440 }
441
442 QObjectPrivate *objectPrivate = QObjectPrivate::get(object);
443 delete std::exchange(objectPrivate->metaObject, nullptr);
444
445 QQmlRefPointer<QQmlContextData> levelContext = outerContext;
446 qsizetype replacedLevels = 0;
447 for (qsizetype i = 0, end = levels.size(); i < end; ++i) {
448 CompositeLevel &level = levels[i];
449 levelContext = level.context = enginePrivate->createComponentRootContext(
450 level.newCu, levelContext, level.objectIndex);
451 levelContext->setContextObject(object);
452
453 // The base type chain may have changed. Only match the old contexts up to the first
454 // level that differs.
455 if (i >= oldLevelContexts.size() || i != replacedLevels)
456 continue;
457 const QQmlRefPointer<QQmlContextData> &oldLevelContext = oldLevelContexts[i];
458 const QQmlRefPointer<QV4::ExecutableCompilationUnit> oldLevelCu =
459 oldLevelContext->typeCompilationUnit();
460 if ((oldLevelCu == oldUnit ? newUnit : oldLevelCu) != level.newCu)
461 continue;
462 replacedContexts->insert(oldLevelContext.data(), { oldLevelContext, levelContext });
463 ++replacedLevels;
464 }
465
466 if (outerContext->contextObject() == object) {
467 ddata->ownContext = outerContext;
468 ddata->context = outerContext.data();
469 if (!levels.empty())
470 ddata->context->setLinkedContext(levels.front().context);
471 } else if (!levels.empty()) {
472 ddata->ownContext = levels.back().context;
473 ddata->context = ddata->ownContext.data();
474 if (levels.size() > 1)
475 ddata->context->setLinkedContext(levels.front().context);
476 } else {
477 // Non-root child: doesn't own a context. Re-link to the parent context.
478 ddata->context = outerContext.data();
479 }
480
481 // Build the VME meta-object chain base-first, relinking each level's property cache to the
482 // actual (possibly freshly reloaded) parent cache we just used, so the whole chain's offsets
483 // stay consistent even when a composite base type's layout changed on reload.
484 QQmlPropertyCache::ConstPtr parentCache;
485 for (auto it = levels.crbegin(), end = levels.crend(); it != end; ++it) {
486 it->context->addOwnedObject(ddata);
487 QQmlPropertyCache::ConstPtr cache = relinkCache(it->newCu, it->objectIndex, parentCache);
488 if (it->newCu->propertyCachesPtr()->needsVMEMetaObject(it->objectIndex))
489 new QQmlVMEMetaObject(v4, object, cache, it->newCu, it->objectIndex);
490 parentCache = cache;
491 }
492
493 outerContext->addOwnedObject(ddata);
494 if (QQmlPropertyCacheVector *caches = instanceLevel.newCu->propertyCachesPtr();
495 caches->count() > instanceLevel.objectIndex) {
496 QQmlPropertyCache::ConstPtr cache =
497 relinkCache(instanceLevel.newCu, instanceLevel.objectIndex, parentCache);
498 if (caches->needsVMEMetaObject(instanceLevel.objectIndex))
499 new QQmlVMEMetaObject(v4, object, cache, instanceLevel.newCu, instanceLevel.objectIndex);
500 }
501
502 // Repopulate bindings at each composite level (deepest first).
503 // This sets up functions, evaluates bindings (creating child objects), etc.
504 // The object may get queued for deletion as result of some bindings.
505 // In that case we have to abort.
506 for (auto it = levels.crbegin(), end = levels.crend(); it != end && !ddata->isQueuedForDeletion;
507 ++it) {
508 QQmlObjectCreator creator(it->context, it->newCu, outerContext, it->icName, nullptr);
509 creator.repopulateBindings(it->objectIndex, object, it->context,
510 QQmlObjectCreator::InitFlag::IsContextObject
511 | QQmlObjectCreator::InitFlag::IsDocumentRoot);
512
513 QQmlInstantiationInterrupt interrupt;
514 creator.finalize(interrupt);
515 }
516
517 // The object may get queued for deletion as result of some bindings.
518 // In that case don't touch it any further.
519 if (!ddata->isQueuedForDeletion) {
520 // Repopulate bindings at the instance level in the parent CU.
521 QQmlObjectCreator creator(instanceLevel.context, instanceLevel.newCu, outerContext, QString(),
522 nullptr);
523 creator.repopulateBindings(instanceLevel.objectIndex, object, outerContext,
524 QQmlObjectCreator::InitFlag::None);
525 QQmlInstantiationInterrupt interrupt;
526 creator.finalize(interrupt);
527
528 // Restore externally set bindings and values that were stashed before reset.
529 // First refresh child object pointers — they may have been replaced during rebuild.
530 patchCtx.refreshObjects();
531 patchCtx.restoreExternalState();
532 }
533}
534
536static BindingKind bindingKind(const QV4::CompiledData::Binding *binding)
537{
538 switch (binding->type()) {
539 case QV4::CompiledData::Binding::Type_Script:
540 return BindingKind::Script;
541 case QV4::CompiledData::Binding::Type_Translation:
542 case QV4::CompiledData::Binding::Type_TranslationById:
544 case QV4::CompiledData::Binding::Type_Number:
545 case QV4::CompiledData::Binding::Type_Boolean:
546 case QV4::CompiledData::Binding::Type_String:
547 case QV4::CompiledData::Binding::Type_Null:
549 default:
551 }
552}
553
554// A trivial diff is one we can apply without rebuilding any VME meta-object and without
555// adding or removing objects: only existing bindings and binding-expression bodies change. Such a
556// diff is patched in place (see patchInPlace()). Everything else falls back to rebuilding roots.
557static bool changeIsTrivial(const QV4::CompiledData::Change &change,
558 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
559 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
560{
561 using ChangeType = QV4::CompiledData::ChangeType;
562 switch (change.type) {
563 // Source-location-only changes have no runtime effect.
564 case ChangeType::AliasLocationChanged:
565 case ChangeType::BindingLocationChanged:
566 case ChangeType::EnumLocationChanged:
567 case ChangeType::FunctionLocationChanged:
568 case ChangeType::ImportLocationChanged:
569 case ChangeType::InlineComponentLocationChanged:
570 case ChangeType::ObjectLocationChanged:
571 case ChangeType::PropertyLocationChanged:
572 case ChangeType::RequiredPropertyExtraDataLocationChanged:
573 case ChangeType::SignalLocationChanged:
574
575 // Index-only changes where the element's own position moved but it carries no per-instance
576 // state addressed by that position: a binding is re-applied by its target property, an enum is
577 // resolved at compile time. Remapping the object to the new unit is enough; no rebuild needed.
578 case ChangeType::BindingIndexChanged:
579 case ChangeType::EnumIndexChanged:
580 case ChangeType::InlineComponentIndexChanged:
581 case ChangeType::RequiredPropertyExtraDataIndexChanged:
582
583 // A property's position is a coreIndex into the live instance's VME meta-object storage.
584 // Adding, removing or reordering a property reshapes that storage; remapProperties() relocates
585 // the per-instance state (and shifts trailing aliases/signals) before the unit remap re-points
586 // the property cache (see applyDiff()), so these patch in place. A property added or removed
587 // *with* an initializer also emits a BindingAdded/BindingRemoved hunk, which is not trivial, so
588 // the reload rebuilds anyway until those are supported. We don't have to do anything here in
589 // that case. Alias/Signal index changes carry state outside that storage (alias endpoint
590 // arrays, method-index-keyed connections) and still rebuild explicitly.
591 case ChangeType::PropertyAdded:
592 case ChangeType::PropertyIndexChanged:
593 case ChangeType::PropertyRemoved:
594
595 // Internal table changes that are byproducts of recompiling expressions. They are picked up
596 // when we translate functions and remap compilation units; no structure changes.
597 case ChangeType::UnitMetadataChanged:
598 case ChangeType::ConstantAdded:
599 case ChangeType::ConstantChanged:
600 case ChangeType::ConstantRemoved:
601 case ChangeType::StringDataAdded:
602 case ChangeType::StringDataChanged:
603 case ChangeType::StringDataRemoved:
604 case ChangeType::LookupAdded:
605 case ChangeType::LookupChanged:
606 case ChangeType::LookupRemoved:
607 case ChangeType::RegExpAdded:
608 case ChangeType::RegExpChanged:
609 case ChangeType::RegExpRemoved:
610 case ChangeType::ClassAdded:
611 case ChangeType::ClassChanged:
612 case ChangeType::ClassRemoved:
613 case ChangeType::BlockAdded:
614 case ChangeType::BlockChanged:
615 case ChangeType::BlockRemoved:
616 case ChangeType::TemplateObjectAdded:
617 case ChangeType::TemplateObjectChanged:
618 case ChangeType::TemplateObjectRemoved:
619 case ChangeType::JSClassAdded:
620 case ChangeType::JSClassChanged:
621 case ChangeType::JSClassRemoved:
622 case ChangeType::TranslationDataAdded:
623 case ChangeType::TranslationDataChanged:
624 case ChangeType::TranslationDataRemoved:
625 return true;
626 case ChangeType::FunctionAdded:
627 case ChangeType::FunctionChanged:
628 case ChangeType::FunctionIndexChanged:
629 case ChangeType::FunctionRemoved:
630 // Functions are matched by name across the reload (see the differ). Binding/handler
631 // expressions pick up their new function by name via refreshBindings(), VME methods via
632 // refreshVmeMethods() — both name-based, so an added/removed/reordered/changed function needs
633 // no rebuild. (A newly added function backs a newly added binding, applied separately.)
634 return true;
635 case ChangeType::BindingChanged: {
636 Q_ASSERT(change.objectIndex >= 0);
637 Q_ASSERT(change.objectIndex < oldUnit->objectCount());
638 Q_ASSERT(change.objectIndex < newUnit->objectCount());
639
640 const QV4::CompiledData::Object *oldObj = oldUnit->objectAt(change.objectIndex);
641 Q_ASSERT(change.oldIndex < oldObj->nBindings);
642 const QV4::CompiledData::Binding *oldBinding = oldObj->bindingTable() + change.oldIndex;
643
644 const QV4::CompiledData::Object *newObj = newUnit->objectAt(change.objectIndex);
645 Q_ASSERT(change.newIndex < newObj->nBindings);
646 const QV4::CompiledData::Binding *newBinding = newObj->bindingTable() + change.newIndex;
647
648 const BindingKind oldKind = bindingKind(oldBinding);
649 const BindingKind newKind = bindingKind(newBinding);
650 if (oldKind == BindingKind::Unpatchable || newKind == BindingKind::Unpatchable)
651 return false;
652
653 // We cannot swap a binding for one of a different kind, yet: turning a literal into a
654 // script or translation binding (or vice versa) would have to install or drop a live
655 // binding, and we cannot recompile a script binding into a translation binding either.
656 // TODO: Fix this.
657 if (oldKind != newKind)
658 return false;
659
660 // A BindingChanged at a stable index can also mean the binding was moved to a different
661 // target property (its propertyNameIndex changed). In-place patching cannot relocate a
662 // binding: it would have to remove it from the old property and install it on the new one.
663 // That is structural, so fall back to the rebuild path.
664
665 return BindingPatchContext::targetPropertyName(oldUnit, change.objectIndex, oldBinding)
666 == BindingPatchContext::targetPropertyName(newUnit, change.objectIndex, newBinding);
667 }
668 case ChangeType::BindingAdded:
669 case ChangeType::BindingRemoved: {
670 // Installing/removing a binding in place is possible for plain value and script bindings
671 // whose target is a direct property of a real object. A binding on a value-type group or
672 // attached sub-object (which has no inherited type, hence no live QObject at its own index)
673 // still rebuilds; applyBindingChanges() drives the rest, respecting cross-level precedence.
674 const bool added = change.type == ChangeType::BindingAdded;
675 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit = added ? newUnit : oldUnit;
676 const quint32 bindingIndex = added ? change.newIndex : change.oldIndex;
677
678 Q_ASSERT(change.objectIndex >= 0 && change.objectIndex < unit->objectCount());
679 const QV4::CompiledData::Object *obj = unit->objectAt(change.objectIndex);
680 Q_ASSERT(bindingIndex < obj->nBindings);
681
682 if (bindingKind(obj->bindingTable() + bindingIndex) == BindingKind::Unpatchable)
683 return false;
684
685 // Group/attached sub-objects have no inherited type.
686 return unit->resolvedType(obj->inheritedTypeNameIndex) != nullptr;
687 }
688 default:
689 // ObjectAdded/Removed/Changed, Property/Signal/Alias/Enum content changes, Signal/Alias/
690 // Function Added/Removed, Import/InlineComponent/RequiredPropertyExtraData changes: structural,
691 // need a rebuild.
692 return false;
693 }
694}
695
696static bool isTrivialDiff(const QV4::CompiledData::CompilationUnitDiff &diff,
697 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
698 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
699{
700 return std::all_of(diff.changes.cbegin(), diff.changes.cend(),
701 [&](const QV4::CompiledData::Change &change) {
702 return changeIsTrivial(change, oldUnit, newUnit);
703 });
704}
705
706// Re-apply changed literal bindings. A literal value is written only if the property still holds
707// the value the old unit assigned; anything else is an external override we must leave untouched.
708// Changed script bindings carry no literal value and are handled by function translation instead.
709//
710// The binding may live on a value-type group sub-object (font.pixelSize) or an attached object
711// (Keys.enabled), which has no standalone QObject at its compilation-unit index. We use
712// BindingPatchContext to map every compilation-unit object index reachable from a live instance to
713// the owning QObject and the property-name prefix to address it.
714static void patchConstantBindings(const std::vector<QObject *> &objects,
715 const QV4::CompiledData::CompilationUnitDiff &diff,
716 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
717 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
718{
719 for (QObject *object : objects) {
720 // Skip half-dead objects, mirroring rebuildObject(): an invalidated context means the
721 // object is on its way out and must not be patched.
722 QQmlData *ddata = QQmlData::get(object);
723 if (!ddata->context || !ddata->outerContext || !ddata->outerContext->isValid()
724 || ddata->isQueuedForDeletion) {
725 continue;
726 }
727
728 QVarLengthArray<BindingPatchContext, 4> contexts;
729 for (int index : objectIndices(object, oldUnit))
730 contexts.append(BindingPatchContext(object, oldUnit, index));
731
732 for (const QV4::CompiledData::Change &change : diff.changes) {
733 if (change.type != QV4::CompiledData::ChangeType::BindingChanged)
734 continue;
735
736 for (BindingPatchContext &context : contexts) {
737 if (context.applyBindingChange(newUnit, change))
738 break;
739 }
740 }
741 }
742}
743
744// Point every live object that still references oldUnit at newUnit, in both its ddata and its
745// VME meta-object chain. Used after in-place patching, which keeps all object indices stable.
746//
747// The ddata compilation unit and the VME chain are remapped independently: a composite-type
748// instance can carry oldUnit in its VME chain while its ddata->compilationUnit is a different
749// executable unit (the "topmost", possibly inaddressible, instantiation).
750static void remapObjectsToNewUnit(const std::vector<QObject *> &objects,
751 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
752 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
753{
754 for (QObject *object : objects) {
755 QQmlData *ddata = QQmlData::get(object);
756
757 // A QQmlComponent (e.g. an explicit or implicit `delegate:`) caches the compilation unit it
758 // instantiates from. Remap it so future create() calls produce instances from newUnit.
759 if (QQmlComponent *component = qobject_cast<QQmlComponent *>(object)) {
760 QQmlComponentPrivate *cp = QQmlComponentPrivate::get(component);
761 if (cp->compilationUnit() == oldUnit)
762 cp->setCompilationUnit(newUnit);
763 }
764
765 // Remap the ddata compilation unit if it points at oldUnit. An object whose index is out
766 // of range in the new CU is obsolete (it belonged to the old type definition); leave its
767 // ddata alone and let the VME nulling below retire it.
768 if (ddata->compilationUnit == oldUnit && ddata->cuObjectIndex < newUnit->objectCount())
769 ddata->compilationUnit = newUnit;
770
771 if (!ddata->hasVMEMetaObject)
772 continue;
773
774 // Remap (or retire) every VME meta-object in the chain that points at oldUnit.
775 auto *mainVme = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
776 for (QQmlVMEMetaObject *vmeMeta = mainVme; vmeMeta;
777 vmeMeta = vmeMeta->parentVMEMetaObject()) {
778 if (vmeMeta->compilationUnit() != oldUnit)
779 continue;
780 if (vmeMeta->qmlObjectId() < newUnit->objectCount()) {
781 vmeMeta->setCompilationUnit(newUnit);
782
783 // Give the live instance the reloaded type's identity. The instance must present
784 // newUnit's property cache.
785 const QQmlPropertyCache::ConstPtr newCache =
786 newUnit->propertyCachesPtr()->at(vmeMeta->qmlObjectId());
787 if (newCache) {
788 vmeMeta->setPropertyCache(newCache);
789 if (vmeMeta == mainVme)
790 ddata->propertyCache = newCache;
791 }
792 } else {
793 // Obsolete: null it so stale alias lookups (triggered by refreshBindings) safely
794 // return nullptr from findCompiledObject() instead of asserting.
795 vmeMeta->setCompilationUnit(nullptr);
796 }
797 }
798 }
799}
800
801// Returns true if the object is instantiated as a component root of the old unit that we can
802// rebuild in place (see canRebuildComponentRootInPlace()), or as a component root of a different,
803// unchanged unit. The latter is rebuilt against its own unit. Otherwise, for example if the object
804// is not a component root at all or not even created from QML, return false.
806 QObject *object,
807 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
808 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
809{
810 bool foundComponentRoot = false;
811 for (int index : objectIndices(object, oldUnit)) {
812 if (index >= oldUnit->objectCount() || !isComponentRoot(oldUnit, index))
813 continue;
814 if (!canRebuildComponentRootInPlace(oldUnit, newUnit, index))
815 return false;
816 foundComponentRoot = true;
817 }
818
819 if (foundComponentRoot)
820 return true;
821
822 const QQmlData *ddata = QQmlData::get(object);
823 return ddata && ddata->compilationUnit && ddata->compilationUnit != oldUnit
824 && isComponentRoot(ddata->compilationUnit, ddata->cuObjectIndex);
825}
826
827// A component root whose non-composite base type changed or that carries deferred bindings cannot
828// be rebuilt in place. Instead we travel the scope hierarchy upwards to the enclosing component
829// root that instantiates it and rebuild that: its reset() + repopulateBindings() recreates the
830// problematic object from scratch, as a fresh QObject. If that enclosing root is problematic, too,
831// we keep travelling up. Returns the object to rebuild, or nullptr if we reach the root scope
832// without finding a rebuildable enclosing root.
834 QObject *object,
835 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
836 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
837{
838 Q_ASSERT(object);
839
840 const QQmlData *ddata = QQmlData::get(object);
841 if (!ddata)
842 return nullptr;
843
844 // Walk up the context hierarchy. The root of a component instance is the context object of
845 // its own context. Contexts may also have no context object or one that is not a component
846 // root at all (e.g. a delegate model's item). If we reach the root context, we give up.
847 for (QQmlRefPointer<QQmlContextData> context(ddata->outerContext); context;
848 context = context->parent()) {
849 QObject *outer = context->contextObject();
850 if (outer && outer != object && isRebuildableComponentRoot(outer, oldUnit, newUnit))
851 return outer;
852 }
853
854 return nullptr;
855}
856
857// TODO: This is dangerous. We are manipulating compilation units exposed to multiple
858// engines on potentially multiple threads.
860 QV4::ExecutionEngine *engine,
861 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &oldUnit,
862 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &newUnit)
863{
864 const auto units = engine->compilationUnits();
865 for (const auto &cu : units) {
866 for (auto *typeRef : std::as_const(cu->baseCompilationUnit()->resolvedTypes)) {
867 if (typeRef->isSelfReference())
868 continue;
869 if (typeRef->compilationUnit() != oldUnit)
870 continue;
871
872 typeRef->setCompilationUnit(newUnit);
873
874 QQmlPropertyCache::ConstPtr newCache;
875 const QQmlType type = typeRef->type();
876 if (type.isInlineComponent()) {
877 if (const int icId = newUnit->inlineComponentId(type.elementName()); icId >= 0)
878 newCache = newUnit->propertyCaches.at(icId);
879 } else {
880 newCache = newUnit->rootPropertyCache();
881 }
882
883 if (!newCache)
884 continue;
885
886 const QQmlPropertyCache::ConstPtr oldCache = typeRef->typePropertyCache();
887 typeRef->setTypePropertyCache(newCache);
888 relinkDerivedCaches(cu, oldCache, newCache);
889 }
890 }
891}
892
893// Re-create the VME method function objects from newUnit so that an object's methods follow the
894// unit remap, mirroring what refreshBindings() does for QQmlJavaScriptExpression-based bindings.
895static void refreshVmeMethods(const std::vector<QObject *> &objects,
896 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
897{
898 QV4::ExecutionEngine *v4 = newUnit->engine;
899 QV4::Scope scope(v4);
900 QV4::ScopedValue function(scope);
901
902 for (QObject *object : objects) {
903 QQmlData *ddata = QQmlData::get(object);
904 if (!ddata || !ddata->hasVMEMetaObject)
905 continue;
906
907 auto *mainVme = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
908 for (QQmlVMEMetaObject *vme = mainVme; vme; vme = vme->parentVMEMetaObject()) {
909 const auto cu = vme->compilationUnit();
910 if (cu != newUnit)
911 continue;
912
913 const QQmlRefPointer<QQmlContextData> context = vme->contextData();
914 if (!context)
915 continue;
916
917 const int objectIndex = vme->qmlObjectId();
918 if (objectIndex < 0 || objectIndex >= cu->objectCount())
919 continue;
920
921 const QV4::CompiledData::Object *obj = cu->objectAt(objectIndex);
922 const QQmlPropertyCache::ConstPtr cache = cu->propertyCachesPtr()->at(objectIndex);
923 if (!cache)
924 continue;
925
926 QV4::Scoped<QV4::QmlContext> qmlContext(
927 scope, QV4::QmlContext::create(v4->rootContext(), context, object));
928
929 const quint32_le *functionIdx = obj->functionOffsetTable();
930 for (quint32 i = 0; i < obj->nFunctions; ++i, ++functionIdx) {
931 QV4::Function *runtimeFunction = cu->runtimeFunctions[*functionIdx];
932 const QString name = runtimeFunction->name()->toQString();
933
934 const QQmlPropertyData *property = cache->property(name, object, context);
935 if (!property || !property->isVMEFunction())
936 continue;
937
938 // Mirror QQmlObjectCreator::setupFunctions(): generators need their own creator.
939 function = runtimeFunction->isGenerator()
940 ? QV4::GeneratorFunction::create(qmlContext, runtimeFunction)
941 : QV4::FunctionObject::createScriptFunction(qmlContext, runtimeFunction);
942 mainVme->setVmeMethod(property->coreIndex(), function);
943 }
944 }
945 }
946}
947
948// Apply property adds/removes/reorders to live instances by reshaping their VME meta-object storage,
949// before remapObjectsToNewUnit() re-points them at the reshaped property cache. Each affected object
950// index yields an old->new local-property map (oldToNew, -1 for removed) plus the new property count;
951// these are handed to the VME level in each instance that carries that object index.
957
958static void reshapeProperties(const std::vector<QObject *> &objects,
959 const QV4::CompiledData::CompilationUnitDiff &diff,
960 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
961 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
962{
963 using ChangeType = QV4::CompiledData::ChangeType;
964
965 QHash<int, PropertyReshape> reshapes;
966 const auto ensure = [&](int objectIndex) -> PropertyReshape & {
967 auto it = reshapes.find(objectIndex);
968 if (it != reshapes.end())
969 return *it;
970
971 Q_ASSERT(objectIndex >= 0 && objectIndex < oldUnit->objectCount());
972 const int oldCount = oldUnit->objectAt(objectIndex)->nProperties;
973 const int newCount = objectIndex < newUnit->objectCount()
974 ? newUnit->objectAt(objectIndex)->nProperties
975 : oldCount;
976 PropertyReshape reshape;
977 reshape.newPropertyCount = newCount;
978 reshape.oldToNew.resize(oldCount);
979 for (int i = 0; i < oldCount; ++i)
980 reshape.oldToNew[i] = i;
981 return *reshapes.insert(objectIndex, reshape);
982 };
983
984 for (const QV4::CompiledData::Change &change : diff.changes) {
985 switch (change.type) {
986 case ChangeType::PropertyIndexChanged: {
987 PropertyReshape &reshape = ensure(change.objectIndex);
988 Q_ASSERT(change.oldIndex < quint32(reshape.oldToNew.size()));
989 Q_ASSERT(change.newIndex < quint32(reshape.newPropertyCount));
990 reshape.oldToNew[change.oldIndex] = int(change.newIndex);
991 break;
992 }
993 case ChangeType::PropertyRemoved: {
994 // For a Removed change oldIndex holds the old position.
995 PropertyReshape &reshape = ensure(change.objectIndex);
996 Q_ASSERT(change.oldIndex < quint32(reshape.oldToNew.size()));
997 reshape.oldToNew[change.oldIndex] = -1;
998 break;
999 }
1000 case ChangeType::PropertyAdded:
1001 // The new property occupies a slot not named by oldToNew; ensuring the reshape exists
1002 // makes remapProperties() grow the storage.
1003 ensure(change.objectIndex);
1004 break;
1005 default:
1006 break;
1007 }
1008 }
1009
1010 if (reshapes.isEmpty())
1011 return;
1012
1013 for (QObject *object : objects) {
1014 QQmlData *ddata = QQmlData::get(object);
1015 if (!ddata || !ddata->hasVMEMetaObject)
1016 continue;
1017
1018 for (QQmlVMEMetaObject *vme =
1019 static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
1020 vme; vme = vme->parentVMEMetaObject()) {
1021 if (vme->compilationUnit() != oldUnit)
1022 continue;
1023 const auto it = reshapes.constFind(vme->qmlObjectId());
1024 if (it != reshapes.constEnd())
1025 vme->remapProperties(it->newPropertyCount, it->oldToNew);
1026 }
1027 }
1028}
1029
1030// A property add/remove changes oldUnit's property count, which shifts the coreIndex/signal indices
1031// of everything declared after it. When oldUnit contains a composite *base* type of a live derived
1032// instance, the members the derived levels own shift too, and the derived types' property caches
1033// must be rebased onto the new base layout. remapProperties() already shifts the per-instance state
1034// across the whole chain; this detects that the derived caches additionally need relinking.
1035//
1036// It doesn't matter whether the base type is the main type of oldUnit or an inline component. In
1037// either case vme->compilationUnit() == oldUnit will hold.
1038static bool
1039propertyCountChangeAffectsDerived(const QV4::CompiledData::CompilationUnitDiff &diff,
1040 const std::vector<QObject *> &objects,
1041 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit)
1042{
1043 using ChangeType = QV4::CompiledData::ChangeType;
1044 const bool countChanges = std::any_of(diff.changes.cbegin(), diff.changes.cend(),
1045 [](const QV4::CompiledData::Change &change) {
1046 return change.type == ChangeType::PropertyAdded
1047 || change.type == ChangeType::PropertyRemoved;
1048 });
1049 if (!countChanges)
1050 return false;
1051
1052 for (QObject *object : objects) {
1053 QQmlData *ddata = QQmlData::get(object);
1054 if (!ddata || !ddata->hasVMEMetaObject)
1055 continue;
1056 auto *mainVme = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
1057 for (QQmlVMEMetaObject *vme = mainVme->parentVMEMetaObject(); vme;
1058 vme = vme->parentVMEMetaObject()) {
1059 if (vme->compilationUnit() == oldUnit)
1060 return true;
1061 }
1062 }
1063 return false;
1064}
1065
1066// After redirectResolvedTypeReferences() has rebased the derived types' caches in their compilation
1067// units, give each live instance the refreshed cache for every VME level, so a derived level adopts
1068// the cache rebased onto the changed base's new layout.
1069static void relinkDerivedInstanceCaches(const std::vector<QObject *> &objects)
1070{
1071 for (QObject *object : objects) {
1072 QQmlData *ddata = QQmlData::get(object);
1073 if (!ddata || !ddata->hasVMEMetaObject)
1074 continue;
1075
1076 auto *mainVme = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(object)->metaObject);
1077 for (QQmlVMEMetaObject *vme = mainVme; vme; vme = vme->parentVMEMetaObject()) {
1078 const auto cu = vme->compilationUnit();
1079 if (!cu)
1080 continue;
1081 const QQmlPropertyCacheVector *caches = cu->propertyCachesPtr();
1082 if (vme->qmlObjectId() < 0 || vme->qmlObjectId() >= caches->count())
1083 continue;
1084 const QQmlPropertyCache::ConstPtr cache = caches->at(vme->qmlObjectId());
1085 if (cache && cache != vme->propertyCache()) {
1086 vme->setPropertyCache(cache);
1087 if (vme == mainVme)
1088 ddata->propertyCache = cache;
1089 }
1090 }
1091 }
1092}
1093
1094// The composite levels of a live instance, most-derived first, each with the context it lives in.
1095// Like collectCompositeLevels() but for an already-created instance: the base levels' contexts are
1096// read from the instance's linked-context chain (matched by type compilation unit) instead of being
1097// created. Used to reason about binding precedence across type levels.
1098static std::vector<CompositeLevel>
1100 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1101 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
1102{
1103 QQmlData *ddata = QQmlData::get(object);
1104 CompositeLevel instanceLevel{ ddata->compilationUnit, ddata->compilationUnit,
1105 ddata->cuObjectIndex, QString(),
1106 QQmlRefPointer<QQmlContextData>(ddata->context) };
1107
1108 std::vector<CompositeLevel> levels;
1109 levels.push_back(instanceLevel);
1110 for (const CompositeLevel &base : collectCompositeLevels(instanceLevel, oldUnit, newUnit))
1111 levels.push_back(base);
1112
1113 for (CompositeLevel &level : levels) {
1114 if (level.context)
1115 continue;
1116 for (QQmlRefPointer<QQmlContextData> ctx(ddata->context); ctx; ctx = ctx->linkedContext()) {
1117 const auto typeUnit = ctx->typeCompilationUnit();
1118 if (typeUnit && level.newCu
1119 && typeUnit->baseCompilationUnit() == level.newCu->baseCompilationUnit()) {
1120 level.context = ctx;
1121 break;
1122 }
1123 }
1124 }
1125 return levels;
1126}
1127
1128// Index of the (patchable) binding on the object at objectIndex in cu that targets property `name`,
1129// or -1 if none binds it.
1130static int bindingIndexForProperty(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &cu,
1131 int objectIndex, const QString &name)
1132{
1133 if (objectIndex < 0 || objectIndex >= cu->objectCount())
1134 return -1;
1135 const QV4::CompiledData::Object *obj = cu->objectAt(objectIndex);
1136 const QV4::CompiledData::Binding *bindings = obj->bindingTable();
1137 for (quint32 i = 0; i < obj->nBindings; ++i) {
1138 if (bindingKind(bindings + i) != BindingKind::Unpatchable
1139 && BindingPatchContext::targetPropertyName(cu, objectIndex, bindings + i) == name) {
1140 return int(i);
1141 }
1142 }
1143 return -1;
1144}
1145
1146// Install, on the live instance, the binding at bindingIndex of the object at level.objectIndex in
1147// level.newCu that targets property `name`. A literal is written directly (no context needed); a
1148// script binding goes through the object creator so precedence/override with any lower-level binding
1149// is handled by the engine.
1150static void installBinding(QObject *object, const CompositeLevel &level, int bindingIndex,
1151 const QString &name)
1152{
1153 if (!level.newCu)
1154 return;
1155
1156 const QV4::CompiledData::Binding *binding =
1157 level.newCu->objectAt(level.objectIndex)->bindingTable() + bindingIndex;
1158 const QVariant literal = BindingPatchContext::literalValue(level.newCu, binding);
1159 if (literal.isValid()) {
1160 QQmlProperty(object, name).write(literal);
1161 return;
1162 }
1163
1164 if (!level.context)
1165 return;
1166 QQmlData *ddata = QQmlData::get(object);
1167 QQmlObjectCreator creator(level.context, level.newCu,
1168 QQmlRefPointer<QQmlContextData>(ddata->outerContext), level.icName,
1169 nullptr);
1170 if (creator.applyLateBinding(level.objectIndex, object, level.context, bindingIndex)) {
1171 QQmlInstantiationInterrupt interrupt;
1172 creator.finalize(interrupt);
1173 }
1174}
1175
1177
1178static const QV4::CompiledData::Binding *bindingAt(const CuRef &unit, int objectIndex,
1179 quint32 bindingIndex)
1180{
1181 return unit->objectAt(objectIndex)->bindingTable() + bindingIndex;
1182}
1183
1184static bool isScriptBinding(const QV4::CompiledData::Binding *b)
1185{
1186 return b->type() == QV4::CompiledData::Binding::Type_Script;
1187}
1188
1189// Collect, per changed object index, the set of target property names whose effective binding
1190// must be recomputed.
1192 const QV4::CompiledData::CompilationUnitDiff &diff, const CuRef &oldUnit,
1193 const CuRef &newUnit)
1194{
1195 using ChangeType = QV4::CompiledData::ChangeType;
1196
1197 QHash<int, QSet<QString>> affected;
1198 for (const QV4::CompiledData::Change &change : diff.changes) {
1199 QString name;
1200 switch (change.type) {
1201 case ChangeType::BindingAdded:
1202 name = BindingPatchContext::targetPropertyName(
1203 newUnit, change.objectIndex,
1204 bindingAt(newUnit, change.objectIndex, change.newIndex));
1205 break;
1206 case ChangeType::BindingRemoved:
1207 name = BindingPatchContext::targetPropertyName(
1208 oldUnit, change.objectIndex,
1209 bindingAt(oldUnit, change.objectIndex, change.oldIndex));
1210 break;
1211 case ChangeType::BindingChanged:
1212 // Only a literal<->script flip needs (re)installing here; a same-kind change is handled
1213 // by patchConstantBindings (literal) or refreshBindings (script).
1214 if (isScriptBinding(bindingAt(newUnit, change.objectIndex, change.newIndex))
1215 != isScriptBinding(bindingAt(oldUnit, change.objectIndex, change.oldIndex))) {
1216 name = BindingPatchContext::targetPropertyName(
1217 newUnit, change.objectIndex,
1218 bindingAt(newUnit, change.objectIndex, change.newIndex));
1219 }
1220 break;
1221 default:
1222 break;
1223 }
1224 if (!name.isEmpty())
1225 affected[change.objectIndex].insert(name);
1226 }
1227 return affected;
1228}
1229
1230// Index into levels of the level that was reloaded (now on newUnit) and owns the change to the
1231// object at changedObjectIndex, or -1 if this instance doesn't carry the changed level.
1232static int reloadedLevelIndex(const std::vector<CompositeLevel> &levels, int changedObjectIndex,
1233 const CuRef &newUnit)
1234{
1235 const auto baseOf = [](const CuRef &unit) {
1236 return unit ? unit->baseCompilationUnit() : nullptr;
1237 };
1238 for (int i = 0, end = int(levels.size()); i < end; ++i) {
1239 if (levels[i].objectIndex == changedObjectIndex
1240 && baseOf(levels[i].newCu) == newUnit->baseCompilationUnit()) {
1241 return i;
1242 }
1243 }
1244 return -1;
1245}
1246
1247// Whether a level more derived than `reloaded` binds the property, and thus keeps overriding it.
1248static bool isOverriddenByDerivedLevel(const std::vector<CompositeLevel> &levels, int reloaded,
1249 const QString &name)
1250{
1251 return std::any_of(levels.begin(), levels.begin() + reloaded, [&](const CompositeLevel &level) {
1252 return bindingIndexForProperty(level.newCu, level.objectIndex, name) >= 0;
1253 });
1254}
1255
1256// Drop the current binding of the property, then (re)install the most-derived binding at or below
1257// the reloaded level, or reset the property if nothing binds it anymore.
1258static void reinstallProperty(QObject *object, const std::vector<CompositeLevel> &levels,
1259 int reloaded, const QString &name)
1260{
1261 const QQmlProperty prop(object, name);
1262 if (prop.isValid()) {
1263 QQmlPropertyPrivate::removeBinding(
1264 object, QQmlPropertyIndex(QQmlPropertyPrivate::get(prop)->core.coreIndex()));
1265 }
1266
1267 for (auto it = levels.begin() + reloaded, end = levels.end(); it != end; ++it) {
1268 const int bindingIndex = bindingIndexForProperty(it->newCu, it->objectIndex, name);
1269 if (bindingIndex >= 0) {
1270 installBinding(object, *it, bindingIndex, name);
1271 return;
1272 }
1273 }
1274
1275 if (!prop.isValid())
1276 return;
1277
1278 // Revert to what a freshly created instance without the initializer would show:
1279 // its reset value if resettable, otherwise the type's default.
1280 if (prop.isResettable())
1281 prop.reset();
1282 else
1283 prop.write(QVariant(prop.propertyMetaType()));
1284}
1285
1286// Apply binding structure changes in place, honoring cross-level precedence: only the highest-
1287// precedence (most-derived) binding of a property is live. Handles BindingAdded, BindingRemoved, and
1288// a BindingChanged that flips a property between a literal value and a script binding (a same-kind
1289// change is already covered by patchConstantBindings / refreshBindings). For each affected instance
1290// and target property, the effective binding is recomputed: if a more-derived level binds it the
1291// change is a no-op; otherwise the current binding is dropped and the most-derived binding at or
1292// below the reloaded level is (re)installed, or the property is reset if nothing binds it anymore.
1293static void applyBindingChanges(const std::vector<QObject *> &objects,
1294 const QV4::CompiledData::CompilationUnitDiff &diff,
1295 const CuRef &oldUnit, const CuRef &newUnit)
1296{
1297 const QHash<int, QSet<QString>> affected = collectAffectedProperties(diff, oldUnit, newUnit);
1298 if (affected.isEmpty())
1299 return;
1300
1301 for (QObject *object : objects) {
1302 QQmlData *ddata = QQmlData::get(object);
1303 if (!ddata || !ddata->context || !ddata->outerContext || !ddata->outerContext->isValid()
1304 || ddata->isQueuedForDeletion) {
1305 continue;
1306 }
1307
1308 const std::vector<CompositeLevel> levels =
1309 instanceCompositeLevels(object, oldUnit, newUnit);
1310
1311 for (auto it = affected.constBegin(), end = affected.constEnd(); it != end; ++it) {
1312 const int reloaded = reloadedLevelIndex(levels, it.key(), newUnit);
1313 if (reloaded < 0)
1314 continue;
1315
1316 for (const QString &name : it.value()) {
1317 if (!isOverriddenByDerivedLevel(levels, reloaded, name))
1318 reinstallProperty(object, levels, reloaded, name);
1319 }
1320 }
1321 }
1322}
1323
1324PatchResult applyDiff(std::vector<QObject *> &objects,
1325 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1326 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
1327{
1328 // Ensure the new CU's runtime data (strings, lookups, functions) is populated.
1329 if (!newUnit->runtimeStrings)
1330 newUnit->populate();
1331
1332 // Fast path: if only existing bindings changed -- nothing structural about any VME
1333 // meta-object, and no objects added or removed -- patch the affected objects in place. We
1334 // leave every object and its VME meta-object where it is, re-apply the changed literal
1335 // bindings (patchConstantBindings), point the objects at newUnit (remapObjectsToNewUnit), and
1336 // let the in-place refreshBindings() translate the still-live expressions' functions to newUnit
1337 // by name (so an added/removed/reordered function doesn't misalign the survivors).
1338 const QV4::CompiledData::CompilationUnitDiff diff =
1339 QV4::CompiledData::diffCompilationUnits(oldUnit->unitData(), newUnit->unitData());
1340 if (diff.success && isTrivialDiff(diff, oldUnit, newUnit)) {
1341 patchConstantBindings(objects, diff, oldUnit, newUnit);
1342 // Reshape per-instance property state (add/remove/reorder) to the new layout while the
1343 // objects still present the old cache, so remapObjectsToNewUnit()'s re-point lines up with
1344 // the resized storage.
1345 reshapeProperties(objects, diff, oldUnit, newUnit);
1346
1347 // A property count change on a composite base type shifts the members its derived types
1348 // inherit. Rebase the derived types' caches onto the new base layout (relinking their
1349 // compilation units), then give the live derived instances those refreshed caches.
1350 const bool derivedAffected = propertyCountChangeAffectsDerived(diff, objects, oldUnit);
1351 if (derivedAffected) {
1352 redirectResolvedTypeReferences(newUnit->engine, oldUnit->baseCompilationUnit(),
1353 newUnit->baseCompilationUnit());
1354 }
1355 remapObjectsToNewUnit(objects, oldUnit, newUnit);
1356 if (derivedAffected)
1357 relinkDerivedInstanceCaches(objects);
1358 refreshVmeMethods(objects, newUnit);
1359 // Install/remove added/removed bindings (post-remap: the objects present newUnit's caches
1360 // and their methods are refreshed, so a new binding's expression resolves correctly).
1361 applyBindingChanges(objects, diff, oldUnit, newUnit);
1363 }
1364
1365 // Otherwise we rebuild whole component roots: the document root (index 0) and any
1366 // inline-component roots. We don't touch other objects directly. Rebuilding a root runs a
1367 // full reset() + repopulateBindings() that cascades down the entire instantiation (and
1368 // through its composite base levels), recreating every object below it. Since every
1369 // non-root object is, by construction, a descendant of a component root, this is
1370 // sufficient. A component root that cannot be rebuilt in place - its C++ base type changed, or
1371 // it carries deferred bindings (see below) - is handled by rebuilding the enclosing component
1372 // root instead, which may live in a different compilation unit and recreates the root as a
1373 // fresh object (of the new C++ class, with its deferred bindings armed and executed).
1374 //
1375 // A consequence worth remembering: because every live object is recreated, every
1376 // QQmlJavaScriptExpression on those objects is recreated too, freshly bound to newUnit. By
1377 // the time refreshBindings() runs, the only expressions still referencing oldUnit are the
1378 // dead, detached leftovers of the resets. That is why the rebuild path's refreshBindings()
1379 // can simply disable (null) them instead of remapping them to newUnit.
1380 // A rebuild target carries the compilation units to rebuild it with. Usually that's oldUnit ->
1381 // newUnit. But when we travel up to an enclosing component root in a *different* compilation
1382 // unit (because a root's base type changed), that unit is itself unchanged: we rebuild it
1383 // against itself (a full re-instantiation of its children) after redirecting the resolved type
1384 // references, so the problematic object is recreated with its new C++ base type.
1385 std::vector<RebuildTarget> rebuild;
1386 QSet<QObject *> rebuildSet;
1387 bool redirectedTypes = false;
1388
1389 const auto addRebuild = [&](QObject *object, int index,
1390 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldCu,
1391 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newCu) {
1392 if (rebuildSet.contains(object))
1393 return;
1394 rebuildSet.insert(object);
1395 rebuild.push_back({ object, index, oldCu, newCu });
1396 };
1397
1398 for (QObject *object : objects) {
1399 const QVarLengthArray<int, 4> indices = objectIndices(object, oldUnit);
1400 for (int index : indices) {
1401 // Objects instantiated by an enclosing component (indices beyond the CU) are recreated
1402 // when that enclosing root is rebuilt, so we don't record them here. Neither do we
1403 // record any other objects but component roots. Instances of inner components are not
1404 // recreated by rebuilding the enclosing component. Therefore, we record their roots.
1405 if (index >= oldUnit->objectCount() || !isComponentRoot(oldUnit, index))
1406 continue;
1407
1408 // A component root whose own non-composite base type changed, which carries deferred
1409 // bidnings, or which isn't a component root anymore cannot be rebuilt in place. Rebuild
1410 // the enclosing component root instead, so that the object is recreated from scratch
1411 // as an instance of the new C++ class with new deferred bindings. A surviving instance
1412 // of an inner component stays on oldUnit then. Travelling up the scope hierarchy this
1413 // way only fails once we hit the root scope.
1414 if (!canRebuildComponentRootInPlace(oldUnit, newUnit, index)) {
1415 QObject *outer = outerRebuildTarget(object, oldUnit, newUnit);
1416 if (!outer)
1417 return PatchResult::Failed;
1418
1419 // The enclosing root may recreate the object from a different compilation unit
1420 // (the object was instantiated as an external type). Point that unit at newUnit
1421 // first, so the recreated object gets the new definition.
1422 if (!redirectedTypes) {
1423 redirectResolvedTypeReferences(newUnit->engine, oldUnit->baseCompilationUnit(),
1424 newUnit->baseCompilationUnit());
1425 redirectedTypes = true;
1426 }
1427
1428 // Rebuild the enclosing root against its own compilation unit (or newUnit, if that
1429 // root lives in oldUnit itself, e.g. an inline component). The unchanged outer unit
1430 // re-instantiates its children, recreating the problematic object.
1431 const QQmlRefPointer<QV4::ExecutableCompilationUnit> outerOld =
1432 QQmlData::get(outer)->compilationUnit;
1433 const QQmlRefPointer<QV4::ExecutableCompilationUnit> outerNew =
1434 outerOld->baseCompilationUnit() == oldUnit->baseCompilationUnit()
1435 ? newUnit
1436 : outerOld;
1437 addRebuild(outer, QQmlData::get(outer)->cuObjectIndex, outerOld, outerNew);
1438 break;
1439 }
1440
1441 addRebuild(object, indices.first(), oldUnit, newUnit);
1442 break;
1443 }
1444 }
1445
1446 // Sort by ascending cuIndex so that parent objects are rebuilt before their
1447 // children. A parent's reset() properly retires children (they still point to
1448 // oldUnit) and repopulateBindings recreates them. This avoids leaking orphaned
1449 // children and naturally handles cases where the context needs more ID slots.
1450 std::sort(rebuild.begin(), rebuild.end(),
1451 [](const RebuildTarget &a, const RebuildTarget &b) { return a.index < b.index; });
1452
1453 // Pre-compute which objects to skip: if an ancestor is also in the rebuild
1454 // list, the child will be properly retired and recreated during the ancestor's
1455 // rebuild. Rebuilding it individually would be wasted work on a stale pointer.
1456 QSet<QObject *> skip;
1457 for (const RebuildTarget &target : rebuild) {
1458 const QObjectList &children = target.object->children();
1459 for (QObject *child : children)
1460 skip.insert(child);
1461 }
1462
1463 ReplacedContexts replacedContexts;
1464 for (const RebuildTarget &target : rebuild) {
1465 if (skip.contains(target.object))
1466 continue;
1467
1468 rebuildObject(target.object, target.index, target.oldCu, target.newCu, &replacedContexts);
1469 }
1470
1471 // Everything lexically inside the rebuilt components has been recreated or rebuilt now. Whatever
1472 // was instantiated by oldUnit and survived without being rebuilt is obsolete. We cannot remap it
1473 // to newUnit by index since indices may have shifted. Retire it.
1474 for (QObject *object : objects) {
1475 const QQmlData *ddata = QQmlData::get(object);
1476 if (ddata->compilationUnit != oldUnit || ddata->isQueuedForDeletion)
1477 continue;
1478 BindingPatchContext::clearBindingsRecursive(object);
1479 BindingPatchContext::retireObject(object);
1480 }
1481
1482 return PatchResult::Rebuilt;
1483}
1484
1485// Map each of oldUnit's runtime functions to its newUnit counterpart, matched by name. Function
1486// names are only unique within an object (a method "foo"; a binding's "expression for <property>"),
1487// so matching is done per object index (positional; valid because a trivial diff neither adds nor
1488// removes objects) with an ordinal to disambiguate same-named functions within an object. This
1489// tolerates a function being added/removed/reordered in the table: an unchanged expression still
1490// finds its function even though the raw index moved.
1491static QHash<QV4::Function *, QV4::Function *>
1492buildFunctionTranslationMap(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1493 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
1494{
1495 const auto collect = [](const QQmlRefPointer<QV4::ExecutableCompilationUnit> &unit,
1496 int objectIndex) {
1497 QHash<QString, int> keyToGlobalIndex;
1498 QHash<QString, int> seen;
1499 const QV4::CompiledData::Unit *data = unit->unitData();
1500 const QV4::CompiledData::Object *obj = data->qmlUnit()->objectAt(objectIndex);
1501 const auto add = [&](quint32 globalIndex) {
1502 const QString name = data->stringAtInternal(data->functionAt(globalIndex)->nameIndex);
1503 keyToGlobalIndex.insert(name + QChar(QChar::Null) + QString::number(seen[name]++),
1504 int(globalIndex));
1505 };
1506 const quint32_le *methods = obj->functionOffsetTable();
1507 for (quint32 m = 0; m < obj->nFunctions; ++m)
1508 add(methods[m]);
1509 const QV4::CompiledData::Binding *bindings = obj->bindingTable();
1510 for (quint32 b = 0; b < obj->nBindings; ++b) {
1511 if (bindings[b].type() == QV4::CompiledData::Binding::Type_Script)
1512 add(bindings[b].value.compiledScriptIndex);
1513 }
1514 return keyToGlobalIndex;
1515 };
1516
1517 QHash<QV4::Function *, QV4::Function *> map;
1518 const int count = std::min(oldUnit->objectCount(), newUnit->objectCount());
1519 for (int o = 0; o < count; ++o) {
1520 const QHash<QString, int> oldKeys = collect(oldUnit, o);
1521 const QHash<QString, int> newKeys = collect(newUnit, o);
1522 for (auto it = oldKeys.constBegin(), end = oldKeys.constEnd(); it != end; ++it) {
1523 const auto newIt = newKeys.constFind(it.key());
1524 if (newIt == newKeys.constEnd())
1525 continue;
1526 QV4::Function *oldFunction = oldUnit->runtimeFunctions.value(it.value());
1527 QV4::Function *newFunction = newUnit->runtimeFunctions.value(newIt.value());
1528 if (oldFunction && newFunction)
1529 map.insert(oldFunction, newFunction);
1530 }
1531 }
1532 return map;
1533}
1534
1535// Translate a still-live expression's function from oldUnit to its name-matched counterpart in
1536// newUnit (see buildFunctionTranslationMap), or clear it if there is no newUnit. Expressions whose
1537// function does not point into oldUnit are left untouched.
1538// Returns true if the resulting function is valid afterwards (and should be re-evaluated).
1539static bool
1540translateExpressionFunction(QQmlJavaScriptExpression *expr,
1541 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1542 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit,
1543 const QHash<QV4::Function *, QV4::Function *> &functionMap)
1544{
1545 QV4::Function *f = expr->function();
1546 if (!f)
1547 return false;
1548
1549 if (f->executableCompilationUnit() != oldUnit.data())
1550 return true;
1551
1552 if (!newUnit) {
1553 expr->setFunction(nullptr);
1554 return false;
1555 }
1556
1557 if (QV4::Function *mapped = functionMap.value(f)) {
1558 expr->setFunction(mapped);
1559 } else {
1560 // Not reached from an object's methods/bindings (e.g. a nested closure). Fall back to the
1561 // same index, which is correct as long as the function table didn't shift around it.
1562 const qsizetype index = oldUnit->runtimeFunctions.indexOf(f);
1563 if (index >= 0 && index < newUnit->runtimeFunctions.size())
1564 expr->setFunction(newUnit->runtimeFunctions[index]);
1565 }
1566
1567 return true;
1568}
1569
1571 const QQmlRefPointer<QQmlContextData> &context,
1572 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1573 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit,
1574 const QHash<QV4::Function *, QV4::Function *> &functionMap)
1575{
1576 for (auto child = context->childContexts(); child; child = child->nextChild())
1577 translateAndRefreshExpressionsRecursive(child, oldUnit, newUnit, functionMap);
1578
1579 for (auto *expr = context->expressions(); expr; expr = expr->nextExpression()) {
1580 if (translateExpressionFunction(expr, oldUnit, newUnit, functionMap))
1581 expr->refresh();
1582 }
1583}
1584
1585void refreshBindings(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &oldUnit,
1586 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &newUnit)
1587{
1588 const QHash<QV4::Function *, QV4::Function *> functionMap = newUnit
1589 ? buildFunctionTranslationMap(oldUnit, newUnit)
1590 : QHash<QV4::Function *, QV4::Function *>();
1591 translateAndRefreshExpressionsRecursive(
1592 QQmlContextData::get(oldUnit->engine->qmlEngine()->rootContext()), oldUnit, newUnit,
1593 functionMap);
1594}
1595
1596} // namespace QQmlPreview
1597
1598QT_END_NAMESPACE
static void applyBindingChanges(const std::vector< QObject * > &objects, const QV4::CompiledData::CompilationUnitDiff &diff, const CuRef &oldUnit, const CuRef &newUnit)
static void installBinding(QObject *object, const CompositeLevel &level, int bindingIndex, const QString &name)
static void reshapeProperties(const std::vector< QObject * > &objects, const QV4::CompiledData::CompilationUnitDiff &diff, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static void relinkDerivedInstanceCaches(const std::vector< QObject * > &objects)
static std::vector< CompositeLevel > collectCompositeLevels(const CompositeLevel &instanceLevel, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static bool translateExpressionFunction(QQmlJavaScriptExpression *expr, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit, const QHash< QV4::Function *, QV4::Function * > &functionMap)
static bool isScriptBinding(const QV4::CompiledData::Binding *b)
static void refreshVmeMethods(const std::vector< QObject * > &objects, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static QHash< int, QSet< QString > > collectAffectedProperties(const QV4::CompiledData::CompilationUnitDiff &diff, const CuRef &oldUnit, const CuRef &newUnit)
void refreshBindings(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static QHash< QV4::Function *, QV4::Function * > buildFunctionTranslationMap(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static void rebuildObject(QObject *object, int cuIndex, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit, ReplacedContexts *replacedContexts)
static void refreshBindingPropertyData(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &cu, int objectIndex, const QQmlPropertyCache::ConstPtr &cache)
static bool propertyCountChangeAffectsDerived(const QV4::CompiledData::CompilationUnitDiff &diff, const std::vector< QObject * > &objects, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit)
static bool canRebuildComponentRootInPlace(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit, int objectIndex)
static bool isRebuildableComponentRoot(QObject *object, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static bool hasChangedNonCompositeBaseType(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit, int objectIndex)
static QVarLengthArray< int, 4 > objectIndices(QObject *object, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit)
static std::vector< CompositeLevel > instanceCompositeLevels(QObject *object, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static QQmlPropertyCache::ConstPtr relinkCache(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &cu, int objectIndex, const QQmlPropertyCache::ConstPtr &actualParent)
static void relinkDerivedCaches(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &cu, const QQmlPropertyCache::ConstPtr &oldBaseCache, const QQmlPropertyCache::ConstPtr &newBaseCache)
static BindingKind bindingKind(const QV4::CompiledData::Binding *binding)
void redirectResolvedTypeReferences(QV4::ExecutionEngine *engine, const QQmlRefPointer< QV4::CompiledData::CompilationUnit > &oldUnit, const QQmlRefPointer< QV4::CompiledData::CompilationUnit > &newUnit)
PatchResult applyDiff(std::vector< QObject * > &objects, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static bool isComponentRoot(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &unit, int objectIndex)
static bool isTrivialDiff(const QV4::CompiledData::CompilationUnitDiff &diff, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static bool isOverriddenByDerivedLevel(const std::vector< CompositeLevel > &levels, int reloaded, const QString &name)
static void reinstallProperty(QObject *object, const std::vector< CompositeLevel > &levels, int reloaded, const QString &name)
static QQmlRefPointer< QQmlContextData > currentContext(const QQmlRefPointer< QQmlContextData > &context, const ReplacedContexts &replacedContexts)
static QQmlPropertyCache::ConstPtr nonCompositeBaseType(const QQmlPropertyCache::ConstPtr &propertyCache)
static void translateAndRefreshExpressionsRecursive(const QQmlRefPointer< QQmlContextData > &context, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit, const QHash< QV4::Function *, QV4::Function * > &functionMap)
static const QV4::CompiledData::Binding * bindingAt(const CuRef &unit, int objectIndex, quint32 bindingIndex)
static bool changeIsTrivial(const QV4::CompiledData::Change &change, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static int reloadedLevelIndex(const std::vector< CompositeLevel > &levels, int changedObjectIndex, const CuRef &newUnit)
static int bindingIndexForProperty(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &cu, int objectIndex, const QString &name)
static QObject * outerRebuildTarget(QObject *object, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static void patchConstantBindings(const std::vector< QObject * > &objects, const QV4::CompiledData::CompilationUnitDiff &diff, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
static QVarLengthArray< QQmlRefPointer< QQmlContextData >, 4 > levelContexts(QObject *object, const QQmlRefPointer< QQmlContextData > &outerContext)
static void remapObjectsToNewUnit(const std::vector< QObject * > &objects, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &oldUnit, const QQmlRefPointer< QV4::ExecutableCompilationUnit > &newUnit)
Combined button and popup list for selecting options.
QQmlRefPointer< QV4::ExecutableCompilationUnit > newCu
QQmlRefPointer< QV4::ExecutableCompilationUnit > oldCu
QQmlRefPointer< QQmlContextData > newContext
QQmlRefPointer< QQmlContextData > oldContext