From a7ab214ce39f82845e52bb2813a834eef0429281 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 3 Jul 2026 19:28:59 +0200 Subject: [PATCH] refactor: make context inherit module handle container Refs: #39 --- furc/src/main.cpp | 2 +- furvm/include/furvm/context.hpp | 45 +-------------------------------- furvm/src/executor.cpp | 2 +- furvm/src/main.cpp | 4 +-- 4 files changed, 5 insertions(+), 48 deletions(-) diff --git a/furc/src/main.cpp b/furc/src/main.cpp index d370a17..45d8de4 100644 --- a/furc/src/main.cpp +++ b/furc/src/main.cpp @@ -61,7 +61,7 @@ int main(void) { } auto context = std::make_shared(); - auto furvmMod = context->emplace_module("main", furc::back::furvm_generator::generate(mod)); + auto furvmMod = context->emplace("main", furc::back::furvm_generator::generate(mod)); std::ofstream file("./a.fmod", std::ios::binary); furvmMod->serialize(file); diff --git a/furvm/include/furvm/context.hpp b/furvm/include/furvm/context.hpp index 73bb4ce..e588c97 100644 --- a/furvm/include/furvm/context.hpp +++ b/furvm/include/furvm/context.hpp @@ -13,7 +13,7 @@ namespace furvm { -class context { +class context : public handle_container { public: friend class executor; public: @@ -36,49 +36,6 @@ public: context(const context&) = delete; context& operator=(const context&) = delete; -public: - /** - * @brief Emplaces a module in the context. - * - * @param args Arguments forwarded to the module constructor. - * @return The emplaced module. - */ - template - auto emplace_module(Args&&... args) { - return m_modules.emplace(std::forward(args)...); - } - - /** - * @brief Returns a module from the context. - * - * @param args Module's id. - * @return A handle to the module. - */ - template - auto module_at(Args&&... args) { - return m_modules.at(std::forward(args)...); - } - - /** - * @brief Returns a module from the context. - * - * @param args Module's id. - * @return A handle to the module. - */ - template - auto module_at(Args&&... args) const { - return m_modules.at(std::forward(args)...); - } - - /** - * @brief Erases a module from the context. - * - * @param args Module's id. - */ - template - void erase_module(Args&&... args) { - m_modules.erase(std::forward(args)...); - } public: /** * @brief Emplaces an executor in the context. diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index 07c6ba2..56a7a70 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -16,7 +16,7 @@ namespace furvm { void executor::push_frame(const mod_h& mod, function function) { mod_h modInst = mod; while (function.type() == function_t::Import) { - modInst = m_context->module_at(function.imp().mod); + modInst = m_context->at(function.imp().mod); function = *modInst->function_at(function.imp().function); } diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 715992c..bcd353d 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -23,12 +23,12 @@ static constexpr std::array s_bytecode = { int main(void) { auto context = std::make_shared(); - furvm::mod_h furlangModule = context->emplace_module("furlang"); + furvm::mod_h furlangModule = context->emplace("furlang"); furvm::function_h printFunc = furlangModule->emplace_function("print", 1, "print"); furlangModule->set_native_function("print", [](furvm::executor& executor) { std::cout << executor.load_thing(0)->integer() << '\n'; }); - furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end()); + furvm::mod_h mainModule = context->emplace("main", s_bytecode.begin(), s_bytecode.end()); furvm::function_h mainFunc = mainModule->emplace_function("main", 0, 0); mainModule->emplace_function(furlangModule, printFunc).dispatch();