MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / loadScript

Method loadScript

app/src/DataModel/Scripting/LuaScriptEngine.cpp:301–354  ·  view source on GitHub ↗

* @brief Validates and loads a Lua frame parser script, caching parse() in the * registry so the hotpath skips a global lookup on every frame. */

Source from the content-addressed store, hash-verified

299 * registry so the hotpath skips a global lookup on every frame.
300 */
301bool DataModel::LuaScriptEngine::loadScript(const QString& script,
302 int sourceId,
303 bool showMessageBoxes)
304{
305 Q_ASSERT(sourceId >= 0);
306 Q_ASSERT(!script.isEmpty());
307
308 destroyState();
309 m_sourceId = sourceId;
310 createState();
311
312 try {
313 const QByteArray utf8 = script.toUtf8();
314 const auto fileName = QStringLiteral("parser_%1.lua").arg(sourceId).toUtf8();
315 const int status =
316 luaL_loadbuffer(m_state, utf8.constData(), utf8.size(), fileName.constData());
317 if (status != LUA_OK) {
318 const QString errorMsg = QString::fromUtf8(lua_tostring(m_state, -1));
319 lua_pop(m_state, 1);
320 if (showMessageBoxes) {
321 Misc::Utilities::showMessageBox(
322 QObject::tr("Lua Syntax Error"),
323 QObject::tr("The parser code contains an error:\n\n%1").arg(errorMsg),
324 QMessageBox::Critical);
325 } else {
326 qWarning() << "[LuaScriptEngine] Source" << sourceId << "syntax error:" << errorMsg;
327 }
328 return false;
329 }
330
331 if (!runLoadedChunk(sourceId, showMessageBoxes))
332 return false;
333
334 if (!ensureParseFunction(sourceId, showMessageBoxes))
335 return false;
336
337 if (sourceId == 0 && !probeParseFunction(sourceId, showMessageBoxes))
338 return false;
339
340 lua_getglobal(m_state, "parse");
341 m_parseRef = luaL_ref(m_state, LUA_REGISTRYINDEX);
342
343 m_loaded = true;
344 return true;
345 } catch (const std::exception& e) {
346 qWarning() << "[LuaScriptEngine] Source" << sourceId << "load uncaught exception:" << e.what();
347 } catch (...) {
348 qWarning() << "[LuaScriptEngine] Source" << sourceId << "load uncaught non-std exception";
349 }
350
351 destroyState();
352 createState();
353 return false;
354}
355
356/**
357 * @brief Runs the just-loaded chunk under the watchdog and reports any runtime error.

Callers

nothing calls this directly

Calls 4

lua_getglobalFunction · 0.85
luaL_refFunction · 0.85
isEmptyMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected