From d29954ae3359c4ac5d6e59cef8249427568bc00a Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Mon, 19 Jul 2021 12:12:06 +0300 Subject: [PATCH] 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 --- src/scripting/scripting.cpp | 7 ++++++- src/scripting/scripting.h | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/scripting/scripting.cpp b/src/scripting/scripting.cpp index 6870121c9c..a53ee14616 100644 --- a/src/scripting/scripting.cpp +++ b/src/scripting/scripting.cpp @@ -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. diff --git a/src/scripting/scripting.h b/src/scripting/scripting.h index 85ce7b360d..8814dec94c 100644 --- a/src/scripting/scripting.h +++ b/src/scripting/scripting.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -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