Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2024 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_EXTENSIONS_SCRIPTENGINE_H
23 : #define MUSE_EXTENSIONS_SCRIPTENGINE_H
24 :
25 : #include <QString>
26 : #include <QJSValue>
27 : #include <QJSEngine>
28 : #include <QVariantMap>
29 : #include <QByteArray>
30 :
31 : #include "modularity/ioc.h"
32 : #include "global/io/ifilesystem.h"
33 : #include "global/types/ret.h"
34 : #include "global/api/iapiregister.h"
35 :
36 : #include "global/api/iapiengine.h"
37 :
38 : namespace muse::extensions {
39 : class JsModuleLoader;
40 : class ScriptEngine
41 : {
42 : GlobalInject<io::IFileSystem> fileSystem;
43 : GlobalInject<muse::api::IApiRegister> apiRegister;
44 :
45 : public:
46 : ScriptEngine(const modularity::ContextPtr& iocCtx, const muse::api::ApiContext& apiContext);
47 : ~ScriptEngine();
48 :
49 : struct CallData {
50 : QVariantMap context;
51 : QStringList args;
52 : };
53 :
54 : void setScriptPath(const io::path_t& arg);
55 : io::path_t scriptPath() const;
56 :
57 : Ret evaluate();
58 :
59 : Ret call(const QString& funcName, const QJSValueList& args = {}, QJSValue* retVal = nullptr);
60 :
61 : void setGlobalProperty(const QString& name, const QJSValue& val);
62 : QJSValue globalProperty(const QString& name) const;
63 :
64 : void throwError(const QString& message);
65 :
66 : // js modules
67 : QJSValue requireModule(const QString& module);
68 : QJSValue requireFile(const QString& filePath);
69 : QJSValue exports() const;
70 : void setExports(const QJSValue& obj);
71 0 : QJSEngine* jsEngine() const
72 : {
73 0 : return m_engine;
74 : }
75 :
76 : static void dump(const QString& name, const QJSValue& val);
77 : static Ret jsValueToRet(const QJSValue& val);
78 :
79 : private:
80 :
81 : ScriptEngine(ScriptEngine* engine);
82 :
83 : RetVal<ByteArray> readScriptContent(const io::path_t& scriptPath) const;
84 : RetVal<QJSValue> evaluateContent(const QByteArray& fileContent, const io::path_t& filePath);
85 : Ret doCall(const QString& funcName, const QJSValueList& args, QJSValue* retVal);
86 :
87 : struct FuncData {
88 : QString funcName;
89 : QJSValue func;
90 : };
91 :
92 : const modularity::ContextPtr m_iocContext;
93 : QJSEngine* m_engine = nullptr;
94 : muse::api::JsApiEngine* m_apiengine = nullptr;
95 : QObject* m_api = nullptr;
96 : JsModuleLoader* m_moduleLoader = nullptr;
97 : bool m_isRequireMode = false;
98 : io::path_t m_scriptPath;
99 : QByteArray m_lastEvalScript;
100 : FuncData m_lastCallFunc;
101 : };
102 : }
103 :
104 : #endif // MUSE_EXTENSIONS_SCRIPTENGINE_H
|