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