refactor(furvm): remove executor handle

This commit is contained in:
2026-08-13 14:10:43 +02:00
parent 713aec199f
commit 21e92dcb4b
6 changed files with 21 additions and 47 deletions
+1 -1
View File
@@ -9,7 +9,7 @@
struct context { struct context {
furvm::context_p context = std::make_shared<furvm::context>(); furvm::context_p context = std::make_shared<furvm::context>();
furvm::mod_h mod; furvm::mod_h mod;
furvm::executor_h executor; furvm::executor* executor = nullptr;
furvm::function_h mainFunction; furvm::function_h mainFunction;
bool running = true; bool running = true;
+1 -1
View File
@@ -56,7 +56,7 @@ int main(int argc, char** argv) {
std::cerr << "Failed to load module " << argv[1] << ": " << ex.what() << '\n'; std::cerr << "Failed to load module " << argv[1] << ": " << ex.what() << '\n';
return 1; 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.mainFunction = ctx.mod->function_at("main", furvm::function_sig{});
ctx.mod->set_native_function("println", [](furvm::executor& executor) { ctx.mod->set_native_function("println", [](furvm::executor& executor) {
+10 -22
View File
@@ -11,6 +11,7 @@
#include <cstddef> #include <cstddef>
#include <utility> #include <utility>
#include <vector>
namespace furvm { namespace furvm {
@@ -39,15 +40,10 @@ public:
context(const context&) = delete; context(const context&) = delete;
context& operator=(const context&) = delete; context& operator=(const context&) = delete;
public: public:
/**
* @brief Emplaces an executor in the context.
*
* @param args Arguments forwarded to executor's constructor.
* @return A handle to the emplaced executor.
*/
template <typename... Args> template <typename... Args>
auto emplace_executor(Args&&... args) { auto& allocate_executor() {
return m_executors.emplace_back(std::forward<Args>(args)...); executor executor(this);
return m_executors.emplace_back(std::move(executor));
} }
/** /**
@@ -57,7 +53,7 @@ public:
* @return A handle to the executor. * @return A handle to the executor.
*/ */
template <typename... Args> template <typename... Args>
auto executor_at(Args&&... args) { auto& executor_at(Args&&... args) {
return m_executors.at(std::forward<Args>(args)...); return m_executors.at(std::forward<Args>(args)...);
} }
@@ -68,19 +64,11 @@ public:
* @return A handle to the executor. * @return A handle to the executor.
*/ */
template <typename... Args> template <typename... Args>
auto executor_at(Args&&... args) const { const auto& executor_at(Args&&... args) const {
return m_executors.at(std::forward<Args>(args)...); return m_executors.at(std::forward<Args>(args)...);
} }
/** const std::vector<executor>& executors() const { return m_executors; }
* @brief Erases an executor from the context.
*
* @param args Id of the executor.
*/
template <typename... Args>
void erase_executor(Args&&... args) {
m_executors.erase(std::forward<Args>(args)...);
}
public: public:
template <typename... Args> template <typename... Args>
auto emplace_thing(Args&&... args) { auto emplace_thing(Args&&... args) {
@@ -128,9 +116,9 @@ public:
thing_type_store& tt_store() { return m_thingTypeStore; } thing_type_store& tt_store() { return m_thingTypeStore; }
private: private:
handle_container<mod_h> m_modules; handle_container<mod_h> m_modules;
handle_container<thing_h> m_things; handle_container<thing_h> m_things;
handle_container<executor_h> m_executors; std::vector<executor> m_executors;
furlang::arena m_thingArena; furlang::arena m_thingArena;
thing_allocator<std::byte> m_thingAllocator; thing_allocator<std::byte> m_thingAllocator;
+5 -9
View File
@@ -32,6 +32,10 @@ static inline executor_flags operator~(executor_flags flags) {
using executor_callback = std::function<void(executor&)>; using executor_callback = std::function<void(executor&)>;
class executor { class executor {
friend class context;
private:
executor(context* context)
: m_context(context) {}
public: public:
/** /**
* @brief Executor frame. * @brief Executor frame.
@@ -47,14 +51,6 @@ public:
std::vector<thing_h> variables; /**< Frame variables. */ std::vector<thing_h> variables; /**< Frame variables. */
}; };
public: public:
/**
* @brief Returns a new executor.
*
* @param context Context.
*/
executor(const context_p& context)
: m_context(context) {}
~executor() = default; ~executor() = default;
/** /**
@@ -181,7 +177,7 @@ private:
thing_type* mod_to_thing_type(const mod_h& mod, const mod_type& type) const; thing_type* mod_to_thing_type(const mod_h& mod, const mod_type& type) const;
private: private:
executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization) executor_flags m_flags{}; // NOLINT(bugprone-invalid-enum-default-initialization)
context_p m_context; context* m_context;
std::stack<struct frame> m_frames; std::stack<struct frame> m_frames;
std::stack<thing_h> m_stack; std::stack<thing_h> m_stack;
-10
View File
@@ -195,21 +195,11 @@ enum class executor_flags : std::uint32_t;
*/ */
class executor; class executor;
/**
* @brief An alias to a executor shared pointer.
*/
using executor_p = std::shared_ptr<executor>;
/** /**
* @brief Furvm executor's index. * @brief Furvm executor's index.
*/ */
using executor_id = std::uint32_t; using executor_id = std::uint32_t;
/**
* @brief A handle to an executor.
*/
using executor_h = handle<executor, refcount_header<executor_id>>;
// context.hpp // context.hpp
/** /**
+4 -4
View File
@@ -80,11 +80,11 @@ int main(int argc, char** argv) {
std::cout << '\n'; std::cout << '\n';
}); });
furvm::executor_h executor = context->emplace_executor(context); auto& executor = context->allocate_executor();
executor->push_frame(mod, *mainFunc); executor.push_frame(mod, *mainFunc);
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) { while ((executor.flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
executor->step(); executor.step();
} }
return 0; return 0;