8#ifdef MAYAFLUX_PLATFORM_WINDOWS
16 std::unique_ptr<Lila::Lila> g_instance;
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;
26 std::lock_guard<std::mutex> guard(g_mutex);
30 "start_lila: already running on port " + std::to_string(g_port));
34 auto instance = std::make_unique<Lila::Lila>();
36 for (
const auto& p : g_pending_include_paths)
37 instance->add_include_path(p);
38 g_pending_include_paths.clear();
40 for (
const auto& f : g_pending_compile_flags)
41 instance->add_compile_flag(f);
42 g_pending_compile_flags.clear();
44 if (!instance->initialize(Lila::OperationMode::Both,
static_cast<int>(port),
true)) {
46 "start_lila: initialization failed: " + instance->get_last_error());
50 for (
const auto& lib : g_pending_libraries)
51 instance->load_library(lib);
52 g_pending_libraries.clear();
54 for (
const auto& code : g_pending_evals)
56 g_pending_evals.clear();
58 g_instance = std::move(instance);
62 "start_lila: running on port " + std::to_string(port));
68 std::lock_guard<std::mutex> guard(g_mutex);
75 "stop_lila: stopping on port " + std::to_string(g_port));
77 g_instance->stop_server();
81 if (clear_persistent_store) {
83 LILA_DEBUG(Lila::Emitter::SYSTEM,
"stop_lila: cleared persistent store");
89 std::lock_guard<std::mutex> guard(g_mutex);
90 return static_cast<bool>(g_instance);
95 std::lock_guard<std::mutex> guard(g_mutex);
96 return g_instance ? g_port : uint16_t {};
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();
112 std::lock_guard<std::mutex> guard(g_mutex);
114 g_instance->add_include_path(path);
117 g_pending_include_paths.push_back(path);
122 std::lock_guard<std::mutex> guard(g_mutex);
124 g_instance->add_compile_flag(flag);
127 g_pending_compile_flags.push_back(flag);
135 "lila_add_header: could not find " + filename);
139 lila_eval(
"#include \"" + filename +
"\"");
145 std::lock_guard<std::mutex> guard(g_mutex);
147 g_instance->load_library(path);
150 g_pending_libraries.push_back(path);
155 std::lock_guard<std::mutex> guard(g_mutex);
157 return g_instance->eval(code);
159 g_pending_evals.push_back(code);
bool lila_active()
True if a Lila instance is currently running in this process.
void stop_lila(bool clear_persistent_store)
Stop the running Lila interpreter and TCP server.
std::optional< std::filesystem::path > find_include_dir(const std::string &filename)
Find the directory containing a header staged for inclusion.
bool start_lila(uint16_t port)
Start a Lila interpreter and TCP server inside this process.
uint16_t lila_port()
TCP port of the running Lila instance, or 0 if none is running.
bool lila_eval(const std::string &code)
Evaluate a code snippet in the Lila interpreter.
bool lila_add_header(const std::string &filename)
Stage a header for inclusion in the Lila interpreter.
void lila_load_library(const std::string &path)
Load a shared library into the JIT symbol table at runtime.
void lila_add_compile_flag(const std::string &flag)
Add a compile flag to the Lila interpreter.
void lila_add_include_path(const std::string &path)
Add an include path to the Lila interpreter.
void cleanup_persistent_store()