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

SCRIPT: Implements free functions as method of another class + get out and err streams

Now a context has an error and output stream that can be retrieved from
the lua state. FreeFunctions are implemented inside a scriptable object
to be able to store some things at the object createion
parent 7ccb4412
Aucune branche associée trouvée
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,3 +16,16 @@ SampleObject::foo(lua_State *L) noexcept
fprintf(stderr, "SampleObject(%p)::foo -> %f\n", static_cast<void *>(obj), bar);
return 0;
}
FreeFunctions::FreeFunctions() noexcept {}
FreeFunctions::~FreeFunctions() noexcept {}
int
FreeFunctions::prettyPrint(lua_State *L) noexcept
{
auto *context = LuaContext::getContext(L);
auto &out = context->getOutputStream();
out << "prettyPrint!!\n";
return 0;
}
......@@ -115,4 +115,17 @@ script_class (SampleObject) {
static constexpr inline auto metaMethods = { luaRegDefaultGC, LUA_DECL_CREATE(SampleObject) };
static constexpr inline auto methods = { LUA_DECL_METHOD(SampleObject, foo) };
};
// Holds all the free functions (well, not really free functions here...)
script_class (FreeFunctions) {
LUA_SCRIPTABLE_CLASS(FreeFunctions)
FreeFunctions() noexcept;
~FreeFunctions() noexcept;
static int prettyPrint(lua_State *) noexcept;
static constexpr inline auto metaMethods = { luaRegDefaultGC, LUA_DECL_CREATE(FreeFunctions) };
static constexpr inline auto methods = { LUA_DECL_METHOD(FreeFunctions, prettyPrint) };
};
}
#include "CRTPLuaScriptObject.hh"
#include "ScriptDocument.hh"
#include "LuaContext.hh"
#include "LuaFreeFunctions.hh"
#include "lua.hpp"
#include <cstdio>
#include <QtGlobal>
......@@ -11,20 +10,50 @@
// LuaContext implementation
using namespace Vivy::Script;
LuaContext::LuaContext() noexcept
LuaContext::LuaContext(std::stringstream &out, std::stringstream &err) noexcept
: L(luaL_newstate())
, streamOut(out)
, 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)");
FreeFunctions::registerFunctions(L); // Register the free functions
SampleObject::Register(L); // Load the C++ objects
loadString(libVivy.readAll().toStdString().c_str()); // Load the lib
// Load all C++ objects
SampleObject::Register(L);
FreeFunctions::Register(L);
// Load the lib
loadString(libVivy.readAll().toStdString().c_str());
// Register this context
contextList.emplace(L, this);
}
LuaContext::~LuaContext() noexcept
{
contextList.erase(L); // Unregister the context
lua_close(L); // Close lua
}
std::stringstream &
LuaContext::getOutputStream() noexcept
{
return streamOut;
}
LuaContext::~LuaContext() noexcept { lua_close(L); }
std::stringstream &
LuaContext::getErrorStream() noexcept
{
return streamErr;
}
LuaContext *
LuaContext::getContext(const lua_State *const state) noexcept
{
return contextList.contains(state) ? contextList.at(state) : nullptr;
}
LuaContext::Code
LuaContext::loadDocument(std::shared_ptr<ScriptDocument> doc) noexcept
......@@ -80,3 +109,9 @@ LuaContext::getLastError() noexcept
lua_pop(L, 1);
return ret;
}
lua_State *
LuaContext::getState() noexcept
{
return L;
}
#pragma once
#include <sstream>
#include <map>
struct lua_State;
class QString;
......@@ -13,6 +16,10 @@ namespace Vivy::Script
// New Lua script instance
class LuaContext final {
lua_State *L;
static inline std::map<const lua_State *const, LuaContext *> contextList = {};
std::stringstream &streamOut;
std::stringstream &streamErr;
public:
enum class Code {
......@@ -20,15 +27,17 @@ public:
Error,
};
public:
LuaContext() noexcept;
LuaContext(std::stringstream &out, std::stringstream &err) noexcept;
~LuaContext() noexcept;
Code loadDocument(std::shared_ptr<ScriptDocument>) noexcept;
QString getLastError() noexcept;
lua_State *getState() noexcept;
std::stringstream &getOutputStream() noexcept;
std::stringstream &getErrorStream() noexcept;
operator lua_State *() noexcept { return L; }
static LuaContext *getContext(const lua_State *const) noexcept;
private:
// Exec all loaded strings and files
......
#include "LuaFreeFunctions.hh"
#include "LuaContext.hh"
#include <initializer_list>
#include <QtGlobal>
using namespace Vivy::Script;
static int
vivyPrint(lua_State *L) noexcept
{
luaL_checktype(L, 1, LUA_TSTRING);
qDebug() << "LUA - MSG:" << lua_tostring(L, 1);
return 0;
}
static inline constexpr std::initializer_list<std::tuple<const char *, int (*)(lua_State *)>>
functionsToRegister = {
{ "print", vivyPrint },
};
void
FreeFunctions::registerFunctions(lua_State *L) noexcept
{
for (const auto &[name, functionPtr] : functionsToRegister) {
lua_register(L, name, functionPtr);
qDebug() << "LUA: Register function" << name;
}
}
#pragma once
#include "../Utils.hh"
struct lua_State;
namespace Vivy::Script::FreeFunctions
{
void registerFunctions(lua_State *) noexcept;
}
......@@ -36,5 +36,5 @@ ScriptStore::getLoadedScripts() const noexcept
void
ScriptStore::resetLoadedScripts() noexcept
{
luaContext.reset(new Script::LuaContext());
luaContext.reset(new Script::LuaContext(streamOut, streamErr));
}
......@@ -3,6 +3,7 @@
#include "../CRTPStore.hh"
#include "ScriptDocument.hh"
#include "LuaContext.hh"
#include <sstream>
namespace Vivy
{
......@@ -10,6 +11,8 @@ class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> {
VIVY_STORAGE_CLASS(ScriptStore, ScriptDocument)
std::unique_ptr<Script::LuaContext> luaContext{ nullptr };
std::stringstream streamOut;
std::stringstream streamErr;
public:
using Item = std::tuple<Uuid, QString>;
......
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