@@ -104,9 +104,9 @@ private:
|
|||||||
std::vector<thing_p> m_things;
|
std::vector<thing_p> m_things;
|
||||||
std::vector<executor_p> m_executors;
|
std::vector<executor_p> m_executors;
|
||||||
|
|
||||||
std::queue<thing_handle> m_deadThings;
|
std::queue<thing_id> m_deadThings;
|
||||||
std::vector<void*> m_deadThingData;
|
std::vector<void*> m_deadThingData;
|
||||||
furlang::arena m_thingArena;
|
furlang::arena m_thingArena;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace furvm
|
} // namespace furvm
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public:
|
|||||||
* @param mod Module.
|
* @param mod Module.
|
||||||
* @param function Function handle.
|
* @param function Function handle.
|
||||||
*/
|
*/
|
||||||
void push_frame(const mod_p& mod, function_handle function);
|
void push_frame(const mod_p& mod, function_id function);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pops the top frame.
|
* @brief Pops the top frame.
|
||||||
|
|||||||
@@ -35,13 +35,13 @@ public:
|
|||||||
: m_name(name), m_type(function_t::Native), m_value(native) {}
|
: m_name(name), m_type(function_t::Native), m_value(native) {}
|
||||||
|
|
||||||
template <typename Name, typename ModuleName>
|
template <typename Name, typename ModuleName>
|
||||||
function(Name&& name, ModuleName&& moduleName, function_handle function)
|
function(Name&& name, ModuleName&& moduleName, function_id function)
|
||||||
: m_name(std::forward<Name>(name)),
|
: m_name(std::forward<Name>(name)),
|
||||||
m_type(function_t::Import),
|
m_type(function_t::Import),
|
||||||
m_value(std::forward<ModuleName>(moduleName), function) {}
|
m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||||
|
|
||||||
template <typename ModuleName>
|
template <typename ModuleName>
|
||||||
function(const char* name, ModuleName&& moduleName, function_handle function)
|
function(const char* name, ModuleName&& moduleName, function_id function)
|
||||||
: m_name(name), m_type(function_t::Import), m_value(std::forward<ModuleName>(moduleName), function) {}
|
: m_name(name), m_type(function_t::Import), m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||||
|
|
||||||
~function();
|
~function();
|
||||||
@@ -108,7 +108,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return A handle to the module.
|
* @return A handle to the module.
|
||||||
*/
|
*/
|
||||||
function_handle imported_function() const {
|
function_id imported_function() const {
|
||||||
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||||
return m_value.imp.function;
|
return m_value.imp.function;
|
||||||
}
|
}
|
||||||
@@ -120,8 +120,8 @@ private:
|
|||||||
std::size_t position = 0;
|
std::size_t position = 0;
|
||||||
native_function native;
|
native_function native;
|
||||||
struct {
|
struct {
|
||||||
std::string moduleName;
|
std::string moduleName;
|
||||||
function_handle function;
|
function_id function;
|
||||||
} imp;
|
} imp;
|
||||||
|
|
||||||
value() = default;
|
value() = default;
|
||||||
@@ -133,7 +133,7 @@ private:
|
|||||||
: native(native) {}
|
: native(native) {}
|
||||||
|
|
||||||
template <typename Name>
|
template <typename Name>
|
||||||
value(Name&& moduleName, function_handle function)
|
value(Name&& moduleName, function_id function)
|
||||||
: imp({ std::forward<Name>(moduleName), function }) {}
|
: imp({ std::forward<Name>(moduleName), function }) {}
|
||||||
|
|
||||||
~value() {}
|
~value() {}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ using function_p = std::shared_ptr<function>;
|
|||||||
/**
|
/**
|
||||||
* @brief Furvm function's index.
|
* @brief Furvm function's index.
|
||||||
*/
|
*/
|
||||||
using function_handle = std::uint16_t;
|
using function_id = std::uint16_t;
|
||||||
|
|
||||||
// module.hpp
|
// module.hpp
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ using thing_p = std::shared_ptr<thing>;
|
|||||||
/**
|
/**
|
||||||
* @brief Furvm thing's index.
|
* @brief Furvm thing's index.
|
||||||
*/
|
*/
|
||||||
using thing_handle = std::uint32_t;
|
using thing_id = std::uint32_t;
|
||||||
|
|
||||||
// executor.hpp
|
// executor.hpp
|
||||||
|
|
||||||
@@ -159,7 +159,7 @@ using executor_p = std::shared_ptr<executor>;
|
|||||||
/**
|
/**
|
||||||
* @brief Furvm executor's index.
|
* @brief Furvm executor's index.
|
||||||
*/
|
*/
|
||||||
using executor_handle = std::uint32_t;
|
using executor_id = std::uint32_t;
|
||||||
|
|
||||||
// context.hpp
|
// context.hpp
|
||||||
|
|
||||||
|
|||||||
@@ -103,25 +103,13 @@ public:
|
|||||||
* @param id Id of the function.
|
* @param id Id of the function.
|
||||||
* @return The function.
|
* @return The function.
|
||||||
*/
|
*/
|
||||||
constexpr const function_p& function_at(function_handle id) const { return m_functions.at(id); }
|
constexpr const function_p& function_at(function_id id) const { return m_functions.at(id); }
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Returns an id of a function.
|
|
||||||
*
|
|
||||||
* @param function Function to get the id of.
|
|
||||||
* @return The function's id.
|
|
||||||
*/
|
|
||||||
function_handle function_id(const function_p& function) const {
|
|
||||||
if (auto it = std::find(m_functions.begin(), m_functions.end(), function); it != m_functions.end())
|
|
||||||
return it - m_functions.begin();
|
|
||||||
throw std::runtime_error("function not in the module");
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
function_handle emplace_function(Args&&... args) {
|
function_id emplace_function(Args&&... args) {
|
||||||
function_p function = std::make_shared<class function>(std::forward<Args>(args)...);
|
function_p function = std::make_shared<class function>(std::forward<Args>(args)...);
|
||||||
m_functionMap.emplace(function->name(), function);
|
m_functionMap.emplace(function->name(), function);
|
||||||
function_handle id = m_functions.size();
|
function_id id = m_functions.size();
|
||||||
m_functions.emplace_back(std::move(function));
|
m_functions.emplace_back(std::move(function));
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class thing final {
|
|||||||
public:
|
public:
|
||||||
using nref_t = std::size_t; /**< Type of reference count. */
|
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. */
|
static constexpr thing_id GENERATION_SIZE = 12; /**< Bit size of generation part in thing_handle. */
|
||||||
public:
|
public:
|
||||||
thing(const context_p& context, thing_t type);
|
thing(const context_p& context, thing_t type);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
namespace furvm {
|
namespace furvm {
|
||||||
|
|
||||||
void executor::push_frame(const mod_p& mod, function_handle function) {
|
void executor::push_frame(const mod_p& mod, function_id function) {
|
||||||
const auto& func = mod->function_at(function);
|
const auto& func = mod->function_at(function);
|
||||||
if (func->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
|
if (func->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
|
||||||
m_frames.emplace((struct executor::frame){ mod, func->position(), m_stack.size() });
|
m_frames.emplace((struct executor::frame){ mod, func->position(), m_stack.size() });
|
||||||
@@ -168,9 +168,9 @@ void executor::step() {
|
|||||||
store_thing(variable, std::move(pop_thing()));
|
store_thing(variable, std::move(pop_thing()));
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Call: {
|
case instruction_t::Call: {
|
||||||
function_handle funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
|
function_id funcId = static_cast<std::uint16_t>(frame.mod->byte(frame.position)) |
|
||||||
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
|
(static_cast<std::uint16_t>(frame.mod->byte(frame.position + 1)) << 8);
|
||||||
frame.position += 2;
|
frame.position += 2;
|
||||||
|
|
||||||
const function_p& function = frame.mod->function_at(funcId);
|
const function_p& function = frame.mod->function_at(funcId);
|
||||||
switch (function->type()) {
|
switch (function->type()) {
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ bool serializer::serialize_module(std::ostream& os, const mod_p& mod) {
|
|||||||
write_u32(os, VERSION);
|
write_u32(os, VERSION);
|
||||||
|
|
||||||
write_u32(os, mod->m_functions.size());
|
write_u32(os, mod->m_functions.size());
|
||||||
for (function_handle id = 0; id < static_cast<function_handle>(mod->m_functions.size()); ++id) {
|
for (function_id id = 0; id < static_cast<function_id>(mod->m_functions.size()); ++id) {
|
||||||
const auto& func = mod->m_functions[id];
|
const auto& func = mod->m_functions[id];
|
||||||
write_u16(os, id);
|
write_u16(os, id);
|
||||||
write_u8(os, static_cast<std::uint8_t>(func->type()));
|
write_u8(os, static_cast<std::uint8_t>(func->type()));
|
||||||
|
|||||||
+2
-2
@@ -201,13 +201,13 @@ thing_p operator>=(const thing_p& lhs, const thing_p& rhs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
thing_p thing::clone(const thing_p& thing) {
|
thing_p thing::clone(const thing_p& thing) {
|
||||||
thing_handle id = thing->m_context->m_things.size();
|
thing_id id = thing->m_context->m_things.size();
|
||||||
if (!thing->m_context->m_deadThings.empty()) {
|
if (!thing->m_context->m_deadThings.empty()) {
|
||||||
id = thing->m_context->m_deadThings.front();
|
id = thing->m_context->m_deadThings.front();
|
||||||
thing->m_context->m_deadThings.pop();
|
thing->m_context->m_deadThings.pop();
|
||||||
id += 1 << GENERATION_SIZE;
|
id += 1 << GENERATION_SIZE;
|
||||||
}
|
}
|
||||||
thing_handle idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
|
thing_id idx = id & ((1ULL << ((sizeof(id) * 8) - GENERATION_SIZE)) - 1);
|
||||||
|
|
||||||
auto th = std::make_shared<class thing>(thing->m_context, thing->m_type);
|
auto th = std::make_shared<class thing>(thing->m_context, thing->m_type);
|
||||||
switch (thing->m_type) {
|
switch (thing->m_type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user