From c54d8d6ea171a92b320218815c35b488f776fb9a Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 9 Sep 2021 09:51:38 +0200
Subject: [PATCH] SCRIPT: Manual way of creating the FreeFunctions object to
 get the current vivy document

---
 src/Lib/Script/CRTPLuaScriptObject.hh         | 27 ++++++++++++-------
 .../CRTPLuaScriptObject/FreeFunctions.cc      | 16 +++++++++++
 src/Lib/Script/LuaContext.cc                  |  7 +++++
 src/Lib/Script/LuaContext.hh                  | 16 ++++++-----
 src/VivyApplication.hh                        |  4 +--
 5 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/src/Lib/Script/CRTPLuaScriptObject.hh b/src/Lib/Script/CRTPLuaScriptObject.hh
index 480c5695..311acb65 100644
--- a/src/Lib/Script/CRTPLuaScriptObject.hh
+++ b/src/Lib/Script/CRTPLuaScriptObject.hh
@@ -22,19 +22,24 @@ namespace Vivy::Script
 
 #define method_list static constexpr inline auto
 
-#define LUA_SCRIPTABLE_CLASS(theClassName)                                                  \
-    VIVY_UNMOVABLE_OBJECT(theClassName)                                                     \
-    static constexpr char className[] = #theClassName;                                      \
-    friend CRTPLuaScriptObject<theClassName>;                                               \
-    theClassName(lua_State *L) noexcept                                                     \
-    {                                                                                       \
-        out(LuaContext::getContext(L)) << "Create instance of " << theClassName::className; \
-    }                                                                                       \
+#define LUA_SCRIPTABLE_CLASS_MANUAL_CTOR(theClassName) \
+    VIVY_UNMOVABLE_OBJECT(theClassName)                \
+    static constexpr char className[] = #theClassName; \
+    friend CRTPLuaScriptObject<theClassName>;          \
     ~theClassName() noexcept {}
 
+#define LUA_SCRIPTABLE_CLASS(theClassName)                               \
+    LUA_SCRIPTABLE_CLASS_MANUAL_CTOR(theClassName)                       \
+    theClassName(lua_State *L) noexcept                                  \
+    {                                                                    \
+        err(LuaContext::getContext(L))                                   \
+            << "Create instance of " << theClassName::className << "\n"; \
+    }
+
 // The type of the thing that a job iterate over
 enum class JobIteratorType { Line = 1, Syllabe = 2 };
