diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc index 4a9d54f9a55c7488e7a321a1b44b65213f926af4..155c485292d287d80455d05227be1d649b71fe93 100644 --- a/src/Lib/Script/LuaContext.cc +++ b/src/Lib/Script/LuaContext.cc @@ -20,15 +20,15 @@ LuaContext::LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &er if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text)) qFatal("Failed to find packaged libVivy (:lia/lib.lua)"); + // Register this context + contextList.emplace(L, this); + // 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 diff --git a/src/Lib/Script/ScriptStore.cc b/src/Lib/Script/ScriptStore.cc index adf9bc7dc6202d6ba1d24f788fa65746a0a5cafd..53ac9c7f2e5ad9c22cf4129e4631c08553e6b5cc 100644 --- a/src/Lib/Script/ScriptStore.cc +++ b/src/Lib/Script/ScriptStore.cc @@ -1,5 +1,6 @@ #include "ScriptStore.hh" #include "../Uuid.hh" +#include <iostream> using namespace Vivy; using namespace std::string_literals; @@ -38,3 +39,19 @@ ScriptStore::resetLoadedScripts() noexcept { luaContext.reset(new Script::LuaContext(streamOut, streamErr)); } + +void +ScriptStore::executeScript(Uuid id) noexcept +{ + auto script = documents.at(id); + luaContext->loadDocument(script); + std::string bufferOut; + bufferOut.resize(outBufferSize + 1); + + std::cout << "STDOUT:\n"; + while (streamOut.readsome(bufferOut.data(), static_cast<std::streamsize>(bufferOut.size()))) + std::cout << bufferOut.data(); + std::cout << "STDERR:\n"; + while (streamErr.readsome(bufferOut.data(), static_cast<std::streamsize>(bufferOut.size()))) + std::cout << bufferOut.data(); +} diff --git a/src/Lib/Script/ScriptStore.hh b/src/Lib/Script/ScriptStore.hh index c217626407ae5c13a07d8187697fa614e98d8549..88f8451508979e60aa1c6e52bcdd7509e99cf5e1 100644 --- a/src/Lib/Script/ScriptStore.hh +++ b/src/Lib/Script/ScriptStore.hh @@ -11,18 +11,24 @@ class LuaContext; namespace Vivy { +// TODO: For now the execution of a script is handled by the ScriptStore, FW +// will be to create a LuaExecutor that will handle the execution of scripts. class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> { VIVY_STORAGE_CLASS(ScriptStore, ScriptDocument) + static constexpr inline int outBufferSize = 1024; + public: using Item = std::tuple<Uuid, QString>; using LoggerType = std::stringstream; explicit ScriptStore() noexcept; + // TODO: Things that should be moved to the LuaExecutor class. void resetLoadedScripts() noexcept; void loadScriptFolder(const QString &folderPath); std::vector<Item> getLoadedScripts() const noexcept; + void executeScript(Uuid) noexcept; private: std::unique_ptr<Script::LuaContext> luaContext{ nullptr }; diff --git a/src/Main.cc b/src/Main.cc index d3a75093f4e098ecd178a9ca9e8e9b2830eab53d..7fb3b3ff18a661594cab1def4edf71ebc839162a 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -1,8 +1,10 @@ #include "VivyApplication.hh" +#include "VivyCli.hh" #include "Lib/Script/LuaContext.hh" int main(int argc, char **argv) noexcept { - return Vivy::VivyApplication(argc, argv).exec(); + return (argc >= 2) ? (Vivy::VivyCli(argc, argv).exec()) + : (Vivy::VivyApplication(argc, argv).exec()); } diff --git a/src/VivyCli.cc b/src/VivyCli.cc new file mode 100644 index 0000000000000000000000000000000000000000..9cb4b5762696472578202ea716964de73a1f2210 --- /dev/null +++ b/src/VivyCli.cc @@ -0,0 +1,19 @@ +#include "VivyCli.hh" + +using namespace Vivy; + +VivyCli::VivyCli(int &argc, char **argv) noexcept +{ + if (argc >= 2) { + selectedDoc = scriptStore.loadDocument(QString(argv[1])); + } +} + +int +VivyCli::exec() noexcept +{ + if (selectedDoc == nullptr) + return 1; + scriptStore.executeScript(selectedDoc->getUuid()); + return 0; +} diff --git a/src/VivyCli.hh b/src/VivyCli.hh new file mode 100644 index 0000000000000000000000000000000000000000..9ceef273fa57a52fb9d2e0d2bfbf7eb31aefb037 --- /dev/null +++ b/src/VivyCli.hh @@ -0,0 +1,19 @@ +#pragma once + +#ifndef __cplusplus +#error "This is a C++ header" +#endif + +#include "Lib/Script/ScriptStore.hh" + +namespace Vivy +{ +class VivyCli final { + ScriptStore scriptStore{}; + std::shared_ptr<ScriptDocument> selectedDoc{ nullptr }; + +public: + VivyCli(int &argc, char **argv) noexcept; + int exec() noexcept; +}; +}