refactor: serialize types in module

Refs: #23
This commit is contained in:
2026-07-04 16:15:40 +02:00
parent cf284592fd
commit 100c542fe1
3 changed files with 32 additions and 14 deletions
+5 -5
View File
@@ -101,7 +101,7 @@ public:
std::memcpy(res.m_data, m_data, m_size);
} break;
case type_t::List: {
copy_list(resolve_type(m_type->list, m_modules), res.get<list_t>(), get<list_t>());
copy_list(resolve_type(*m_type->list, m_modules), res.get<list_t>(), get<list_t>());
} break;
case type_t::Import: throw std::runtime_error("unreachable");
}
@@ -274,10 +274,10 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>();
if (newSize < 0 || newSize == list.size) return;
std::byte* newData = new std::byte[compute_size_na(resolve_type(m_type->list, m_modules)) * newSize];
std::byte* newData = new std::byte[compute_size_na(resolve_type(*m_type->list, m_modules)) * newSize];
std::memcpy(newData,
list.data,
compute_size_na(resolve_type(m_type->list, m_modules)) * std::min(list.size, newSize));
compute_size_na(resolve_type(*m_type->list, m_modules)) * std::min(list.size, newSize));
list.size = newSize;
delete[] list.data;
list.data = newData;
@@ -287,7 +287,7 @@ public:
if (m_type->t != type_t::List) throw bad_thing_access();
auto& list = get<list_t>();
if (index < 0 || index >= list.size) throw std::out_of_range("index out of range");
thing res = { m_type->list, m_modules, m_allocator };
thing res = { *m_type->list, m_modules, m_allocator };
res.get<reference_t>() = list.data; // TODO: Account for padding, alignment and stuff
return std::move(res);
}
@@ -314,7 +314,7 @@ private:
case type_t::Reference: std::memcpy(dst.data, src.data, size); break;
case type_t::List:
for (std::size_t i = 0; i < size; ++i) {
copy_list(innerType->list,
copy_list(*innerType->list,
*std::launder(reinterpret_cast<list_t*>(dst.data)),
*std::launder(reinterpret_cast<list_t*>(src.data)));
}
+5 -9
View File
@@ -2,10 +2,9 @@
#define FURVM_TYPE_HPP
#include "furvm/fwd.hpp"
#include "furvm/handle.hpp" // IWYU pragma: keep
#include <cstdint>
#include <memory>
#include <utility>
namespace furvm {
@@ -18,8 +17,8 @@ enum class type_t : std::uint32_t {
};
using primitive_type = std::uint64_t;
using reference_type = std::shared_ptr<type>;
using list_type = std::shared_ptr<type>;
using reference_type = type_p;
using list_type = type_h;
struct import_type {
mod_id mod;
@@ -44,11 +43,8 @@ struct type {
type(const reference_type& reference)
: t(type_t::Reference), reference(reference) {}
static type make_list(const list_type& list) {
type type(type_t::List);
new (&type.list) list_type(list);
return type;
}
type(const list_type& list)
: t(type_t::List), list(list) {}
type(const import_type& imp)
: t(type_t::Import), imp(imp) {}