refactor(furvm): use the handle system

Refs: #12
This commit is contained in:
2026-06-17 13:59:54 +02:00
parent e8ccaa6390
commit 00bcc0e6a5
10 changed files with 112 additions and 53 deletions
+1 -1
View File
@@ -13,4 +13,4 @@ void context::collect() {
}
}
} // namespace furvm
} // namespace furvm
+18 -18
View File
@@ -13,10 +13,9 @@
namespace furvm {
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() });
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() });
}
struct executor::frame executor::pop_frame() {
@@ -83,12 +82,12 @@ void executor::step() {
struct frame& frame = m_frames.top();
instruction_t instr = static_cast<instruction_t>(frame.mod->byte(frame.position++));
instruction_t instr = static_cast<instruction_t>((*frame.mod)->byte(frame.position++));
switch (instr) {
case instruction_t::NoOperation: break;
case instruction_t::PushB2I: {
auto thing = std::make_shared<class thing>(m_context, thing_t::Int32);
thing->int32() = frame.mod->byte(frame.position++);
thing->int32() = (*frame.mod)->byte(frame.position++);
push_thing(std::move(thing));
} break;
case instruction_t::Drop: {
@@ -156,37 +155,38 @@ void executor::step() {
push_thing(lhs >= rhs);
} break;
case instruction_t::Load: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
variable_t variable = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2;
push_thing(load_thing(variable));
} break;
case instruction_t::Store: {
variable_t variable = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
variable_t variable = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2;
store_thing(variable, std::move(pop_thing()));
} break;
case instruction_t::Call: {
function_id funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
function_id funcId = static_cast<std::uint16_t>((*frame.mod)->byte(frame.position)) |
(static_cast<std::uint16_t>((*frame.mod)->byte(frame.position + 1)) << 8);
frame.position += 2;
const function_p& function = frame.mod->function_at(funcId);
const function_h& function = (*frame.mod)->function_at(funcId);
switch (function->type()) {
case function_t::Normal: push_frame(frame.mod, funcId); break;
case function_t::Normal: 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());
// 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: {
frame.position += frame.mod->byte(frame.position++);
frame.position += (*frame.mod)->byte(frame.position++);
} break;
case instruction_t::JumpNotZero: {
byte offset = frame.mod->byte(frame.position++);
byte offset = (*frame.mod)->byte(frame.position++);
auto cond = pop_thing();
if (cond->int32() != 0) frame.position += offset;
} break;
+37
View File
@@ -54,4 +54,41 @@ function& function::operator=(function&& other) noexcept {
return *this;
}
function::function(const function& other)
: m_name(other.m_name), m_type(other.m_type) {
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
} break;
case function_t::Native: {
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;
} break;
}
}
function& function::operator=(const function& other) {
if (this == &other) return *this;
m_name = other.m_name;
m_type = other.m_type;
switch (m_type) {
case function_t::Normal: {
m_value.position = other.m_value.position;
} break;
case function_t::Native: {
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;
} break;
}
return *this;
}
} // namespace furvm
+5 -4
View File
@@ -3,6 +3,7 @@
#include "furvm/context.hpp"
#include "furvm/executor.hpp"
#include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/serializer.hpp"
#include "furvm/thing.hpp"
@@ -24,11 +25,11 @@ static constexpr std::array<furvm::byte, 8> s_bytecode = {
int main(void) {
auto context = std::make_shared<furvm::context>();
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
mainModule->emplace_function("main", 0);
furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
furvm::function_h mainFunc = (*mainModule)->emplace_function("main", 0);
auto executor = context->emplace_executor(context);
executor->push_frame(mainModule, 0);
furvm::executor_h executor = context->emplace_executor(context);
executor->push_frame(mainModule, mainFunc);
static constexpr std::size_t FPC = 3; // Frames per collection
+1 -1
View File
@@ -73,7 +73,7 @@ bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
write_u32(os, mod->m_functions.size());
for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) {
const auto& func = mod->m_functions[id];
const auto& func = mod->function_at(id);
write_u16(os, id);
write_u8(os, static_cast<std::uint8_t>(func->type()));
switch (func->type()) {