From dfaa3081cbf2c5fd5ee4176577554f60380b5998 Mon Sep 17 00:00:00 2001 From: CHatingPython Date: Fri, 17 Jul 2026 20:41:27 +0200 Subject: [PATCH] fix: use thing::true_type and fix module serialization --- furvm/include/furvm/thing.hpp | 10 +++++----- furvm/src/main.cpp | 13 ++++++------- furvm/src/module.cpp | 16 ++++++++++------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/furvm/include/furvm/thing.hpp b/furvm/include/furvm/thing.hpp index 6a96483..1b12813 100644 --- a/furvm/include/furvm/thing.hpp +++ b/furvm/include/furvm/thing.hpp @@ -433,7 +433,7 @@ public: * @return The integer value. */ thing_type::s64 integer() const { - switch (m_type.type) { + switch (true_type().type) { case thing_type::S8: return get(); case thing_type::S16: return get(); case thing_type::S32: return get(); @@ -448,11 +448,11 @@ public: void resize(thing_type::u64 newSize) { if (!is(thing_type::Array)) throw bad_thing_access(); - if (m_type.value.array.size > 0) throw std::runtime_error("cannot resize a static array"); + if (true_type().value.array.size > 0) throw std::runtime_error("cannot resize a static array"); auto& array = get(); if (newSize < 0 || newSize == array.dynamic.size) return; - std::size_t innerSize = compute_size_na(*m_type.value.array.type); + std::size_t innerSize = compute_size_na(*true_type().value.array.type); std::byte* newData = new std::byte[innerSize * newSize]; std::memcpy(newData, array.dynamic.data, @@ -577,7 +577,7 @@ private: private: template decltype(auto) visit_primitive(Func&& func) const { - switch (m_type.type) { + switch (true_type().type) { case thing_type::S8: return std::forward(func)(get()); case thing_type::S16: return std::forward(func)(get()); case thing_type::S32: return std::forward(func)(get()); @@ -592,7 +592,7 @@ private: template thing binary_op(const thing& rhs, const Op& op) const { - if (thing_type::is_primitive(m_type.type) && thing_type::is_primitive(rhs.m_type.type)) { + if (thing_type::is_primitive(true_type().type) && thing_type::is_primitive(true_type().type)) { static constexpr enum thing_type::type promotions[8 * 8] = { // S8 thing_type::S8, diff --git a/furvm/src/main.cpp b/furvm/src/main.cpp index d884881..96a49f6 100644 --- a/furvm/src/main.cpp +++ b/furvm/src/main.cpp @@ -13,7 +13,7 @@ static void print_thing(const furvm::thing& thing) { using namespace furvm; - switch (thing.type().type) { + switch (thing.true_type().type) { case thing_type::S8: std::cout << thing.cast_to(); break; case thing_type::S16: std::cout << thing.get(); break; case thing_type::S32: std::cout << thing.get(); break; @@ -22,7 +22,7 @@ static void print_thing(const furvm::thing& thing) { case thing_type::U16: std::cout << thing.get(); break; case thing_type::U32: std::cout << thing.get(); break; case thing_type::U64: std::cout << thing.get(); break; - case furvm::thing_type::Array: + case thing_type::Array: std::cout << "{ "; for (thing_type::u64 i = 0; i < thing.length(); ++i) { if (i > 0) std::cout << ", "; @@ -37,20 +37,20 @@ static void print_thing(const furvm::thing& thing) { int main(int argc, char** argv) { auto context = std::make_shared(); -#if 0 // NOLINT +#if 1 // NOLINT if (argc != 2) { std::cerr << "Usage: " << argv[0] << " \n"; return 1; } - std::ifstream file(argv[1]); if (!file.is_open()) { std::cerr << "Failed to open " << argv[1] << '\n'; return 1; } - furvm::mod_h mod = context->emplace("main", std::move(furvm::mod::load(file))); + furvm::mod_h mod = context->emplace("main", std::move(furvm::mod::load(file))); + auto mainFunc = mod->function_at("main", furvm::function_sig{}); #else static const furvm::byte s_bytecode[] = { static_cast(furvm::instruction_t::Array), @@ -74,11 +74,10 @@ int main(int argc, char** argv) { auto mainFunc = mod->emplace_function("main", furvm::function_sig{}, 0); mod->emplace_function(furvm::function_sig{ { u64Type }, u64Type }, "print").dispatch(); #endif - mod->set_native_function("print", [](furvm::executor& executor) { + mod->set_native_function("println", [](furvm::executor& executor) { auto arg = std::move(*executor.load_thing(0)); print_thing(arg); std::cout << '\n'; - executor.push_thing(std::move(arg)); }); furvm::executor_h executor = context->emplace_executor(context); diff --git a/furvm/src/module.cpp b/furvm/src/module.cpp index 3ac33c2..5d3d394 100644 --- a/furvm/src/module.cpp +++ b/furvm/src/module.cpp @@ -25,6 +25,7 @@ std::ostream& mod::serialize(std::ostream& os) const { } auto type = *m_types.at(id); + detail::serialize(os, static_cast(type.type)); switch (type.type) { case mod_type::S8: case mod_type::S16: @@ -67,7 +68,7 @@ std::ostream& mod::serialize(std::ostream& os) const { } // Function signature - detail::serialize(os, func->signature().params.size()); + detail::serialize(os, static_cast(func->signature().params.size())); for (std::uint32_t i = 0; i < func->signature().params.size(); ++i) detail::serialize(os, func->signature().params[i].id()); @@ -116,7 +117,10 @@ mod mod::load(std::istream& is) { case mod_type::U8: case mod_type::U16: case mod_type::U32: - case mod_type::U64: break; + case mod_type::U64: { + mod_type theType = { (enum mod_type::type)type }; + mod.emplace_type(id, theType).dispatch(); + } break; case mod_type::Ptr: { mod_type_id typeId = 0; detail::load(is, typeId); @@ -165,17 +169,17 @@ mod mod::load(std::istream& is) { decltype(std::declval().position()) offset = 0; detail::load(is, offset); if (name.empty()) - mod.emplace_function(std::move(name), id, std::move(signature), offset).dispatch(); - else mod.emplace_function(id, std::move(signature), offset).dispatch(); + else + mod.emplace_function(std::move(name), id, std::move(signature), offset).dispatch(); } break; case function_t::Native: { std::string native; detail::load(is, native); if (name.empty()) - mod.emplace_function(std::move(name), id, std::move(signature), std::move(native)).dispatch(); - else mod.emplace_function(id, std::move(signature), std::move(native)).dispatch(); + else + mod.emplace_function(std::move(name), id, std::move(signature), std::move(native)).dispatch(); } break; case function_t::Import: { std::string modName;