aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlboundsignal.cpp
blob: 3f9ce26764044f09843c2e7750a45f1e4100fa8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlboundsignal_p.h"

#include <private/qmetaobject_p.h>
#include <private/qmetaobjectbuilder_p.h>
#include "qqmlengine_p.h"
#include "qqmlglobal_p.h"
#include <private/qqmlprofiler_p.h>
#include <private/qqmldebugconnector_p.h>
#include <private/qqmldebugserviceinterfaces_p.h>
#include "qqmlinfo.h"

#include <private/qjsvalue_p.h>
#include <private/qv4value_p.h>
#include <private/qv4jscall_p.h>
#include <private/qv4qobjectwrapper_p.h>
#include <private/qv4qmlcontext_p.h>

#include <QtCore/qdebug.h>

#include <qtqml_tracepoints_p.h>

QT_BEGIN_NAMESPACE

Q_TRACE_POINT(qtqml, QQmlHandlingSignal_entry, const QQmlEngine *engine, const QString &function,
              const QString &fileName, int line, int column)
Q_TRACE_POINT(qtqml, QQmlHandlingSignal_exit)

QQmlBoundSignalExpression::QQmlBoundSignalExpression(const QObject *target, int index, const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope,
        const QString &expression, const QString &fileName, quint16 line, quint16 column,
        const QString &handlerName, const QString &parameterString)
    : QQmlJavaScriptExpression(),
      m_index(index),
      m_target(target)
{
    init(ctxt, scope);

    QV4::ExecutionEngine *v4 = engine()->handle();

    QString function;

    // Add some leading whitespace to account for the binding's column offset.
    // It's 2 off because a, we start counting at 1 and b, the '(' below is not counted.
    function += QString(qMax(column, (quint16)2) - 2, QChar(QChar::Space))
              + QLatin1String("(function ") + handlerName + QLatin1Char('(');

    if (parameterString.isEmpty()) {
        QString error;
        //TODO: look at using the property cache here (as in the compiler)
        //      for further optimization
        QMetaMethod signal = QMetaObjectPrivate::signal(m_target->metaObject(), m_index);
        function += QQmlPropertyCache::signalParameterStringForJS(v4, signal.parameterNames(), &error);

        if (!error.isEmpty()) {
            qmlWarning(scopeObject()) << error;
            return;
        }
    } else
        function += parameterString;

    function += QLatin1String(") { ") + expression + QLatin1String(" })");
    QV4::Scope valueScope(v4);
    QV4::ScopedFunctionObject f(valueScope, evalFunction(context(), scopeObject(), function, fileName, line));
    QV4::ScopedContext context(valueScope, f->scope());
    setupFunction(context, f->function());
}

QQmlBoundSignalExpression::QQmlBoundSignalExpression(const QObject *target, int index, const QQmlRefPointer<QQmlContextData> &ctxt,
        QObject *scopeObject, QV4::Function *function, QV4::ExecutionContext *scope)
    : QQmlJavaScriptExpression(),
      m_index(index),
      m_target(target)
{
    // It's important to call init first, because m_index gets remapped in case of cloned signals.
    init(ctxt, scopeObject);

    QV4::ExecutionEngine *engine = ctxt->engine()->handle();

    if (!function->isClosureWrapper()) {
        QList<QByteArray> signalParameters = QMetaObjectPrivate::signal(m_target->metaObject(), m_index).parameterNames();
        if (!signalParameters.isEmpty()) {
            QString error;
            QQmlPropertyCache::signalParameterStringForJS(engine, signalParameters, &error);
            if (!error.isEmpty()) {
                qmlWarning(scopeObject) << error;
                return;
            }
            function->updateInternalClass(engine, signalParameters);
        }
    }

    QV4::Scope valueScope(engine);
    QV4::Scoped<QV4::QmlContext> qmlContext(valueScope, scope);
    if (!qmlContext)
        qmlContext = QV4::QmlContext::create(engine->rootContext(), ctxt, scopeObject);
    if (auto closure = function->nestedFunction()) {
        // If the function is marked as having a nested function, then the user wrote:
        //   onSomeSignal: function() { /*....*/ }
        // So take that nested function:
        setupFunction(qmlContext, closure);
    } else {
        setupFunction(qmlContext, function);

        // If it's a closure wrapper but we cannot directly access the nested function
        // we need to run the outer function to get the nested one.
        if (function->isClosureWrapper()) {
            bool isUndefined = false;
            QV4::ScopedFunctionObject result(
                        valueScope, QQmlJavaScriptExpression::evaluate(&isUndefined));

            Q_ASSERT(!isUndefined);
            Q_ASSERT(result->function());
            Q_ASSERT(result->function()->compilationUnit == function->compilationUnit);

            QV4::Scoped<QV4::ExecutionContext> callContext(valueScope, result->scope());
            setupFunction(callContext, result->function());
        }
    }
}

