refactor(furvm): refactor function

Refs: #12
This commit is contained in:
2026-06-16 12:48:16 +02:00
parent 43dac1ec6d
commit 78acfb6ee2
8 changed files with 94 additions and 81 deletions
+6 -5
View File
@@ -25,9 +25,10 @@ executor_p executor::create(const context_p& context) {
return std::move(ex);
}
void executor::push_frame(const function_p& function) {
if (function->type() != function_t::Normal) return;
m_frames.emplace((struct executor::frame){ function->mod(), function->position(), m_stack.size() });
void executor::push_frame(const mod_p& mod, function_handle 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() });
}
struct executor::frame executor::pop_frame() {
@@ -185,11 +186,11 @@ void executor::step() {
const function_p& function = frame.mod->function_at(funcId);
switch (function->type()) {
case function_t::Normal: push_frame(function); break;
case function_t::Normal: push_frame(frame.mod, funcId); 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(impMod->function_at(function->imported_function()));
push_frame(frame.mod, function->imported_function());
} break;
}
} break;
+3 -10
View File
@@ -4,12 +4,6 @@
namespace furvm {
function::function(funkcja, function_handle id, std::size_t position, const mod_p& mod)
: m_id(id), m_type(function_t::Normal), m_module(mod), m_value(position) {}
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:
@@ -22,7 +16,7 @@ function::~function() {
}
function::function(function&& other) noexcept
: m_id(other.m_id), m_type(other.m_type), m_module(std::move(other.m_module)) {
: m_name(std::move(other.m_name)), m_type(other.m_type) {
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
@@ -41,9 +35,8 @@ function::function(function&& other) noexcept
function& function::operator=(function&& other) noexcept {
if (this == &other) return *this;
m_id = other.m_id;
m_type = other.m_type;
m_module = std::move(other.m_module);
m_name = std::move(other.m_name);
m_type = other.m_type;
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
+2 -3
View File
@@ -25,14 +25,13 @@ int main(void) {
auto context = std::make_shared<furvm::context>();
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
auto mainFunction = furvm::function::create(mainModule, 0);
mainModule->emplace_function("main", 0);
std::ofstream file("./test.furm", std::ios::binary);
furvm::serializer::serialize_module(file, mainModule);
auto executor = furvm::executor::create(context);
executor->push_frame(mainFunction);
executor->push_frame(mainModule, 0);
static constexpr std::size_t FPC = 3; // Frames per collection
+5 -2
View File
@@ -1,6 +1,8 @@
#include "furvm/serializer.hpp"
#include "furvm/function.hpp" // IWYU pragma: keep
#include "furvm/fwd.hpp"
#include "furvm/module.hpp" // IWYU pragma: keep
#include <array>
#include <cstring>
@@ -73,8 +75,9 @@ bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
write_u32(os, VERSION);
write_u32(os, mod->m_functions.size());
for (const auto& func : mod->m_functions) {
write_u16(os, func->id());
for (function_handle id = 0; id < static_cast<function_handle>(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()));
switch (func->type()) {
case function_t::Normal: {