refactor(furvm): improve functions

Define native functions inside the module instead of the function
itself (native functions hold only the key to the definition), introduce
private functions, add parameter count to function and fix some bugs.

Closes: #20
Closes: #25
This commit is contained in:
2026-06-28 12:37:36 +02:00
parent 55932f6113
commit 8702298cd2
6 changed files with 157 additions and 54 deletions
+16 -5
View File
@@ -13,22 +13,33 @@ static constexpr std::array<furvm::byte, 8> s_bytecode = {
67,
furvm::byte(furvm::instruction_t::Clone),
furvm::byte(furvm::instruction_t::Add),
furvm::byte(furvm::instruction_t::Call),
1,
0,
furvm::byte(furvm::instruction_t::Return),
};
int main(void) {
auto context = std::make_shared<furvm::context>();
furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
furvm::function_h mainFunc = mainModule->emplace_function("main", 0);
furvm::mod_h furlangModule = context->emplace_module("furlang");
furvm::function_h printFunc = furlangModule->emplace_function("print", 1, "print");
furlangModule->set_native_function("print", [](furvm::executor& executor) {
furvm::thing_h thing = executor.load_thing(0);
switch (thing->type()) {
case furvm::thing_t::Int32: {
std::cout << thing->int32() << '\n';
} break;
}
});
mainModule->serialize(std::cout);
furvm::mod_h mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
furvm::function_h mainFunc = mainModule->emplace_function("main", 0, 0);
mainModule->emplace_function(furlangModule, printFunc).dispatch();
furvm::executor_h executor = context->emplace_executor(context);
executor->push_frame(mainModule, *mainFunc);
static constexpr std::size_t FPC = 3; // Frames per collection
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
executor->step();
}