refactor(furvm): use the handle system

Refs: #12
This commit is contained in:
2026-06-17 13:59:54 +02:00
parent e8ccaa6390
commit 00bcc0e6a5
10 changed files with 112 additions and 53 deletions
+13 -9
View File
@@ -2,6 +2,7 @@
#define FURVM_CONTEXT_HPP
#include "furlang/arena.hpp"
#include "furvm/executor.hpp"
#include "furvm/fwd.hpp"
#include "furvm/module.hpp"
@@ -40,12 +41,13 @@ public:
* @brief Emplaces a new module in this context.
*
* @param args Arguments to forward to module's constructor.
* @return An index to the emplaced module.
* @return A handle to the module.
*/
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<mod, Args...>>>
auto& emplace_module(Args&&... args) {
mod_p mod = std::make_shared<class mod>(std::forward<Args>(args)...);
return m_modules.emplace(mod->name(), std::move(mod)).first->second;
mod_h emplace_module(Args&&... args) {
mod_p mod = std::make_shared<class mod>(std::forward<Args>(args)...);
std::string name = mod->name();
return { name, m_modules[name] = std::move(mod) };
}
/**
@@ -89,9 +91,11 @@ public:
constexpr size_t module_count() const { return m_modules.size(); }
public:
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<executor, Args...>>>
auto& emplace_executor(Args&&... args) {
executor_p executor = std::make_shared<class executor>(std::forward<Args>(args)...);
return m_executors.emplace_back(std::move(executor));
executor_h emplace_executor(Args&&... args) {
executor executor(std::forward<Args>(args)...);
executor_id id = m_executors.size();
m_executors.emplace_back(std::move(executor));
return { id, m_executors[id] };
}
public:
/**
@@ -101,8 +105,8 @@ public:
private:
std::unordered_map<std::string, mod_p> m_modules;
std::vector<thing_p> m_things;
std::vector<executor_p> m_executors;
std::vector<thing_p> m_things;
std::vector<executor> m_executors;
std::queue<thing_id> m_deadThings;
std::vector<void*> m_deadThingData;
+11 -4
View File
@@ -33,7 +33,7 @@ public:
* Call frame.
*/
struct frame {
mod_p mod; /**< Shared pointer to a module with the bytecode. */
mod_h mod; /**< Shared pointer to a module with the bytecode. */
std::size_t position; /**< Cursor to a current instruction in the bytecode. */
std::size_t stackBase; /**< Snapshot of the stack size before this frame. */
@@ -60,8 +60,15 @@ public:
*/
executor& operator=(executor&&) noexcept = default;
executor(const executor&) = delete;
executor& operator=(const executor&) = delete;
/**
* @brief Copy constructor.
*/
executor(const executor&) = default;
/**
* @brief Copy constructor.
*/
executor& operator=(const executor&) = default;
public:
/**
* @brief Returns flags of this executor.
@@ -76,7 +83,7 @@ public:
* @param mod Module.
* @param function Function handle.
*/
void push_frame(const mod_p& mod, function_id function);
void push_frame(const mod_h& mod, const function_h& function);
/**
* @brief Pops the top frame.
+9 -2
View File
@@ -56,8 +56,15 @@ public:
*/
function& operator=(function&&) noexcept;
function(const function&) = delete;
function& operator=(const function&) = delete;
/**
* @brief Copy constructor.
*/
function(const function&);
/**
* @brief Copy constructor.
*/
function& operator=(const function&);
public:
/**
* @brief Returns a name of this function.
+14 -12
View File
@@ -4,7 +4,7 @@
#include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
@@ -91,8 +91,9 @@ public:
* @return The function.
*/
template <typename Name>
constexpr const function_p& function_at(Name&& name) const {
return m_functions.at(std::forward<Name>(name));
function_h function_at(Name&& name) const {
if (auto it = m_functionMap.find(std::forward<Name>(name)); it != m_functionMap.end()) return it->second;
throw std::runtime_error("invalid function");
}
/**
@@ -101,22 +102,23 @@ public:
* @param id Id of the function.
* @return The function.
*/
constexpr const function_p& function_at(function_id id) const { return m_functions.at(id); }
function_h function_at(function_id id) const { return { id, m_functions.at(id) }; }
template <typename... Args>
function_id emplace_function(Args&&... args) {
function_p function = std::make_shared<class function>(std::forward<Args>(args)...);
m_functionMap.emplace(function->name(), function);
function_id id = m_functions.size();
m_functions.emplace_back(std::move(function));
return id;
function_h emplace_function(Args&&... args) {
function function(std::forward<Args>(args)...);
std::string name = function.name();
function_id id = m_functions.size();
function_h handle = { id, m_functions.emplace_back(std::move(function)) };
m_functionMap.emplace(std::move(name), handle);
return handle;
}
private:
std::string m_name;
bytecode_t m_bytecode;
std::unordered_map<std::string, function_p> m_functionMap;
std::vector<function_p> m_functions;
std::unordered_map<std::string, function_h> m_functionMap;
std::vector<function> m_functions;
};
} // namespace furvm