feat(furvm): add basic function

This commit is contained in:
2026-06-11 14:46:17 +02:00
parent 5472b77ea0
commit ba39f5ab27
7 changed files with 244 additions and 8 deletions
+4 -2
View File
@@ -2,6 +2,7 @@
#include "furvm/context.hpp" // IWYU pragma: keep
#include "furvm/exceptions.hpp"
#include "furvm/function.hpp" // IWYU pragma: keep
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
@@ -22,8 +23,9 @@ executor_p executor::create(const context_p& context) {
return std::move(ex);
}
void executor::push_frame(const mod_p& mod, std::size_t position) {
m_frames.emplace((struct executor::frame){ mod, position, m_stack.size() });
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() });
}
struct executor::frame executor::pop_frame() {
+43
View File
@@ -0,0 +1,43 @@
#include "furvm/function.hpp"
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(function&& other) noexcept
: m_id(other.m_id), m_type(other.m_type), m_module(std::move(other.m_module)) {
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(std::move(other.m_value.native));
} break;
}
other.m_value.position = 0;
}
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);
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(std::move(other.m_value.native));
} break;
}
other.m_value.position = 0;
return *this;
}
} // namespace furvm
+12 -1
View File
@@ -2,10 +2,13 @@
#include "furvm/context.hpp"
#include "furvm/executor.hpp"
#include "furvm/function.hpp"
#include "furvm/instruction.hpp"
#include "furvm/thing.hpp"
#include <array>
#include <cstddef>
#include <iostream>
#include <memory>
static constexpr std::array<furvm::byte, 6> s_bytecode = {
@@ -17,13 +20,21 @@ static constexpr std::array<furvm::byte, 6> s_bytecode = {
furvm::byte(furvm::instruction_t::Return),
};
void print(const furvm::executor_p& exec) {
std::cout << exec->pop_thing()->int32() << '\n';
}
int main(void) {
auto context = std::make_shared<furvm::context>();
auto mainModule = context->emplace(s_bytecode.begin(), s_bytecode.end());
auto mainFunction = furvm::function::create(mainModule, 0);
auto printFunction = furvm::function::create(mainModule, print);
auto executor = furvm::executor::create(context);
executor->push_frame(mainModule, 0);
executor->push_frame(mainFunction);
static constexpr std::size_t FPC = 3; // Frames per collection