void QQmlBoundSignalExpression::init(const QQmlRefPointer<QQmlContextData> &ctxt, QObject *scope)
{
    setNotifyOnValueChanged(false);
    setContext(ctxt);
    setScopeObject(scope);

    Q_ASSERT(m_target && m_index > -1);
    m_index = QQmlPropertyCache::originalClone(m_target, m_index);
}

QQmlBoundSignalExpression::~QQmlBoundSignalExpression()
{
}

QString QQmlBoundSignalExpression::expressionIdentifier() const
{
    QQmlSourceLocation loc = sourceLocation();
    return loc.sourceFile + QLatin1Char(':') + QString::number(loc.line);
}

void QQmlBoundSignalExpression::expressionChanged()
{
    // bound signals do not notify on change.
}

QString QQmlBoundSignalExpression::expression() const
{
    if (expressionFunctionValid())
        return QStringLiteral("function() { [native code] }");
    return QString();
}

// Parts of this function mirror code in QQmlExpressionPrivate::value() and v4Value().
// Changes made here may need to be made there and vice versa.
void QQmlBoundSignalExpression::evaluate(void **a)
{
    if (!expressionFunctionValid())
        return;

    QQmlEngine *qmlengine = engine();

    // If there is no engine, we have no way to evaluate anything.
    // This can happen on destruction.
    if (!qmlengine)
        return;

    QQmlEnginePrivate *ep = QQmlEnginePrivate::get(qmlengine);
    QV4::ExecutionEngine *v4 = qmlengine->handle();
    QV4::Scope scope(v4);

    ep->referenceScarceResources(); // "hold" scarce resources in memory during evaluation.

    if (a) {
        //TODO: lookup via signal index rather than method index as an optimization
        const QMetaObject *targetMeta = m_target->metaObject();
        const QMetaMethod metaMethod = targetMeta->method(
                    QMetaObjectPrivate::signal(targetMeta, m_index).methodIndex());

        int argCount = metaMethod.parameterCount();
        QQmlMetaObject::ArgTypeStorage<9> storage;
        storage.reserve(argCount + 1);
        storage.append(QMetaType()); // We're not interested in the return value
        for (int i = 0; i < argCount; ++i) {
            const QMetaType type = metaMethod.parameterMetaType(i);
            if (!type.isValid())
                argCount = 0;
            else if (type.flags().testFlag(QMetaType::IsEnumeration))
                storage.append(type.underlyingType());
            else
                storage.append(type);
        }

        QQmlJavaScriptExpression::evaluate(a, storage.constData(), argCount);
    } else {
        void *ignoredResult = nullptr;
        QMetaType invalidType;
        QQmlJavaScriptExpression::evaluate(&ignoredResult, &invalidType, 0);
    }

    ep->dereferenceScarceResources(); // "release" scarce resources if top-level expression evaluation is complete.
}

////////////////////////////////////////////////////////////////////////


