refactor(furvm): enforce argument types

This commit is contained in:
2026-07-09 15:02:37 +02:00
parent b7da2f3d32
commit abbc1714c1
2 changed files with 26 additions and 3 deletions
+19
View File
@@ -5,6 +5,7 @@
#include "furvm/handle.hpp" // IWYU pragma: keep #include "furvm/handle.hpp" // IWYU pragma: keep
#include <cstdint> #include <cstdint>
#include <stdexcept>
namespace furvm { namespace furvm {
@@ -29,11 +30,17 @@ struct array_type {
* Size of the array. If size is equal to zero, then the array becomes dynamic. * Size of the array. If size is equal to zero, then the array becomes dynamic.
*/ */
std::size_t size; std::size_t size;
bool operator==(const array_type& rhs) const { return type == rhs.type && size == rhs.size; }
bool operator!=(const array_type& rhs) const { return !this->operator==(rhs); }
}; };
struct import_type { struct import_type {
mod_id mod; mod_id mod;
type_id type; type_id type;
bool operator==(const import_type& rhs) const { return mod == rhs.mod && type == rhs.type; }
bool operator!=(const import_type& rhs) const { return !this->operator==(rhs); }
}; };
struct type { struct type {
@@ -108,6 +115,18 @@ struct type {
return *this; return *this;
} }
bool operator==(const type& rhs) const {
if (t != rhs.t) return false;
switch (t) {
case type_t::Primitive: return primitive == rhs.primitive;
case type_t::Array: return array == rhs.array;
case type_t::Import: return imp == rhs.imp;
}
throw std::runtime_error("unreachable");
}
bool operator!=(const type& rhs) const { return !this->operator==(rhs); }
}; };
using byte_t = std::int8_t; /**< A 1-byte integer. */ using byte_t = std::int8_t; /**< A 1-byte integer. */
+7 -3
View File
@@ -21,10 +21,14 @@ void executor::push_frame(const mod_h& mod, function function) {
function = *modInst->function_at(function.imp().function); function = *modInst->function_at(function.imp().function);
} }
auto signature = function.signature();
std::vector<thing_h> args; std::vector<thing_h> args;
args.reserve(function.signature().params.size()); args.reserve(signature.params.size());
while (args.size() < args.capacity()) for (const auto& param : signature.params) {
args.push_back(pop_thing()); auto arg = pop_thing();
if (arg->type().type != param) throw std::runtime_error("function argument type mismatch");
args.push_back(std::move(arg));
}
switch (function.type()) { switch (function.type()) {
case function_t::Normal: { case function_t::Normal: {