@@ -42,6 +42,11 @@ void executor::push_thing(const thing_p& thing) {
|
||||
m_stack.push(thing);
|
||||
}
|
||||
|
||||
void executor::push_thing(thing_p&& thing) {
|
||||
thing->add_reference();
|
||||
m_stack.push(std::move(thing));
|
||||
}
|
||||
|
||||
thing_p executor::pop_thing() {
|
||||
if (m_frames.top().stackBase >= m_stack.size()) throw stack_underflow();
|
||||
thing_p top = std::move(m_stack.top());
|
||||
@@ -66,7 +71,7 @@ void executor::step() {
|
||||
case instruction_t::PushB2I: {
|
||||
auto thing = thing::create(m_context, thing_t::Int32);
|
||||
thing->int32() = frame.mod->byte(frame.position++);
|
||||
m_stack.push(std::move(thing));
|
||||
push_thing(std::move(thing));
|
||||
} break;
|
||||
case instruction_t::Drop: {
|
||||
pop_thing();
|
||||
@@ -75,7 +80,7 @@ void executor::step() {
|
||||
push_thing(thing());
|
||||
} break;
|
||||
case instruction_t::Clone: {
|
||||
push_thing(thing::clone(thing()));
|
||||
push_thing(std::move(thing::clone(thing())));
|
||||
} break;
|
||||
case instruction_t::Return: {
|
||||
pop_frame();
|
||||
@@ -84,7 +89,7 @@ void executor::step() {
|
||||
case instruction_t::ReturnValue: {
|
||||
auto value = pop_thing();
|
||||
pop_frame();
|
||||
m_stack.push(std::move(value));
|
||||
push_thing(std::move(value));
|
||||
} break;
|
||||
case instruction_t::PushConstant: throw std::runtime_error("unimplemented");
|
||||
default: throw std::runtime_error("unknown instruction");
|
||||
|
||||
+3
-1
@@ -8,9 +8,11 @@
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
static constexpr std::array<furvm::byte, 4> s_bytecode = {
|
||||
static constexpr std::array<furvm::byte, 6> s_bytecode = {
|
||||
furvm::byte(furvm::instruction_t::PushB2I),
|
||||
67,
|
||||
furvm::byte(furvm::instruction_t::Clone),
|
||||
furvm::byte(furvm::instruction_t::Drop),
|
||||
furvm::byte(furvm::instruction_t::Drop),
|
||||
furvm::byte(furvm::instruction_t::Return),
|
||||
};
|
||||
|
||||
+41
-7
@@ -55,17 +55,51 @@ thing::~thing() {
|
||||
m_context->m_deadThingData.push_back(m_data);
|
||||
}
|
||||
|
||||
thing_p thing::create(const context_p& context, thing_t type) {
|
||||
thing_handle id = context->m_things.size();
|
||||
if (!context->m_deadThings.empty()) {
|
||||
id = context->m_deadThings.front();
|
||||
context->m_deadThings.pop();
|
||||
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 = {};
|
||||
other.m_type = {};
|
||||
other.m_refCount = {};
|
||||
other.m_data = nullptr;
|
||||
}
|
||||
|
||||
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_refCount = other.m_refCount;
|
||||
m_data = other.m_data;
|
||||
|
||||
other.m_id = {};
|
||||
other.m_type = {};
|
||||
other.m_refCount = {};
|
||||
other.m_data = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
thing_p thing::clone(const thing_p& thing) {
|
||||
thing_handle id = thing->m_context->m_things.size();
|
||||
if (!thing->m_context->m_deadThings.empty()) {
|
||||
id = thing->m_context->m_deadThings.front();
|
||||
thing->m_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, type, context);
|
||||
context->m_things.emplace(context->m_things.begin() + idx, th);
|
||||
auto th = std::make_shared<class thing>(rzecz{}, id, thing->m_type, thing->m_context);
|
||||
switch (thing->m_type) {
|
||||
// Primitives
|
||||
default: {
|
||||
memcpy(th->m_data, thing->m_data, thing_type_size(thing->m_type));
|
||||
}
|
||||
}
|
||||
|
||||
thing->m_context->m_things.emplace(thing->m_context->m_things.begin() + idx, th);
|
||||
return std::move(th);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user