diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index f9ae6ae..a8397ef 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -6,7 +6,10 @@ #include "furvm/module.hpp" #include +#include #include +#include +#include #include namespace furvm { @@ -33,73 +36,64 @@ public: context& operator=(const context&) = delete; public: /** - * @brief Adds a module to this context. + * @brief Emplaces a new module in this context. * * @param args Arguments to forward to module's constructor. * @return An index to the emplaced module. */ - template >> - constexpr const auto& emplace(Args&&... args) { - module_handle id = static_cast(m_modules.size()); - return m_modules.emplace_back(std::make_unique(id, std::forward(args)...)); + template >> + auto& emplace_module(Args&&... args) { + mod_p mod = std::make_shared(std::forward(args)...); + return m_modules.emplace(mod->name(), std::move(mod)).first->second; } /** * @brief Erases a module from this context. * - * @param index Index to the module. - * @return Old value. + * @param name Name of the module to erase. + * @return A shared pointer to the erased module. */ - mod_p erase(module_handle index) { - if (index >= m_modules.size()) return nullptr; - return std::move(m_modules[index]); + template + mod_p erase_module(Name&& name) { + return std::move(m_modules.erase(std::forward(name))->second); } /** * @brief Returns a module of this context. * - * @param index Position of the module. + * @param name Name of the module. * @return The module. */ - constexpr mod_p& operator[](module_handle index) { return m_modules[index]; } + template + constexpr mod_p& module_at(Name&& name) { + return m_modules.at(std::forward(name)); + } /** * @brief Returns a module of this context. * - * @param index Position of the module. + * @param name Name of the module. * @return The module. */ - constexpr const mod_p& operator[](module_handle index) const { return m_modules[index]; } - - /** - * @brief Returns a module of this context. - * - * @param index Position of the module. - * @return The module. - */ - constexpr mod_p& at(module_handle index) { return m_modules.at(index); } - - /** - * @brief Returns a module of this context. - * - * @param index Position of the module. - * @return The module. - */ - constexpr const mod_p& at(module_handle index) const { return m_modules.at(index); } + template + constexpr const mod_p& module_at(Name&& name) const { + return m_modules.at(std::forward(name)); + } /** * @brief Returns how many does this context have modules. * * @return The module count. */ - constexpr size_t size() const { return m_modules.size(); } + constexpr size_t module_count() const { return m_modules.size(); } public: /** * @brief Removes unreferenced things from the thing list. */ void collect(); private: - std::vector m_modules; + std::unordered_map m_modules; + std::vector m_things; std::vector m_executors; @@ -110,4 +104,4 @@ private: } // namespace furvm -#endif // FURVM_CONTEXT_HPP \ No newline at end of file +#endif // FURVM_CONTEXT_HPP diff --git a/furvm/include/furvm/function.hpp b/furvm/include/furvm/function.hpp index 2b3fb93..2831cc1 100644 --- a/furvm/include/furvm/function.hpp +++ b/furvm/include/furvm/function.hpp @@ -6,6 +6,8 @@ #include #include +#include +#include namespace furvm { @@ -46,7 +48,7 @@ public: */ function(funkcja, function_handle id, const native_function& native, const mod_p& mod); - ~function() = default; + ~function(); /** * @brief Move constructor. @@ -120,13 +122,13 @@ public: } /** - * @brief Returns a module of imported function. + * @brief Returns a name of the function's module. * - * @return A handle to the module. + * @return The name. */ - module_handle imported_module() const { + const std::string& imported_module() const { if (m_type != function_t::Import) throw std::runtime_error("function type mismatch"); - return m_value.imp.mod; + return m_value.imp.moduleName; } /** @@ -147,7 +149,7 @@ private: std::size_t position = 0; native_function native; struct { - module_handle mod; + std::string moduleName; function_handle function; } imp; @@ -159,8 +161,9 @@ private: value(const native_function& native) : native(native) {} - value(module_handle mod, function_handle function) - : imp({ mod, function }) {} + template + value(Name&& moduleName, function_handle function) + : imp({ std::forward(moduleName), function }) {} ~value() {} diff --git a/furvm/include/furvm/fwd.hpp b/furvm/include/furvm/fwd.hpp index aaa832a..04f5a2f 100644 --- a/furvm/include/furvm/fwd.hpp +++ b/furvm/include/furvm/fwd.hpp @@ -93,11 +93,6 @@ class mod; */ using mod_p = std::shared_ptr; -/** - * @brief Furvm module's index. - */ -using module_handle = std::uint32_t; - // thing.hpp /** diff --git a/furvm/include/furvm/module.hpp b/furvm/include/furvm/module.hpp index 709a763..582c38e 100644 --- a/furvm/include/furvm/module.hpp +++ b/furvm/include/furvm/module.hpp @@ -3,6 +3,7 @@ #include "furvm/fwd.hpp" +#include #include namespace furvm { @@ -16,12 +17,24 @@ public: /** * @brief Construct a new module. * - * @param id Id of this module. + * @param name Name of this module. + * @param args Arguments to forward to bytecode's constructor. + */ + template >> + mod(Name&& name, Args&&... args) + : m_name(std::forward(name)), m_bytecode(std::forward(args)...) {} + + /** + * @brief Construct a new module. + * + * @param name C-string name of this module. * @param args Arguments to forward to bytecode's constructor. */ template >> - mod(module_handle id, Args&&... args) - : m_id(id), m_bytecode(std::forward(args)...) {} + mod(const char* name, Args&&... args) + : m_name(name), m_bytecode(std::forward(args)...) {} ~mod() = default; @@ -39,11 +52,11 @@ public: mod& operator=(const mod&) = delete; public: /** - * @brief Returns an id of this module. + * @brief Returns this module's name. * - * @return The id. + * @return The name. */ - constexpr module_handle id() const { return m_id; } + constexpr const std::string& name() const { return m_name; } /** * @brief Returns a byte from bytecode of this module. @@ -52,6 +65,20 @@ public: * @return The byte. */ constexpr byte byte(std::size_t offset) const { return m_bytecode.at(offset); } + + /** + * @brief Returns a reference to the module's bytecode. + * + * @return The reference to bytecode. + */ + constexpr bytecode_t& bytecode() { return m_bytecode; } + + /** + * @brief Returns a const reference to the module's bytecode. + * + * @return The reference to bytecode. + */ + constexpr const bytecode_t& bytecode() const { return m_bytecode; } public: /** * @brief Returns a function from this module. @@ -61,7 +88,7 @@ public: */ constexpr const function_p& function_at(function_handle id) const { return m_functions.at(id); } private: - module_handle m_id; + std::string m_name; bytecode_t m_bytecode; std::vector m_functions; }; diff --git a/furvm/src/function.cpp b/furvm/src/function.cpp index ac74dc8..7092a3c 100644 --- a/furvm/src/function.cpp +++ b/furvm/src/function.cpp @@ -1,5 +1,7 @@ #include "furvm/function.hpp" +#include + namespace furvm { function::function(funkcja, function_handle id, std::size_t position, const mod_p& mod) @@ -8,6 +10,17 @@ function::function(funkcja, function_handle id, std::size_t position, const mod_ function::function(funkcja, function_handle id, const native_function& native, const mod_p& mod) : m_id(id), m_type(function_t::Native), m_module(mod), m_value(native) {} +function::~function() { + switch (m_type) { + case function_t::Normal: + case function_t::Native: + default: break; + case function_t::Import: { + m_value.imp.moduleName.~basic_string(); + } break; + } +} + function::function(function&& other) noexcept : m_id(other.m_id), m_type(other.m_type), m_module(std::move(other.m_module)) { switch (m_type) { @@ -18,8 +31,8 @@ function::function(function&& other) noexcept new (&m_value.native) native_function(std::move(other.m_value.native)); } break; case function_t::Import: { - m_value.imp.mod = other.m_value.imp.mod; - m_value.imp.function = other.m_value.imp.function; + m_value.imp.moduleName = std::move(other.m_value.imp.moduleName); + m_value.imp.function = other.m_value.imp.function; } break; } other.m_value.position = 0; @@ -39,8 +52,8 @@ function& function::operator=(function&& other) noexcept { new (&m_value.native) native_function(std::move(other.m_value.native)); } break; case function_t::Import: { - m_value.imp.mod = other.m_value.imp.mod; - m_value.imp.function = other.m_value.imp.function; + m_value.imp.moduleName = std::move(other.m_value.imp.moduleName); + m_value.imp.function = other.m_value.imp.function; } break; } other.m_value.position = 0; diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 16a07bd..e2287ab 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -24,7 +24,7 @@ static constexpr std::array s_bytecode = { int main(void) { auto context = std::make_shared(); - auto mainModule = context->emplace(s_bytecode.begin(), s_bytecode.end()); + auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end()); auto mainFunction = furvm::function::create(mainModule, 0);