diff --git a/src/Lib/Script/CRTPLuaScriptObject.hh b/src/Lib/Script/CRTPLuaScriptObject.hh index 9d189783a16d21334ca3f57d41ce74a0fc5d110e..623de553698c19946a57833b74b432ce2cb739de 100644 --- a/src/Lib/Script/CRTPLuaScriptObject.hh +++ b/src/Lib/Script/CRTPLuaScriptObject.hh @@ -48,6 +48,13 @@ getJobIteratorTypeFromString(const std::string_view it) noexcept // CRTP to expose objects to Lua template <class Object> class CRTPLuaScriptObject { protected: + static void _Noreturn luaGlobalError(lua_State *const L, const std::string &str) noexcept + { + const auto *const context = LuaContext::getContext(L); + lua_pushfstring(L, "%s:0:%s", context->getCurrentLuaFile().c_str(), str.c_str()); + lua_error(L); + } + static bool PushFunctionFromRegistry(lua_State *const L, const int key) noexcept { if (key >= 0) { diff --git a/src/Lib/Script/CRTPLuaScriptObject/ModuleDeclaration.cc b/src/Lib/Script/CRTPLuaScriptObject/ModuleDeclaration.cc index ba0a4b32526fd89552bce24580a18fa4205ec56d..de147455ce4b2439a21037dcea61e5ea26afe0e8 100644 --- a/src/Lib/Script/CRTPLuaScriptObject/ModuleDeclaration.cc +++ b/src/Lib/Script/CRTPLuaScriptObject/ModuleDeclaration.cc @@ -50,8 +50,6 @@ ModuleDeclaration::importMethod(lua_State *const L) noexcept return 1; } -#include <iostream> - int ModuleDeclaration::exportMethod(lua_State *const L) noexcept { @@ -204,16 +202,13 @@ ModuleDeclaration::pushToRuntime(lua_State *const L) noexcept self->importNames.emplace_back(self->moduleName); // You can use localy defined things Utils::uniqAndSort<std::string>(self->importNames); // Better for all std algorithms - if (self->validateModule(L)) { - err(context) << "Register module \"" << self->moduleName << "\" in the runtime!\n"; - lua_settop(L, 1); - if (context->registerDeclaration(self) == LuaContext::Code::Error) - context->setFailed("Failed to register module " + self->moduleName); - } + self->validateModule(L); // May abort the LUA context + err(context) << "Register module \"" << self->moduleName << "\" in the runtime!\n"; + lua_settop(L, 1); - else - context->setFailed("Module " + self->moduleName + " is invalid"); + if (context->registerDeclaration(self) == LuaContext::Code::Error) + context->setFailed("Failed to register module " + self->moduleName); // TODO: Import needed modules here! const std::string_view importNeededModulesHere = "Import needed modules here!"; @@ -221,16 +216,12 @@ ModuleDeclaration::pushToRuntime(lua_State *const L) noexcept LUA_RETURN_NOTHING(L); } -bool +void ModuleDeclaration::validateModule(lua_State *const L) const noexcept { - auto *const context = LuaContext::getContext(L); - // Minimal module file - if (moduleName.empty() || authorName.empty()) { - context->setFailed("The module does not have the minimal required information"); - return false; - } + if (moduleName.empty() || authorName.empty()) + luaGlobalError(L, "The module does not have the minimal required information"); // Imports { @@ -250,20 +241,15 @@ ModuleDeclaration::validateModule(lua_State *const L) const noexcept Utils::sortedSetDifference(usedImports, importNames); if (!unusedImports.empty()) { - std::string listStrImports = ""; - std::size_t stringSize = 0; + std::size_t stringSize = 0; + std::string listStrImports = + "There are imported modules that are used withour begin declared:"; for (const auto &str : unusedImports) stringSize += 1 + str.size(); listStrImports.reserve(stringSize); for (const auto &str : unusedImports) listStrImports += " " + str; - - context->setFailed("There are imported modules that are " - "used without being declared:" + - listStrImports + "\n"); - return false; + luaGlobalError(L, listStrImports); } } - - return true; }