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
108 virtual void refresh();
109
111 public:
112 inline DeleteWatcher(QQmlJavaScriptExpression *);
113 inline ~DeleteWatcher();
114 inline bool wasDeleted() const;
115 private:
116 friend class QQmlJavaScriptExpression;
117 QObject *_c;
118 QQmlJavaScriptExpression **_w;
119 QQmlJavaScriptExpression *_s;
120 };
121
122 inline bool hasError() const;
123 inline bool hasDelayedError() const;
124 QQmlError error(QQmlEngine *) const;
125 void clearError();
126 void clearActiveGuards();
128 virtual bool mustCaptureBindableProperty() const {return true;}
129
131 const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope, const QString &code,
132 const QString &filename, quint16 line);
133
134 QQmlEngine *engine() const { return m_context ? m_context->engine() : nullptr; }
135 bool hasUnresolvedNames() const { return m_context && m_context->hasUnresolvedNames(); }
136
137 bool needsPropertyChangeTrigger(QObject *target, int propertyIndex);
138 QPropertyChangeTrigger *allocatePropertyChangeTrigger(QObject *target, int propertyIndex);
139
140protected:
141 void createQmlBinding(const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope,
142 const QString &code, const QString &filename, quint16 line);
143
144 void setupFunction(QV4::ExecutionContext *qmlContext, QV4::Function *f);
145 void setCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit);
146
147 // We store some flag bits in the following flag pointers.
148 // activeGuards:flag1 - notifyOnValueChanged
149 // activeGuards:flag2 - useSharedContext
151
156
158
163
165
166private:
167 friend class QQmlContextData;
169 friend void QQmlJavaScriptExpressionGuard_callback(QQmlNotifierEndpoint *, void **);
173
174 // Not refcounted as the context will clear the expressions when destructed.
175 QQmlContextData *m_context;
176
177 QQmlJavaScriptExpression **m_prevExpression;
178 QQmlJavaScriptExpression *m_nextExpression;
179
180 QV4::PersistentValue m_qmlScope;
181 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit;
182
183 QV4::Function *m_v4Function;
184
185protected:
186 TriggerList *qpropertyChangeTriggers = nullptr;
187};
188
189class Q_QML_EXPORT QQmlPropertyCapture
190{
191public:
192 QQmlPropertyCapture(QQmlEngine *engine, QQmlJavaScriptExpression *e, QQmlJavaScriptExpression::DeleteWatcher *w)
193 : engine(engine), expression(e), watcher(w), errorString(nullptr) { }
194
195 ~QQmlPropertyCapture() {
196 Q_ASSERT(guards.isEmpty());
197 Q_ASSERT(errorString == nullptr);
198 }
199
200 void captureProperty(QQmlNotifier *);
201 void captureProperty(QObject *, int, int, bool doNotify = true);
202 void captureProperty(QObject *, const QQmlPropertyCache *, const QQmlPropertyData *, bool doNotify = true);
203 void captureTranslation();
204
205 QQmlEngine *engine;
206 QQmlJavaScriptExpression *expression;
207 QQmlJavaScriptExpression::DeleteWatcher *watcher;
208 QForwardFieldList<QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next> guards;
209 QStringList *errorString;
210
211private:
212 void captureBindableProperty(QObject *o, const QMetaObject *metaObjectForBindable, int c);
213 void captureNonBindableProperty(QObject *o, int n, int c, bool doNotify);
214};
215
216QQmlJavaScriptExpression::DeleteWatcher::DeleteWatcher(QQmlJavaScriptExpression *e)
217: _c(nullptr), _w(nullptr), _s(e)
218{
219 if (e->m_scopeObject.isT1()) {
220 _w = &_s;
221 _c = e->m_scopeObject.asT1();
222 e->m_scopeObject = this;
223 } else {
224 // Another watcher is already registered
225 _w = &e->m_scopeObject.asT2()->_s;
226 }
227}
228
229QQmlJavaScriptExpression::DeleteWatcher::~DeleteWatcher()
230{
231 Q_ASSERT(*_w == nullptr || (*_w == _s && _s->m_scopeObject.isT2()));
232 if (*_w && _s->m_scopeObject.asT2() == this)
233 _s->m_scopeObject = _c;
234}
235
236bool QQmlJavaScriptExpression::DeleteWatcher::wasDeleted() const
237{
238 return *_w == nullptr;
239}
240
241bool QQmlJavaScriptExpression::notifyOnValueChanged() const
242{
243 return activeGuards.tag() == NotifyOnValueChanged;
244}
245
246QObject *QQmlJavaScriptExpression::scopeObject() const
247{
248 if (m_scopeObject.isT1()) return m_scopeObject.asT1();
249 else return m_scopeObject.asT2()->_c;
250}
251
252void QQmlJavaScriptExpression::setScopeObject(QObject *v)
253{
254 if (m_scopeObject.isT1()) m_scopeObject = v;
255 else m_scopeObject.asT2()->_c = v;
256}
257
258bool QQmlJavaScriptExpression::hasError() const
259{
260 return !m_error.isNull() && m_error->isValid();
261}
262
263bool QQmlJavaScriptExpression::hasDelayedError() const
264{
265 return !m_error.isNull();
266}
267
268inline void QQmlJavaScriptExpression::clearError()
269{
270 delete m_error.data();
271 m_error = nullptr;
272}
273
274QQmlJavaScriptExpressionGuard::QQmlJavaScriptExpressionGuard(QQmlJavaScriptExpression *e)
275 : QQmlNotifierEndpoint(QQmlNotifierEndpoint::QQmlJavaScriptExpressionGuard),
276 expression(e), next(nullptr)
277{
278}
279
280QQmlJavaScriptExpressionGuard *
281QQmlJavaScriptExpressionGuard::New(QQmlJavaScriptExpression *e,
282 QQmlEngine *engine)
283{
284 Q_ASSERT(e);
285 return QQmlEnginePrivate::get(engine)->jsExpressionGuardPool.New(e);
286}
287
288void QQmlJavaScriptExpressionGuard::Delete()
289{
290 QRecyclePool<QQmlJavaScriptExpressionGuard>::Delete(this);
291}
292
293
294QT_END_NAMESPACE
295
296#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)
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 **)