10#ifdef MAYAFLUX_PLATFORM_WINDOWS
18 constexpr size_t k_stage_count = 4;
21 std::shared_ptr<Lila::Lila> instance;
22 std::optional<Lila::Address> address;
24 std::array<std::vector<std::string>, k_stage_count> staged;
25 std::set<std::string> libraries;
28 enum class Outcome : uint8_t {
35 Memory::SnapshotCell<State> g_state;
37 struct ObserverEntry {
39 std::shared_ptr<const EvalObserver> observer;
42 Memory::SnapshotCell<std::vector<ObserverEntry>> g_observers;
43 std::atomic<ObserverId> g_next_observer { 1 };
45 void dispatch_eval(
void*,
const Lila::EvalReport& report)
47 const auto observers = g_observers.read();
51 for (
const auto& entry : *observers) {
52 (*entry.observer)(report);
56 std::unexpected<Failure>
refuse(std::string message, std::vector<std::filesystem::path> tried = {})
58 return std::unexpected(
Failure { .message = std::move(message), .tried = std::move(tried) });
63 return static_cast<size_t>(stage);
66 std::string where_text(
const std::optional<Lila::Address>& address)
71 if (
const auto* tcp = std::get_if<Lila::TcpAddress>(&*address)) {
72 return tcp->host +
":" + std::to_string(tcp->port);
74 return "socket " + std::get<Lila::UnixAddress>(*address).path.value_or(std::filesystem::path {}).string();
77 std::shared_ptr<Lila::Lila> running_instance()
79 auto view = g_state.read();
80 return view ? view->instance :
nullptr;
83 bool library_known(
const std::string& path)
85 auto view = g_state.read();
86 return view && view->libraries.contains(path);
89 Result<void> run_eval(Lila::Lila& lila,
const std::string& code)
91 if (
auto evaluated = lila.
eval(code); !evaluated) {
92 return refuse(std::string(evaluated.error()));
97 Result<void> run_load(Lila::Lila& lila,
const std::string& path)
100 return refuse(std::string(loaded.error()), { path });
108 return run_eval(lila, value);
112 if (library_known(value)) {
115 auto loaded = run_load(lila, value);
117 g_state.update([&value](State& state) { state.libraries.insert(value); });
122 return refuse(
"takes effect only before start_session: " + value);
132 Outcome outcome = Outcome::Staged;
134 g_state.update([&](State& state) {
135 if (state.instance) {
136 outcome = Outcome::Running;
139 if (state.starting) {
140 outcome = Outcome::Starting;
143 if (stage ==
Stage::Library && !state.libraries.insert(value).second) {
144 outcome = Outcome::Duplicate;
147 state.staged[index_of(stage)].push_back(value);
148 outcome = Outcome::Staged;
152 if (outcome == Outcome::Staged || outcome == Outcome::Duplicate) {
155 if (outcome == Outcome::Starting) {
156 return refuse(
"session is starting");
159 if (
auto instance = running_instance()) {
160 return apply_now(*instance, stage, value);
171 case Reason::EmptyRequest:
172 message =
"empty request";
174 case Reason::InvalidName:
175 message =
"invalid name";
177 case Reason::Missing:
178 message =
"not found";
180 case Reason::NotDirectory:
181 message =
"not a directory";
183 case Reason::NotRegularFile:
184 message =
"not a regular file";
186 case Reason::MarkerMissing:
187 message =
"marker file missing";
189 case Reason::TooLong:
190 message =
"path too long";
193 message =
"unsafe location";
195 case Reason::NotSocket:
196 message =
"not a socket";
199 message =
"already in use";
201 case Reason::StaleNotRemoved:
202 message =
"stale entry could not be removed";
205 if (!failure.
request.empty()) {
206 message +=
": " + failure.
request;
208 return Failure { .message = std::move(message), .tried = failure.
tried };
215 enum class Refusal : uint8_t {
221 Refusal refusal = Refusal::None;
222 std::optional<Lila::Address> running;
223 std::array<std::vector<std::string>, k_stage_count> staged;
225 g_state.update([&](State& state) {
226 if (state.instance) {
227 refusal = Refusal::Running;
228 running = state.address;
231 if (state.starting) {
232 refusal = Refusal::Starting;
235 refusal = Refusal::None;
236 staged = std::exchange(state.staged, {});
237 state.starting =
true;
241 if (refusal == Refusal::Running) {
242 return refuse(
"session already running on " + where_text(running));
244 if (refusal == Refusal::Starting) {
245 return refuse(
"session is starting");
248 auto instance = std::make_shared<Lila::Lila>();
249 instance->add_eval_observer(&dispatch_eval,
nullptr);
252 instance->add_include_path(path);
255 instance->add_compile_flag(flag);
259 instance->stop_listening();
260 g_state.update([](State& state) {
261 state.starting =
false;
262 state.libraries.clear();
264 return std::unexpected(std::move(failure));
267 if (
const auto ready = instance->initialize(
true); !ready) {
268 return abandon(
Failure { .message =
"initialization failed: " + std::string(ready.error()), .tried = {} });
272 if (
auto loaded = run_load(*instance, library); !loaded) {
273 return abandon(std::move(loaded.error()));
277 if (
auto evaluated = run_eval(*instance, code); !evaluated) {
278 return abandon(std::move(evaluated.error()));
282 if (
const auto listening = instance->listen(where); !listening) {
283 return abandon(
Failure { .message =
"listen failed: " + std::string(listening.error()), .tried = {} });
286 const auto* resolved = instance->address();
287 const std::optional<Lila::Address> bound = resolved ? std::optional<Lila::Address>(*resolved) : std::nullopt;
288 g_state.update([&](State& state) {
289 state.instance = instance;
290 state.address = bound;
291 state.starting =
false;
294 LILA_INFO(Lila::Emitter::SYSTEM,
"start_session: running on " + where_text(bound));
300 std::shared_ptr<Lila::Lila> instance;
301 std::optional<Lila::Address> stopped;
303 g_state.update([&](State& state) {
304 if (!state.instance) {
308 instance = std::move(state.instance);
309 stopped = std::exchange(state.address, std::nullopt);
310 state.libraries.clear();
319 "stop_session: stopping on " + where_text(stopped));
321 instance->stop_listening();
324 if (clear_persistent_store) {
326 LILA_DEBUG(Lila::Emitter::SYSTEM,
"stop_session: cleared persistent store");
332 auto view = g_state.read();
333 return view && view->instance;
338 auto view = g_state.read();
339 return view && view->instance ? view->address : std::nullopt;
349 const ObserverId id = g_next_observer.fetch_add(1);
350 const auto held = std::make_shared<const EvalObserver>(std::move(observer));
351 g_observers.update([&](std::vector<ObserverEntry>& entries) {
352 entries.push_back({ .id = id, .observer = held });
359 g_observers.update([
id](std::vector<ObserverEntry>& entries) {
360 return std::erase_if(entries, [
id](
const ObserverEntry& entry) {
return entry.id == id; }) > 0;
371 const std::string file = located->path.generic_string();
372 if (file.find_first_of(
"\"\r\n") != std::string::npos) {
373 return refuse(
"path cannot be included: " + file);
376 return eval(
"#include \"" + file +
"\"");
381 auto instance = running_instance();
383 return refuse(
"no active session");
386 void* address = instance->get_symbol_address(name);
388 return refuse(
"symbol not found: " + name);
Status load_library(const std::string &path)
Load a shared library into the JIT symbol table.
Status eval(const std::string &code)
Evaluates a C++ code snippet.
Failure to_failure(const Platform::AcquireFailure &failure)
Result< void > apply_or_stage(Stage stage, std::string value)
Apply an item to the running session, or stage it until start_session.
std::expected< T, Failure > Result
Result of a Host call.
std::function< void(const EvalReport &)> EvalObserver
Result< void * > symbol_address(const std::string &name)
Address of a symbol defined in the running session.
Result< void > eval(const std::string &code)
Evaluate a code snippet.
std::optional< Lila::Address > session_address()
Where the running session listens, with anything resolved at start filled in.
uint64_t ObserverId
Identifies an observer for remove_eval_observer.
void remove_eval_observer(ObserverId id)
Stop observing.
bool session_active()
True if a session is running in this process.
Result< void > start_session(const Lila::Address &where)
Start a Lila interpreter and a server inside this process.
ObserverId on_eval(EvalObserver observer)
Observe every evaluation in the session, whoever sent it.
Result< void > eval_file(const std::filesystem::path &path)
Evaluate a source file by including it.
void stop_session(bool clear_persistent_store)
Stop the running session.
void refuse(std::string_view message)
void cleanup_persistent_store()
Why a Host call was refused, with every path that was checked.