@@ -86,7 +86,7 @@ public:
|
||||
* @param mod Module.
|
||||
* @param function Function handle.
|
||||
*/
|
||||
void push_frame(const mod_h& mod, const function_h& function);
|
||||
void push_frame(const mod_h& mod, function function);
|
||||
|
||||
/**
|
||||
* @brief Pops the top frame.
|
||||
|
||||
@@ -18,31 +18,24 @@ enum class function_t : std::uint8_t {
|
||||
|
||||
using native_function = std::function<void(executor&)>;
|
||||
|
||||
struct import_function {
|
||||
mod_id mod;
|
||||
function_id function;
|
||||
};
|
||||
|
||||
class function {
|
||||
public:
|
||||
template <typename Name>
|
||||
function(Name&& name, bytecode_pos position)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Normal), m_value(position) {}
|
||||
|
||||
function(const char* name, bytecode_pos position)
|
||||
: m_name(name), m_type(function_t::Normal), m_value(position) {}
|
||||
|
||||
template <typename Name>
|
||||
function(Name&& name, const native_function& native)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Native), m_value(native) {}
|
||||
|
||||
function(const char* name, const native_function& native)
|
||||
: m_name(name), m_type(function_t::Native), m_value(native) {}
|
||||
|
||||
template <typename Name, typename ModuleName>
|
||||
function(Name&& name, ModuleName&& moduleName, function_id function)
|
||||
: m_name(std::forward<Name>(name)),
|
||||
m_type(function_t::Import),
|
||||
m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||
|
||||
template <typename ModuleName>
|
||||
function(const char* name, ModuleName&& moduleName, function_id function)
|
||||
: m_name(name), m_type(function_t::Import), m_value(std::forward<ModuleName>(moduleName), function) {}
|
||||
template <typename Name>
|
||||
function(Name&& name, const import_function& imp)
|
||||
: m_name(std::forward<Name>(name)), m_type(function_t::Import), m_value(imp) {}
|
||||
|
||||
~function();
|
||||
|
||||
@@ -105,19 +98,9 @@ public:
|
||||
*
|
||||
* @return The name.
|
||||
*/
|
||||
const std::string& imported_module() const {
|
||||
const import_function& imp() const {
|
||||
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||
return m_value.imp.moduleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a module of imported function.
|
||||
*
|
||||
* @return A handle to the module.
|
||||
*/
|
||||
function_id imported_function() const {
|
||||
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||
return m_value.imp.function;
|
||||
return m_value.imp;
|
||||
}
|
||||
private:
|
||||
std::string m_name;
|
||||
@@ -126,10 +109,7 @@ private:
|
||||
union value {
|
||||
std::size_t position = 0;
|
||||
native_function native;
|
||||
struct {
|
||||
std::string moduleName;
|
||||
function_id function;
|
||||
} imp;
|
||||
import_function imp;
|
||||
|
||||
value() = default;
|
||||
|
||||
@@ -139,9 +119,8 @@ private:
|
||||
value(const native_function& native)
|
||||
: native(native) {}
|
||||
|
||||
template <typename Name>
|
||||
value(Name&& moduleName, function_id function)
|
||||
: imp({ std::forward<Name>(moduleName), function }) {}
|
||||
value(const import_function& imp)
|
||||
: imp(imp) {}
|
||||
|
||||
~value() {}
|
||||
|
||||
|
||||
+13
-9
@@ -12,9 +12,17 @@
|
||||
|
||||
namespace furvm {
|
||||
|
||||
void executor::push_frame(const mod_h& mod, const function_h& function) {
|
||||
if (function->type() != function_t::Normal) throw std::runtime_error("unexpected function type");
|
||||
m_frames.emplace((struct executor::frame){ mod, function->position(), m_stack.size() });
|
||||
void executor::push_frame(const mod_h& mod, function function) {
|
||||
while (function.type() == function_t::Import) {
|
||||
function = *m_context->module_at(function.imp().mod)->function_at(function.imp().function);
|
||||
}
|
||||
switch (function.type()) {
|
||||
case function_t::Normal: {
|
||||
m_frames.emplace((struct executor::frame){ mod, function.position(), m_stack.size() });
|
||||
} break;
|
||||
case function_t::Native:
|
||||
default: throw std::runtime_error("unexpected function type");
|
||||
}
|
||||
}
|
||||
|
||||
struct executor::frame executor::pop_frame() {
|
||||
@@ -155,13 +163,9 @@ void executor::step() {
|
||||
|
||||
const function_h& function = frame.mod->function_at(funcId);
|
||||
switch (function->type()) {
|
||||
case function_t::Normal: push_frame(frame.mod, function); break;
|
||||
case function_t::Normal:
|
||||
case function_t::Import: push_frame(frame.mod, *function); break;
|
||||
case function_t::Native: function->native()(*this); break;
|
||||
case function_t::Import: {
|
||||
// const mod_p& impMod = m_context->m_modules.at(function->imported_module());
|
||||
// push_frame(frame.mod, function->imported_function());
|
||||
throw std::runtime_error("unimplemented");
|
||||
} break;
|
||||
}
|
||||
} break;
|
||||
case instruction_t::Jump: {
|
||||
|
||||
@@ -10,7 +10,7 @@ function::~function() {
|
||||
case function_t::Native:
|
||||
default: break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName.~basic_string();
|
||||
m_value.imp.~import_function();
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -25,8 +25,7 @@ function::function(function&& other) noexcept
|
||||
new (&m_value.native) native_function(std::move(other.m_value.native));
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = std::move(other.m_value.imp.moduleName);
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = std::move(other.m_value.imp);
|
||||
} break;
|
||||
}
|
||||
other.m_value.position = 0;
|
||||
@@ -45,8 +44,7 @@ function& function::operator=(function&& other) noexcept {
|
||||
new (&m_value.native) native_function(std::move(other.m_value.native));
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = std::move(other.m_value.imp.moduleName);
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = std::move(other.m_value.imp);
|
||||
} break;
|
||||
}
|
||||
other.m_value.position = 0;
|
||||
@@ -64,8 +62,7 @@ function::function(const function& other)
|
||||
new (&m_value.native) native_function(other.m_value.native);
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = other.m_value.imp.moduleName;
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = other.m_value.imp;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
@@ -83,8 +80,7 @@ function& function::operator=(const function& other) {
|
||||
new (&m_value.native) native_function(other.m_value.native);
|
||||
} break;
|
||||
case function_t::Import: {
|
||||
m_value.imp.moduleName = other.m_value.imp.moduleName;
|
||||
m_value.imp.function = other.m_value.imp.function;
|
||||
m_value.imp = other.m_value.imp;
|
||||
} break;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ int main(void) {
|
||||
furvm::function_h mainFunc = mainModule->emplace_function("main", 0);
|
||||
|
||||
furvm::executor_h executor = context->emplace_executor(context);
|
||||
executor->push_frame(mainModule, mainFunc);
|
||||
executor->push_frame(mainModule, *mainFunc);
|
||||
|
||||
static constexpr std::size_t FPC = 3; // Frames per collection
|
||||
|
||||
|
||||
Reference in New Issue
Block a user