From 6fbdb2299b849892aa11f2ff777fd4cd689513bd Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 19 Jun 2026 11:36:20 +0200 Subject: [PATCH] refactor(furvm): refactor import function Refs: #12 --- furvm/include/furvm/executor.hpp | 2 +- furvm/include/furvm/function.hpp | 47 +++++++++----------------------- furvm/src/executor.cpp | 22 +++++++++------ furvm/src/function.cpp | 14 ++++------ furvm/src/main.cpp | 2 +- 5 files changed, 33 insertions(+), 54 deletions(-) diff --git a/furvm/include/furvm/executor.hpp b/furvm/include/furvm/executor.hpp index ae60589..e266a17 100644 --- a/furvm/include/furvm/executor.hpp +++ b/furvm/include/furvm/executor.hpp @@ -86,7 +86,7 @@ public: * @param mod Module. * @param function Function handle. */ - void push_frame(const mod_h& mod, const function_h& function); + void push_frame(const mod_h& mod, function function); /** * @brief Pops the top frame. diff --git a/furvm/include/furvm/function.hpp b/furvm/include/furvm/function.hpp index e09bacb..4a729be 100644 --- a/furvm/include/furvm/function.hpp +++ b/furvm/include/furvm/function.hpp @@ -18,31 +18,24 @@ enum class function_t : std::uint8_t { using native_function = std::function; +struct import_function { + mod_id mod; + function_id function; +}; + class function { public: template function(Name&& name, bytecode_pos position) : m_name(std::forward(name)), m_type(function_t::Normal), m_value(position) {} - function(const char* name, bytecode_pos position) - : m_name(name), m_type(function_t::Normal), m_value(position) {} - template function(Name&& name, const native_function& native) : m_name(std::forward(name)), m_type(function_t::Native), m_value(native) {} - function(const char* name, const native_function& native) - : m_name(name), m_type(function_t::Native), m_value(native) {} - - template - 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_id function) - : m_name(name), m_type(function_t::Import), m_value(std::forward(moduleName), function) {} + template + function(Name&& name, const import_function& imp) + : m_name(std::forward(name)), m_type(function_t::Import), m_value(imp) {} ~function(); @@ -105,19 +98,9 @@ public: * * @return The name. */ - const std::string& imported_module() const { + const import_function& imp() const { if (m_type != function_t::Import) throw std::runtime_error("function type mismatch"); - return m_value.imp.moduleName; - } - - /** - * @brief Returns a module of imported function. - * - * @return A handle to the module. - */ - function_id imported_function() const { - if (m_type != function_t::Import) throw std::runtime_error("function type mismatch"); - return m_value.imp.function; + return m_value.imp; } private: std::string m_name; @@ -126,10 +109,7 @@ private: union value { std::size_t position = 0; native_function native; - struct { - std::string moduleName; - function_id function; - } imp; + import_function imp; value() = default; @@ -139,9 +119,8 @@ private: value(const native_function& native) : native(native) {} - template - value(Name&& moduleName, function_id function) - : imp({ std::forward(moduleName), function }) {} + value(const import_function& imp) + : imp(imp) {} ~value() {} diff --git a/furvm/src/executor.cpp b/furvm/src/executor.cpp index 86b99d0..c784346 100644 --- a/furvm/src/executor.cpp +++ b/furvm/src/executor.cpp @@ -12,9 +12,17 @@ namespace furvm { -void executor::push_frame(const mod_h& mod, const function_h& function) { - if (function->type() != function_t::Normal) throw std::runtime_error("unexpected function type"); - m_frames.emplace((struct executor::frame){ mod, function->position(), m_stack.size() }); +void executor::push_frame(const mod_h& mod, function function) { + while (function.type() == function_t::Import) { + function = *m_context->module_at(function.imp().mod)->function_at(function.imp().function); + } + switch (function.type()) { + case function_t::Normal: { + m_frames.emplace((struct executor::frame){ mod, function.position(), m_stack.size() }); + } break; + case function_t::Native: + default: throw std::runtime_error("unexpected function type"); + } } struct executor::frame executor::pop_frame() { @@ -155,13 +163,9 @@ void executor::step() { const function_h& function = frame.mod->function_at(funcId); switch (function->type()) { - case function_t::Normal: push_frame(frame.mod, function); break; + case function_t::Normal: + case function_t::Import: push_frame(frame.mod, *function); break; case function_t::Native: function->native()(*this); break; - case function_t::Import: { - // const mod_p& impMod = m_context->m_modules.at(function->imported_module()); - // push_frame(frame.mod, function->imported_function()); - throw std::runtime_error("unimplemented"); - } break; } } break; case instruction_t::Jump: { diff --git a/furvm/src/function.cpp b/furvm/src/function.cpp index 82df446..e649be8 100644 --- a/furvm/src/function.cpp +++ b/furvm/src/function.cpp @@ -10,7 +10,7 @@ function::~function() { case function_t::Native: default: break; case function_t::Import: { - m_value.imp.moduleName.~basic_string(); + m_value.imp.~import_function(); } break; } } @@ -25,8 +25,7 @@ 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.moduleName = std::move(other.m_value.imp.moduleName); - m_value.imp.function = other.m_value.imp.function; + m_value.imp = std::move(other.m_value.imp); } break; } other.m_value.position = 0; @@ -45,8 +44,7 @@ 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.moduleName = std::move(other.m_value.imp.moduleName); - m_value.imp.function = other.m_value.imp.function; + m_value.imp = std::move(other.m_value.imp); } break; } other.m_value.position = 0; @@ -64,8 +62,7 @@ function::function(const function& other) new (&m_value.native) native_function(other.m_value.native); } break; case function_t::Import: { - m_value.imp.moduleName = other.m_value.imp.moduleName; - m_value.imp.function = other.m_value.imp.function; + m_value.imp = other.m_value.imp; } break; } } @@ -83,8 +80,7 @@ function& function::operator=(const function& other) { new (&m_value.native) native_function(other.m_value.native); } break; case function_t::Import: { - m_value.imp.moduleName = other.m_value.imp.moduleName; - m_value.imp.function = other.m_value.imp.function; + m_value.imp = other.m_value.imp; } break; } diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index 724aea9..dd6edbc 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -30,7 +30,7 @@ int main(void) { furvm::function_h mainFunc = mainModule->emplace_function("main", 0); furvm::executor_h executor = context->emplace_executor(context); - executor->push_frame(mainModule, mainFunc); + executor->push_frame(mainModule, *mainFunc); static constexpr std::size_t FPC = 3; // Frames per collection