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_p.h
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
6#ifndef QQMLVMEMETAOBJECT_P_H
7#define QQMLVMEMETAOBJECT_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include "qqmldata_p.h"
21#include <private/qbipointer_p.h>
22#include <private/qqmlguard_p.h>
23#include <private/qqmlguardedcontextdata_p.h>
24#include <private/qqmlpropertyvalueinterceptor_p.h>
25#include <private/qv4object_p.h>
26#include <private/qv4value_p.h>
27
28#include <QtCore/private/qobject_p.h>
29
30#if QT_CONFIG(regularexpression)
31#include <QtCore/qregularexpression.h>
32#endif
33
34#include <QtCore/qbitarray.h>
35#include <QtCore/qdatetime.h>
36#include <QtCore/qdebug.h>
37#include <QtCore/qlist.h>
38#include <QtCore/qmetaobject.h>
39
41
42class QQmlVMEMetaObject;
44{
46
47public:
50
51 QQmlVMEMetaObject *metaObject() const { return m_metaObject; }
52 QV4::Heap::Object *list() const { return m_list; }
53 quintptr id() const { return m_id; }
54
55 // if m_propertyAndMethodStorage of the object is gone,
56 // the list is unusable
57 bool isValid() const { return m_list; }
58
59 void append(QObject *o) const;
60 void replace(qsizetype i, QObject *o) const;
61 QObject *at(qsizetype i) const;
62
64 {
65 Q_ASSERT(isValid());
66 return m_list->arrayData->length();
67 }
68
69 void clear() const
70 {
71 Q_ASSERT(isValid());
72
73 QV4::Scope scope(m_list->internalClass->engine);
74 QV4::ScopedObject object(scope, m_list);
75 m_list->arrayData->vtable()->truncate(object, 0);
76 }
77
78 void removeLast() const
79 {
80 Q_ASSERT(isValid());
81
82 const uint length = m_list->arrayData->length();
83 if (length == 0)
84 return;
85
86 QV4::Scope scope(m_list->internalClass->engine);
87 QV4::ScopedObject object(scope, m_list);
88 m_list->arrayData->vtable()->truncate(object, length - 1);
89 }
90
91 void activateSignal() const;
92
93private:
94 QQmlVMEMetaObject *m_metaObject = nullptr;
95 QV4::Heap::Object *m_list = nullptr;
96 quintptr m_id = 0;
97};
98
100{
101public:
103
104 inline void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index);
105
106 QQmlVMEMetaObject *m_target;
108
109private:
110 static void objectDestroyedImpl(QQmlGuardImpl *guard);
111};
112
113
114class Q_QML_EXPORT QQmlInterceptorMetaObject : public QDynamicMetaObjectData
115{
116public:
117 QQmlInterceptorMetaObject(QObject *obj, const QQmlPropertyCache::ConstPtr &cache);
118 ~QQmlInterceptorMetaObject() override;
119
120 void registerInterceptor(QQmlPropertyIndex index, QQmlPropertyValueInterceptor *interceptor);
121
122 static QQmlInterceptorMetaObject *get(QObject *obj);
123
124#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
125 const QMetaObject *toDynamicMetaObject(QObject *o) const override;
126#else
127 QMetaObject *toDynamicMetaObject(QObject *o) override;
128#endif
129
130 // Used by auto-tests for inspection
131 QQmlPropertyCache::ConstPtr propertyCache() const { return cache; }
132
133 // Re-point this meta-object at an identically-shaped property cache. Used by the QML preview's
134 // in-place patch to give a live instance the reloaded type's identity, so it keeps satisfying
135 // is-a checks against consumers whose type references were redirected to the new unit. The
136 // lazily-built dynamic meta-object is dropped so it is rebuilt from the new cache on next use.
137 void setPropertyCache(const QQmlPropertyCache::ConstPtr &newCache)
138 {
139 cache = newCache;
140 metaObject = QTaggedPointer<const QMetaObject, MetaObjectValidity>();
141 }
142
143 bool intercepts(QQmlPropertyIndex propertyIndex) const
144 {
145 for (auto it = interceptors; it; it = it->m_next) {
146 if (it->m_propertyIndex == propertyIndex)
147 return true;
148 }
149 if (auto parentInterceptor = ((parent.isT1() && parent.flag()) ? static_cast<QQmlInterceptorMetaObject *>(parent.asT1()) : nullptr))
150 return parentInterceptor->intercepts(propertyIndex);
151 return false;
152 }
153
154 void invalidate() { metaObject.setTag(MetaObjectInvalid); }
155
156 // only used for Bindable so far; Write instead passes a flag via the metacall
157 enum InterceptionBehavior { Intercept, SkipInterception };
158 static void setInterceptionMode(QObject *object, InterceptionBehavior behavior);
159
160 QTaggedPointer<QObject> object = nullptr;
161 QQmlPropertyCache::ConstPtr cache;
162
163protected:
164 void remapInterceptorPropertyIndices(int coreBase, const QList<int> &oldToNew, int delta);
165
166 int metaCall(QObject *o, QMetaObject::Call c, int id, void **a) override;
167 bool intercept(QMetaObject::Call c, int id, void **a)
168 {
169 if (!interceptors)
170 return false;
171
172 switch (c) {
173 case QMetaObject::WriteProperty:
174 if (*reinterpret_cast<int*>(a[3]) & QQmlPropertyData::BypassInterceptor)
175 return false;
176 break;
177 case QMetaObject::BindableProperty:
178 if (object.tag() == SkipInterception) {
179 return false;
180 }
181 break;
182 default:
183 return false;
184 }
185
186 return doIntercept(c, id, a);
187 }
188
189 QBiPointer<QDynamicMetaObjectData, const QMetaObject> parent;
190
191 enum MetaObjectValidity { MetaObjectValid, MetaObjectInvalid };
192 QTaggedPointer<const QMetaObject, MetaObjectValidity> metaObject;
193
194private:
195 bool doIntercept(QMetaObject::Call c, int id, void **a);
196 QQmlPropertyValueInterceptor *interceptors = nullptr;
197};
198
199inline QQmlInterceptorMetaObject *QQmlInterceptorMetaObject::get(QObject *obj)
200{
201 if (obj) {
202 if (QQmlData *data = QQmlData::get(obj)) {
203 if (data->hasInterceptorMetaObject)
204 return static_cast<QQmlInterceptorMetaObject *>(QObjectPrivate::get(obj)->metaObject);
205 }
206 }
207
208 return nullptr;
209}
210
211inline void QQmlInterceptorMetaObject::setInterceptionMode(
212 QObject *object, QQmlInterceptorMetaObject::InterceptionBehavior behavior)
213{
214 QQmlInterceptorMetaObject *interceptorMetaObject = get(object);
215 while (interceptorMetaObject) {
216 interceptorMetaObject->object.setTag(behavior);
217 auto parent = interceptorMetaObject->parent;
218 interceptorMetaObject = ((parent.isT1() && parent.flag()) ? static_cast<QQmlInterceptorMetaObject *>(parent.asT1()) : nullptr);
219 }
220}
221
223class Q_QML_EXPORT QQmlVMEMetaObject : public QQmlInterceptorMetaObject
224{
225public:
226 QQmlVMEMetaObject(
227 QV4::ExecutionEngine *engine, QObject *obj, const QQmlPropertyCache::ConstPtr &cache,
228 const QQmlRefPointer<QV4::ExecutableCompilationUnit> &qmlCompilationUnit,
229 int qmlObjectId);
230 ~QQmlVMEMetaObject() override;
231
232 bool aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const;
233 QV4::ReturnedValue vmeMethod(int index) const;
234 void setVmeMethod(int index, const QV4::Value &function);
235 QV4::ReturnedValue vmeProperty(int index) const;
236 void setVMEProperty(int index, const QV4::Value &v);
237
238 void remapProperties(int newPropertyCount, const QList<int> &oldToNew);
239
240 static void connectAlias(QObject *o, int coreIndex, bool indexInSignalRange);
241
242 static inline QQmlVMEMetaObject *get(QObject *o, QV4::ExecutionEngine *engine);
243 static bool aliasTarget(QObject *o, int coreIndex, QObject **aObject, int *aCoreIndex,
244 int *aValueTypeIndex);
245
246 static void list_append(QQmlListProperty<QObject> *prop, QObject *o);
247 static void list_clear(QQmlListProperty<QObject> *prop);
248 static void list_append_nosignal(QQmlListProperty<QObject> *prop, QObject *o);
249 static void list_clear_nosignal(QQmlListProperty<QObject> *prop);
250
251 inline int propOffset() const;
252 inline int propCount() const;
253 inline int aliasOffset() const;
254 inline int aliasCount() const;
255 inline int signalOffset() const;
256 inline int signalCount() const;
257 inline int methodOffset() const;
258 inline int methodCount() const;
259
260 QV4::MemberData *propertyAndMethodStorageAsMemberData() const;
261
262 int readPropertyAsInt(int id) const;
263 bool readPropertyAsBool(int id) const;
264 double readPropertyAsDouble(int id) const;
265 QString readPropertyAsString(int id) const;
266 QSizeF readPropertyAsSizeF(int id) const;
267 QPointF readPropertyAsPointF(int id) const;
268 QUrl readPropertyAsUrl(int id) const;
269 QDate readPropertyAsDate(int id) const;
270 QTime readPropertyAsTime(int id) const;
271 QDateTime readPropertyAsDateTime(int id) const;
272
273#if QT_CONFIG(regularexpression)
274 QRegularExpression readPropertyAsRegularExpression(int id) const;
275#endif
276
277 QRectF readPropertyAsRectF(int id) const;
278 QObject *readPropertyAsQObject(int id) const;
279 void initPropertyAsList(int id) const;
280
281 void writeProperty(int id, int v);
282 void writeProperty(int id, bool v);
283 void writeProperty(int id, double v);
284 void writeProperty(int id, const QString& v);
285
286 template<typename VariantCompatible>
287 void writeProperty(int id, const VariantCompatible &v)
288 {
289 QV4::MemberData *md = propertyAndMethodStorageAsMemberData();
290 if (md) {
291 QV4::Scope scope(m_engine);
292 QV4::Scoped<QV4::MemberData>(scope, md)->set(
293 m_engine, id, m_engine->newVariantObject(
294 QMetaType::fromType<VariantCompatible>(), &v));
295 }
296 }
297
298 void writeProperty(int id, QObject *v);
299
300 void ensureQObjectWrapper();
301
302 void mark(QV4::MarkStack *markStack);
303
304 void connectAlias(const QV4::CompiledData::Object *compiledObject, int aliasId);
305
306 QV4::ReturnedValue method(int) const;
307
308 QV4::ReturnedValue readVarProperty(int) const;
309 void writeVarProperty(int, const QV4::Value &);
310 QVariant readPropertyAsVariant(int) const;
311
312 inline QQmlVMEMetaObject *parentVMEMetaObject() const;
313 inline void setParentVMEMetaObject(QQmlVMEMetaObject *newParent);
314
315 void activate(QObject *, int, void **);
316
317 QQmlVMEVariantQObjectPtr *getQObjectGuardForProperty(int) const;
318
319 QQmlRefPointer<QQmlContextData> contextData() const { return m_ctxt.contextData(); }
320 QV4::ExecutionEngine *engine() const { return m_engine; }
321 QQmlRefPointer<QV4::ExecutableCompilationUnit> compilationUnit() const
322 {
323 return m_compilationUnit;
324 }
325
326 void setCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit)
327 {
328 m_compilationUnit = compilationUnit;
329 }
330
331 int qmlObjectId() const { return m_qmlObjectId; }
332
333protected:
334 int metaCall(QObject *o, QMetaObject::Call _c, int _id, void **_a) override;
335 bool getListProperty(int id, QQmlListProperty<QObject> *target);
336
337private:
338 friend class QQmlVMEMetaObjectEndpoint;
339 friend class QQmlVMEResolvedList;
340 friend class QQmlVMEVariantQObjectPtr;
341
342 static inline QQmlVMEMetaObject *get(QObject *o);
343
344 const QV4::CompiledData::Object *findCompiledObject() const {
345 // If the executable CU has been stripped of its engine, it has an empty base CU
346 if (!m_compilationUnit || !m_compilationUnit->engine)
347 return nullptr;
348
349 Q_ASSERT(m_qmlObjectId >= 0 && m_qmlObjectId < m_compilationUnit->objectCount());
350 return m_compilationUnit->objectAt(m_qmlObjectId);
351 }
352
353 void writeKnownVarProperty(int id, const QVariant &value);
354
355 QV4::ExecutionEngine *m_engine;
356 QQmlGuardedContextData m_ctxt;
357
358 QQmlVMEMetaObjectEndpoint *m_aliasEndpoints;
359 QV4::WeakValue m_propertyAndMethodStorage;
360
361 QList<QQmlVMEVariantQObjectPtr *> m_varObjectGuards;
362
363 void remapStoredValues(int newPropertyCount, const QList<int> &oldToNew);
364 void remapObjectGuards(const QList<int> &oldToNew);
365
366 // keep a reference to the compilation unit in order to still
367 // do property access when the context has been invalidated.
368 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit;
369 int m_qmlObjectId = -1;
370 int m_numAliases = 0;
371};
372
373QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj)
374{
375 if (!obj)
376 return nullptr;
377
378 QQmlData *ddata = QQmlData::get(obj);
379 if (!ddata || !ddata->hasVMEMetaObject)
380 return nullptr;
381
382 return static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(obj)->metaObject);
383}
384
385QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj, QV4::ExecutionEngine *engine)
386{
387 QQmlVMEMetaObject *vme = get(obj);
388 return (vme && vme->engine() == engine) ? vme : nullptr;
389}
390
391int QQmlVMEMetaObject::propOffset() const
392{
393 // Regular properties are before aliases
394 return cache->propertyOffset();
395}
396
397int QQmlVMEMetaObject::propCount() const
398{
399 // Properties excluding aliases
400 return cache->ownPropertyCount() - m_numAliases;
401}
402
403int QQmlVMEMetaObject::aliasOffset() const
404{
405 // Aliases are after regular properties
406 return propOffset() + propCount();
407}
408
409int QQmlVMEMetaObject::aliasCount() const
410{
411 // We need to cache this since we don't want to count over and over.
412 return m_numAliases;
413}
414
415int QQmlVMEMetaObject::signalOffset() const
416{
417 // Signals are before regular methods
418 return cache->methodOffset();
419}
420
421int QQmlVMEMetaObject::signalCount() const
422{
423 // Signals including signals for properties and aliases
424 return cache->ownSignalCount();
425}
426
427int QQmlVMEMetaObject::methodOffset() const
428{
429 // Regular methods are after signals
430 return signalOffset() + signalCount();
431}
432
433int QQmlVMEMetaObject::methodCount() const
434{
435 // Methods excluding signals
436 return cache->ownMethodCount() - signalCount();
437}
438
439QQmlVMEMetaObject *QQmlVMEMetaObject::parentVMEMetaObject() const
440{
441 if (parent.isT1() && parent.flag())
442 return static_cast<QQmlVMEMetaObject *>(parent.asT1());
443
444 return nullptr;
445}
446
447inline void QQmlVMEMetaObject::setParentVMEMetaObject(QQmlVMEMetaObject *newParent)
448{
449 parent = newParent;
450 parent.setFlag();
451}
452
453QT_END_NAMESPACE
454
455#endif // QQMLVMEMETAOBJECT_P_H
QTaggedPointer< QQmlVMEMetaObject, Tag > metaObject
QQmlVMEMetaObject * metaObject() const
void replace(qsizetype i, QObject *o) const
qsizetype size() const
QV4::Heap::Object * list() const
void append(QObject *o) const
QObject * at(qsizetype i) const
void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index)
Combined button and popup list for selecting options.
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