From 3e4cfcb88ef0105932af8ce121eb76e3284c5f18 Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Tue, 24 May 2022 00:29:30 +0200 Subject: [PATCH 1/2] Simplify invocation of JS functions without parameters --- src/controllers/scripting/controllerscriptenginebase.h | 2 +- src/controllers/scripting/controllerscriptmoduleengine.cpp | 4 ++-- .../scripting/legacy/controllerscriptenginelegacy.h | 2 +- .../scripting/legacy/controllerscriptinterfacelegacy.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/controllers/scripting/controllerscriptenginebase.h b/src/controllers/scripting/controllerscriptenginebase.h index 232683fdabf..2bccd8f031c 100644 --- a/src/controllers/scripting/controllerscriptenginebase.h +++ b/src/controllers/scripting/controllerscriptenginebase.h @@ -25,7 +25,7 @@ class ControllerScriptEngineBase : public QObject { virtual bool initialize(); - bool executeFunction(QJSValue functionObject, const QJSValueList& arguments); + bool executeFunction(QJSValue functionObject, const QJSValueList& arguments = {}); /// Shows a UI dialog notifying of a script evaluation error. /// Precondition: QJSValue.isError() == true diff --git a/src/controllers/scripting/controllerscriptmoduleengine.cpp b/src/controllers/scripting/controllerscriptmoduleengine.cpp index 7f3f8ad5e73..bccb728fc05 100644 --- a/src/controllers/scripting/controllerscriptmoduleengine.cpp +++ b/src/controllers/scripting/controllerscriptmoduleengine.cpp @@ -32,7 +32,7 @@ bool ControllerScriptModuleEngine::initialize() { } QJSValue initFunction = mod.property("init"); - if (!executeFunction(initFunction, QJSValueList{})) { + if (!executeFunction(initFunction)) { shutdown(); return false; } @@ -47,6 +47,6 @@ bool ControllerScriptModuleEngine::initialize() { } void ControllerScriptModuleEngine::shutdown() { - executeFunction(m_shutdownFunction, QJSValueList()); + executeFunction(m_shutdownFunction); ControllerScriptEngineBase::shutdown(); } diff --git a/src/controllers/scripting/legacy/controllerscriptenginelegacy.h b/src/controllers/scripting/legacy/controllerscriptenginelegacy.h index 3e70b69a7ac..70dea6a392e 100644 --- a/src/controllers/scripting/legacy/controllerscriptenginelegacy.h +++ b/src/controllers/scripting/legacy/controllerscriptenginelegacy.h @@ -36,7 +36,7 @@ class ControllerScriptEngineLegacy : public ControllerScriptEngineBase { QJSValue wrapArrayBufferCallback(const QJSValue& callback); bool callFunctionOnObjects(const QList& scriptFunctionPrefixes, const QString&, - const QJSValueList& args = QJSValueList(), + const QJSValueList& args = {}, bool bFatalError = false); QJSValue m_makeArrayBufferWrapperFunction; diff --git a/src/controllers/scripting/legacy/controllerscriptinterfacelegacy.cpp b/src/controllers/scripting/legacy/controllerscriptinterfacelegacy.cpp index a9eea9d4191..00abff10020 100644 --- a/src/controllers/scripting/legacy/controllerscriptinterfacelegacy.cpp +++ b/src/controllers/scripting/legacy/controllerscriptinterfacelegacy.cpp @@ -522,7 +522,7 @@ void ControllerScriptInterfaceLegacy::timerEvent(QTimerEvent* event) { stopTimer(timerId); } - m_pScriptEngineLegacy->executeFunction(timerTarget.callback, QJSValueList()); + m_pScriptEngineLegacy->executeFunction(timerTarget.callback); } void ControllerScriptInterfaceLegacy::softTakeover( From a4b7c20c292a722edd7e7d6ea4830daeb83e926c Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Tue, 24 May 2022 00:33:31 +0200 Subject: [PATCH 2/2] Construct immutable QJSValueList from std::initializer_list args --- src/controllers/midi/midicontroller.cpp | 13 ++++++------ .../legacy/controllerscriptenginelegacy.cpp | 20 +++++++++---------- .../scripting/legacy/scriptconnection.cpp | 9 +++++---- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/controllers/midi/midicontroller.cpp b/src/controllers/midi/midicontroller.cpp index 3131cb50244..a10a2a22491 100644 --- a/src/controllers/midi/midicontroller.cpp +++ b/src/controllers/midi/midicontroller.cpp @@ -266,12 +266,13 @@ void MidiController::processInputMapping(const MidiInputMapping& mapping, } QJSValue function = pEngine->wrapFunctionCode(mapping.control.item, 5); - QJSValueList args; - args << QJSValue(channel); - args << QJSValue(control); - args << QJSValue(value); - args << QJSValue(status); - args << QJSValue(mapping.control.group); + const auto args = QJSValueList{ + channel, + control, + value, + status, + mapping.control.group, + }; if (!pEngine->executeFunction(function, args)) { qCWarning(m_logBase) << "MidiController: Invalid script function" << mapping.control.item; diff --git a/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp b/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp index 24ffbd27442..14b2fc9569a 100644 --- a/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp +++ b/src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp @@ -152,13 +152,12 @@ bool ControllerScriptEngineLegacy::initialize() { wrapFunctionCode(functionName, 2))); } - QJSValueList args; - if (m_pController) { - args << QJSValue(m_pController->getName()); - } else { // m_pController is nullptr in tests. - args << QJSValue(); - } - args << QJSValue(m_logger().isDebugEnabled()); + // m_pController is nullptr in tests. + const auto controllerName = m_pController ? m_pController->getName() : QString{}; + const auto args = QJSValueList{ + controllerName, + m_logger().isDebugEnabled(), + }; if (!callFunctionOnObjects(m_scriptFunctionPrefixes, "init", args, true)) { shutdown(); return false; @@ -182,9 +181,10 @@ bool ControllerScriptEngineLegacy::handleIncomingData(const QByteArray& data) { return false; } - QJSValueList args; - args << m_pJSEngine->toScriptValue(data); - args << QJSValue(static_cast(data.size())); + const auto args = QJSValueList{ + m_pJSEngine->toScriptValue(data), + static_cast(data.size()), + }; for (const QJSValue& function : std::as_const(m_incomingDataFunctions)) { ControllerScriptEngineBase::executeFunction(function, args); diff --git a/src/controllers/scripting/legacy/scriptconnection.cpp b/src/controllers/scripting/legacy/scriptconnection.cpp index 2f66346db3f..4ad9ea71754 100644 --- a/src/controllers/scripting/legacy/scriptconnection.cpp +++ b/src/controllers/scripting/legacy/scriptconnection.cpp @@ -3,10 +3,11 @@ #include "controllers/scripting/legacy/controllerscriptenginelegacy.h" void ScriptConnection::executeCallback(double value) const { - QJSValueList args; - args << QJSValue(value); - args << QJSValue(key.group); - args << QJSValue(key.item); + const auto args = QJSValueList{ + value, + key.group, + key.item, + }; QJSValue func = callback; // copy function because QJSValue::call is not const QJSValue result = func.call(args); if (result.isError()) {