forked from KPGPMC/furlang
feat(furvm): add import function
This commit is contained in:
@@ -12,6 +12,7 @@ namespace furvm {
|
|||||||
enum class function_t : std::uint8_t {
|
enum class function_t : std::uint8_t {
|
||||||
Normal = 0, /**< A normal bytecode function. */
|
Normal = 0, /**< A normal bytecode function. */
|
||||||
Native, /**< A native function implemented through furvm API. */
|
Native, /**< A native function implemented through furvm API. */
|
||||||
|
Import, /**< A function imported from another module. */
|
||||||
};
|
};
|
||||||
|
|
||||||
using native_function = std::function<void(executor&)>;
|
using native_function = std::function<void(executor&)>;
|
||||||
@@ -117,6 +118,26 @@ public:
|
|||||||
if (m_type != function_t::Native) throw std::runtime_error("function type mismatch");
|
if (m_type != function_t::Native) throw std::runtime_error("function type mismatch");
|
||||||
return m_value.native;
|
return m_value.native;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a module of imported function.
|
||||||
|
*
|
||||||
|
* @return A handle to the module.
|
||||||
|
*/
|
||||||
|
module_handle imported_module() const {
|
||||||
|
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||||
|
return m_value.imp.mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns a module of imported function.
|
||||||
|
*
|
||||||
|
* @return A handle to the module.
|
||||||
|
*/
|
||||||
|
function_handle imported_function() const {
|
||||||
|
if (m_type != function_t::Import) throw std::runtime_error("function type mismatch");
|
||||||
|
return m_value.imp.function;
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
function_handle m_id;
|
function_handle m_id;
|
||||||
function_t m_type;
|
function_t m_type;
|
||||||
@@ -125,6 +146,10 @@ private:
|
|||||||
union value {
|
union value {
|
||||||
std::size_t position = 0;
|
std::size_t position = 0;
|
||||||
native_function native;
|
native_function native;
|
||||||
|
struct {
|
||||||
|
module_handle mod;
|
||||||
|
function_handle function;
|
||||||
|
} imp;
|
||||||
|
|
||||||
value() = default;
|
value() = default;
|
||||||
|
|
||||||
@@ -134,6 +159,9 @@ private:
|
|||||||
value(const native_function& native)
|
value(const native_function& native)
|
||||||
: native(native) {}
|
: native(native) {}
|
||||||
|
|
||||||
|
value(module_handle mod, function_handle function)
|
||||||
|
: imp({ mod, function }) {}
|
||||||
|
|
||||||
~value() {}
|
~value() {}
|
||||||
|
|
||||||
value(value&& other) = delete;
|
value(value&& other) = delete;
|
||||||
|
|||||||
@@ -118,6 +118,10 @@ void executor::step() {
|
|||||||
switch (function->type()) {
|
switch (function->type()) {
|
||||||
case function_t::Normal: push_frame(function); break;
|
case function_t::Normal: push_frame(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(impMod->function_at(function->imported_function()));
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case instruction_t::Return: {
|
case instruction_t::Return: {
|
||||||
|
|||||||
Reference in New Issue
Block a user