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