diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 230bfda..ba0f6b8 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -104,9 +104,9 @@ private: std::vector m_things; std::vector m_executors; - std::queue m_deadThings; - std::vector m_deadThingData; - furlang::arena m_thingArena; + std::queue m_deadThings; + std::vector m_deadThingData; + furlang::arena m_thingArena; }; } // namespace furvm diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index 858f48f..88f69be 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -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. diff --git a/furvm/include/furvm/function.hpp b/furvm/include/furvm/function.hpp index 4356a05..ca19c01 100644 --- a/furvm/include/furvm/function.hpp +++ b/furvm/include/furvm/function.hpp @@ -35,13 +35,13 @@ public: : m_name(name), m_type(function_t::Native), m_value(native) {} template - function(Name&& name, ModuleName&& moduleName, function_handle function) + function(Name&& name, ModuleName&& moduleName, function_id function) : m_name(std::forward(name)), m_type(function_t::Import), m_value(std::forward(moduleName), function) {} template - 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), 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; } @@ -120,8 +120,8 @@ private: std::size_t position = 0; native_function native; struct { - std::string moduleName; - function_handle function; + std::string moduleName; + function_id function; } imp; value() = default; @@ -133,7 +133,7 @@ private: : native(native) {} template - value(Name&& moduleName, function_handle function) + value(Name&& moduleName, function_id function) : imp({ std::forward(moduleName), function }) {} ~value() {} diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index ef8e3f2..8098d78 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -81,7 +81,7 @@ using function_p = std::shared_ptr; /** * @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; /** * @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; /** * @brief Furvm executor's index. */ -using executor_handle = std::uint32_t; +using executor_id = std::uint32_t; // context.hpp diff --git a/furvm/include/furvm/module.hpp b/furvm/include/furvm/module.hpp index d39e85d..4c394e1 100644 --- a/furvm/include/furvm/module.hpp +++ b/furvm/include/furvm/module.hpp @@ -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 - function_handle emplace_function(Args&&... args) { + function_id emplace_function(Args&&... args) { function_p function = std::make_shared(std::forward(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; } diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 7c0ce11..d85c5fc 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -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); diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index 9479e6c..3514bcd 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -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,9 +168,9 @@ void executor::step() { store_thing(variable, std::move(pop_thing())); } break; case instruction_t::Call: { - function_handle funcId = static_cast(frame.mod->byte(frame.position)) | - (static_cast(frame.mod->byte(frame.position + 1)) << 8); - frame.position += 2; + function_id funcId = static_cast(frame.mod->byte(frame.position)) | + (static_cast(frame.mod->byte(frame.position + 1)) << 8); + frame.position += 2; const function_p& function = frame.mod->function_at(funcId); switch (function->type()) { diff --git a/furvm/src/serializer.cpp b/furvm/src/serializer.cpp index dc76b58..bac4019 100644 --- a/furvm/src/serializer.cpp +++ b/furvm/src/serializer.cpp @@ -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(mod->m_functions.size()); ++id) { + for (function_id id = 0; id < static_cast(mod->m_functions.size()); ++id) { const auto& func = mod->m_functions[id]; write_u16(os, id); write_u8(os, static_cast(func->type())); diff --git a/furvm/src/thing.cpp b/furvm/src/thing.cpp index 4289e02..75cae45 100644 --- a/furvm/src/thing.cpp +++ b/furvm/src/thing.cpp @@ -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(thing->m_context, thing->m_type); switch (thing->m_type) {