refactor: remove name from function

Closes: #47
This commit is contained in:
2026-07-06 22:06:53 +02:00
parent 7c74850aff
commit 2519560451
6 changed files with 45 additions and 82 deletions
+5 -21
View File
@@ -36,29 +36,21 @@ public:
/**
* @brief Constructs a normal function.
*
* @param name Name of the function.
* @param paramCount Paremeter count.
* @param position Offset in bytecode of the function.
*/
template <typename Name>
function(Name&& name, std::uint32_t paramCount, bytecode_pos position)
: m_name(std::forward<Name>(name)), m_type(function_t::Normal), m_paramCount(paramCount), m_value(position) {}
function(std::uint32_t paramCount, bytecode_pos position)
: m_type(function_t::Normal), m_paramCount(paramCount), m_value(position) {}
/**
* @brief Constructs a native function.
*
* @param name Name of the function.
* @param paramCount Paremeter count.
* @param native Native function tag.
*/
template <typename Name,
typename Native,
typename = std::enable_if_t<std::is_constructible_v<native_function, Native>>>
function(Name&& name, std::uint32_t paramCount, Native&& native)
: m_name(std::forward<Name>(name)),
m_type(function_t::Native),
m_paramCount(paramCount),
m_value(std::forward<Native>(native)) {}
template <typename Native, typename = std::enable_if_t<std::is_constructible_v<native_function, Native>>>
function(std::uint32_t paramCount, Native&& native)
: m_type(function_t::Native), m_paramCount(paramCount), m_value(std::forward<Native>(native)) {}
/**
* @brief Constructs an import function.
@@ -103,13 +95,6 @@ public:
*/
function& operator=(const function&);
public:
/**
* @brief Returns a name of this function.
*
* @return The name.
*/
constexpr const std::string& name() const { return m_name; }
/**
* @brief Returns a type of this function.
*
@@ -154,7 +139,6 @@ public:
return m_value.imp;
}
private:
std::string m_name;
function_t m_type;
std::uint32_t m_paramCount;
+21 -45
View File
@@ -86,51 +86,38 @@ public:
function_h emplace_function(Args&&... args) {
function_h function;
if constexpr (std::is_constructible_v<class function, Args...>) {
function = m_functions.emplace_back(std::forward<Args>(args)...);
function = std::move(m_functions.emplace_back(std::forward<Args>(args)...));
} else {
function = m_functions.emplace(std::forward<Args>(args)...);
function = std::move(m_functions.emplace(std::forward<Args>(args)...));
}
m_functionMap[function->name()] = function.id();
m_publicFunctions[function->name()] = function.id();
m_functionMap[function.id()] = "";
return std::move(function);
}
/**
* @brief Emplaces a function in the module's function container.
*
* Emplaces the function in module's function container and name to function map .
* Emplaces the function in module's function container and name to function map.
*
* @param args Arguments forwarded into the container's emplace_back function.
* @return A handle to the emplaced function.
*/
template <typename... Args>
function_h emplace_function_private(Args&&... args) {
function_h function = m_functions.emplace_back(std::forward<Args>(args)...);
m_functionMap[function->name()] = function.id();
template <typename NameFwd,
typename... Args,
typename = std::enable_if_t<std::is_constructible_v<std::string, NameFwd>>>
function_h emplace_function_named(NameFwd&& name, Args&&... args) {
function_h function;
if constexpr (std::is_constructible_v<class function, Args...>) {
function = std::move(m_functions.emplace_back(std::forward<Args>(args)...));
} else {
function = std::move(m_functions.emplace(std::forward<Args>(args)...));
}
std::string nameInst = std::forward<NameFwd>(name);
m_functionNames[nameInst] = function.id();
m_functionMap[function.id()] = nameInst;
return std::move(function);
}
/**
* @brief Inserts a function in the module's function container.
*
* @param function Function to insert.
*/
template <typename Function>
auto push_back(Function&& function) {
return emplace_function(std::forward<Function>(function));
}
/**
* @brief Inserts a function in the module's function container.
*
* @param function Function to insert.
*/
template <typename Function>
auto push_back_private(Function&& function) {
return emplace_function_private(std::forward<Function>(function));
}
/**
* @brief Returns a function from the module.
*
@@ -155,7 +142,7 @@ public:
*/
template <typename NameFwd, typename = std::enable_if_t<std::is_constructible_v<std::string, NameFwd>>>
auto function_at(NameFwd&& name) {
return function_at(m_publicFunctions.at(std::forward<NameFwd>(name)));
return function_at(m_functionNames.at(std::forward<NameFwd>(name)));
}
/**
@@ -166,18 +153,7 @@ public:
*/
template <typename NameFwd, typename = std::enable_if_t<std::is_constructible_v<std::string, NameFwd>>>
auto function_at(NameFwd&& name) const {
return function_at(m_publicFunctions.at(std::forward<NameFwd>(name)));
}
/**
* @brief Returns an id of a function from the module.
*
* @param name Name of the function.
* @return An id of the function.
*/
template <typename NameFwd>
auto get_function_id(NameFwd&& name) {
return m_functionMap.at(std::forward<NameFwd>(name));
return function_at(m_functionNames.at(std::forward<NameFwd>(name)));
}
/**
@@ -262,8 +238,8 @@ public:
private:
bytecode_t m_bytecode;
std::unordered_map<std::string, function_id> m_functionMap;
std::unordered_map<std::string, function_id> m_publicFunctions;
std::unordered_map<std::string, function_id> m_functionNames;
std::unordered_map<function_id, std::string> m_functionMap;
handle_container<function_h> m_functions;
handle_container<type_h> m_types;