Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QQMLJAVASCRIPTEXPRESSION_P_H
5#define QQMLJAVASCRIPTEXPRESSION_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qglobal.h>
19#include <QtCore/qtaggedpointer.h>
20#include <QtQml/qqmlerror.h>
21#include <private/qqmlengine_p.h>
22#include <QtQml/private/qbipointer_p.h>
23
25
27
29{
30public:
31 inline QQmlDelayedError() : nextError(nullptr), prevError(nullptr) {}
33
35
37 if (prevError) {
38 if (nextError) nextError->prevError = prevError;
39 *prevError = nextError;
40 nextError = nullptr;
41 prevError = nullptr;
42 }
43 return m_error;
44 }
45
46 inline bool isValid() const { return m_error.isValid(); }
47 inline const QQmlError &error() const { return m_error; }
48 inline void clearError() { m_error = QQmlError(); }
49
50 void setErrorLocation(const QQmlSourceLocation &sourceLocation);
51 void setErrorDescription(const QString &description);
52 void setErrorObject(QObject *object);
53
54 // Call only from catch(...) -- will re-throw if no JS exception
56
57private:
58
59 mutable QQmlError m_error;
60
61 QQmlDelayedError *nextError;
62 QQmlDelayedError **prevError;
63};
64
65class Q_QML_EXPORT QQmlJavaScriptExpression
66{
67 Q_DISABLE_COPY_MOVE(QQmlJavaScriptExpression)
68public:
71
72 virtual QString expressionIdentifier() const;
73 virtual void expressionChanged() = 0;
74
75 QV4::ReturnedValue evaluate(bool *isUndefined);
76 QV4::ReturnedValue evaluate(QV4::CallData *callData, bool *isUndefined);
77 bool evaluate(void **a, const QMetaType *types, int argc);
78
79 inline bool notifyOnValueChanged() const;
80
81 void setNotifyOnValueChanged(bool v);
82 void resetNotifyOnValueChanged();
83
84 inline QObject *scopeObject() const;
85 inline void setScopeObject(QObject *v);
86
87 virtual QQmlSourceLocation sourceLocation() const;
88
89 bool hasContext() const { return m_context != nullptr; }
90 bool hasValidContext() const { return m_context && m_context->isValid(); }
91 QQmlContext *publicContext() const { return m_context ? m_context->asQQmlContext() : nullptr; }
92
93 QQmlRefPointer<QQmlContextData> context() const { return m_context; }
94 void setContext(const QQmlRefPointer<QQmlContextData> &context);
95
97 {
98 m_nextExpression = *listHead;
99 if (m_nextExpression)
100 m_nextExpression->m_prevExpression = &m_nextExpression;
101 m_prevExpression = listHead;
102 *listHead = this;
103 }
104
105 QV4::Function *function() const { return m_v4Function; }
106
107 virtual void refresh();
108
110 public:
112 inline ~DeleteWatcher();
113 inline bool wasDeleted() const;
114 private:
116 QObject *_c;
119 };
120
121 inline bool hasError() const;
122 inline bool hasDelayedError() const;
123 QQmlError error(QQmlEngine *) const;
124 void clearError();
125 void clearActiveGuards();
126 QQmlDelayedError *delayedError();
127 virtual bool mustCaptureBindableProperty() const {return true;}
128
129 static QV4::ReturnedValue evalFunction(
130 const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope, const QString &code,
131 const QString &filename, quint16 line);
132
133 QQmlEngine *engine() const { return m_context ? m_context->engine() : nullptr; }
134 bool hasUnresolvedNames() const { return m_context && m_context->hasUnresolvedNames(); }
135
136 bool needsPropertyChangeTrigger(QObject *target, int propertyIndex);
137 QPropertyChangeTrigger *allocatePropertyChangeTrigger(QObject *target, int propertyIndex);
138
139protected:
140 void createQmlBinding(const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope,
141 const QString &code, const QString &filename, quint16 line);
142
143 void setupFunction(QV4::ExecutionContext *qmlContext, QV4::Function *f);
144 void setCompilationUnit(const QQmlRefPointer<QV4::ExecutableCompilationUnit> &compilationUnit);
145
146 // We store some flag bits in the following flag pointers.
147 // activeGuards:flag1 - notifyOnValueChanged
148 // activeGuards:flag2 - useSharedContext
149 QBiPointer<QObject, DeleteWatcher> m_scopeObject;
150
151 enum GuardTag {
153 NotifyOnValueChanged
154 };
155
156 QForwardFieldList<QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next, GuardTag> activeGuards;
157
158 enum Tag {
160 InEvaluationLoop
161 };
162
163 QTaggedPointer<QQmlDelayedError, Tag> m_error;
164
165private:
166 friend class QQmlContextData;
172
173 // Not refcounted as the context will clear the expressions when destructed.
174 QQmlContextData *m_context;
175
176 QQmlJavaScriptExpression **m_prevExpression;
177 QQmlJavaScriptExpression *m_nextExpression;
178
179 QV4::PersistentValue m_qmlScope;
180 QQmlRefPointer<QV4::ExecutableCompilationUnit> m_compilationUnit;
181
182 QV4::Function *m_v4Function;
183
184protected:
185 TriggerList *qpropertyChangeTriggers = nullptr;
186};
187
188class Q_QML_EXPORT QQmlPropertyCapture
189{
190public:
193
195 Q_ASSERT(guards.isEmpty());
196 Q_ASSERT(errorString == nullptr);
197 }
198
199 void captureProperty(QQmlNotifier *);
200 void captureProperty(QObject *, int, int, bool doNotify = true);
201 void captureProperty(QObject *, const QQmlPropertyCache *, const QQmlPropertyData *, bool doNotify = true);
202 void captureTranslation();
203
207 QForwardFieldList<QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next> guards;
209
210private:
211 void captureBindableProperty(QObject *o, const QMetaObject *metaObjectForBindable, int c);
212 void captureNonBindableProperty(QObject *o, int n, int c, bool doNotify);
213};
214
216: _c(nullptr), _w(nullptr), _s(e)
217{
218 if (e->m_scopeObject.isT1()) {
219 _w = &_s;
220 _c = e->m_scopeObject.asT1();
221 e->m_scopeObject = this;
222 } else {
223 // Another watcher is already registered
224 _w = &e->m_scopeObject.asT2()->_s;
225 }
226}
227
229{
230 Q_ASSERT(*_w == nullptr || (*_w == _s && _s->m_scopeObject.isT2()));
231 if (*_w && _s->m_scopeObject.asT2() == this)
232 _s->m_scopeObject = _c;
233}
234
236{
237 return *_w == nullptr;
238}
239
244
246{
247 if (m_scopeObject.isT1()) return m_scopeObject.asT1();
248 else return m_scopeObject.asT2()->_c;
249}
250
252{
253 if (m_scopeObject.isT1()) m_scopeObject = v;
254 else m_scopeObject.asT2()->_c = v;
255}
256
258{
259 return !m_error.isNull() && m_error->isValid();
260}
261
263{
264 return !m_error.isNull();
265}
266
268{
269 delete m_error.data();
270 m_error = nullptr;
271}
272
278
282{
283 Q_ASSERT(e);
284 return QQmlEnginePrivate::get(engine)->jsExpressionGuardPool.New(e);
285}
286
291
292
294
295#endif // QQMLJAVASCRIPTEXPRESSION_P_H
\inmodule QtCore
Definition qmetatype.h:341
\inmodule QtCore
Definition qobject.h:103
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
bool addError(QQmlEnginePrivate *)
void setErrorObject(QObject *object)
Q_REQUIRED_RESULT QQmlError removeError()
void catchJavaScriptException(QV4::ExecutionEngine *engine)
void setErrorLocation(const QQmlSourceLocation &sourceLocation)
const QQmlError & error() const
void setErrorDescription(const QString &description)
static QQmlEnginePrivate * get(QQmlEngine *e)
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQmlError class encapsulates a QML error.
Definition qqmlerror.h:18
bool isValid() const
Returns true if this error is valid, otherwise false.
static QQmlJavaScriptExpressionGuard * New(QQmlJavaScriptExpression *e, QQmlEngine *engine)
void insertIntoList(QQmlJavaScriptExpression **listHead)
QForwardFieldList< QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next, GuardTag > activeGuards
QQmlRefPointer< QQmlContextData > context() const
QBiPointer< QObject, DeleteWatcher > m_scopeObject
virtual void expressionChanged()=0
virtual bool mustCaptureBindableProperty() const
QTaggedPointer< QQmlDelayedError, Tag > m_error
QQmlJavaScriptExpression * expression
QForwardFieldList< QQmlJavaScriptExpressionGuard, &QQmlJavaScriptExpressionGuard::next > guards
QQmlJavaScriptExpression::DeleteWatcher * watcher
QQmlPropertyCapture(QQmlEngine *engine, QQmlJavaScriptExpression *e, QQmlJavaScriptExpression::DeleteWatcher *w)
static void Delete(T *)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
T * data() const noexcept
bool isNull() const noexcept
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
quint64 ReturnedValue
static void * context
#define Q_REQUIRED_RESULT
static bool doNotify(QObject *, QEvent *)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
DBusConnection const char DBusError * error
GLsizei const GLfloat * v
[13]
GLfloat GLfloat GLfloat w
[0]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLsizei GLenum GLenum * types
GLfloat GLfloat f
GLenum target
GLfloat n
const GLubyte * c
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
void QQmlJavaScriptExpressionGuard_callback(QQmlNotifierEndpoint *e, void **)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned short quint16
Definition qtypes.h:48
QFutureWatcher< int > watcher
QObject::connect nullptr
engine evaluate("var myObject = new MyObject()")
[8]
QJSEngine engine
[0]
\inmodule QtCore