MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Runtime.cpp
Go to the documentation of this file.
1#include "Runtime.hpp"
2
4#include "Lila/Lila.hpp"
5
7
8#ifdef MAYAFLUX_PLATFORM_WINDOWS
10#endif
11
13
14namespace {
15 std::mutex g_mutex;
16 std::unique_ptr<Lila::Lila> g_instance;
17 uint16_t g_port {};
18 std::vector<std::string> g_pending_include_paths;
19 std::vector<std::string> g_pending_compile_flags;
20 std::vector<std::string> g_pending_libraries;
21 std::vector<std::string> g_pending_evals;
22}
23
24bool start_lila(uint16_t port)
25{
26 std::lock_guard<std::mutex> guard(g_mutex);
27
28 if (g_instance) {
29 LILA_WARN(Lila::Emitter::SYSTEM,
30 "start_lila: already running on port " + std::to_string(g_port));
31 return false;
32 }
33
34 auto instance = std::make_unique<Lila::Lila>();
35
36 for (const auto& p : g_pending_include_paths)
37 instance->add_include_path(p);
38 g_pending_include_paths.clear();
39
40 for (const auto& f : g_pending_compile_flags)
41 instance->add_compile_flag(f);
42 g_pending_compile_flags.clear();
43
44 if (!instance->initialize(Lila::OperationMode::Both, static_cast<int>(port), true)) {
45 LILA_ERROR(Lila::Emitter::SYSTEM,
46 "start_lila: initialization failed: " + instance->get_last_error());
47 return false;
48 }
49
50 for (const auto& lib : g_pending_libraries)
51 instance->load_library(lib);
52 g_pending_libraries.clear();
53
54 for (const auto& code : g_pending_evals)
55 instance->eval(code);
56 g_pending_evals.clear();
57
58 g_instance = std::move(instance);
59 g_port = port;
60
61 LILA_INFO(Lila::Emitter::SYSTEM,
62 "start_lila: running on port " + std::to_string(port));
63 return true;
64}
65
66void stop_lila(bool clear_persistent_store)
67{
68 std::lock_guard<std::mutex> guard(g_mutex);
69
70 if (!g_instance) {
71 return;
72 }
73
74 LILA_INFO(Lila::Emitter::SYSTEM,
75 "stop_lila: stopping on port " + std::to_string(g_port));
76
77 g_instance->stop_server();
78 g_instance.reset();
79 g_port = 0;
80
81 if (clear_persistent_store) {
83 LILA_DEBUG(Lila::Emitter::SYSTEM, "stop_lila: cleared persistent store");
84 }
85}
86
88{
89 std::lock_guard<std::mutex> guard(g_mutex);
90 return static_cast<bool>(g_instance);
91}
92
93uint16_t lila_port()
94{
95 std::lock_guard<std::mutex> guard(g_mutex);
96 return g_instance ? g_port : uint16_t {};
97}
98
99std::optional<std::filesystem::path> find_include_dir(const std::string& filename)
100{
101 const auto cwd = std::filesystem::current_path();
102 for (const auto& entry : std::filesystem::recursive_directory_iterator(cwd)) {
103 if (entry.is_regular_file() && entry.path().filename() == filename) {
104 return entry.path().parent_path();
105 }
106 }
107 return std::nullopt;
108}
109
110void lila_add_include_path(const std::string& path)
111{
112 std::lock_guard<std::mutex> guard(g_mutex);
113 if (g_instance) {
114 g_instance->add_include_path(path);
115 return;
116 }
117 g_pending_include_paths.push_back(path);
118}
119
120void lila_add_compile_flag(const std::string& flag)
121{
122 std::lock_guard<std::mutex> guard(g_mutex);
123 if (g_instance) {
124 g_instance->add_compile_flag(flag);
125 return;
126 }
127 g_pending_compile_flags.push_back(flag);
128}
129
130bool lila_add_header(const std::string& filename)
131{
132 auto dir = find_include_dir(filename);
133 if (!dir) {
134 LILA_WARN(Lila::Emitter::SYSTEM,
135 "lila_add_header: could not find " + filename);
136 return false;
137 }
138 lila_add_include_path(dir->string());
139 lila_eval("#include \"" + filename + "\"");
140 return true;
141}
142
143void lila_load_library(const std::string& path)
144{
145 std::lock_guard<std::mutex> guard(g_mutex);
146 if (g_instance) {
147 g_instance->load_library(path);
148 return;
149 }
150 g_pending_libraries.push_back(path);
151}
152
153bool lila_eval(const std::string& code)
154{
155 std::lock_guard<std::mutex> guard(g_mutex);
156 if (g_instance) {
157 return g_instance->eval(code);
158 }
159 g_pending_evals.push_back(code);
160 return true;
161}
162
163} // namespace MayaFlux::Host::Live
#define LILA_WARN(emitter, msg)
#define LILA_ERROR(emitter, msg)
#define LILA_DEBUG(emitter, msg)
#define LILA_INFO(emitter, msg)
bool lila_active()
True if a Lila instance is currently running in this process.
Definition Runtime.cpp:87
void stop_lila(bool clear_persistent_store)
Stop the running Lila interpreter and TCP server.
Definition Runtime.cpp:66
std::optional< std::filesystem::path > find_include_dir(const std::string &filename)
Find the directory containing a header staged for inclusion.
Definition Runtime.cpp:99
bool start_lila(uint16_t port)
Start a Lila interpreter and TCP server inside this process.
Definition Runtime.cpp:24
uint16_t lila_port()
TCP port of the running Lila instance, or 0 if none is running.
Definition Runtime.cpp:93
bool lila_eval(const std::string &code)
Evaluate a code snippet in the Lila interpreter.
Definition Runtime.cpp:153
bool lila_add_header(const std::string &filename)
Stage a header for inclusion in the Lila interpreter.
Definition Runtime.cpp:130
void lila_load_library(const std::string &path)
Load a shared library into the JIT symbol table at runtime.
Definition Runtime.cpp:143
void lila_add_compile_flag(const std::string &flag)
Add a compile flag to the Lila interpreter.
Definition Runtime.cpp:120
void lila_add_include_path(const std::string &path)
Add an include path to the Lila interpreter.
Definition Runtime.cpp:110
void cleanup_persistent_store()
Definition Persist.hpp:14