From 100c542fe1856fb1af2df955934c666419d7d6bd Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Sat, 4 Jul 2026 16:15:40 +0200 Subject: [PATCH] refactor: serialize types in module Refs: #23 --- furvm/include/furvm/thing.hpp | 10 +++++----- furvm/include/furvm/type.hpp | 14 +++++--------- furvm/src/module.cpp | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index baba740..7ead4b5 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -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(), get()); + copy_list(resolve_type(*m_type->list, m_modules), res.get(), get()); } 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(); 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(); 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() = 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(dst.data)), *std::launder(reinterpret_cast(src.data))); } diff --git a/furvm/include/furvm/type.hpp b/furvm/include/furvm/type.hpp index 409bd65..2c492cb 100644 --- a/furvm/include/furvm/type.hpp +++ b/furvm/include/furvm/type.hpp @@ -2,10 +2,9 @@ #define FURVM_TYPE_HPP #include "furvm/fwd.hpp" +#include "furvm/handle.hpp" // IWYU pragma: keep #include -#include -#include 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; -using list_type = std::shared_ptr; +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) {} diff --git a/furvm/src/module.cpp b/furvm/src/module.cpp index fcd272f..fd8f962 100644 --- a/furvm/src/module.cpp +++ b/furvm/src/module.cpp @@ -2,8 +2,10 @@ #include "furvm/detail/serialization.hpp" #include "furvm/fwd.hpp" +#include "furvm/type.hpp" #include +#include namespace furvm { @@ -35,6 +37,26 @@ std::ostream& mod::serialize(std::ostream& os) const { } } + type_id typeCount = type_id(m_types.cend() - m_types.cbegin()); + detail::serialize(os, typeCount); + for (type_id id = 0; id < typeCount; ++id) { + if (!m_types.contains(id)) { + detail::serialize(os, std::uint8_t(0xFF)); // null type + continue; + } + + auto type = *m_types.at(id); + switch (type->t) { + case type_t::Primitive: detail::serialize(os, type->primitive); break; + case type_t::Reference: throw std::runtime_error("reference type serialization is unimplemented"); + case type_t::List: detail::serialize(os, type->list.id()); break; + case type_t::Import: { + detail::serialize(os, type->imp.mod); + detail::serialize(os, type->imp.type); + } break; + } + } + detail::serialize(os, std::uint64_t(m_bytecode.size())); return os.write(reinterpret_cast(m_bytecode.data()), static_cast(m_bytecode.size())); }