fix: use thing::true_type and fix module serialization

This commit is contained in:
2026-07-17 20:41:27 +02:00
parent 46e92deed5
commit dfaa3081cb
3 changed files with 21 additions and 18 deletions
+5 -5
View File
@@ -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<thing_type::s8>();
case thing_type::S16: return get<thing_type::s16>();
case thing_type::S32: return get<thing_type::s32>();
@@ -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<union array>();
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 <typename Func>
decltype(auto) visit_primitive(Func&& func) const {
switch (m_type.type) {
switch (true_type().type) {
case thing_type::S8: return std::forward<Func>(func)(get<thing_type::s8>());
case thing_type::S16: return std::forward<Func>(func)(get<thing_type::s16>());
case thing_type::S32: return std::forward<Func>(func)(get<thing_type::s32>());
@@ -592,7 +592,7 @@ private:
template <typename Op>
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,
+5 -6
View File
@@ -13,7 +13,7 @@
static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
using namespace furvm;
switch (thing.type().type) {
switch (thing.true_type().type) {
case thing_type::S8: std::cout << thing.cast_to<thing_type::s16>(); break;
case thing_type::S16: std::cout << thing.get<thing_type::s16>(); break;
case thing_type::S32: std::cout << thing.get<thing_type::s32>(); break;
@@ -22,7 +22,7 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
case thing_type::U16: std::cout << thing.get<thing_type::u16>(); break;
case thing_type::U32: std::cout << thing.get<thing_type::u32>(); break;
case thing_type::U64: std::cout << thing.get<thing_type::u64>(); 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,13 +37,12 @@ static void print_thing(const furvm::thing<furvm::thing_allocator>& thing) {
int main(int argc, char** argv) {
auto context = std::make_shared<furvm::context>();
#if 0 // NOLINT
#if 1 // NOLINT
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <mod.fmod>\n";
return 1;
}
std::ifstream file(argv[1]);
if (!file.is_open()) {
std::cerr << "Failed to open " << argv[1] << '\n';
@@ -51,6 +50,7 @@ int main(int argc, char** argv) {
}
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::byte>(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);
+10 -6
View File
@@ -25,6 +25,7 @@ std::ostream& mod::serialize(std::ostream& os) const {
}
auto type = *m_types.at(id);
detail::serialize(os, static_cast<std::uint8_t>(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<std::uint32_t>(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<function>().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;