refactor(furvm): rename handle suffixes to ids

Refs: #12
This commit is contained in:
2026-06-16 16:21:49 +02:00
parent c9f714642c
commit c2b32c9604
9 changed files with 24 additions and 36 deletions
+1 -1
View File
@@ -104,7 +104,7 @@ private:
std::vector<thing_p> m_things;
std::vector<executor_p> m_executors;
std::queue<thing_handle> m_deadThings;
std::queue<thing_id> m_deadThings;
std::vector<void*> m_deadThingData;
furlang::arena m_thingArena;
};
+1 -1
View File
@@ -76,7 +76,7 @@ public:
* @param mod Module.
* @param function Function handle.
*/
void push_frame(const mod_p& mod, function_handle function);
void push_frame(const mod_p& mod, function_id function);
/**
* @brief Pops the top frame.
+5 -5
View File
@@ -35,13 +35,13 @@ public:
: m_name(name), m_type(function_t::Native), m_value(native) {}
template <typename Name, typename ModuleName>
function(Name&& name, ModuleName&& moduleName, function_handle function)
function(Name&& name, ModuleName&& moduleName, function_id function)
: m_name(std::forward<Name>(name)),
m_type(function_t::Import),
m_value(std::forward<ModuleName>(moduleName), function) {}
template <typename ModuleName>
function(const char* name, ModuleName&& moduleName, function_handle function)
function(const char* name, ModuleName&& moduleName, function_id function)
: m_name(name), m_type(function_t::Import), m_value(std::forward<ModuleName>(moduleName), function) {}
~function();
@@ -108,7 +108,7 @@ public:
*
* @return A handle to the module.
*/
function_handle imported_function() const {
function_id imported_function() const {
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
return m_value.imp.function;
}
@@ -121,7 +121,7 @@ private:
native_function native;
struct {
std::string moduleName;
function_handle function;
function_id function;
} imp;
value() = default;
@@ -133,7 +133,7 @@ private:
: native(native) {}
template <typename Name>
value(Name&& moduleName, function_handle function)
value(Name&& moduleName, function_id function)
: imp({ std::forward<Name>(moduleName), function }) {}
~value() {}
+3 -3
View File
@@ -81,7 +81,7 @@ using function_p = std::shared_ptr<function>;
/**
* @brief Furvm function's index.
*/
using function_handle = std::uint16_t;
using function_id = std::uint16_t;
// module.hpp
@@ -128,7 +128,7 @@ using thing_p = std::shared_ptr<thing>;
/**
* @brief Furvm thing's index.
*/
using thing_handle = std::uint32_t;
using thing_id = std::uint32_t;
// executor.hpp
@@ -159,7 +159,7 @@ using executor_p = std::shared_ptr<executor>;
/**
* @brief Furvm executor's index.
*/
using executor_handle = std::uint32_t;
using executor_id = std::uint32_t;
// context.hpp
+3 -15
View File
@@ -103,25 +103,13 @@ public:
* @param id Id of the function.
* @return The function.
*/
constexpr const function_p& function_at(function_handle id) const { return m_functions.at(id); }
/**
* @brief Returns an id of a function.
*
* @param function Function to get the id of.
* @return The function's id.
*/
function_handle function_id(const function_p& function) const {
if (auto it = std::find(m_functions.begin(), m_functions.end(), function); it != m_functions.end())
return it - m_functions.begin();
throw std::runtime_error("function not in the module");
}
constexpr const function_p& function_at(function_id id) const { return m_functions.at(id); }
template <typename... Args>
function_handle emplace_function(Args&&... 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_handle id = m_functions.size();
function_id id = m_functions.size();
m_functions.emplace_back(std::move(function));
return id;
}
+1 -1
View File
@@ -56,7 +56,7 @@ class thing final {
public:
using nref_t = std::size_t; /**< Type of reference count. */
static constexpr thing_handle GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
static constexpr thing_id GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
public:
thing(const context_p& context, thing_t type);
+2 -2
View File
@@ -13,7 +13,7 @@
namespace furvm {
void executor::push_frame(const mod_p& mod, function_handle function) {
void executor::push_frame(const mod_p& mod, function_id function) {
const auto& func = mod->function_at(function);
if (func->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
m_frames.emplace((struct executor::frame){ mod, func->position(), m_stack.size() });
@@ -168,7 +168,7 @@ void executor::step() {
store_thing(variable, std::move(pop_thing()));
} break;
case instruction_t::Call: {
function_handle funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
function_id funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
frame.position += 2;
+1 -1
View File
@@ -75,7 +75,7 @@ bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
write_u32(os, VERSION);
write_u32(os, mod->m_functions.size());
for (function_handle id = 0; id < static_cast<function_handle>(mod->m_functions.size()); ++id) {
for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) {
const auto& func = mod->m_functions[id];
write_u16(os, id);
write_u8(os, static_cast<std::uint8_t>(func->type()));
+2 -2
View File
@@ -201,13 +201,13 @@ thing_p operator>=(const thing_p& lhs, const thing_p& rhs) {
}
thing_p thing::clone(const thing_p& thing) {
thing_handle id = thing->m_context->m_things.size();
thing_id id = thing->m_context->m_things.size();
if (!thing->m_context->m_deadThings.empty()) {
id = thing->m_context->m_deadThings.front();
thing->m_context->m_deadThings.pop();
id += 1 << GENERATION_SIZE;
}
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
thing_id idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
auto th = std::make_shared<class thing>(thing->m_context, thing->m_type);
switch (thing->m_type) {