/*! \internal
    \a signal MUST be in the signal index range (see QObjectPrivate::signalIndex()).
    This is different from QMetaMethod::methodIndex().
*/
QQmlBoundSignal::QQmlBoundSignal(QObject *target, int signal, QObject *owner,
                                 QQmlEngine *engine)
    : QQmlNotifierEndpoint(QQmlNotifierEndpoint::QQmlBoundSignal),
      m_prevSignal(nullptr), m_nextSignal(nullptr),
      m_enabled(true)
{
    addToObject(owner);

    /*
        If this is a cloned method, connect to the 'original'. For example,
        for the signal 'void aSignal(int parameter = 0)', if the method
        index refers to 'aSignal()', get the index of 'aSignal(int)'.
        This ensures that 'parameter' will be available from QML.
    */
    signal = QQmlPropertyCache::originalClone(target, signal);
    QQmlNotifierEndpoint::connect(target, signal, engine);
}

QQmlBoundSignal::~QQmlBoundSignal()
{
    removeFromObject();
}

void QQmlBoundSignal::addToObject(QObject *obj)
{
    Q_ASSERT(!m_prevSignal);
    Q_ASSERT(obj);

    QQmlData *data = QQmlData::get(obj, true);

    m_nextSignal = data->signalHandlers;
    if (m_nextSignal) m_nextSignal->m_prevSignal = &m_nextSignal;
    m_prevSignal = &data->signalHandlers;
    data->signalHandlers = this;
}

void QQmlBoundSignal::removeFromObject()
{
    if (m_prevSignal) {
        *m_prevSignal = m_nextSignal;
        if (m_nextSignal) m_nextSignal->m_prevSignal = m_prevSignal;
        m_prevSignal = nullptr;
        m_nextSignal = nullptr;
    }
}

/*!
    Returns the signal expression.
*/
QQmlBoundSignalExpression *QQmlBoundSignal::expression() const
{
    return m_expression.data();
}

/*!
    Sets the signal expression to \a e.

    The QQmlBoundSignal instance takes ownership of \a e (and does not add a reference).
*/
void QQmlBoundSignal::takeExpression(QQmlBoundSignalExpression *e)
{
    m_expression.adopt(e);
    if (m_expression)
        m_expression->setNotifyOnValueChanged(false);
}

/*!
    This property holds whether the item will emit signals.

    The QQmlBoundSignal callback will only emit a signal if this property is set to true.

    By default, this property is true.
 */
void QQmlBoundSignal::setEnabled(bool enabled)
{
    if (m_enabled == enabled)
        return;

    m_enabled = enabled;
}

void QQmlBoundSignal_callback(QQmlNotifierEndpoint *e, void **a)
{
    QQmlBoundSignal *s = static_cast<QQmlBoundSignal*>(e);

    if (!s->m_expression || !s->m_enabled)
        return;

    QV4DebugService *service = QQmlDebugConnector::service<QV4DebugService>();
    if (service)
        service->signalEmitted(QString::fromUtf8(QMetaObjectPrivate::signal(
                                                     s->m_expression->target()->metaObject(),
                                                     s->signalIndex()).methodSignature()));

    QQmlEngine *engine;
    if (s->m_expression && (engine = s->m_expression->engine())) {
        Q_TRACE_SCOPE(QQmlHandlingSignal, engine,
                      s->m_expression->function() ? s->m_expression->function()->name()->toQString() : QString(),
                      s->m_expression->sourceLocation().sourceFile, s->m_expression->sourceLocation().line,
                      s->m_expression->sourceLocation().column);
        QQmlHandlingSignalProfiler prof(QQmlEnginePrivate::get(engine)->profiler,
                                        s->m_expression.data());
        s->m_expression->evaluate(a);
        if (s->m_expression && s->m_expression->hasError()) {
            QQmlEnginePrivate::warning(engine, s->m_expression->error(engine));
        }
    }
}

////////////////////////////////////////////////////////////////////////

QQmlPropertyObserver::QQmlPropertyObserver(QQmlBoundSignalExpression *expr)
    : QPropertyObserver([](QPropertyObserver *self, QUntypedPropertyData *) {
                           auto This = static_cast<QQmlPropertyObserver*>(self);
                           This->expression->evaluate(nullptr);
                       })
{
    expression.adopt(expr);
}

QT_END_NAMESPACE