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