From 47e0ff88edd4660513f1d4f3d731008461532a13 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 2 Oct 2024 15:07:24 -0500 Subject: Get and save user keybinds. --- src/luacxx.cpp | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src/luacxx.cpp') 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> LuaContext::getGlobalStringArray(const char* std::vector output{}; output.reserve(static_cast(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(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); } -- cgit v1.2.3