Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 26bbe15f rédigé par Kubat's avatar Kubat
Parcourir les fichiers

SCRIPT: The module validation aborts the lua interpreter on errors

parent b8b925e0
Branches
Aucune étiquette associée trouvée
1 requête de fusion!16Add a way to execute a Lua file with Vivy in a click-click way
...@@ -48,6 +48,13 @@ getJobIteratorTypeFromString(const std::string_view it) noexcept ...@@ -48,6 +48,13 @@ getJobIteratorTypeFromString(const std::string_view it) noexcept
// CRTP to expose objects to Lua // CRTP to expose objects to Lua
template <class Object> class CRTPLuaScriptObject { template <class Object> class CRTPLuaScriptObject {
protected: 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 static bool PushFunctionFromRegistry(lua_State *const L, const int key) noexcept
{ {
if (key >= 0) { if (key >= 0) {
......
...@@ -50,8 +50,6 @@ ModuleDeclaration::importMethod(lua_State *const L) noexcept ...@@ -50,8 +50,6 @@ ModuleDeclaration::importMethod(lua_State *const L) noexcept
return 1; return 1;
} }
#include <iostream>
int int
ModuleDeclaration::exportMethod(lua_State *const L) noexcept ModuleDeclaration::exportMethod(lua_State *const L) noexcept
{ {
...@@ -204,16 +202,13 @@ ModuleDeclaration::pushToRuntime(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 self->importNames.emplace_back(self->moduleName); // You can use localy defined things
Utils::uniqAndSort<std::string>(self->importNames); // Better for all std algorithms Utils::uniqAndSort<std::string>(self->importNames); // Better for all std algorithms
if (self->validateModule(L)) {
self->validateModule(L); // May abort the LUA context
err(context) << "Register module \"" << self->moduleName << "\" in the runtime!\n"; err(context) << "Register module \"" << self->moduleName << "\" in the runtime!\n";
lua_settop(L, 1); lua_settop(L, 1);
if (context->registerDeclaration(self) == LuaContext::Code::Error) if (context->registerDeclaration(self) == LuaContext::Code::Error)
context->setFailed("Failed to register module " + self->moduleName); context->setFailed("Failed to register module " + self->moduleName);
}
else
context->setFailed("Module " + self->moduleName + " is invalid");
// TODO: Import needed modules here! // TODO: Import needed modules here!
const std::string_view importNeededModulesHere = "Import needed modules here!"; const std::string_view importNeededModulesHere = "Import needed modules here!";
...@@ -221,16 +216,12 @@ ModuleDeclaration::pushToRuntime(lua_State *const L) noexcept ...@@ -221,16 +216,12 @@ ModuleDeclaration::pushToRuntime(lua_State *const L) noexcept
LUA_RETURN_NOTHING(L); LUA_RETURN_NOTHING(L);
} }
bool void
ModuleDeclaration::validateModule(lua_State *const L) const noexcept ModuleDeclaration::validateModule(lua_State *const L) const noexcept
{ {
auto *const context = LuaContext::getContext(L);
// Minimal module file // Minimal module file
if (moduleName.empty() || authorName.empty()) { if (moduleName.empty() || authorName.empty())
context->setFailed("The module does not have the minimal required information"); luaGlobalError(L, "The module does not have the minimal required information");
return false;
}
// Imports // Imports
{ {
...@@ -250,20 +241,15 @@ ModuleDeclaration::validateModule(lua_State *const L) const noexcept ...@@ -250,20 +241,15 @@ ModuleDeclaration::validateModule(lua_State *const L) const noexcept
Utils::sortedSetDifference(usedImports, importNames); Utils::sortedSetDifference(usedImports, importNames);
if (!unusedImports.empty()) { 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) for (const auto &str : unusedImports)
stringSize += 1 + str.size(); stringSize += 1 + str.size();
listStrImports.reserve(stringSize); listStrImports.reserve(stringSize);
for (const auto &str : unusedImports) for (const auto &str : unusedImports)
listStrImports += " " + str; listStrImports += " " + str;
luaGlobalError(L, listStrImports);
context->setFailed("There are imported modules that are "
"used without being declared:" +
listStrImports + "\n");
return false;
} }
} }
return true;
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter