From 21e92dcb4b5150efb3af000d7149a3e004c16538 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Thu, 13 Aug 2026 14:10:43 +0200 Subject: [PATCH] refactor(furvm): remove executor handle --- furdb/include/context.hpp | 2 +- furdb/src/main.cpp | 2 +- furvm/include/furvm/context.hpp | 32 ++++++++++---------------------- furvm/include/furvm/executor.hpp | 14 +++++--------- furvm/include/furvm/fwd.hpp | 10 ---------- furvm/src/main.cpp | 8 ++++---- 6 files changed, 21 insertions(+), 47 deletions(-) diff --git a/furdb/include/context.hpp b/furdb/include/context.hpp index 3eb2ec9..10c01cc 100644 --- a/furdb/include/context.hpp +++ b/furdb/include/context.hpp @@ -9,7 +9,7 @@ struct context { furvm::context_p context = std::make_shared(); furvm::mod_h mod; - furvm::executor_h executor; + furvm::executor* executor = nullptr; furvm::function_h mainFunction; bool running = true; diff --git a/furdb/src/main.cpp b/furdb/src/main.cpp index fad14e8..51e10e0 100644 --- a/furdb/src/main.cpp +++ b/furdb/src/main.cpp @@ -56,7 +56,7 @@ int main(int argc, char** argv) { std::cerr << "Failed to load module " << argv[1] << ": " << ex.what() << '\n'; return 1; } - ctx.executor = ctx.context->emplace_executor(ctx.context); + ctx.executor = &ctx.context->allocate_executor(); ctx.mainFunction = ctx.mod->function_at("main", furvm::function_sig{}); ctx.mod->set_native_function("println", [](furvm::executor& executor) { diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 96455d5..6548a51 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -11,6 +11,7 @@ #include #include +#include namespace furvm { @@ -39,15 +40,10 @@ public: context(const context&) = delete; context& operator=(const context&) = delete; public: - /** - * @brief Emplaces an executor in the context. - * - * @param args Arguments forwarded to executor's constructor. - * @return A handle to the emplaced executor. - */ template - auto emplace_executor(Args&&... args) { - return m_executors.emplace_back(std::forward(args)...); + auto& allocate_executor() { + executor executor(this); + return m_executors.emplace_back(std::move(executor)); } /** @@ -57,7 +53,7 @@ public: * @return A handle to the executor. */ template - auto executor_at(Args&&... args) { + auto& executor_at(Args&&... args) { return m_executors.at(std::forward(args)...); } @@ -68,19 +64,11 @@ public: * @return A handle to the executor. */ template - auto executor_at(Args&&... args) const { + const auto& executor_at(Args&&... args) const { return m_executors.at(std::forward(args)...); } - /** - * @brief Erases an executor from the context. - * - * @param args Id of the executor. - */ - template - void erase_executor(Args&&... args) { - m_executors.erase(std::forward(args)...); - } + const std::vector& executors() const { return m_executors; } public: template auto emplace_thing(Args&&... args) { @@ -128,9 +116,9 @@ public: thing_type_store& tt_store() { return m_thingTypeStore; } private: - handle_container m_modules; - handle_container m_things; - handle_container m_executors; + handle_container m_modules; + handle_container m_things; + std::vector m_executors; furlang::arena m_thingArena; thing_allocator m_thingAllocator; diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index 6b7f001..66961f2 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -32,6 +32,10 @@ static inline executor_flags operator~(executor_flags flags) { using executor_callback = std::function; class executor { + friend class context; +private: + executor(context* context) + : m_context(context) {} public: /** * @brief Executor frame. @@ -47,14 +51,6 @@ public: std::vector variables; /**< Frame variables. */ }; public: - /** - * @brief Returns a new executor. - * - * @param context Context. - */ - executor(const context_p& context) - : m_context(context) {} - ~executor() = default; /** @@ -181,7 +177,7 @@ private: thing_type* mod_to_thing_type(const mod_h& mod, const mod_type& type) const; private: executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) - context_p m_context; + context* m_context; std::stack m_frames; std::stack m_stack; diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index ae84ed9..ba1ecda 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -195,21 +195,11 @@ enum class executor_flags : std::uint32_t; */ class executor; -/** - * @brief An alias to a executor shared pointer. - */ -using executor_p = std::shared_ptr; - /** * @brief Furvm executor's index. */ using executor_id = std::uint32_t; -/** - * @brief A handle to an executor. - */ -using executor_h = handle>; - // context.hpp /** diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index b954d03..cbfd137 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -80,11 +80,11 @@ int main(int argc, char** argv) { std::cout << '\n'; }); - furvm::executor_h executor = context->emplace_executor(context); - executor->push_frame(mod, *mainFunc); + auto& executor = context->allocate_executor(); + executor.push_frame(mod, *mainFunc); - while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) { - executor->step(); + while ((executor.flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) { + executor.step(); } return 0;