• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ossia / score / 32592948311

22 Aug 2026 07:10PM UTC coverage: 20.95% (+0.2%) from 20.797%
32592948311

Pull #2212

github

web-flow
Merge aa1811323 into bc2529e99
Pull Request #2212: test: make rendering testable from master

64 of 327 new or added lines in 26 files covered. (19.57%)

136 existing lines in 15 files now uncovered.

44893 of 214283 relevant lines covered (20.95%)

5977.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.01
/src/plugins/score-plugin-js/JS/ApplicationPlugin.cpp
1
#include "ApplicationPlugin.hpp"
2

3
#include <JS/DocumentPlugin.hpp>
4
#include <JS/Qml/DeviceContext.hpp>
5
#include <JS/Qml/EditContext.hpp>
6
#include <JS/Qml/Utils.hpp>
7
#include <JS/Qml/ViewContext.hpp>
8
#include <Library/LibrarySettings.hpp>
9
#include <LocalTree/LocalTreeDocumentPlugin.hpp>
10

11
#include <core/application/ApplicationInterface.hpp>
12
#include <core/document/Document.hpp>
13
#include <core/presenter/DocumentManager.hpp>
14

15
#include <ossia/detail/thread.hpp>
16

17
#include <ossia-qt/invoke.hpp>
18
#include <ossia-qt/qml_protocols.hpp>
19

20
#include <QCommandLineParser>
21
#include <QFile>
22
#include <QFileInfo>
23

24
#if __has_include(<QQuickWindow>)
25
#include <QGuiApplication>
26
#include <QQuickItem>
27
#include <QQuickWindow>
28
#endif
29

30
#if SCORE_HAS_GPU_JS
31
#include <Gfx/Settings/Model.hpp>
32
#endif
33

34
#include <ossia/network/context.hpp>
35

