Commit 71a1ff04 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

scripting: Make QTimer constructible

If a QObject is exposed to js using QJSEngine::newQMetaObject(), a new
instance of it can be made only with constructors exposed by Q_INVOKABLE

At the moment, QTimer's constructor is not Q_INVOKABLE, so code such as
`const timer = new QTimer()` will not work.

BUG: 439630
FIXED-IN: 5.22.4


(cherry picked from commit d29954ae)
parent 54ce4007
......@@ -92,6 +92,11 @@ void KWin::AbstractScript::stop()
deleteLater();
}
KWin::ScriptTimer::ScriptTimer(QObject *parent)
: QTimer(parent)
{
}
KWin::Script::Script(int id, QString scriptName, QString pluginName, QObject* parent)
: AbstractScript(id, scriptName, pluginName, parent)
, m_engine(new QJSEngine(this))
......@@ -167,7 +172,7 @@ void KWin::Script::slotScriptLoadedFromFile()
m_engine->installExtensions(QJSEngine::ConsoleExtension);
// Make the timer visible to QJSEngine.
QJSValue timerMetaObject = m_engine->newQMetaObject(&QTimer::staticMetaObject);
QJSValue timerMetaObject = m_engine->newQMetaObject(&ScriptTimer::staticMetaObject);
m_engine->globalObject().setProperty("QTimer", timerMetaObject);
// Expose enums.
......
......@@ -18,6 +18,7 @@
#include <QStringList>
#include <QJSEngine>
#include <QJSValue>
#include <QTimer>
#include <QDBusContext>
#include <QDBusMessage>
......@@ -83,6 +84,18 @@ private:
bool m_running;
};
/**
* In order to be able to construct QTimer objects in javascript, the constructor
* must be declared with Q_INVOKABLE.
*/
class ScriptTimer : public QTimer
{
Q_OBJECT
public:
Q_INVOKABLE ScriptTimer(QObject *parent = nullptr);
};
class Script : public AbstractScript, QDBusContext
{
Q_OBJECT
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment