LCOV - code coverage report
Current view: top level - muse/framework/extensions/internal - jsmoduleloader.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 0.0 % 64 0
Test Date: 2026-11-05 04:07:02

            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              : #include <QFileInfo>
      23              : 
      24              : #include "jsmoduleloader.h"
      25              : 
      26              : #include "scriptengine.h"
      27              : 
      28              : #include "log.h"
      29              : 
      30              : using namespace muse::extensions;
      31              : 
      32            0 : JsModuleLoader::JsModuleLoader(const modularity::ContextPtr& iocCtx, QObject* parent)
      33            0 :     : QObject(parent), muse::Contextable(iocCtx)
      34              : {
      35            0 : }
      36              : 
      37            0 : void JsModuleLoader::pushEngine(ScriptEngine* engine)
      38              : {
      39            0 :     m_engineList.append(engine);
      40            0 : }
      41              : 
      42            0 : void JsModuleLoader::popEngine()
      43              : {
      44            0 :     if (!m_engineList.isEmpty()) {
      45            0 :         m_engineList.removeLast();
      46              :     }
      47            0 : }
      48              : 
      49            0 : ScriptEngine* JsModuleLoader::engine() const
      50              : {
      51            0 :     ScriptEngine* engine = !m_engineList.isEmpty() ? m_engineList.last() : nullptr;
      52            0 :     return engine;
      53              : }
      54              : 
      55            0 : QJSValue JsModuleLoader::require(QString module)
      56              : {
      57              :     //! TODO Make recursion protection
      58            0 :     IF_ASSERT_FAILED(engine()) {
      59            0 :         return QJSValue();
      60              :     }
      61              : 
      62              :     //! NOTE Only public modules are available
      63            0 :     if (module.startsWith("MuseInternal.") || module.startsWith("api.")) {
      64            0 :         LOGE() << "Not allowed module: " << module;
      65            0 :         return QJSValue();
      66              :     }
      67              : 
      68              :     // require built-in module
      69            0 :     if (module.startsWith("MuseApi.")) {
      70            0 :         return engine()->requireModule(module);
      71              :     }
      72              : 
      73              :     // require js file
      74            0 :     bool ok = false;
      75            0 :     QString filePath = resolvePath(module, &ok);
      76            0 :     if (ok) {
      77            0 :         return engine()->requireFile(filePath);
      78              :     }
      79              : 
      80              :     // Require application module
      81            0 :     return engine()->requireModule(module);
      82            0 : }
      83              : 
      84            0 : QJSValue JsModuleLoader::exports() const
      85              : {
      86            0 :     IF_ASSERT_FAILED(engine()) {
      87            0 :         return QJSValue();
      88              :     }
      89              : 
      90            0 :     return engine()->exports();
      91              : }
      92              : 
      93            0 : void JsModuleLoader::setExports(QJSValue exports)
      94              : {
      95            0 :     IF_ASSERT_FAILED(engine()) {
      96            0 :         return;
      97              :     }
      98              : 
      99            0 :     return engine()->setExports(exports);
     100              : }
     101              : 
     102            0 : QString JsModuleLoader::scriptPath() const
     103              : {
     104            0 :     IF_ASSERT_FAILED(engine()) {
     105            0 :         return QString();
     106              :     }
     107            0 :     return QFileInfo(engine()->scriptPath().toQString()).absolutePath();
     108              : }
     109              : 
     110            0 : QString JsModuleLoader::resolvePath(const QString& module, bool* ok)
     111              : {
     112            0 :     QString basePath = scriptPath();
     113            0 :     return resolvePath(basePath, module, ok);
     114            0 : }
     115              : 
     116            0 : QString JsModuleLoader::resolvePath(const QString& basePath, const QString& module, bool* _ok)
     117              : {
     118            0 :     TRACEFUNC;
     119            0 :     QString moduleFile = module.endsWith(".js") ? module : (module + ".js");
     120              : 
     121              :     //! NOTE Check relative path
     122            0 :     io::path_t path = basePath + "/" + moduleFile;
     123            0 :     bool ok = fileSystem()->exists(path);
     124            0 :     if (!ok) {
     125              :         //! NOTE Search module in default paths
     126            0 :         const io::path_t defPath = configuration()->defaultPath();
     127            0 :         path = defPath + "/" + moduleFile;
     128            0 :         ok = fileSystem()->exists(path);
     129            0 :         if (!ok) {
     130            0 :             const io::path_t userPath = configuration()->userPath();
     131            0 :             path = userPath + "/" + moduleFile;
     132            0 :             ok = fileSystem()->exists(path);
     133            0 :         }
     134            0 :     }
     135              : 
     136            0 :     if (!ok) {
     137            0 :         LOGD() << "Not found module file: " << module;
     138              :     }
     139              : 
     140            0 :     if (_ok) {
     141            0 :         *_ok = ok;
     142              :     }
     143              : 
     144            0 :     return ok ? path.toQString() : "";
     145            0 : }
        

Generated by: LCOV version 2.0-1