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 -42
View File
@@ -53,28 +53,12 @@ public:
class thing final {
friend class executor;
private:
/**
* @brief A private token for the private constructor.
*
* Also `rzecz` in Polish translates to `thing` I think.
*/
struct rzecz {
explicit rzecz() = default;
};
public:
using nref_t = std::size_t; /**< Type of reference count. */
static constexpr thing_handle GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
public:
/**
* @brief Private constructor.
*
* @param id
* @param type
* @param context
*/
thing(rzecz, thing_handle id, thing_t type, const context_p& context);
thing(const context_p& context, thing_t type);
~thing();
@@ -189,29 +173,6 @@ public:
*/
friend thing_p operator>=(const thing_p& lhs, const thing_p& rhs);
public:
/**
* @brief Returns a new thing.
*
* @param context Furvm context.
* @param args Arguments to forward to the thing constructor.
* @return Shared pointer to the new thing.
*/
template <typename... Args,
typename = std::enable_if_t<std::is_constructible_v<thing, rzecz, thing_handle, Args..., const context_p&>>>
static thing_p create(const context_p& context, Args&&... args) {
thing_handle id = context->m_things.size();
if (!context->m_deadThings.empty()) {
id = context->m_deadThings.front();
context->m_deadThings.pop();
id += 1 << GENERATION_SIZE;
}
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
auto th = std::make_shared<thing>(rzecz{}, id, std::forward<Args>(args)..., context);
context->m_things.emplace(context->m_things.begin() + idx, th);
return std::move(th);
}
/**
* @brief Returns a clone of the thing.
*
@@ -251,9 +212,8 @@ public:
*/
constexpr nref_t reference_count() const { return m_refCount; }
private:
thing_handle m_id;
thing_t m_type;
context_p m_context;
thing_t m_type;
nref_t m_refCount = 0;
void* m_data = nullptr;
+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: {