refactor(furvm): refactor thing

A pretty sloppy thing class refactor.

Refs: #12
This commit is contained in:
2026-06-16 14:59:29 +02:00
parent 0dfa8c1c02
commit c9f714642c
4 changed files with 21 additions and 70 deletions
+2 -1
View File
@@ -8,6 +8,7 @@
#include "furvm/thing.hpp"
#include <cstdint>
#include <memory>
#include <stdexcept>
namespace furvm {
@@ -86,7 +87,7 @@ void executor::step() {
switch (instr) {
case instruction_t::NoOperation: break;
case instruction_t::PushB2I: {
auto thing = thing::create(m_context, thing_t::Int32);
auto thing = std::make_shared<class thing>(m_context, thing_t::Int32);
thing->int32() = frame.mod->byte(frame.position++);
push_thing(std::move(thing));
} break;
-3
View File
@@ -27,9 +27,6 @@ int main(void) {
auto mainModule = context->emplace_module("main", s_bytecode.begin(), s_bytecode.end());
mainModule->emplace_function("main", 0);
std::ofstream file("./test.furm", std::ios::binary);
furvm::serializer::serialize_module(file, mainModule);
auto executor = context->emplace_executor(context);
executor->push_frame(mainModule, 0);
+16 -23
View File
@@ -17,8 +17,8 @@ std::size_t thing_type_size(thing_t type) {
return 0;
}
thing::thing(rzecz, thing_handle id, thing_t type, const context_p& context)
: m_id(id), m_type(type), m_context(context) {
thing::thing(const context_p& context, thing_t type)
: m_context(context), m_type(type) {
std::size_t size = thing_type_size(type);
std::byte* data = nullptr;
if (!m_context->m_deadThingData.empty()) {
@@ -56,12 +56,7 @@ thing::~thing() {
}
thing::thing(thing&& other) noexcept
: m_id(other.m_id),
m_type(other.m_type),
m_context(std::move(other.m_context)),
m_refCount(other.m_refCount),
m_data(other.m_data) {
other.m_id = {};
: m_context(std::move(other.m_context)), m_type(other.m_type), m_refCount(other.m_refCount), m_data(other.m_data) {
other.m_type = {};
other.m_refCount = {};
other.m_data = nullptr;
@@ -69,13 +64,11 @@ thing::thing(thing&& other) noexcept
thing& thing::operator=(thing&& other) noexcept {
if (this == &other) return *this;
m_id = other.m_id;
m_type = other.m_type;
m_context = std::move(other.m_context);
m_type = other.m_type;
m_refCount = other.m_refCount;
m_data = other.m_data;
other.m_id = {};
other.m_type = {};
other.m_refCount = {};
other.m_data = nullptr;
@@ -89,7 +82,7 @@ static constexpr std::uint16_t thing_type_pair(thing_t lhs, thing_t rhs) {
thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() + rhs->int32();
return res;
}
@@ -100,7 +93,7 @@ thing_p operator+(const thing_p& lhs, const thing_p& rhs) {
thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() - rhs->int32();
return res;
}
@@ -111,7 +104,7 @@ thing_p operator-(const thing_p& lhs, const thing_p& rhs) {
thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() * rhs->int32();
return res;
}
@@ -122,7 +115,7 @@ thing_p operator*(const thing_p& lhs, const thing_p& rhs) {
thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() / rhs->int32();
return res;
}
@@ -133,7 +126,7 @@ thing_p operator/(const thing_p& lhs, const thing_p& rhs) {
thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = lhs->int32() % rhs->int32();
return res;
}
@@ -144,7 +137,7 @@ thing_p operator%(const thing_p& lhs, const thing_p& rhs) {
thing_p operator==(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() == rhs->int32());
return res;
}
@@ -155,7 +148,7 @@ thing_p operator==(const thing_p& lhs, const thing_p& rhs) {
thing_p operator!=(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() != rhs->int32());
return res;
}
@@ -166,7 +159,7 @@ thing_p operator!=(const thing_p& lhs, const thing_p& rhs) {
thing_p operator<(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() < rhs->int32());
return res;
}
@@ -177,7 +170,7 @@ thing_p operator<(const thing_p& lhs, const thing_p& rhs) {
thing_p operator>(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() > rhs->int32());
return res;
}
@@ -188,7 +181,7 @@ thing_p operator>(const thing_p& lhs, const thing_p& rhs) {
thing_p operator<=(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() <= rhs->int32());
return res;
}
@@ -199,7 +192,7 @@ thing_p operator<=(const thing_p& lhs, const thing_p& rhs) {
thing_p operator>=(const thing_p& lhs, const thing_p& rhs) {
switch (thing_type_pair(lhs->m_type, rhs->m_type)) {
case thing_type_pair(thing_t::Int32, thing_t::Int32): {
auto res = thing::create(lhs->m_context, thing_t::Int32);
auto res = std::make_shared<thing>(lhs->m_context, thing_t::Int32);
res->int32() = static_cast<std::int32_t>(lhs->int32() >= rhs->int32());
return res;
}
@@ -216,7 +209,7 @@ thing_p thing::clone(const thing_p& thing) {
}
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
auto th = std::make_shared<class thing>(rzecz{}, id, thing->m_type, thing->m_context);
auto th = std::make_shared<class thing>(thing->m_context, thing->m_type);
switch (thing->m_type) {
// Primitives
default: {