36
namespace JS
37
{
38
// Whether --script was given a program or the path of one. An existing file is
39
// always a path; anything with JS punctuation in it is a program.
40
static bool stringIsScript(const QString& input)
200 ✔
41
{
42
  if(input.isEmpty())
200 ✔
43
    return false;
194 ✔
44

45
  if(QFileInfo fileInfo{input}; fileInfo.exists() && fileInfo.isFile())
10 ✔
46
    return false;
4 ✔
47

48
  if(input.length() > 4096)
2 ✔
NEW
49
    return true;
×
50

51
  for(QChar ch : input)
38 ✔
52
  {
53
    const char16_t c = ch.unicode();
37 ✔
54
    if(c == '\n' || c == '\r' || c == ';' || c == '{' || c == '}' || c == '('
37 ✔
55
       || c == ')')
37 ✔
56
      return true;
1 ✔
57
  }
58

59
  return false;
1 ✔
60
}
200 ✔
61

62
ApplicationPlugin::ApplicationPlugin(const score::GUIApplicationContext& ctx)
200 ✔
63
    : score::GUIApplicationPlugin{ctx}
200 ✔
64
{
200 ✔
65
  // For the console
66
  m_consoleEngine.globalObject().setProperty("Score", m_consoleEngine.newQObject(new EditJsContext));
200 ✔
67
  m_consoleEngine.globalObject().setProperty("Util", m_consoleEngine.newQObject(new JsUtils));
200 ✔
68
  m_consoleEngine.globalObject().setProperty(
400 ✔
69
      "System", m_consoleEngine.newQObject(new JsSystem));
200 ✔
70
  m_consoleEngine.globalObject().setProperty(
400 ✔
71
      "Library", m_consoleEngine.newQObject(new JsLibrary));
200 ✔
72
  m_consoleEngine.globalObject().setProperty("Device", m_consoleEngine.newQObject(new DeviceContext{m_consoleEngine}));
200 ✔
73
  m_consoleEngine.globalObject().setProperty("View", m_consoleEngine.newQObject(new JsViewContext));
200 ✔
74
  connect(&m_consoleEngine, &QQmlEngine::exit, this, [&](int retCode) {
204 ✔
75
    for(auto& doc : score::GUIAppContext().docManager.documents())
8 ✔
76
      doc->commandStack().markCurrentIndexAsSaved();
4 ✔
77
    // quit() is exit(0), which discarded the code Qt.exit() was given: a script
78
    // could stop the app but never report that it had failed.
79
    qApp->exit(retCode);
4 ✔
80
    QTimer::singleShot(
4 ✔
81
        500, [] { score::GUIApplicationInterface::instance().forceExit(); });
×
82
  });
4 ✔
83
  m_asioContext = std::make_shared<ossia::net::network_context>();
200 ✔
84
  m_processMessages = true;
200 ✔
85
  m_consoleEngine.globalObject().setProperty(
400 ✔
86
      "Protocols",
200 ✔
87
      m_consoleEngine.newQObject(new ossia::qt::qml_protocols{m_asioContext, this}));
200 ✔
88
  m_asioThread = std::thread{[this] {
400 ✔
89
    ossia::set_thread_name("ossia app asio");
200 ✔
90
    while(m_processMessages)
400 ✔
91
    {
92
      m_asioContext->run();
200 ✔
93
    }
94
  }};
200 ✔
95

96
  // For scripts of processes that run in the ui thread:
97
  m_scriptProcessUIEngine.globalObject().setProperty(
400 ✔
98
      "Util", m_scriptProcessUIEngine.newQObject(new JsUtils));
200 ✔
99
  m_scriptProcessUIEngine.globalObject().setProperty(
400 ✔
100
      "System", m_scriptProcessUIEngine.newQObject(new JsSystem));
200 ✔
101
  m_scriptProcessUIEngine.globalObject().setProperty(
400 ✔
102
      "Library", m_scriptProcessUIEngine.newQObject(new JsLibrary));
200 ✔
103
  m_scriptProcessUIEngine.globalObject().setProperty(
400 ✔
104
      "View", m_scriptProcessUIEngine.newQObject(new JsViewContext));
200 ✔
105

106
  // Command-line option parsing
107
  QCommandLineParser parser;
200 ✔
108

109
  QCommandLineOption script_opt(
200 ✔
110
      "script", QCoreApplication::translate("js", "script"), "Script", "");
200 ✔
111
  parser.addOption(script_opt);
200 ✔
112

113
  parser.parse(ctx.applicationSettings.arguments);
200 ✔
114
  const auto script = parser.value(script_opt);
200 ✔
115
  if(stringIsScript(script))
200 ✔
116
  {
117
    this->m_start_script = script;
1 ✔
118
  }
1 ✔
119
  else if(!script.isEmpty())
199 ✔
120
  {
121
    QFile f{script};
5 ✔
122
    if(f.open(QIODevice::ReadOnly))
5 ✔
123
    {
124
      this->m_start_script = f.readAll();
4 ✔
125
      this->m_start_script_name = script;
4 ✔
126
      this->m_start_script_path = QFileInfo{f}.canonicalPath();
4 ✔
127
    }
4 ✔
128
    else
129
    {
130
      qCritical() << "--script: cannot open" << script << ":" << f.errorString();
1 ✔
131
      this->m_start_script_failed = true;
1 ✔
132
    }
133
  }
5 ✔
UNCOV
134
}
×
135

136
void ApplicationPlugin::on_newDocument(score::Document& doc)
97 ✔
137
{
138
  score::addDocumentPlugin<DocumentPlugin>(doc);
97 ✔
139
}
97 ✔
140

141
ApplicationPlugin::~ApplicationPlugin()
398 ✔
142
{
199 ✔
143
  m_processMessages = false;
199 ✔
144
  m_asioContext->context.stop();
199 ✔
145
  m_asioThread.join();
199 ✔
146
}
398 ✔
147

148
void ApplicationPlugin::on_createdDocument(score::Document& doc)
102 ✔
149
{
150
  // Local Tree
151
  LocalTree::DocumentPlugin* lt = doc.context().findPlugin<LocalTree::DocumentPlugin>();
102 ✔
152
  if(lt)
102 ✔
153
  {
154
    auto& root = lt->device().get_root_node();
102 ✔
155

156
    auto node = root.create_child("script");
102 ✔
157
    auto address = node->create_parameter(ossia::val_type::STRING);
102 ✔
158
    address->set_value(std::string{});
102 ✔
159
    address->set_access(ossia::access_mode::SET);
102 ✔
160
    address->add_callback([&](const ossia::value& v) {
102 ✔
161
      ossia::qt::run_async(
×
162
          this, [this, str = QString::fromStdString(ossia::convert<std::string>(v))] {
×
163
        auto res = m_consoleEngine.evaluate(str);
×
164
        if(res.isError())
×
165
        {
166
          qDebug() << res.toString();
×
167
        }
×
168
      });
×
169
    });
×
170
  }
102 ✔
171

172
  // Custom data
173
  if(auto customData = doc.context().findPlugin<DocumentPlugin>(); !customData)
102 ✔
174
    score::addDocumentPlugin<DocumentPlugin>(doc);
×
175

176
  if(m_start_script_failed)
102 ✔
177
  {
178
    qGuiApp->exit(2);
1 ✔
179
    return;
1 ✔
180
  }
181

182
  if(!m_start_script.isEmpty())
101 ✔
183
  {
184
    QTimer::singleShot(100, this, [this] {
10 ✔
185
      if(!m_start_script_path.isEmpty())
5 ✔
186
        m_consoleEngine.addImportPath(m_start_script_path);
4 ✔
187

188
      // Report a throwing --script: an unresolvable readFile returns an empty
189
      // string and eval("") is a no-op, so the process would otherwise exit
190
      // reporting success and a harness could not tell that from a pass.
191
      const auto res = m_consoleEngine.evaluate(m_start_script, m_start_script_name);
5 ✔
192
      if(res.isError())
5 ✔
193
      {
194
        qCritical().noquote()
2 ✔
195
            << "--script:"
1 ✔
196
            << (m_start_script_name.isEmpty() ? QStringLiteral("<inline>")
1 ✔
197
                                              : m_start_script_name)
1 ✔
198
            << "line" << res.property("lineNumber").toInt() << ":" << res.toString();
1 ✔
199
        qGuiApp->exit(3);
1 ✔
200
      }
1 ✔
201
    });
5 ✔
202
  }
5 ✔
203
}
102 ✔
204
void ApplicationPlugin::afterStartup()
6 ✔
205
{
206
  // Dummy engine setup for JS processes
207
  // eng.importModule(
208
  //     "/home/jcelerier/Documents/ossia/score/packages/default/Scripts/include/"
209
  //     "tonal.mjs");
210
  for(auto& p : this->context.settings<Library::Settings::Model>().getIncludePaths())
6 ✔
211
  {
212
    m_scriptProcessUIEngine.addImportPath(p);
×
213
  }
214

215
#if __has_include(<QQuickWindow>)
216
  if(QFileInfo f{context.applicationSettings.ui}; f.isFile())
6 ✔
217
  {
218
    m_comp = new QQmlComponent{&m_consoleEngine, f.absoluteFilePath(), this};
×
219

220
    if(auto obj = m_comp->create())
×
221
    {
222
      if(auto item = qobject_cast<QQuickItem*>(obj))
×
223
      {
224
        m_window = new QQuickWindow{};
×
225
  // QWidget gets these from QWidgetPrivate::adjustFlags; a bare QQuickWindow
226
  // does not, and on platforms where Qt draws the chrome itself (wasm) that
227
  // leaves the window with no title bar, close or minimise button.
228
  m_window->setFlags(
×
229
      m_window->flags() | Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint
×
230
      | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint
×
231
      | Qt::WindowMaximizeButtonHint);
×
232
#if defined(__EMSCRIPTEN__)
233
  // Qt for wasm reports ShowIsFullScreen unconditionally, so QWindow::show()
234
  // turns into showFullScreen() for every top level: the requested size is
235
  // discarded and the full-screen state suppresses the frame. Only Qt::Dialog
236
  // and Qt::Popup opt out (QWasmIntegration::defaultWindowState).
237
  m_window->setFlags(m_window->flags() | Qt::Dialog);
238
#endif
239
        m_window->setWidth(640);
×
240
        m_window->setHeight(480);
×
241
        item->setParentItem(m_window->contentItem());
×
242
#if defined(__EMSCRIPTEN__)
243
        m_window->showNormal();
244
#else
245
        m_window->show();
×
246
#endif
247
        return;
×
248
      }
249
    }
×
250
    else
251
    {
252
      qDebug() << m_comp->errorString();
×
253
      qGuiApp->exit(1);
×
254
    }
255
    delete m_comp;
×
256
  }
×
257
#endif
258
}
6 ✔
259
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc