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

SCRIPT: Load the lib.lua and module.lua at the end of the LuaContext creation

parent b693b8f3
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
......@@ -16,9 +16,6 @@ LuaContext::LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &er
, streamErr(err)
{
luaL_openlibs(L);
QFile libVivy(":lua/lib.lua");
if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text))
qFatal("Failed to find packaged libVivy (:lia/lib.lua)");
// Register this context
contextList.emplace(L, this);
......@@ -31,7 +28,8 @@ LuaContext::LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &er
ModuleDeclaration::Register(L);
// Load the lib
loadString(libVivy.readAll().toStdString().c_str());
loadPackagedFile(":lua/lib.lua");
loadPackagedFile(":lua/module.lua");
}
LuaContext::~LuaContext() noexcept
......@@ -40,6 +38,15 @@ LuaContext::~LuaContext() noexcept
contextList.erase(L); // Unregister the context
}
void
LuaContext::loadPackagedFile(const QString &url) noexcept
{
QFile libVivy(url);
if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text))
qFatal("FATA -> failed to find packaged Vivy file %s", url.toStdString().c_str());
loadString(libVivy.readAll().toStdString().c_str());
}
LuaContext *
LuaContext::getContext(const lua_State *const state) noexcept
{
......@@ -79,7 +86,7 @@ LuaContext::Code
LuaContext::loadString(const char *str) noexcept
{
if (luaL_loadstring(L, str) != LUA_OK) {
err(this) << "Error loading string\n";
err(this) << "Error loading string: " << getLastError().toStdString() << "\n";
return Code::Error;
}
return exec();
......@@ -89,7 +96,7 @@ LuaContext::Code
LuaContext::loadFile(const char *file) noexcept
{
if (luaL_loadfile(L, file) != LUA_OK) {
err(this) << "Error loading file " << file << "\n";
err(this) << "Error loading file " << file << ": " << getLastError().toStdString() << "\n";
return Code::Error;
}
return exec();
......
......@@ -48,6 +48,7 @@ private:
Code loadString(const char *) noexcept;
Code loadFile(const char *) noexcept;
void loadPackagedFile(const QString &) noexcept;
// Swap the env, needed before executing a user script!
void swapEnv() noexcept;
......
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