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
qqmljavascriptexpression_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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
4
5#ifndef QQMLJAVASCRIPTEXPRESSION_P_H
6#define QQMLJAVASCRIPTEXPRESSION_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qglobal.h>
20#include <QtCore/qtaggedpointer.h>
21#include <QtQml/qqmlerror.h>
22#include <private/qqmlengine_p.h>
23#include <QtQml/private/qbipointer_p.h>
24
25QT_BEGIN_NAMESPACE
26
27struct QQmlSourceLocation;
28
30{
31public:
32 inline QQmlDelayedError() : nextError(nullptr), prevError(nullptr) {}
33 inline ~QQmlDelayedError() { (void)removeError(); }
34
35 bool addError(QQmlEnginePrivate *);
36
37 Q_REQUIRED_RESULT inline QQmlError removeError() {
38 if (prevError) {
41 nextError = nullptr;
42 prevError = nullptr;
43 }
44 return m_error;
45 }
46
47 inline bool isValid() const { return m_error.isValid(); }
48 inline const QQmlError &error() const { return m_error; }
49 inline void clearError() { m_error = QQmlError(); }
50
52 void setErrorDescription(const QString &description);
53 void setErrorObject(QObject *object);
54
55 // Call only from catch(...) -- will re-throw if no JS exception
56 void catchJavaScriptException(QV4::ExecutionEngine *engine);
57
58private:
59
60 mutable QQmlError m_error;
61
62 QQmlDelayedError *nextError;
63 QQmlDelayedError **prevError;
64};
65
66class Q_QML_EXPORT QQmlJavaScriptExpression
67{
68 Q_DISABLE_COPY_MOVE(QQmlJavaScriptExpression)
69public:
70 QQmlJavaScriptExpression();
71 virtual ~QQmlJavaScriptExpression();
72
73 virtual QString expressionIdentifier() const;
74 virtual void expressionChanged() = 0;
75
76 QV4::ReturnedValue evaluate(bool *isUndefined);
77 QV4::ReturnedValue evaluate(QV4::CallData *callData, bool *isUndefined);
78 bool evaluate(void **a, const QMetaType *types, int argc);
79
80 inline bool notifyOnValueChanged() const;
81
82 void setNotifyOnValueChanged(bool v);
84
85 inline QObject *scopeObject() const;
86 inline void setScopeObject(QObject *v);
87
88 virtual QQmlSourceLocation sourceLocation() const;
89
90 bool hasContext() const { return m_context != nullptr; }
91 bool hasValidContext() const { return m_context && m_context->isValid(); }
92 QQmlContext *publicContext() const { return m_context ? m_context->asQQmlContext() : nullptr; }
93
94 QQmlRefPointer<QQmlContextData> context() const { return m_context; }
95 void setContext(const QQmlRefPointer<QQmlContextData> &context);
96
97 void insertIntoList(QQmlJavaScriptExpression **listHead)
98 {
99 m_nextExpression = *listHead;
100 if (m_nextExpression)
101 m_nextExpression->m_prevExpression = &m_nextExpression;
102 m_prevExpression = listHead;
103 *listHead = this;
104 }
105
106 QV4::Function *function() const { return m_v4Function; }
107 void setFunction(QV4::Function *f);
108
109 QQmlJavaScriptExpression *nextExpression() const { return m_nextExpression; }
110
111 virtual void refresh();
112
114 public:
115 inline DeleteWatcher(QQmlJavaScriptExpression *);
116 inline ~DeleteWatcher();
117 inline bool wasDeleted() const;
118 private:
119 friend class QQmlJavaScriptExpression;
120 QObject *_c;
121 QQmlJavaScriptExpression **_w;
122 QQmlJavaScriptExpression *_s;
123 };
124
125 inline bool hasError() const;
126 inline bool hasDelayedError() const;
127 QQmlError error(QQmlEngine *) const;
128 void clearError();
129 void clearActiveGuards();
131 virtual bool mustCaptureBindableProperty() const {return true;}
132
134 const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope, const QString &code,
135 const QString &filename, quint16 line);
136
137 QQmlEngine *engine() const { return m_context ? m_context->engine() : nullptr; }
138 bool hasUnresolvedNames() const { return m_context && m_context->hasUnresolvedNames(); }
139
140 bool needsPropertyChangeTrigger(QObject *target, int propertyIndex);
141 QPropertyChangeTrigger *allocatePropertyChangeTrigger(QObject *target, int propertyIndex);
142
143protected:
144 void createQmlBinding(const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope,
145 const QString &code, const QString &filename, quint16 line);
146
147 void setupFunction(QV4::ExecutionContext *qmlContext, QV4::Function *f);
148 void setCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit);
149
150 // We store some flag bits in the following flag pointers.
151 // activeGuards:flag1 - notifyOnValueChanged
152 // activeGuards:flag2 - useSharedContext
154
159
161
166
168
169private:
170 friend class QQmlContextData;
172 friend void QQmlJavaScriptExpressionGuard_callback(QQmlNotifierEndpoint *, void **);
176
177 // Not refcounted as the context will clear the expressions when destructed.
178 QQmlContextData *m_context;
179
180 QQmlJavaScriptExpression **m_prevExpression;
181 QQmlJavaScriptExpression *m_nextExpression;
182
183 QV4::PersistentValue m_qmlScope;
184 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit;
185
186 QV4::Function *m_v4Function;
187
188protected:
189 TriggerList *qpropertyChangeTriggers = nullptr;
190};
191
192class Q_QML_EXPORT QQmlPropertyCapture
193{
194public:
195 QQmlPropertyCapture(QQmlEngine *engine, QQmlJavaScriptExpression *e, QQmlJavaScriptExpression::DeleteWatcher *w)
196 : engine(engine), expression(e), watcher(w), errorString(nullptr) { }
197
198 ~QQmlPropertyCapture() {
199 Q_ASSERT(guards.isEmpty());
200 Q_ASSERT(errorString == nullptr);
201 }
202
203 void captureProperty(QQmlNotifier *);
204 void captureProperty(QObject *, int, int, bool doNotify = true);
205 void captureProperty(QObject *, const QQmlPropertyCache *, const QQmlPropertyData *, bool doNotify = true);
206 void captureTranslation();
207
208 QQmlEngine *engine;
209 QQmlJavaScriptExpression *expression;
210 QQmlJavaScriptExpression::DeleteWatcher *watcher;
211 QForwardFieldList<QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next> guards;
212 QStringList *errorString;
213
214private:
215 void captureBindableProperty(QObject *o, const QMetaObject *metaObjectForBindable, int c);
216 void captureNonBindableProperty(QObject *o, int n, int c, bool doNotify);
217};
218
219QQmlJavaScriptExpression::DeleteWatcher::DeleteWatcher(QQmlJavaScriptExpression *e)
220: _c(nullptr), _w(nullptr), _s(e)
221{
222 if (e->m_scopeObject.isT1()) {
223 _w = &_s;
224 _c = e->m_scopeObject.asT1();
225 e->m_scopeObject = this;
226 } else {
227 // Another watcher is already registered
228 _w = &e->m_scopeObject.asT2()->_s;
229 }
230}
231
232QQmlJavaScriptExpression::DeleteWatcher::~DeleteWatcher()
233{
234 Q_ASSERT(*_w == nullptr || (*_w == _s && _s->m_scopeObject.isT2()));
235 if (*_w && _s->m_scopeObject.asT2() == this)
236 _s->m_scopeObject = _c;
237}
238
239bool QQmlJavaScriptExpression::DeleteWatcher::wasDeleted() const
240{
241 return *_w == nullptr;
242}
243
244bool QQmlJavaScriptExpression::notifyOnValueChanged() const
245{
246 return activeGuards.tag() == NotifyOnValueChanged;
247}
248
249QObject *QQmlJavaScriptExpression::scopeObject() const
250{
251 if (m_scopeObject.isT1()) return m_scopeObject.asT1();
252 else return m_scopeObject.asT2()->_c;
253}
254
255void QQmlJavaScriptExpression::setScopeObject(QObject *v)
256{
257 if (m_scopeObject.isT1()) m_scopeObject = v;
258 else m_scopeObject.asT2()->_c = v;
259}
260
261bool QQmlJavaScriptExpression::hasError() const
262{
263 return !m_error.isNull() && m_error->isValid();
264}
265
266bool QQmlJavaScriptExpression::hasDelayedError() const
267{
268 return !m_error.isNull();
269}
270
271inline void QQmlJavaScriptExpression::clearError()
272{
273 delete m_error.data();
274 m_error = nullptr;
275}
276
277QQmlJavaScriptExpressionGuard::QQmlJavaScriptExpressionGuard(QQmlJavaScriptExpression *e)
278 : QQmlNotifierEndpoint(QQmlNotifierEndpoint::QQmlJavaScriptExpressionGuard),
279 expression(e), next(nullptr)
280{
281}
282
283QQmlJavaScriptExpressionGuard *
284QQmlJavaScriptExpressionGuard::New(QQmlJavaScriptExpression *e,
285 QQmlEngine *engine)
286{
287 Q_ASSERT(e);
288 return QQmlEnginePrivate::get(engine)->jsExpressionGuardPool.New(e);
289}
290
291void QQmlJavaScriptExpressionGuard::Delete()
292{
293 QRecyclePool<QQmlJavaScriptExpressionGuard>::Delete(this);
294}
295
296
297QT_END_NAMESPACE
298
299#endif // QQMLJAVASCRIPTEXPRESSION_P_H
QOpenGLContext * context() const
bool addError(QQmlEnginePrivate *)
void setErrorObject(QObject *object)
void catchJavaScriptException(QV4::ExecutionEngine *engine)
void setErrorDescription(const QString &description)
QQmlEngine * engine() const
Return the QQmlEngine this incubation controller is set on, or 0 if it has not been set on any engine...
void insertIntoList(QQmlJavaScriptExpression **listHead)
void setupFunction(QV4::ExecutionContext *qmlContext, QV4::Function *f)
void setCompilationUnit(const QQmlRefPointer< QV4::ExecutableCompilationUnit > &compilationUnit)
bool evaluate(void **a, const QMetaType *types, int argc)
bool needsPropertyChangeTrigger(QObject *target, int propertyIndex)
QV4::ReturnedValue evaluate(bool *isUndefined)
virtual QString expressionIdentifier() const
QForwardFieldList< QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next, GuardTag > activeGuards
void setContext(const QQmlRefPointer< QQmlContextData > &context)
QQmlJavaScriptExpression * nextExpression() const
static QV4::ReturnedValue evalFunction(const QQmlRefPointer< QQmlContextData > &ctxt, QObject *scope, const QString &code, const QString &filename, quint16 line)
QQmlError error(QQmlEngine *) const
QV4::ReturnedValue evaluate(QV4::CallData *callData, bool *isUndefined)
virtual QQmlSourceLocation sourceLocation() const
QBiPointer< QObject, DeleteWatcher > m_scopeObject
virtual void expressionChanged()=0
QPropertyChangeTrigger * allocatePropertyChangeTrigger(QObject *target, int propertyIndex)
virtual bool mustCaptureBindableProperty() const
QTaggedPointer< QQmlDelayedError, Tag > m_error
Combined button and popup list for selecting options.
void QQmlJavaScriptExpressionGuard_callback(QQmlNotifierEndpoint *e, void **)