diff --git a/src/Lib/Script/CRTPLuaScriptObject.cc b/src/Lib/Script/CRTPLuaScriptObject.cc index d7dd9ac53aa5f74964eb46896fcea9f3efcf43a7..87ae0a28682f5a8ac9626939c4ec49fa7123c038 100644 --- a/src/Lib/Script/CRTPLuaScriptObject.cc +++ b/src/Lib/Script/CRTPLuaScriptObject.cc @@ -72,8 +72,14 @@ int ModuleDeclaration::setImplementationFile(lua_State *const L) noexcept { auto *const self = ModuleDeclaration::CHECK(L, 1); + auto *const context = LuaContext::getContext(L); self->implementationFile = CHECK_STRING_VIEW(L, 2); Utils::trim(self->implementationFile); + self->implementationFile = + QFileInfo(QFileInfo(context->getCurrentLuaFile().c_str()).absoluteDir(), + self->implementationFile.c_str()) + .absoluteFilePath() + .toStdString(); LUA_RETURN_NOTHING(L); } diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc index 0b15a53970878a13ae595bf3cbe877d26cd00221..19967c7cc92c5462f92ee28494045d71e3d65988 100644 --- a/src/Lib/Script/LuaContext.cc +++ b/src/Lib/Script/LuaContext.cc @@ -58,14 +58,32 @@ LuaContext::getContext(const lua_State *const state) noexcept LuaContext::Code LuaContext::loadDocument(std::shared_ptr<ScriptDocument> doc) noexcept { - QFileInfo fileCheck(doc->getName()); + const QFileInfo fileCheck(doc->getName()); if (!fileCheck.exists()) { err(this) << "File " << doc->getName().toStdString() << " for document " << doc->getUuid().toString().toStdString() << " doesn't exists\n"; return Code::Error; } - return loadFile(fileCheck.absoluteFilePath().toStdString().c_str()); + lastLoadedModule = ""; + auto rc = loadFile(fileCheck.absoluteFilePath().toStdString().c_str()); + if (rc != Code::Success) { + setFailed("Failed to load the module file"); + return rc; + } + + if (lastLoadedModule.empty()) { + setFailed("No module was loaded"); + return Code::Error; + } + + auto *const mod = getModule(lastLoadedModule); + const QFileInfo impl(mod->implementationFile.c_str()); + if (Code::Success != loadFile(impl.absoluteFilePath().toStdString().c_str())) { + setFailed("Failed to load implementation file"); + return Code::Error; + } + return Code::Success; } void @@ -137,6 +155,7 @@ LuaContext::registerModuleDeclaration(const ModuleDeclaration *const mod) noexce ModuleDeclaration::CHECK(L, -1); const int r = luaL_ref(L, LUA_REGISTRYINDEX); modules.emplace(mod->moduleName, r); + lastLoadedModule = mod->moduleName; return Code::Success; } @@ -178,13 +197,13 @@ void LuaContext::setFailed(const std::string &msg) noexcept { failureString = msg; - streamErr << "LuaContext is now in failure state: " << msg; + streamErr << "LuaContext is now in failure state: " << msg << "\n"; } bool LuaContext::isFailed() const noexcept { - return failureString.empty(); + return failureString.size() != 0; } const std::string & diff --git a/src/Lib/Script/LuaContext.hh b/src/Lib/Script/LuaContext.hh index 63abb7ca354388f1f332afb92e0c5181defb6169..faf5808ca03ab6ca5f9d28f6cfeb5028997d76b9 100644 --- a/src/Lib/Script/LuaContext.hh +++ b/src/Lib/Script/LuaContext.hh @@ -25,6 +25,7 @@ class LuaContext final { lua_State *L{ nullptr }; std::string failureString{}; std::string currentFile{}; + std::string lastLoadedModule{}; std::map<const std::string_view, const int> modules = {}; static inline std::map<const lua_State *const, LuaContext *const> contextList = {}; @@ -67,6 +68,7 @@ public: // Makes only sens when called from within loadFile and loadPackagedFile const std::string &getCurrentLuaFile() const; + void setLastLoadedModule(const std::string &) noexcept; private: // Exec all loaded strings and files diff --git a/src/Lib/Script/ScriptStore.cc b/src/Lib/Script/ScriptStore.cc index 6a0d6f8f6263832e1dcfb0ef0aae342e90072301..68669652e2c9d5aff8c2e33d4c3a6f8b36276658 100644 --- a/src/Lib/Script/ScriptStore.cc +++ b/src/Lib/Script/ScriptStore.cc @@ -4,6 +4,7 @@ using namespace Vivy; using namespace std::string_literals; +using Code = Script::LuaContext::Code; ScriptStore::ScriptStore() noexcept { resetLoadedScripts(); } @@ -13,10 +14,14 @@ ScriptStore::loadScriptFolder(const QString &folderPath) QDir folder(folderPath); const auto filter = QDir::Files | QDir::NoSymLinks | QDir::Readable; - // TODO: Sort the list with the dependencies between the scripts + // Load all module files, the loadDocument will ask for the implementation + // file. for (const QString &file : folder.entryList(filter)) { + if (!Utils::scriptFileSuffix.contains(QFileInfo(file).suffix())) + continue; + auto doc = loadDocument(file); - if (luaContext->loadDocument(doc) != Script::LuaContext::Code::Success) { + if (luaContext->loadDocument(doc) != Code::Success) { const QString error = QStringLiteral("File: ") + file + QStringLiteral(" [") + doc->getUuid().toString() + QStringLiteral("]") + luaContext->getLastLuaError(); @@ -40,11 +45,11 @@ ScriptStore::resetLoadedScripts() noexcept luaContext.reset(new Script::LuaContext(streamOut, streamErr)); } -void +bool ScriptStore::executeScript(Uuid id) noexcept { - auto script = documents.at(id); - luaContext->loadDocument(script); + const auto script = documents.at(id); + const bool ok = luaContext->loadDocument(script) == Code::Success; std::string buffer; std::cout << "STDOUT:\n"; @@ -54,6 +59,17 @@ ScriptStore::executeScript(Uuid id) noexcept std::cout << "STDERR:\n"; while (std::getline(streamErr, buffer, '\n')) std::cout << buffer << "\n"; + + if (!ok) + std::cout << luaContext->getLastLuaError().toStdString(); + + if (luaContext->isFailed()) { + std::cout << "LuaContext is in failure mode!!\n"; + std::cout << luaContext->getFailureDescription() << "\n"; + return false; + } + + return ok; } const std::vector<std::string_view> diff --git a/src/Lib/Script/ScriptStore.hh b/src/Lib/Script/ScriptStore.hh index 9dd345e9e5e22925e02204d1b0cffcf2d468f113..76d6e56e0099fb8fe07292049f94c8b09d73aa86 100644 --- a/src/Lib/Script/ScriptStore.hh +++ b/src/Lib/Script/ScriptStore.hh @@ -27,7 +27,7 @@ public: void resetLoadedScripts() noexcept; void loadScriptFolder(const QString &folderPath); std::vector<Item> getLoadedScripts() const noexcept; - void executeScript(Uuid) noexcept; + bool executeScript(Uuid) noexcept; // Get modules from scripts const std::vector<std::string_view> getLoadedModules() const noexcept; diff --git a/src/VivyCli.cc b/src/VivyCli.cc index 0b7017e838cfbd9fa48336ff28ef4db3121e6081..1305babcbb026ba9c5984a00aae8529bfcdb2202 100644 --- a/src/VivyCli.cc +++ b/src/VivyCli.cc @@ -13,9 +13,9 @@ VivyCli::VivyCli(int &argc, char **argv) noexcept int VivyCli::exec() noexcept { - if (selectedDoc == nullptr) + if ((selectedDoc == nullptr) || (!scriptStore.executeScript(selectedDoc->getUuid()))) return 1; - scriptStore.executeScript(selectedDoc->getUuid()); + for (const auto &str : scriptStore.getLoadedModules()) { std::cout << "Module " << str << " was loaded!\n"; const auto *mod = scriptStore.getModule(str);