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
qqmlvmemetaobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 BasysKom GmbH.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant
5
7
8#include <private/qqmlabstractbinding_p.h>
9#include <private/qqmlrefcount_p.h>
11#include <qqmlinfo.h>
12
13#include <private/qqmlglobal_p.h>
14
15#include <private/qqmlpropertycachecreator_p.h>
16#include <private/qqmlpropertycachemethodarguments_p.h>
17#include <private/qqmlvaluetypewrapper_p.h>
18#include <private/qv4functionobject_p.h>
19#include <private/qv4jscall_p.h>
20#include <private/qv4object_p.h>
21#include <private/qv4qobjectwrapper_p.h>
22#include <private/qv4runtime_p.h>
23#include <private/qv4scopedvalue_p.h>
24#include <private/qv4sequenceobject_p.h>
25#include <private/qv4variantassociationobject_p.h>
26#include <private/qv4variantobject_p.h>
27
28#include <QtCore/qmetasequence.h>
29#include <QtCore/qvarlengtharray.h>
30
31#include <climits> // for CHAR_BIT
32
33QT_BEGIN_NAMESPACE
34
35QQmlVMEResolvedList::QQmlVMEResolvedList(QQmlListProperty<QObject> *prop)
36{
37 // see QQmlVMEMetaObject::metaCall for how this was constructed
38 auto encodedIndex = quintptr(prop->data);
39 constexpr quintptr usableBits = sizeof(quintptr) * CHAR_BIT;
40 quintptr inheritanceDepth = encodedIndex >> (usableBits / 2);
41 m_id = encodedIndex & ((quintptr(1) << (usableBits / 2)) - 1);
42
43 // walk up to the correct meta object if necessary
44 auto mo = static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(prop->object)->metaObject);
45 while (inheritanceDepth--)
46 mo = mo->parentVMEMetaObject();
47 m_metaObject = mo;
48 Q_ASSERT(m_metaObject);
49 Q_ASSERT(::strstr(m_metaObject->toDynamicMetaObject(prop->object)
50 ->property(m_metaObject->propOffset() + m_id)
51 .typeName(),
52 "QQmlListProperty"));
53 Q_ASSERT(m_metaObject->object.data() == prop->object);
54
55 // readPropertyAsList() with checks transformed into Q_ASSERT
56 // and without allocation.
57 if (m_metaObject->m_propertyAndMethodStorage.isUndefined()
58 && m_metaObject->m_propertyAndMethodStorage.valueRef()) {
59 return;
60 }
61
62 if (auto *md = static_cast<QV4::MemberData *>(
63 m_metaObject->m_propertyAndMethodStorage.asManaged())) {
64 const QV4::Value *v = md->data() + m_id;
65 Q_ASSERT(v->as<QV4::Object>());
66 m_list = static_cast<QV4::Heap::Object *>(v->heapObject());
67 Q_ASSERT(m_list);
68 }
69}
70
71void QQmlVMEResolvedList::append(QObject *o) const
72{
73 Q_ASSERT(isValid());
74
75 QV4::Scope scope(m_list->internalClass->engine);
76 QV4::Heap::ArrayData *arrayData = m_list->arrayData;
77
78 const uint length = arrayData->length();
79 if (Q_UNLIKELY(length == std::numeric_limits<uint>::max())) {
80 scope.engine->throwRangeError(QLatin1String("Too many elements."));
81 return;
82 }
83
84 QV4::ScopedObject object(scope, m_list);
85 QV4::ArrayData::realloc(object, QV4::Heap::ArrayData::Simple, length + 1, false);
86 QV4::ScopedValue wrappedObject(scope, QV4::QObjectWrapper::wrap(scope.engine, o));
87 arrayData->vtable()->put(
88 object, length, wrappedObject);
89}
90
91QObject *QQmlVMEResolvedList::at(qsizetype i) const
92{
93 Q_ASSERT(isValid());
94
95 QV4::Scope scope(m_list->internalClass->engine);
96 QV4::Scoped<QV4::QObjectWrapper> result(scope, m_list->arrayData->get(i));
97 return result ? result->object() : nullptr;
98}
99
100void QQmlVMEResolvedList::replace(qsizetype i, QObject *o) const
101{
102 Q_ASSERT(isValid());
103
104 QV4::Scope scope(m_list->internalClass->engine);
105 QV4::ScopedObject object(scope, m_list);
106 QV4::ScopedValue wrappedObject(scope, QV4::QObjectWrapper::wrap(scope.engine, o));
107 m_list->arrayData->vtable()->put(object, i, wrappedObject);
108}
109
111
113{
114 m_metaObject->activate(m_metaObject->object.data(), int(m_id), nullptr);
115}
116
117void QQmlVMEMetaObject::list_append(QQmlListProperty<QObject> *prop, QObject *o)
118{
119 const QQmlVMEResolvedList resolved(prop);
120 if (!resolved.isValid())
121 return;
122 resolved.append(o);
123 resolved.activateSignal();
124}
125
126void QQmlVMEMetaObject::list_append_nosignal(QQmlListProperty<QObject> *prop, QObject *o)
127{
128 const QQmlVMEResolvedList resolved(prop);
129 if (!resolved.isValid())
130 return;
131 resolved.append(o);
132}
133
134static qsizetype list_count(QQmlListProperty<QObject> *prop)
135{
136 const QQmlVMEResolvedList resolved(prop);
137 if (!resolved.isValid())
138 return 0;
139 return resolved.size();
140}
141
142static QObject *list_at(QQmlListProperty<QObject> *prop, qsizetype index)
143{
144 const QQmlVMEResolvedList resolved(prop);
145 if (!resolved.isValid())
146 return nullptr;
147 return resolved.at(index);
148}
149
150void QQmlVMEMetaObject::list_clear(QQmlListProperty<QObject> *prop)
151{
152 const QQmlVMEResolvedList resolved(prop);
153 if (!resolved.isValid())
154 return;
155 resolved.clear();
156 resolved.activateSignal();
157}
158
159void QQmlVMEMetaObject::list_clear_nosignal(QQmlListProperty<QObject> *prop)
160{
161 const QQmlVMEResolvedList resolved(prop);
162 if (!resolved.isValid())
163 return;
164 resolved.clear();
165}
166
167static void list_replace(QQmlListProperty<QObject> *prop, qsizetype index, QObject *o)
168{
169 const QQmlVMEResolvedList resolved(prop);
170 if (!resolved.isValid())
171 return;
172 resolved.replace(index, o);
173 resolved.activateSignal();
174}
175
176static void list_removeLast(QQmlListProperty<QObject> *prop)
177{
178 const QQmlVMEResolvedList resolved(prop);
179 if (!resolved.isValid())
180 return;
181 resolved.removeLast();
182 resolved.activateSignal();
183}
184
189
190void QQmlVMEVariantQObjectPtr::objectDestroyedImpl(QQmlGuardImpl *guard)
191{
192 auto This = static_cast<QQmlVMEVariantQObjectPtr *>(guard);
193 if (!This->m_target || QQmlData::wasDeleted(This->m_target->object.data()))
194 return;
195
196 if (This->m_index >= 0) {
197 QV4::ExecutionEngine *v4 = This->m_target->m_propertyAndMethodStorage.engine();
198 if (v4) {
199 QV4::Scope scope(v4);
200 QV4::Scoped<QV4::MemberData> sp(scope, This->m_target->m_propertyAndMethodStorage.value());
201 if (sp) {
202 QV4::PropertyIndex index{ sp->d(), sp->d()->values.values + This->m_index };
203 index.set(v4, QV4::Value::nullValue());
204 }
205 }
206
207 This->m_target->activate(This->m_target->object.data(), This->m_index, nullptr);
208 }
209}
210
211void QQmlVMEVariantQObjectPtr::setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
212{
213 m_target = target;
214 m_index = index;
215 setObject(obj);
216}
217
231
236
237void QQmlVMEMetaObjectEndpoint_callback(QQmlNotifierEndpoint *e, void **)
238{
240 vmee->tryConnect();
241}
242
244{
245 int aliasId = this - metaObject->m_aliasEndpoints;
246
247 if (metaObject.tag() == EndPointIsConnected) {
248 // This is actually notify. Aliases are after regular properties in the MetaObject.
249 // So add propCount() to get the signal index.
250 int sigIdx = aliasId + metaObject->propCount();
251 metaObject->activate(metaObject->object.data(), sigIdx, nullptr);
252 } else if (metaObject->findCompiledObject()) {
253 const QQmlPropertyData *aliasProperty
254 = metaObject->cache->property(metaObject->aliasOffset() + aliasId);
255 const int targetPropertyIndex = aliasProperty ? aliasProperty->aliasTarget() : -1;
256
257 if (targetPropertyIndex != -1) {
258 QQmlRefPointer<QQmlContextData> ctxt = metaObject->m_ctxt;
259 QObject *target = ctxt->idValue(aliasProperty->aliasTargetObjectId());
260 if (!target)
261 return;
262
263 QQmlPropertyIndex encodedIndex = QQmlPropertyIndex::fromEncoded(targetPropertyIndex);
264 int coreIndex = encodedIndex.coreIndex();
265 int valueTypeIndex = encodedIndex.valueTypeIndex();
266 const QQmlPropertyData *pd = QQmlData::ensurePropertyCache(target)->property(coreIndex);
267 if (pd && valueTypeIndex != -1 && !QQmlMetaType::valueType(pd->propType())) {
268 // deep alias
269 const QQmlPropertyCache::ConstPtr newPropertyCache
270 = QQmlMetaType::propertyCacheForType(pd->propType());
271 void *argv[1] = { &target };
272 QMetaObject::metacall(target, QMetaObject::ReadProperty, coreIndex, argv);
273 if (!target)
274 return;
275 Q_ASSERT(newPropertyCache);
276 pd = newPropertyCache->property(valueTypeIndex);
277 }
278 if (!pd)
279 return;
280
281 if (pd->notifyIndex() != -1 && ctxt->engine())
282 QQmlData::connectEndpoint(this, target, pd->notifyIndex(), ctxt->engine());
283 }
284
285 metaObject.setTag(EndPointIsConnected);
286 }
287}
288
289
290QQmlInterceptorMetaObject::QQmlInterceptorMetaObject(QObject *obj, const QQmlPropertyCache::ConstPtr &cache)
291 : object(obj),
292 cache(cache)
293{
294 QObjectPrivate *op = QObjectPrivate::get(obj);
295
296 if (op->metaObject) {
297 parent = op->metaObject;
298 // Use the extra flag in QBiPointer to know if we can safely cast parent.asT1() to QQmlVMEMetaObject*
299 parent.setFlagValue(QQmlData::get(obj)->hasVMEMetaObject);
300 } else {
301 parent = obj->metaObject();
302 }
303
304 op->metaObject = this;
305 QQmlData::get(obj)->hasInterceptorMetaObject = true;
306}
307
308QQmlInterceptorMetaObject::~QQmlInterceptorMetaObject()
309{
310
311}
312
313static bool propertyIndicesConflict(QQmlPropertyIndex a, QQmlPropertyIndex b)
314{
315 if (a.coreIndex() != b.coreIndex())
316 return false;
317
318 if (!a.hasValueTypeIndex() || !b.hasValueTypeIndex())
319 return true;
320
321 return a.valueTypeIndex() == b.valueTypeIndex();
322}
323
324void QQmlInterceptorMetaObject::registerInterceptor(QQmlPropertyIndex index, QQmlPropertyValueInterceptor *interceptor)
325{
326 for (QQmlPropertyValueInterceptor *vi = interceptors; vi; vi = vi->m_next) {
327 if (Q_UNLIKELY(propertyIndicesConflict(vi->m_propertyIndex, index))) {
328 qWarning() << "Attempting to set another interceptor on"
329 << object->metaObject()->className() << "property"
330 << object->metaObject()->property(index.coreIndex()).name()
331 << "- unsupported";
332 }
333 }
334
335 interceptor->m_propertyIndex = index;
336 interceptor->m_next = interceptors;
337 interceptors = interceptor;
338}
339
340/*!
341 * \internal
342 *
343 * Rewrite (or drop) each interceptor's target coreIndex when the property
344 * layout of a VME level changes: \a coreBase is that level's property offset.
345 * A coreIndex inside the level's property block
346 * [\a{coreBase}, \a{coreBase} + oldToNew.size()) maps through oldToNew (-1
347 * drops the interceptor), while anything at or beyond it (the level's aliases
348 * and the members of derived levels) shifts by delta.
349 *
350 * Used by the QML preview's in-place patch.
351 */
352void QQmlInterceptorMetaObject::remapInterceptorPropertyIndices(int coreBase,
353 const QList<int> &oldToNew,
354 int delta)
355{
356 const int oldPropCount = oldToNew.size();
357 QQmlPropertyValueInterceptor **link = &interceptors;
358 while (QQmlPropertyValueInterceptor *vi = *link) {
359 const int local = vi->m_propertyIndex.coreIndex() - coreBase;
360 int newCore;
361 if (local < 0) {
362 newCore = vi->m_propertyIndex.coreIndex(); // below the changed level: unchanged
363 } else if (local < oldPropCount) {
364 const bool dropInterceptor = (oldToNew[local] == -1);
365 newCore = dropInterceptor ? -1 : coreBase + oldToNew[local];
366 } else {
367 // This level's aliases and the members of derived levels.
368 newCore = vi->m_propertyIndex.coreIndex() + delta;
369 }
370
371 if (newCore == vi->m_propertyIndex.coreIndex()) {
372 link = &vi->m_next;
373 continue;
374 }
375 if (newCore < 0) {
376 // The intercepted property was removed: unlink the interceptor.
377 *link = vi->m_next;
378 continue;
379 }
380 vi->m_propertyIndex = QQmlPropertyIndex(newCore, vi->m_propertyIndex.valueTypeIndex());
381 link = &vi->m_next;
382 }
383}
384
385int QQmlInterceptorMetaObject::metaCall(QObject *o, QMetaObject::Call c, int id, void **a)
386{
387 Q_ASSERT(o == object.data());
388 Q_UNUSED(o);
389
390 if (intercept(c, id, a))
391 return -1;
392 return object->qt_metacall(c, id, a);
393}
394
395bool QQmlInterceptorMetaObject::doIntercept(QMetaObject::Call c, int id, void **a)
396{
397 for (QQmlPropertyValueInterceptor *vi = interceptors; vi; vi = vi->m_next) {
398 if (vi->m_propertyIndex.coreIndex() != id)
399 continue;
400
401 const int valueIndex = vi->m_propertyIndex.valueTypeIndex();
402 const QQmlData *data = QQmlData::get(object.data());
403 const QMetaType metaType = data->propertyCache->property(id)->propType();
404
405 if (metaType.isValid()) {
406 if (valueIndex != -1 && c == QMetaObject::WriteProperty) {
407
408 // If we didn't intend to change the property this interceptor cares about,
409 // then don't bother intercepting it. There may be an animation running on
410 // the property. We shouldn't disturb it.
411 const int changedProperty
412 = (*static_cast<int *>(a[3]) & QQmlPropertyData::HasInternalIndex)
413 ? *static_cast<int *>(a[4])
414 : QV4::ReferenceObject::AllProperties;
415 if (changedProperty == QV4::ReferenceObject::AllProperties
416 || changedProperty == valueIndex) {
417 // TODO: handle intercepting bindable properties for value types?
418 QQmlGadgetPtrWrapper *valueType = QQmlGadgetPtrWrapper::instance(
419 data->context->engine(), metaType);
420 Q_ASSERT(valueType);
421
422 //
423 // Consider the following case:
424 // color c = { 0.1, 0.2, 0.3 }
425 // interceptor exists on c.r
426 // write { 0.2, 0.4, 0.6 }
427 //
428 // The interceptor may choose not to update the r component at this
429 // point (for example, a behavior that creates an animation). But we
430 // need to ensure that the g and b components are updated correctly.
431 //
432 // So we need to perform a full write where the value type is:
433 // r = old value, g = new value, b = new value
434 //
435 // And then call the interceptor which may or may not write the
436 // new value to the r component.
437 //
438 // This will ensure that the other components don't contain stale data
439 // and any relevant signals are emitted.
440 //
441 // To achieve this:
442 // (1) Store the new value type as a whole (needed due to
443 // aliasing between a[0] and static storage in value type).
444 // (2) Read the entire existing value type from object -> valueType temp.
445 // (3) Read the previous value of the component being changed
446 // from the valueType temp.
447 // (4) Write the entire new value type into the temp.
448 // (5) Overwrite the component being changed with the old value.
449 // (6) Perform a full write to the value type (which may emit signals etc).
450 // (7) Issue the interceptor call with the new component value.
451 //
452
453 QMetaProperty valueProp = valueType->property(valueIndex);
454 QVariant newValue(metaType, a[0]);
455
456 valueType->read(object.data(), id);
457 QVariant prevComponentValue = valueType->readOnGadget(valueProp);
458
459 valueType->setValue(newValue);
460 QVariant newComponentValue = valueType->readOnGadget(valueProp);
461
462 // If the intercepted value seemingly has not changed, we still need to
463 // invoke the interceptor. There may be a pending animation that will
464 // change the value soon. Such an animation needs to be canceled if the
465 // current value is explicitly set.
466 // So, we cannot return here if prevComponentValue == newComponentValue.
467 valueType->writeOnGadget(valueProp, std::move(prevComponentValue));
468 valueType->write(
469 object.data(), id,
470 QQmlPropertyData::DontRemoveBinding | QQmlPropertyData::BypassInterceptor,
471 QV4::ReferenceObject::AllProperties);
472
473 vi->write(newComponentValue);
474 return true;
475 }
476 } else if (c == QMetaObject::WriteProperty) {
477 vi->write(QVariant(metaType, a[0]));
478 return true;
479 } else {
480 object->qt_metacall(c, id, a);
481 QUntypedBindable target = *reinterpret_cast<QUntypedBindable *>(a[0]);
482 return vi->bindable(reinterpret_cast<QUntypedBindable *>(a[0]), target);
483 }
484 }
485 }
486
487 return false;
488}
489
490static const QMetaObject *stringCastMetaObject(QObject *o, const QMetaObject *top)
491{
492 for (const QMetaObject *mo = top; mo; mo = mo->superClass()) {
493 if (o->qt_metacast(mo->className()) != nullptr)
494 return mo;
495 }
496 return nullptr;
497}
498
499#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
500const QMetaObject *QQmlInterceptorMetaObject::toDynamicMetaObject(QObject *o) const
501#else
502QMetaObject *QQmlInterceptorMetaObject::toDynamicMetaObject(QObject *o)
503#endif
504{
505 if (!metaObject)
506 metaObject = cache->createMetaObject();
507
508 const QMetaObject *mo = nullptr;
509 if (Q_UNLIKELY(metaObject.tag() == MetaObjectInvalid))
510 mo = stringCastMetaObject(o, metaObject->superClass());
511 if (!mo)
512 mo = metaObject.data();
513
514#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
515 return mo;
516#else
517 return const_cast<QMetaObject *>(mo);
518#endif
519}
520
521QQmlVMEMetaObject::QQmlVMEMetaObject(
522 QV4::ExecutionEngine *engine, QObject *obj, const QQmlPropertyCache::ConstPtr &cache,
523 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &qmlCompilationUnit,
524 int qmlObjectId)
525 : QQmlInterceptorMetaObject(obj, cache)
526 , m_engine(engine)
527 , m_ctxt(QQmlData::get(obj, true)->outerContext)
528 , m_aliasEndpoints(nullptr)
529 , m_compilationUnit(qmlCompilationUnit)
530 , m_qmlObjectId(qmlObjectId)
531{
532 Q_ASSERT(engine);
533 QQmlData::get(obj)->hasVMEMetaObject = true;
534
535 if (const QV4::CompiledData::Object *compiledObject = findCompiledObject()) {
536 m_numAliases = compiledObject->nAliases;
537 if (const uint size = compiledObject->nProperties + compiledObject->nFunctions) {
538 QV4::Heap::MemberData *data = QV4::MemberData::allocate(engine, size);
539 // we only have a weak reference below; if the VMEMetaObject is already marked
540 // (triggered by the allocate call above)
541 // we therefore might never mark the member data; consequently, mark it now
542 QV4::WriteBarrier::markCustom(engine, [data](QV4::MarkStack *ms) {
543 data->mark(ms);
544 });
545 m_propertyAndMethodStorage.set(engine, data);
546 std::fill(data->values.values, data->values.values + data->values.size, QV4::Encode::undefined());
547
548 // Need JS wrapper to ensure properties/methods are marked.
549 ensureQObjectWrapper();
550 }
551
552 // Counts retrieved from the property cache should reflect the CompiledObject
553 Q_ASSERT(propCount() == compiledObject->propertyCount());
554 Q_ASSERT(aliasCount() == compiledObject->aliasCount());
555 Q_ASSERT(signalCount() == compiledObject->signalCount()
556 + compiledObject->propertyCount()
557 + compiledObject->aliasCount());
558 Q_ASSERT(methodCount() == compiledObject->functionCount());
559 }
560
561 // We rely on ordering of properties, alias, signals, and methods.
562 Q_ASSERT(aliasOffset() == propOffset() + propCount());
563 Q_ASSERT(methodOffset() == signalOffset() + signalCount());
564}
565
566QQmlVMEMetaObject::~QQmlVMEMetaObject()
567{
568 if (parent.isT1()) parent.asT1()->objectDestroyed(object.data());
569 delete [] m_aliasEndpoints;
570
571 qDeleteAll(m_varObjectGuards);
572}
573
574QV4::MemberData *QQmlVMEMetaObject::propertyAndMethodStorageAsMemberData() const
575{
576 if (m_propertyAndMethodStorage.isUndefined()) {
577 if (m_propertyAndMethodStorage.valueRef())
578 // in some situations, the QObject wrapper (and associated data,
579 // such as the varProperties array) will have been cleaned up, but the
580 // QObject ptr will not yet have been deleted (eg, waiting on deleteLater).
581 // In this situation, return 0.
582 return nullptr;
583 }
584
585 return static_cast<QV4::MemberData*>(m_propertyAndMethodStorage.asManaged());
586}
587
588void QQmlVMEMetaObject::writeProperty(int id, int v)
589{
590 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
591 if (md)
592 md->set(m_engine, id, QV4::Value::fromInt32(v));
593}
594
595void QQmlVMEMetaObject::writeProperty(int id, bool v)
596{
597 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
598 if (md)
599 md->set(m_engine, id, QV4::Value::fromBoolean(v));
600}
601
602void QQmlVMEMetaObject::writeProperty(int id, double v)
603{
604 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
605 if (md)
606 md->set(m_engine, id, QV4::Value::fromDouble(v));
607}
608
609void QQmlVMEMetaObject::writeProperty(int id, const QString& v)
610{
611 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
612 if (md) {
613 QV4::Scope scope(m_engine);
614 QV4::Scoped<QV4::MemberData>(scope, md)->set(m_engine, id, m_engine->newString(v));
615 }
616}
617
618void QQmlVMEMetaObject::writeProperty(int id, QObject* v)
619{
620 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
621 if (md) {
622 QV4::Scope scope(m_engine);
623 QV4::Scoped<QV4::MemberData>(scope, md)->set(m_engine, id, QV4::Value::fromReturnedValue(
624 QV4::QObjectWrapper::wrap(m_engine, v)));
625 }
626
627 QQmlVMEVariantQObjectPtr *guard = getQObjectGuardForProperty(id);
628 if (v && !guard) {
629 guard = new QQmlVMEVariantQObjectPtr();
630 m_varObjectGuards.append(guard);
631 }
632 if (guard)
633 guard->setGuardedValue(v, this, id);
634}
635
636int QQmlVMEMetaObject::readPropertyAsInt(int id) const
637{
638 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
639 if (!md)
640 return 0;
641
642 QV4::Scope scope(m_engine);
643 QV4::ScopedValue sv(scope, *(md->data() + id));
644 if (!sv->isInt32())
645 return 0;
646 return sv->integerValue();
647}
648
649bool QQmlVMEMetaObject::readPropertyAsBool(int id) const
650{
651 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
652 if (!md)
653 return false;
654
655 QV4::Scope scope(m_engine);
656 QV4::ScopedValue sv(scope, *(md->data() + id));
657 if (!sv->isBoolean())
658 return false;
659 return sv->booleanValue();
660}
661
662double QQmlVMEMetaObject::readPropertyAsDouble(int id) const
663{
664 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
665 if (!md)
666 return 0.0;
667
668 QV4::Scope scope(m_engine);
669 QV4::ScopedValue sv(scope, *(md->data() + id));
670 if (!sv->isDouble())
671 return 0.0;
672 return sv->doubleValue();
673}
674
675QString QQmlVMEMetaObject::readPropertyAsString(int id) const
676{
677 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
678 if (!md)
679 return QString();
680
681 QV4::Scope scope(m_engine);
682 QV4::ScopedValue sv(scope, *(md->data() + id));
683 if (QV4::String *s = sv->stringValue())
684 return s->toQString();
685 return QString();
686}
687
688QUrl QQmlVMEMetaObject::readPropertyAsUrl(int id) const
689{
690 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
691 if (!md)
692 return QUrl();
693
694 QV4::Scope scope(m_engine);
695 QV4::ScopedValue sv(scope, *(md->data() + id));
696 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
697 if (!v || v->d()->data().userType() != QMetaType::QUrl)
698 return QUrl();
699 return v->d()->data().value<QUrl>();
700}
701
702QDate QQmlVMEMetaObject::readPropertyAsDate(int id) const
703{
704 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
705 if (!md)
706 return QDate();
707
708 QV4::Scope scope(m_engine);
709 QV4::ScopedValue sv(scope, *(md->data() + id));
710 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
711 if (!v || v->d()->data().userType() != QMetaType::QDate)
712 return QDate();
713 return v->d()->data().value<QDate>();
714}
715
716QTime QQmlVMEMetaObject::readPropertyAsTime(int id) const
717{
718 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
719 if (!md)
720 return QTime();
721
722 QV4::Scope scope(m_engine);
723 QV4::ScopedValue sv(scope, *(md->data() + id));
724 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
725 if (!v || v->d()->data().userType() != QMetaType::QTime)
726 return QTime();
727 return v->d()->data().value<QTime>();
728}
729
730QDateTime QQmlVMEMetaObject::readPropertyAsDateTime(int id) const
731{
732 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
733 if (!md)
734 return QDateTime();
735
736 QV4::Scope scope(m_engine);
737 QV4::ScopedValue sv(scope, *(md->data() + id));
738 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
739 if (!v || v->d()->data().userType() != QMetaType::QDateTime)
740 return QDateTime();
741 return v->d()->data().value<QDateTime>();
742}
743
744#if QT_CONFIG(regularexpression)
745QRegularExpression QQmlVMEMetaObject::readPropertyAsRegularExpression(int id) const
746{
747 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
748 if (!md)
749 return QRegularExpression();
750
751 QV4::Scope scope(m_engine);
752 QV4::ScopedValue sv(scope, *(md->data() + id));
753 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
754 if (!v || v->d()->data().userType() != QMetaType::QRegularExpression)
755 return QRegularExpression();
756 return v->d()->data().value<QRegularExpression>();
757}
758#endif
759
760QSizeF QQmlVMEMetaObject::readPropertyAsSizeF(int id) const
761{
762 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
763 if (!md)
764 return QSizeF();
765
766 QV4::Scope scope(m_engine);
767 QV4::ScopedValue sv(scope, *(md->data() + id));
768 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
769 if (!v || v->d()->data().userType() != QMetaType::QSizeF)
770 return QSizeF();
771 return v->d()->data().value<QSizeF>();
772}
773
774QPointF QQmlVMEMetaObject::readPropertyAsPointF(int id) const
775{
776 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
777 if (!md)
778 return QPointF();
779
780 QV4::Scope scope(m_engine);
781 QV4::ScopedValue sv(scope, *(md->data() + id));
782 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
783 if (!v || v->d()->data().userType() != QMetaType::QPointF)
784 return QPointF();
785 return v->d()->data().value<QPointF>();
786}
787
788QObject* QQmlVMEMetaObject::readPropertyAsQObject(int id) const
789{
790 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
791 if (!md)
792 return nullptr;
793
794 QV4::Scope scope(m_engine);
795 QV4::ScopedValue sv(scope, *(md->data() + id));
796 const QV4::QObjectWrapper *wrapper = sv->as<QV4::QObjectWrapper>();
797 if (!wrapper)
798 return nullptr;
799 return wrapper->object();
800}
801
802void QQmlVMEMetaObject::initPropertyAsList(int id) const
803{
804 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
805 if (!md)
806 return;
807
808 QV4::Scope scope(m_engine);
809 QV4::ScopedObject v(scope, *(md->data() + id));
810 if (!v) {
811 v = m_engine->newObject();
812 v->arrayCreate();
813 md->set(m_engine, id, v);
814 }
815}
816
817QRectF QQmlVMEMetaObject::readPropertyAsRectF(int id) const
818{
819 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
820 if (!md)
821 return QRectF();
822
823 QV4::Scope scope(m_engine);
824 QV4::ScopedValue sv(scope, *(md->data() + id));
825 const QV4::VariantObject *v = sv->as<QV4::VariantObject>();
826 if (!v || v->d()->data().userType() != QMetaType::QRectF)
827 return QRectF();
828 return v->d()->data().value<QRectF>();
829}
830
831int QQmlVMEMetaObject::metaCall(QObject *o, QMetaObject::Call c, int _id, void **a)
832{
833 Q_ASSERT(o == object.data());
834 Q_UNUSED(o);
835
836 int id = _id;
837
838 if (intercept(c, _id, a))
839 return -1;
840
841 if (c == QMetaObject::ReadProperty || c == QMetaObject::WriteProperty || c == QMetaObject::ResetProperty || c == QMetaObject::BindableProperty) {
842 if (id >= propOffset()) {
843 id -= propOffset();
844
845 const QQmlPropertyData *propertyData = cache->property(_id);
846 if (!propertyData->isAlias()) {
847
848 // the context can be null if accessing var properties from cpp after re-parenting an item.
849 QQmlEnginePrivate *ep = (m_ctxt.isNull() || m_ctxt->engine() == nullptr)
850 ? nullptr
851 : QQmlEnginePrivate::get(m_ctxt->engine());
852
853 if (c == QMetaObject::ReadProperty) {
854 if (propertyData->isQList()) {
855 // _id because this is an absolute property ID.
856 const QQmlPropertyData *propertyData = cache->property(_id);
857 const QMetaType propType = propertyData->propType();
858
859 if (propType.flags().testFlag(QMetaType::IsQmlList)) {
860 if (!getListProperty(
861 id, static_cast<QQmlListProperty<QObject> *>(a[0]))) {
862 return -1;
863 }
864 } else if (QV4::MemberData *md = propertyAndMethodStorageAsMemberData()) {
865 // Value type list
866 QV4::Scope scope(m_engine);
867 QV4::Scoped<QV4::Sequence> sequence(scope, *(md->data() + id));
868 switch (QV4::SequencePrototype::getRawContainer(
869 sequence, a[0], propType)) {
870 case QV4::SequencePrototype::Copied:
871 case QV4::SequencePrototype::WasEqual:
872 break;
873 case QV4::SequencePrototype::TypeMismatch:
874 // sequence can be undefined, in which case this is an empty list
875 // by definition.
876 propType.destruct(a[0]);
877 propType.construct(a[0]);
878 break;
879 }
880 } else {
881 qmlWarning(object.data()) << "Cannot find member data";
882 }
883 } else {
884 const QV4::CompiledData::CommonType t
885 = QQmlPropertyCacheCreatorBase::propertyTypeForMetaType(
886 propertyData->propType());
887 switch (t) {
888 case QV4::CompiledData::CommonType::Void:
889 break;
890 case QV4::CompiledData::CommonType::Int:
891 *reinterpret_cast<int *>(a[0]) = readPropertyAsInt(id);
892 break;
893 case QV4::CompiledData::CommonType::Bool:
894 *reinterpret_cast<bool *>(a[0]) = readPropertyAsBool(id);
895 break;
896 case QV4::CompiledData::CommonType::Real:
897 *reinterpret_cast<double *>(a[0]) = readPropertyAsDouble(id);
898 break;
899 case QV4::CompiledData::CommonType::String:
900 *reinterpret_cast<QString *>(a[0]) = readPropertyAsString(id);
901 break;
902 case QV4::CompiledData::CommonType::Url:
903 *reinterpret_cast<QUrl *>(a[0]) = readPropertyAsUrl(id);
904 break;
905 case QV4::CompiledData::CommonType::Date:
906 *reinterpret_cast<QDate *>(a[0]) = readPropertyAsDate(id);
907 break;
908 case QV4::CompiledData::CommonType::DateTime:
909 *reinterpret_cast<QDateTime *>(a[0]) = readPropertyAsDateTime(id);
910 break;
911 case QV4::CompiledData::CommonType::RegExp:
912#if QT_CONFIG(regularexpression)
913 *reinterpret_cast<QRegularExpression *>(a[0])
914 = readPropertyAsRegularExpression(id);
915#endif
916 break;
917 case QV4::CompiledData::CommonType::Rect:
918 *reinterpret_cast<QRectF *>(a[0]) = readPropertyAsRectF(id);
919 break;
920 case QV4::CompiledData::CommonType::Size:
921 *reinterpret_cast<QSizeF *>(a[0]) = readPropertyAsSizeF(id);
922 break;
923 case QV4::CompiledData::CommonType::Point:
924 *reinterpret_cast<QPointF *>(a[0]) = readPropertyAsPointF(id);
925 break;
926 case QV4::CompiledData::CommonType::Time:
927 *reinterpret_cast<QTime *>(a[0]) = readPropertyAsTime(id);
928 break;
929 case QV4::CompiledData::CommonType::Var:
930 if (ep) {
931 *reinterpret_cast<QVariant *>(a[0]) = readPropertyAsVariant(id);
932 } else {
933 // if the context was disposed,
934 // we just return an invalid variant from read.
935 *reinterpret_cast<QVariant *>(a[0]) = QVariant();
936 }
937 break;
938 case QV4::CompiledData::CommonType::Invalid:
939 if (QV4::MemberData *md = propertyAndMethodStorageAsMemberData()) {
940 QV4::Scope scope(m_engine);
941 QV4::ScopedValue sv(scope, *(md->data() + id));
942
943 // _id because this is an absolute property ID.
944 const QQmlPropertyData *propertyData = cache->property(_id);
945
946 if (propertyData->isQObject()) {
947 if (const auto *wrap = sv->as<QV4::QObjectWrapper>())
948 *reinterpret_cast<QObject **>(a[0]) = wrap->object();
949 else
950 *reinterpret_cast<QObject **>(a[0]) = nullptr;
951 } else {
952 const QMetaType propType = propertyData->propType();
953 const void *data = nullptr;
954 if (const auto *v = sv->as<QV4::VariantObject>()) {
955 const QVariant &variant = v->d()->data();
956 if (variant.metaType() == propType)
957 data = variant.constData();
958 }
959 propType.destruct(a[0]);
960 propType.construct(a[0], data);
961 }
962 } else {
963 qmlWarning(object.data()) << "Cannot find member data";
964 }
965 }
966 }
967 } else if (c == QMetaObject::WriteProperty) {
968 bool needActivate = false;
969
970 if (propertyData->isQList()) {
971 const QMetaType propType = propertyData->propType();
972
973 if (propType.flags().testFlag(QMetaType::IsQmlList)) {
974 // Object list
975 QQmlListProperty<QObject> listProp;
976 if (!getListProperty(id, &listProp))
977 return -1;
978
979 QQmlListProperty<QObject> *input
980 = static_cast<QQmlListProperty<QObject> *>(a[0]);
981
982 // First check if we need to do anything at all. If the lists are
983 // the same we don't.
984 if (listProp.count(&listProp) != input->count(input)) {
985 needActivate = true;
986 } else {
987 for (qsizetype i = 0, end = input->count(input); i < end; ++i) {
988 if (listProp.at(&listProp, i) == input->at(input, i))
989 continue;
990 needActivate = true;
991 break;
992 }
993 }
994
995 // Then clear the property and re-fill it using the input list,
996 // without sending separate signals for each element. We're sending a
997 // summary signal below.
998 if (needActivate) {
999 QQmlVMEMetaObject::list_clear_nosignal(&listProp);
1000 for (qsizetype i = 0, end = input->count(input); i < end; ++i) {
1001 QQmlVMEMetaObject::list_append_nosignal(
1002 &listProp, input->at(input, i));
1003 }
1004 }
1005 } else if (QV4::MemberData *md = propertyAndMethodStorageAsMemberData()) {
1006 // Value type list
1007 QV4::Scope scope(m_engine);
1008 QV4::Scoped<QV4::Sequence> sequence(scope, *(md->data() + id));
1009 switch (QV4::SequencePrototype::setRawContainer(
1010 sequence, a[0], propType)) {
1011 case QV4::SequencePrototype::Copied:
1012 needActivate = true;
1013 break;
1014 case QV4::SequencePrototype::WasEqual:
1015 break;
1016 case QV4::SequencePrototype::TypeMismatch: {
1017 if (const QQmlType type = QQmlMetaType::qmlListType(propType);
1018 type.isSequentialContainer()) {
1019 sequence = QV4::SequencePrototype::fromData(
1020 m_engine, propType, type.listMetaSequence(), a[0]);
1021 } else if (QMetaSequence::Iterable iterable;
1022 QMetaType::convert(
1023 propType, a[0],
1024 QMetaType::fromType<QMetaSequence::Iterable>(),
1025 &iterable)) {
1026 sequence = QV4::SequencePrototype::fromData(
1027 m_engine, propType, iterable.metaContainer(), a[0]);
1028 } else {
1029 sequence = QV4::Encode::undefined();
1030 }
1031 md->set(m_engine, id, sequence);
1032 if (sequence->isUndefined()) {
1033 qmlWarning(object.data())
1034 << "Could not create a QML sequence object for "
1035 << propType.name();
1036 }
1037 needActivate = true;
1038 }
1039 break;
1040 }
1041 } else {
1042 qmlWarning(object.data()) << "Cannot find member data";
1043 }
1044 } else {
1045 const QV4::CompiledData::CommonType t
1046 = QQmlPropertyCacheCreatorBase::propertyTypeForMetaType(
1047 propertyData->propType());
1048 switch (t) {
1049 case QV4::CompiledData::CommonType::Void:
1050 break;
1051 case QV4::CompiledData::CommonType::Int:
1052 needActivate = *reinterpret_cast<int *>(a[0]) != readPropertyAsInt(id);
1053 writeProperty(id, *reinterpret_cast<int *>(a[0]));
1054 break;
1055 case QV4::CompiledData::CommonType::Bool:
1056 needActivate = *reinterpret_cast<bool *>(a[0]) != readPropertyAsBool(id);
1057 writeProperty(id, *reinterpret_cast<bool *>(a[0]));
1058 break;
1059 case QV4::CompiledData::CommonType::Real:
1060 needActivate = *reinterpret_cast<double *>(a[0]) != readPropertyAsDouble(id);
1061 writeProperty(id, *reinterpret_cast<double *>(a[0]));
1062 break;
1063 case QV4::CompiledData::CommonType::String:
1064 needActivate = *reinterpret_cast<QString *>(a[0]) != readPropertyAsString(id);
1065 writeProperty(id, *reinterpret_cast<QString *>(a[0]));
1066 break;
1067 case QV4::CompiledData::CommonType::Url:
1068 needActivate = *reinterpret_cast<QUrl *>(a[0]) != readPropertyAsUrl(id);
1069 writeProperty(id, *reinterpret_cast<QUrl *>(a[0]));
1070 break;
1071 case QV4::CompiledData::CommonType::Date:
1072 needActivate = *reinterpret_cast<QDate *>(a[0]) != readPropertyAsDate(id);
1073 writeProperty(id, *reinterpret_cast<QDate *>(a[0]));
1074 break;
1075 case QV4::CompiledData::CommonType::DateTime:
1076 needActivate = *reinterpret_cast<QDateTime *>(a[0]) != readPropertyAsDateTime(id);
1077 writeProperty(id, *reinterpret_cast<QDateTime *>(a[0]));
1078 break;
1079 case QV4::CompiledData::CommonType::RegExp:
1080#if QT_CONFIG(regularexpression)
1081 needActivate = *reinterpret_cast<QRegularExpression *>(a[0])
1082 != readPropertyAsRegularExpression(id);
1083 writeProperty(id, *reinterpret_cast<QRegularExpression *>(a[0]));
1084#endif
1085 break;
1086 case QV4::CompiledData::CommonType::Rect:
1087 needActivate = *reinterpret_cast<QRectF *>(a[0]) != readPropertyAsRectF(id);
1088 writeProperty(id, *reinterpret_cast<QRectF *>(a[0]));
1089 break;
1090 case QV4::CompiledData::CommonType::Size:
1091 needActivate = *reinterpret_cast<QSizeF *>(a[0]) != readPropertyAsSizeF(id);
1092 writeProperty(id, *reinterpret_cast<QSizeF *>(a[0]));
1093 break;
1094 case QV4::CompiledData::CommonType::Point:
1095 needActivate = *reinterpret_cast<QPointF *>(a[0]) != readPropertyAsPointF(id);
1096 writeProperty(id, *reinterpret_cast<QPointF *>(a[0]));
1097 break;
1098 case QV4::CompiledData::CommonType::Time:
1099 needActivate = *reinterpret_cast<QTime *>(a[0]) != readPropertyAsTime(id);
1100 writeProperty(id, *reinterpret_cast<QTime *>(a[0]));
1101 break;
1102 case QV4::CompiledData::CommonType::Var:
1103 if (ep)
1104 writeKnownVarProperty(id, *reinterpret_cast<QVariant *>(a[0]));
1105 break;
1106 case QV4::CompiledData::CommonType::Invalid:
1107 if (QV4::MemberData *md = propertyAndMethodStorageAsMemberData()) {
1108 QV4::Scope scope(m_engine);
1109 QV4::ScopedValue sv(scope, *(md->data() + id));
1110
1111 // _id because this is an absolute property ID.
1112 const QQmlPropertyData *propertyData = cache->property(_id);
1113
1114 if (propertyData->isQObject()) {
1115 QObject *arg = *reinterpret_cast<QObject **>(a[0]);
1116 if (const auto *wrap = sv->as<QV4::QObjectWrapper>())
1117 needActivate = wrap->object() != arg;
1118 else if (arg != nullptr || !sv->isNull())
1119 needActivate = true;
1120 if (needActivate)
1121 writeProperty(id, arg);
1122 } else {
1123 const QMetaType propType = propertyData->propType();
1124 if (const auto *v = sv->as<QV4::VariantObject>()) {
1125 QVariant &variant = v->d()->data();
1126 if (variant.metaType() != propType) {
1127 needActivate = true;
1128 variant = QVariant(propType, a[0]);
1129 } else if (!propType.equals(variant.constData(), a[0])) {
1130 needActivate = true;
1131 propType.destruct(variant.data());
1132 propType.construct(variant.data(), a[0]);
1133 }
1134 } else {
1135 needActivate = true;
1136 md->set(m_engine, id, m_engine->newVariantObject(
1137 propType, a[0]));
1138 }
1139 }
1140 } else {
1141 qmlWarning(object.data()) << "Cannot find member data";
1142 }
1143 }
1144 }
1145
1146 if (needActivate)
1147 activate(object.data(), id, nullptr);
1148 }
1149
1150 return -1;
1151 }
1152
1153 id -= propCount();
1154
1155 if (id < aliasCount()) {
1156 const QV4::CompiledData::Object *compiledObject = findCompiledObject();
1157 if (!compiledObject)
1158 return -1;
1159
1160 const QQmlPropertyData *aliasProperty = cache->property(aliasOffset() + id);
1161 if (c == QMetaObject::ReadProperty && aliasProperty && aliasProperty->isQObject())
1162 *reinterpret_cast<void **>(a[0]) = nullptr;
1163
1164 if (m_ctxt.isNull())
1165 return -1;
1166
1167 QObject *target = m_ctxt->idValue(
1168 aliasProperty ? aliasProperty->aliasTargetObjectId() : -1);
1169 if (!target)
1170 return -1;
1171
1172 connectAlias(compiledObject, id);
1173
1174 const int targetPropertyIndex = aliasProperty ? aliasProperty->aliasTarget() : -1;
1175
1176 if (targetPropertyIndex == -1) {
1177 *reinterpret_cast<QObject **>(a[0]) = target;
1178 return -1;
1179 }
1180
1181 QQmlData *targetDData = QQmlData::get(target, /*create*/false);
1182 if (!targetDData)
1183 return -1;
1184
1185 QQmlPropertyIndex encodedIndex
1186 = QQmlPropertyIndex::fromEncoded(targetPropertyIndex);
1187 int coreIndex = encodedIndex.coreIndex();
1188 const int valueTypePropertyIndex = encodedIndex.valueTypeIndex();
1189
1190 const auto removePendingBinding
1191 = [c, a](QObject *target, int coreIndex, QQmlPropertyIndex encodedIndex) {
1192 // Remove binding (if any) on write
1193 if (c == QMetaObject::WriteProperty) {
1194 int flags = *reinterpret_cast<int*>(a[3]);
1195 if (flags & QQmlPropertyData::RemoveBindingOnAliasWrite) {
1196 QQmlData *targetData = QQmlData::get(target);
1197 if (targetData && targetData->hasBindingBit(coreIndex))
1198 QQmlPropertyPrivate::removeBinding(target, encodedIndex);
1199 }
1200 }
1201 };
1202
1203 if (valueTypePropertyIndex != -1) {
1204 if (!targetDData->propertyCache)
1205 return -1;
1206 const QQmlPropertyData *pd = targetDData->propertyCache->property(coreIndex);
1207 // Value type property or deep alias
1208 QQmlGadgetPtrWrapper *valueType = QQmlGadgetPtrWrapper::instance(
1209 m_ctxt->engine(), pd->propType());
1210 if (valueType) {
1211
1212 // For value type aliases, the core property provides the bindable.
1213 if (c == QMetaObject::BindableProperty)
1214 return QMetaObject::metacall(target, c, coreIndex, a);
1215
1216 removePendingBinding(target, coreIndex, encodedIndex);
1217 valueType->read(target, coreIndex);
1218 int rv = QMetaObject::metacall(valueType, c, valueTypePropertyIndex, a);
1219
1220 if (c == QMetaObject::WriteProperty)
1221 valueType->write(target, coreIndex, QQmlPropertyData::HasInternalIndex,
1222 valueTypePropertyIndex);
1223
1224 return rv;
1225 } else {
1226 // deep alias
1227 void *argv[1] = { &target };
1228 QMetaObject::metacall(target, QMetaObject::ReadProperty, coreIndex, argv);
1229 removePendingBinding(
1230 target, valueTypePropertyIndex,
1231 QQmlPropertyIndex(valueTypePropertyIndex));
1232 return QMetaObject::metacall(target, c, valueTypePropertyIndex, a);
1233 }
1234
1235 } else {
1236 removePendingBinding(target, coreIndex, encodedIndex);
1237 return QMetaObject::metacall(target, c, coreIndex, a);
1238 }
1239
1240 }
1241 return -1;
1242
1243 }
1244
1245 } else if(c == QMetaObject::InvokeMetaMethod) {
1246
1247 if (id >= signalOffset()) {
1248
1249 id -= signalOffset();
1250 if (id < signalCount()) {
1251 activate(object.data(), id, a);
1252 return -1;
1253 }
1254
1255 id -= signalCount();
1256 if (id < methodCount()) {
1257 QQmlEngine *engine = m_ctxt->engine();
1258 if (!engine)
1259 return -1; // We can't run the method
1260
1261 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine);
1262 QV4::ExecutionEngine *v4 = engine->handle();
1263 ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.
1264 QV4::Scope scope(v4);
1265
1266
1267 QV4::Scoped<QV4::JavaScriptFunctionObject> function(scope, method(id));
1268 if (!function) {
1269 // The function was not compiled. There are some exceptional cases which the
1270 // expression rewriter does not rewrite properly (e.g., \r-terminated lines
1271 // are not rewritten correctly but this bug is deemed out-of-scope to fix for
1272 // performance reasons; see QTBUG-24064) and thus compilation will have failed.
1273 QQmlError e;
1274 e.setDescription(
1275 QStringLiteral(
1276 "Exception occurred during compilation of function: ")
1277 + QString::fromUtf8(metaObject->method(_id).methodSignature()));
1278 ep->warning(e);
1279 return -1; // The dynamic method with that id is not available.
1280 }
1281
1282 auto methodData = cache->method(_id);
1283 auto arguments = methodData->hasArguments() ? methodData->arguments() : nullptr;
1284
1285 if (arguments && arguments->names) {
1286 const quint32 parameterCount = arguments->names->size();
1287 Q_ASSERT(parameterCount == function->formalParameterCount());
1288 function->call(object.data(), a, arguments->types, parameterCount);
1289 } else {
1290 Q_ASSERT(function->formalParameterCount() == 0);
1291 const QMetaType returnType = methodData->propType();
1292 function->call(object.data(), a, &returnType, 0);
1293 }
1294
1295 if (scope.hasException()) {
1296 QQmlError error = scope.engine->catchExceptionAsQmlError();
1297 if (error.isValid())
1298 ep->warning(error);
1299 }
1300
1301 ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
1302 return -1;
1303 }
1304 return -1;
1305 }
1306 }
1307
1308 if (parent.isT1())
1309 return parent.asT1()->metaCall(object.data(), c, _id, a);
1310 else
1311 return object->qt_metacall(c, _id, a);
1312}
1313
1314bool QQmlVMEMetaObject::getListProperty(int id, QQmlListProperty<QObject> *target)
1315{
1316 // when accessing the list, we need to find the correct MetaObject,
1317 // namely this. However, obejct->metaObject might point to any
1318 // MetaObject down the inheritance hierarchy, so we need to store how
1319 // far we have to go down
1320 // To do this, we encode the hierarchy depth together with the id of the
1321 // property in a single quintptr, with the first half storing the depth
1322 // and the second half storing the property id
1323
1324 auto mo = static_cast<QQmlVMEMetaObject *>(
1325 QObjectPrivate::get(object.data())->metaObject);
1326 quintptr inheritanceDepth = 0u;
1327 while (mo && mo != this) {
1328 mo = mo->parentVMEMetaObject();
1329 ++inheritanceDepth;
1330 }
1331 constexpr quintptr idBits = sizeof(quintptr) * CHAR_BIT / 2u;
1332 if (Q_UNLIKELY(inheritanceDepth >= (quintptr(1) << idBits))) {
1333 qmlWarning(object.data()) << "Too many objects in inheritance hierarchy "
1334 "for list property";
1335 return false;
1336 }
1337 if (Q_UNLIKELY(quintptr(id) >= (quintptr(1) << idBits))) {
1338 qmlWarning(object.data()) << "Too many properties in object "
1339 "for list property";
1340 return false;
1341 }
1342 quintptr encodedIndex = (inheritanceDepth << idBits) + id;
1343
1344 initPropertyAsList(id);
1345 *target = QQmlListProperty<QObject>(
1346 object.data(), reinterpret_cast<void *>(quintptr(encodedIndex)),
1347 list_append, list_count, list_at,
1348 list_clear, list_replace, list_removeLast);
1349 return true;
1350}
1351
1352QV4::ReturnedValue QQmlVMEMetaObject::method(int localMethodIndex) const
1353{
1354 if (m_ctxt.isNull() || !m_ctxt->isValid())
1355 return QV4::Encode::undefined();
1356
1357 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1358 if (!md)
1359 return QV4::Encode::undefined();
1360
1361 return (md->data() + localMethodIndex + propCount())->asReturnedValue();
1362}
1363
1364QV4::ReturnedValue QQmlVMEMetaObject::readVarProperty(int id) const
1365{
1366 Q_ASSERT(!findCompiledObject() || findCompiledObject()->propertyTable()[id].commonType() == QV4::CompiledData::CommonType::Var);
1367
1368 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1369 if (md)
1370 return (md->data() + id)->asReturnedValue();
1371 return QV4::Value::undefinedValue().asReturnedValue();
1372}
1373
1374QVariant QQmlVMEMetaObject::readPropertyAsVariant(int id) const
1375{
1376 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1377 if (md) {
1378 const QV4::QObjectWrapper *wrapper = (md->data() + id)->as<QV4::QObjectWrapper>();
1379 if (wrapper)
1380 return QVariant::fromValue(wrapper->object());
1381 const QV4::VariantObject *v = (md->data() + id)->as<QV4::VariantObject>();
1382 if (v)
1383 return v->d()->data();
1384 return QV4::ExecutionEngine::toVariant(*(md->data() + id), QMetaType {});
1385 }
1386 return QVariant();
1387}
1388
1389void QQmlVMEMetaObject::writeVarProperty(int id, const QV4::Value &value)
1390{
1391 Q_ASSERT(!findCompiledObject() || findCompiledObject()->propertyTable()[id].commonType() == QV4::CompiledData::CommonType::Var);
1392
1393 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1394 if (!md)
1395 return;
1396
1397 const QV4::Value &oldValue = (*md)[id];
1398 if (QV4::RuntimeHelpers::strictEqual(oldValue, value))
1399 return;
1400
1401 // Importantly, if the current value is a scarce resource, we need to ensure that it
1402 // gets automatically released by the engine if no other references to it exist.
1403 const QV4::VariantObject *oldVariant = oldValue.as<QV4::VariantObject>();
1404 if (oldVariant)
1405 oldVariant->removeVmePropertyReference();
1406
1407 QObject *valueObject = nullptr;
1408 QQmlVMEVariantQObjectPtr *guard = getQObjectGuardForProperty(id);
1409
1410 // And, if the new value is a scarce resource, we need to ensure that it does not get
1411 // automatically released by the engine until no other references to it exist.
1412 if (QV4::VariantObject *v = const_cast<QV4::VariantObject*>(value.as<QV4::VariantObject>())) {
1413 v->addVmePropertyReference();
1414 md->set(m_engine, id, value);
1415 } else if (QV4::QObjectWrapper *wrapper = const_cast<QV4::QObjectWrapper*>(value.as<QV4::QObjectWrapper>())) {
1416 // We need to track this QObject to signal its deletion
1417 valueObject = wrapper->object();
1418
1419 // Do we already have a QObject guard for this property?
1420 if (valueObject && !guard) {
1421 guard = new QQmlVMEVariantQObjectPtr();
1422 m_varObjectGuards.append(guard);
1423 }
1424 md->set(m_engine, id, value);
1425 } else if (const QV4::Sequence *sequence = value.as<QV4::Sequence>()) {
1426 md->set(m_engine, id, QV4::ReferenceObject::detached(sequence->d()));
1427 } else if (const QV4::QQmlValueTypeWrapper *wrapper = value.as<QV4::QQmlValueTypeWrapper>()) {
1428 md->set(m_engine, id, QV4::ReferenceObject::detached(wrapper->d()));
1429 } else if (const QV4::DateObject *date = value.as<QV4::DateObject>()) {
1430 md->set(m_engine, id, QV4::ReferenceObject::detached(date->d()));
1431 } else if (const QV4::VariantAssociationObject *association = value.as<QV4::VariantAssociationObject>()) {
1432 md->set(m_engine, id, QV4::ReferenceObject::detached(association->d()));
1433 } else {
1434 // TODO: We should have a virtualDetach to reduce the
1435 // boilerplate and avoid having to add new cases when a new
1436 // reference object is added.
1437 Q_ASSERT(!value.as<QV4::ReferenceObject>());
1438 md->set(m_engine, id, value);
1439 }
1440
1441 if (guard)
1442 guard->setGuardedValue(valueObject, this, id);
1443
1444 // Emit change signal as appropriate.
1445 activate(object.data(), id, nullptr);
1446}
1447
1448void QQmlVMEMetaObject::writeKnownVarProperty(int id, const QVariant &value)
1449{
1450 // This can only be called from QQmlVMEMetaObject::metaCall() if we've already determined
1451 // that the property type is "var". No need to double-check it here.
1452 Q_ASSERT(findCompiledObject() && findCompiledObject()->propertyTable()[id].commonType() == QV4::CompiledData::CommonType::Var);
1453
1454 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1455 if (!md)
1456 return;
1457
1458 // Importantly, if the current value is a scarce resource, we need to ensure that it
1459 // gets automatically released by the engine if no other references to it exist.
1460 const QV4::VariantObject *oldv = (md->data() + id)->as<QV4::VariantObject>();
1461 if (oldv)
1462 oldv->removeVmePropertyReference();
1463
1464 // And, if the new value is a scarce resource, we need to ensure that it does not get
1465 // automatically released by the m_engine until no other references to it exist.
1466 QV4::Scope scope(m_engine);
1467 QV4::ScopedValue newv(scope, m_engine->fromVariant(value));
1468 QV4::Scoped<QV4::VariantObject> v(scope, newv);
1469 if (!!v)
1470 v->addVmePropertyReference();
1471
1472 // Write the value and emit change signal as appropriate.
1473 QVariant currentValue = readPropertyAsVariant(id);
1474 md->set(m_engine, id, newv);
1475 if ((currentValue.userType() != value.userType() || currentValue != value))
1476 activate(object.data(), id, nullptr);
1477}
1478
1479QV4::ReturnedValue QQmlVMEMetaObject::vmeMethod(int index) const
1480{
1481 if (index < signalOffset()) {
1482 Q_ASSERT(parentVMEMetaObject());
1483 return parentVMEMetaObject()->vmeMethod(index);
1484 }
1485
1486 Q_ASSERT(index >= methodOffset());
1487 return method(index - methodOffset());
1488}
1489
1490// Used by debugger
1491void QQmlVMEMetaObject::setVmeMethod(int index, const QV4::Value &function)
1492{
1493 if (index < signalOffset()) {
1494 Q_ASSERT(parentVMEMetaObject());
1495 return parentVMEMetaObject()->setVmeMethod(index, function);
1496 }
1497
1498 Q_ASSERT(index >= methodOffset());
1499
1500 const int localMethodIndex = index - methodOffset();
1501 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1502 if (!md)
1503 return;
1504 md->set(m_engine, localMethodIndex + propCount(), function);
1505}
1506
1507QV4::ReturnedValue QQmlVMEMetaObject::vmeProperty(int index) const
1508{
1509 if (index < propOffset()) {
1510 Q_ASSERT(parentVMEMetaObject());
1511 return parentVMEMetaObject()->vmeProperty(index);
1512 }
1513 return readVarProperty(index - propOffset());
1514}
1515
1516void QQmlVMEMetaObject::setVMEProperty(int index, const QV4::Value &v)
1517{
1518 if (index < propOffset()) {
1519 Q_ASSERT(parentVMEMetaObject());
1520 parentVMEMetaObject()->setVMEProperty(index, v);
1521 return;
1522 }
1523 return writeVarProperty(index - propOffset(), v);
1524}
1525
1526/*!
1527 \internal
1528
1529 Describes how the property block of one VME level changes. A property add/remove/reorder
1530 changes the level's own property block and, on a count change, shifts everything the *derived*
1531 levels above it own (plus this level's own aliases and signals) by delta. mapCore/mapSignal
1532 express that mapping across the whole instance: below this level's own range -> unchanged;
1533 inside the property block -> the permutation (or -1 if removed); at or beyond it -> shifted by
1534 delta.
1535*/
1537{
1538 const QList<int> &oldToNew;
1542
1543 int oldPropertyCount() const { return oldToNew.size(); }
1544
1545 int mapCore(int coreIndex) const
1546 {
1547 const int local = coreIndex - coreBase;
1548 if (local < 0)
1549 return coreIndex;
1550 if (local < oldPropertyCount())
1551 return oldToNew[local] < 0 ? -1 : coreBase + oldToNew[local];
1552 return coreIndex + delta;
1553 }
1554
1555 int mapSignal(int signalIndex) const
1556 {
1557 const int k = signalIndex - ownSignalBase;
1558 if (k < 0)
1559 return signalIndex;
1560 if (k < oldPropertyCount())
1561 return oldToNew[k] < 0 ? -1 : ownSignalBase + oldToNew[k];
1562 return signalIndex + delta;
1563 }
1564};
1565
1566/*!
1567 \internal
1568
1569 Property-change notify endpoints (bindings that captured us, onXChanged handlers, alias
1570 endpoints, explicit-signal handlers) are keyed by signal index. Remap every own signal of the
1571 instance from this level up.
1572*/
1573static void remapNotifyEndpoints(QQmlData *ddata, QObject *object, QV4::ExecutionEngine *v4,
1574 const PropertyRemap &remap, int totalSignals)
1575{
1576 // Gather first, because reconnecting rewrites the notify lists.
1577 struct EndpointMove
1578 {
1579 QQmlNotifierEndpoint *endpoint;
1580 int newSignalIndex; // -1: the signal is gone, disconnect the endpoint.
1581 };
1582 QVarLengthArray<EndpointMove, 8> moves;
1583 for (int oldSignal = remap.ownSignalBase; oldSignal < totalSignals; ++oldSignal) {
1584 const int newSignal = remap.mapSignal(oldSignal);
1585 if (newSignal == oldSignal)
1586 continue;
1587 for (QQmlNotifierEndpoint *ep = ddata->notify(oldSignal); ep; ep = ep->nextEndpoint())
1588 moves.append({ ep, newSignal });
1589 }
1590
1591 if (moves.isEmpty())
1592 return;
1593
1594 QQmlEngine *engine = v4->qmlEngine();
1595 for (const EndpointMove &move : moves) {
1596 if (move.newSignalIndex < 0)
1597 move.endpoint->disconnect();
1598 else
1599 QQmlData::connectEndpoint(move.endpoint, object, move.newSignalIndex, engine);
1600 }
1601}
1602
1603/*!
1604 \internal
1605
1606 Bindings store the coreIndex of the property (or alias) they write. Re-target those whose
1607 destination moved. A binding on a *removed* property does not occur in a trivial diff (its
1608 BindingRemoved hunk forces a rebuild), so mapCore never returns -1 for a live binding here.
1609*/
1610static void remapBindingTargets(QQmlData *ddata, QObject *object, const PropertyRemap &remap)
1611{
1612 for (QQmlAbstractBinding *binding = ddata->bindings; binding;
1613 binding = binding->nextBinding()) {
1614 if (binding->targetObject() != object)
1615 continue;
1616 const QQmlPropertyIndex targetIndex = binding->targetPropertyIndex();
1617 const int newCore = remap.mapCore(targetIndex.coreIndex());
1618 if (newCore < 0 || newCore == targetIndex.coreIndex())
1619 continue;
1620 binding->remapTargetPropertyIndex(QQmlPropertyIndex(newCore, targetIndex.valueTypeIndex()));
1621 }
1622}
1623
1624/*!
1625 \internal
1626
1627 Binding bits (and their pending counterparts) are keyed by coreIndex. Snapshot the whole own
1628 range, clear it, then set each survivor's bits at its new position.
1629*/
1630static void remapBindingBits(QQmlData *ddata, QObject *object, const PropertyRemap &remap,
1631 int totalProperties)
1632{
1633 const int coreBase = remap.coreBase;
1634 const int managed = totalProperties - coreBase;
1635 QVarLengthArray<quint8, 16> bits(managed);
1636 for (int i = 0; i < managed; ++i) {
1637 bits[i] = (ddata->hasBindingBit(coreBase + i) ? 0x1 : 0)
1638 | (ddata->hasPendingBindingBit(coreBase + i) ? 0x2 : 0);
1639 }
1640 for (int i = 0; i < managed; ++i) {
1641 ddata->clearBindingBit(coreBase + i);
1642 ddata->clearPendingBindingBit(coreBase + i);
1643 }
1644 for (int i = 0; i < managed; ++i) {
1645 const int newCore = remap.mapCore(coreBase + i);
1646 if (newCore < 0)
1647 continue;
1648 if (bits[i] & 0x1)
1649 ddata->setBindingBit(object, newCore);
1650 if (bits[i] & 0x2)
1651 ddata->setPendingBindingBit(object, newCore);
1652 }
1653}
1654
1655/*!
1656 \internal
1657
1658 Stored values. Layout is [properties | methods]; a count change slides the method block by
1659 delta. Gather both, grow the storage if the new layout no longer fits, then scatter: survivor
1660 property values to their new slots (added slots stay undefined), method function objects by
1661 delta (refreshVmeMethods re-derives them afterwards regardless).
1662*/
1663void QQmlVMEMetaObject::remapStoredValues(int newPropertyCount, const QList<int> &oldToNew)
1664{
1665 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
1666 if (!md)
1667 return;
1668
1669 const int oldPropCount = oldToNew.size();
1670 const int nFunctions = methodCount();
1671
1672 QVarLengthArray<QV4::Value, 8> propValues(oldPropCount);
1673 QVarLengthArray<QV4::Value, 8> methodValues(nFunctions);
1674 const QV4::Value *data = md->data();
1675 for (int i = 0; i < oldPropCount; ++i)
1676 propValues[i] = data[i];
1677 for (int j = 0; j < nFunctions; ++j)
1678 methodValues[j] = data[oldPropCount + j];
1679
1680 const int newTotal = newPropertyCount + nFunctions;
1681 if (uint(newTotal) > md->size()) {
1682 QV4::Heap::MemberData *grown = QV4::MemberData::allocate(m_engine, newTotal);
1683 QV4::WriteBarrier::markCustom(m_engine, [grown](QV4::MarkStack *ms) { grown->mark(ms); });
1684 m_propertyAndMethodStorage.set(m_engine, grown);
1685 md = propertyAndMethodStorageAsMemberData();
1686 }
1687
1688 for (uint i = 0, end = md->size(); i < end; ++i)
1689 md->set(m_engine, i, QV4::Value::undefinedValue());
1690 for (int i = 0; i < oldPropCount; ++i) {
1691 if (oldToNew[i] >= 0)
1692 md->set(m_engine, oldToNew[i], propValues[i]);
1693 }
1694 for (int j = 0; j < nFunctions; ++j)
1695 md->set(m_engine, newPropertyCount + j, methodValues[j]);
1696}
1697
1698/*!
1699 \internal
1700
1701 QObject guards for var/QObject properties track their storage slot. Drop guards whose property
1702 was removed; otherwise follow the property to its new slot.
1703*/
1704void QQmlVMEMetaObject::remapObjectGuards(const QList<int> &oldToNew)
1705{
1706 const int oldPropCount = oldToNew.size();
1707 for (auto it = m_varObjectGuards.begin(); it != m_varObjectGuards.end();) {
1708 QQmlVMEVariantQObjectPtr *guard = *it;
1709 if (guard->m_index >= 0 && guard->m_index < oldPropCount) {
1710 const int mapped = oldToNew[guard->m_index];
1711 if (mapped < 0) {
1712 delete guard;
1713 it = m_varObjectGuards.erase(it);
1714 continue;
1715 }
1716 guard->m_index = mapped;
1717 }
1718 ++it;
1719 }
1720}
1721
1722/*!
1723 * \internal
1724 *
1725 * Relocate this object's per-instance property state to a new property layout.
1726 * \a oldToNew has one entry per *current* local property: oldToNew[i] is that
1727 * property's new local index, or -1 if it was removed. \a newPropertyCount is
1728 * the property count afterwards, so a growing count adds slots (added
1729 * properties occupy the new indices not named by oldToNew) and a shrinking
1730 * count drops them. Relocates the stored values, QObject guards, interceptors,
1731 * property-change notify endpoints, bindings and binding bits — and shifts the
1732 * aliases and explicit signals declared after the property block by the net
1733 * count change - so a live instance keeps working after the QML preview
1734 * re-points it at the reshaped cache.
1735 */
1736void QQmlVMEMetaObject::remapProperties(int newPropertyCount, const QList<int> &oldToNew)
1737{
1738 const int oldPropCount = oldToNew.size();
1739 Q_ASSERT(oldPropCount == propCount());
1740 const int delta = newPropertyCount - oldPropCount;
1741
1742 // A same-count identity permutation is a no-op.
1743 if (delta == 0) {
1744 bool moved = false;
1745 for (int i = 0; i < oldPropCount; ++i) {
1746 if (oldToNew[i] != i) {
1747 moved = true;
1748 break;
1749 }
1750 }
1751 if (!moved)
1752 return;
1753 }
1754
1755 QQmlData *ddata = QQmlData::get(object.data());
1756 Q_ASSERT(ddata);
1757
1758 const PropertyRemap remap{ oldToNew, propOffset(), cache->signalOffset(), delta };
1759
1760 // Use absolute property and signal counts from the main property cache, so the index-keyed
1761 // passes reach the members of any derived levels above this one, not just this level's own.
1762 const QQmlPropertyCache::ConstPtr mainCache =
1763 ddata->propertyCache ? ddata->propertyCache : cache;
1764
1765 // The stored values and QObject guards are this level's own and stay level-local; the rest is
1766 // keyed by absolute coreIndex/signal index.
1767 remapStoredValues(newPropertyCount, oldToNew);
1768 remapObjectGuards(oldToNew);
1769 remapInterceptorPropertyIndices(remap.coreBase, oldToNew, delta);
1770 remapNotifyEndpoints(ddata, object.data(), m_engine, remap, mainCache->signalCount());
1771 remapBindingTargets(ddata, object.data(), remap);
1772 remapBindingBits(ddata, object.data(), remap, mainCache->propertyCount());
1773}
1774
1775void QQmlVMEMetaObject::ensureQObjectWrapper()
1776{
1777 Q_ASSERT(cache);
1778 QV4::QObjectWrapper::ensureWrapper(m_engine, object.data());
1779}
1780
1781void QQmlVMEMetaObject::mark(QV4::MarkStack *markStack)
1782{
1783 if (m_engine != markStack->engine())
1784 return;
1785
1786 m_propertyAndMethodStorage.markOnce(markStack);
1787
1788 if (QQmlVMEMetaObject *parent = parentVMEMetaObject())
1789 parent->mark(markStack);
1790}
1791
1792bool QQmlVMEMetaObject::aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const
1793{
1794 *target = nullptr;
1795 *coreIndex = -1;
1796 *valueTypeIndex = -1;
1797
1798 if (m_ctxt.isNull())
1799 return false;
1800
1801 const QV4::CompiledData::Object *compiledObject = findCompiledObject();
1802 if (!compiledObject)
1803 return false;
1804
1805 const int aliasId = index - propOffset() - compiledObject->nProperties;
1806 Q_ASSERT(index >= propOffset() + int(compiledObject->nProperties));
1807
1808 const QQmlPropertyData *aliasProperty = cache->property(aliasOffset() + aliasId);
1809 *target = m_ctxt->idValue(aliasProperty ? aliasProperty->aliasTargetObjectId() : -1);
1810 if (!*target)
1811 return false;
1812
1813 const int targetPropertyIndex = aliasProperty ? aliasProperty->aliasTarget() : -1;
1814
1815 if (targetPropertyIndex != -1) {
1816 QQmlPropertyIndex encodedIndex = QQmlPropertyIndex::fromEncoded(targetPropertyIndex);
1817 *coreIndex = encodedIndex.coreIndex();
1818 *valueTypeIndex = encodedIndex.valueTypeIndex();
1819 }
1820 return true;
1821}
1822
1823void QQmlVMEMetaObject::connectAlias(const QV4::CompiledData::Object *compiledObject, int aliasId)
1824{
1825 Q_ASSERT(compiledObject);
1826 if (!m_aliasEndpoints)
1827 m_aliasEndpoints = new QQmlVMEMetaObjectEndpoint[compiledObject->nAliases];
1828
1829 QQmlVMEMetaObjectEndpoint *endpoint = m_aliasEndpoints + aliasId;
1830 if (endpoint->metaObject.data()) {
1831 // already connected
1832 Q_ASSERT(endpoint->metaObject.data() == this);
1833 return;
1834 }
1835
1836 const QQmlPropertyData *aliasProperty = cache->property(aliasOffset() + aliasId);
1837 endpoint->metaObject = this;
1838 m_ctxt->idValueBindings(
1839 aliasProperty ? aliasProperty->aliasTargetObjectId() : -1)->connect(endpoint);
1840 endpoint->tryConnect();
1841}
1842
1843void QQmlVMEMetaObject::connectAlias(QObject *o, int coreIndex, bool indexInSignalRange)
1844{
1845 const auto offset = [indexInSignalRange](QQmlVMEMetaObject *vme) {
1846 return indexInSignalRange ? vme->cache->signalOffset() : vme->cache->methodOffset();
1847 };
1848
1849 QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1850 while (vme && offset(vme) > coreIndex)
1851 vme = vme->parentVMEMetaObject();
1852
1853 Q_ASSERT(vme);
1854 if (const QV4::CompiledData::Object *compiledObject = vme->findCompiledObject()) {
1855 const int aliasId = coreIndex - offset(vme) - compiledObject->nProperties;
1856 if (aliasId >= 0 && aliasId < int(compiledObject->nAliases))
1857 vme->connectAlias(compiledObject, aliasId);
1858 }
1859}
1860
1861/*! \internal
1862 \a index is in the local signal index.
1863*/
1864void QQmlVMEMetaObject::activate(QObject *object, int index, void **args)
1865{
1866 QMetaObject::activate(object, cache->signalOffset(), index, args);
1867}
1868
1869bool QQmlVMEMetaObject::aliasTarget(QObject *o, int coreIndex, QObject **aObject, int *aCoreIndex,
1870 int *aValueTypeIndex)
1871{
1872 QQmlVMEMetaObject *vme = QQmlVMEMetaObject::get(o);
1873 while (vme && vme->cache->propertyOffset() > coreIndex)
1874 vme = vme->parentVMEMetaObject();
1875
1876 return vme && vme->aliasTarget(coreIndex, aObject, aCoreIndex, aValueTypeIndex);
1877}
1878
1879QQmlVMEVariantQObjectPtr *QQmlVMEMetaObject::getQObjectGuardForProperty(int index) const
1880{
1881 QList<QQmlVMEVariantQObjectPtr *>::ConstIterator it = m_varObjectGuards.constBegin(), end = m_varObjectGuards.constEnd();
1882 for ( ; it != end; ++it) {
1883 if ((*it)->m_index == index) {
1884 return *it;
1885 }
1886 }
1887
1888 return nullptr;
1889}
1890
1891QT_END_NAMESPACE
QTaggedPointer< QQmlVMEMetaObject, Tag > metaObject
void replace(qsizetype i, QObject *o) const
void append(QObject *o) const
QObject * at(qsizetype i) const
void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
void QQmlVMEMetaObjectEndpoint_callback(QQmlNotifierEndpoint *, void **)
static const QMetaObject * stringCastMetaObject(QObject *o, const QMetaObject *top)
static void remapNotifyEndpoints(QQmlData *ddata, QObject *object, QV4::ExecutionEngine *v4, const PropertyRemap &remap, int totalSignals)
static void list_removeLast(QQmlListProperty< QObject > *prop)
static QObject * list_at(QQmlListProperty< QObject > *prop, qsizetype index)
static qsizetype list_count(QQmlListProperty< QObject > *prop)
static void remapBindingBits(QQmlData *ddata, QObject *object, const PropertyRemap &remap, int totalProperties)
static void remapBindingTargets(QQmlData *ddata, QObject *object, const PropertyRemap &remap)
static void list_replace(QQmlListProperty< QObject > *prop, qsizetype index, QObject *o)
static bool propertyIndicesConflict(QQmlPropertyIndex a, QQmlPropertyIndex b)
int mapSignal(int signalIndex) const
int oldPropertyCount() const
int mapCore(int coreIndex) const
const QList< int > & oldToNew