-static inline JobIteratorType
+
+[[maybe_unused]] static inline JobIteratorType
 getJobIteratorTypeFromString(const std::string_view it) noexcept
 {
     if (it == "LINE")
@@ -324,7 +329,9 @@ public:
 
 // Holds all the free functions (well, not really free functions here...)
 script_class (FreeFunctions) {
-    LUA_SCRIPTABLE_CLASS(FreeFunctions)
+    LUA_SCRIPTABLE_CLASS_MANUAL_CTOR(FreeFunctions)
+
+    FreeFunctions(lua_State *const) noexcept;
 
     static int print(lua_State *const) noexcept;
     static int getModule(lua_State *const) noexcept;
diff --git a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
index 3b49778e..9192f1d1 100644
--- a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
+++ b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
@@ -1,7 +1,23 @@
 #include "../CRTPLuaScriptObject.hh"
+#include "../LuaContext.hh"
+#include "../../../VivyApplication.hh"
 
 using namespace Vivy::Script;
 
+FreeFunctions::FreeFunctions(lua_State *const L) noexcept
+{
+    const auto *const context = LuaContext::getContext(L);
+    out(context) << "Creating the FreeFunctions lua object!";
+
+    std::shared_ptr<VivyDocument> vivyDoc = context->getAttachedVivyDocument();
+    if (vivyDoc) {
+    }
+
+    else {
+        out(context) << "No attached Vivy document in the Lua context";
+    }
+}
+
 int
 FreeFunctions::print(lua_State *const L) noexcept
 {
diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc
index 48ac5354..2998a1eb 100644
--- a/src/Lib/Script/LuaContext.cc
+++ b/src/Lib/Script/LuaContext.cc
@@ -1,6 +1,7 @@
 #include "CRTPLuaScriptObject.hh"
 #include "ScriptDocument.hh"
 #include "LuaContext.hh"
+#include "../Document/VivyDocument.hh"
 
 // LuaContext implementation
 using namespace Vivy::Script;
@@ -158,6 +159,12 @@ LuaContext::getState() noexcept
     return L;
 }
 
+std::shared_ptr<Vivy::VivyDocument>
+LuaContext::getAttachedVivyDocument() const noexcept
+{
+    return attachedVivyDocument;
+}
+
 bool
 LuaContext::moduleExists(const std::string_view mod) const noexcept
 {
diff --git a/src/Lib/Script/LuaContext.hh b/src/Lib/Script/LuaContext.hh
index 06da7b43..318751ab 100644
--- a/src/Lib/Script/LuaContext.hh
+++ b/src/Lib/Script/LuaContext.hh
@@ -9,6 +9,7 @@ struct lua_State;
 namespace Vivy
 {
 class ScriptDocument;
+class VivyDocument;
 }
 
 namespace Vivy::Script
@@ -27,6 +28,7 @@ class LuaContext final {
     VIVY_APP_LOGGABLE_OBJECT(LuaContext, logger)
 
     lua_State *L{ nullptr };
+    std::shared_ptr<Vivy::VivyDocument> attachedVivyDocument{ nullptr };
 
     std::string failureString{};
     std::string currentFile{};
@@ -47,6 +49,7 @@ public:
     ~LuaContext() noexcept;
 
     Code loadDocument(std::shared_ptr<ScriptDocument>) noexcept;
+    Code loadDocument(std::shared_ptr<Vivy::VivyDocument>) noexcept;
 
     QString getLastLuaError() noexcept;
 
@@ -59,9 +62,10 @@ public:
     void setFailedWith(const std::string &) noexcept;
 
     lua_State *getState() noexcept;
+    std::shared_ptr<Vivy::VivyDocument> getAttachedVivyDocument() const noexcept;
 
-    decltype(auto) getOutputStream() noexcept { return logInfo(); }
-    decltype(auto) getErrorStream() noexcept { return logError(); }
+    decltype(auto) getOutputStream() const noexcept { return logInfo(); }
+    decltype(auto) getErrorStream() const noexcept { return logError(); }
 
     operator lua_State *() noexcept { return L; }
     static LuaContext *getContext(const lua_State *const) noexcept;
@@ -99,14 +103,14 @@ private:
     void swapEnv() noexcept;
 };
 
-static inline decltype(auto)
-out(LuaContext *const context) noexcept
+[[maybe_unused]] static inline decltype(auto)
+out(const LuaContext *const context) noexcept
 {
     return context->getOutputStream();
 }
 
-static inline decltype(auto)
-err(LuaContext *const context) noexcept
+[[maybe_unused]] static inline decltype(auto)
+err(const LuaContext *const context) noexcept
 {
     return context->getErrorStream();
 }
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index 7317c7ea..71c8344b 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -16,8 +16,8 @@
 #define VIVY_APP_LOGGABLE_OBJECT_BY_STORED_NAME(name, logger) \
     VIVY_LOGGABLE_OBJECT_BY_STORED_NAME(vivyApp->getLogSink(), name, logger)
 
-#define currentVivyDocument() dynamic_cast<::Vivy::VivyDocument *>(vivyApp->getCurrentDocument())
-#define currentScriptDocument dynamic_cast<::Vivy::ScriptDocument *>(vivyApp->getCurrentDocument())
+#define currentVivyDocument   dynamic_cast<VivyDocument *>(vivyApp->getCurrentDocument())
+#define currentScriptDocument dynamic_cast<ScriptDocument *>(vivyApp->getCurrentDocument())
 
 // Only support dark theme for now
 #define VIVY_ICON_APP     ":icons/vivy.png"
-- 
GitLab