summaryrefslogtreecommitdiff
path: root/src/luacxx.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/luacxx.cpp')
-rw-r--r--src/luacxx.cpp33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/luacxx.cpp b/src/luacxx.cpp
index c4bc063..0a6db82 100644
--- a/src/luacxx.cpp
+++ b/src/luacxx.cpp
@@ -32,6 +32,11 @@ LuaContext::LuaContext(LuaContext&& rhs) : m_state{rhs.m_state}
LuaContext& LuaContext::operator=(LuaContext&& rhs)
{
+ if (m_state != nullptr) {
+ lua_close(m_state);
+ m_state = nullptr;
+ }
+
m_state = rhs.m_state;
rhs.m_state = nullptr;
return *this;
@@ -125,20 +130,34 @@ LuaResult<std::vector<std::string>> LuaContext::getGlobalStringArray(const char*
std::vector<std::string> output{};
output.reserve(static_cast<size_t>(length));
+
+ lua_pushnil(m_state);
+
+ auto result = FudStatus::Success;
for (int64_t index = 1; index <= length; ++index) {
- const char* result{nullptr};
- lua_pushinteger(m_state, index);
- static_cast<void>(lua_gettable(m_state, -2));
- result = lua_tolstring(m_state, -1, nullptr);
+ const char* luaResult{nullptr};
+ auto luaNext = lua_next(m_state, -2);
+ if (luaNext == 0) {
+ lua_pop(m_state, 2);
+ result = FudStatus::Failure;
+ break;
+ }
+ luaResult = lua_tolstring(m_state, -1, nullptr);
- if (result == nullptr) {
+ if (luaResult == nullptr) {
lua_pop(m_state, 1);
- return RetType::error(FudStatus::Failure);
+ result = FudStatus::Failure;
+ break;
}
- output.emplace_back(result);
+ output.emplace_back(luaResult);
+ lua_pop(m_state, 1);
}
lua_pop(m_state, 1);
+ if (result != FudStatus::Success)
+ {
+ return RetType::error(result);
+ }
return RetType::okay(output);
}