diff --git a/include/lua/lua_script.h b/include/lua/lua_script.h index 5a9089f..270634d 100644 --- a/include/lua/lua_script.h +++ b/include/lua/lua_script.h @@ -34,7 +34,7 @@ class LuaScript { if (auto function = state_.top()) { return (*function)(std::forward(args)...); } - return { CallError::Code::unknown }; + return CallError { CallError::Code::unknown, "Symbol does not match given type" }; } return { CallError::Code::symbolNotFound }; } diff --git a/include/lua/lua_state.h b/include/lua/lua_state.h index a8a9fa2..3bc80f1 100644 --- a/include/lua/lua_state.h +++ b/include/lua/lua_state.h @@ -119,6 +119,11 @@ class LuaState { auto topFunction(); const void* topPointer(); + /** + * Check if top-most stack value is of type nil. + */ + bool isNil(); + /** * Pop top-most value if it is a function. * diff --git a/src/lua_state.cpp b/src/lua_state.cpp index d525a44..6c75962 100644 --- a/src/lua_state.cpp +++ b/src/lua_state.cpp @@ -14,7 +14,7 @@ extern "C" { namespace { constexpr auto MINIMUM_LUA_VERSION = 502; } // namespace -static_assert(LUA_VERSION_NUM > MINIMUM_LUA_VERSION); +static_assert(LUA_VERSION_NUM >= MINIMUM_LUA_VERSION); namespace ppplugin { LuaState::LuaState() @@ -127,6 +127,11 @@ bool LuaState::isFunction() return lua_type(state(), -1) == LUA_TFUNCTION; } +bool LuaState::isNil() +{ + return lua_type(state(), -1) == LUA_TNIL; +} + void LuaState::registerPanicHandler(LuaCFunction handler) { lua_atpanic(state(), handler); @@ -145,7 +150,11 @@ void LuaState::markGlobal(const std::string& variable_name) bool LuaState::pushGlobal(const std::string& variable_name) { - auto type = lua_getglobal(state(), variable_name.c_str()); - return type != LUA_TNONE; + lua_getglobal(state(), variable_name.c_str()); + if (isNil()) { + discardTop(); + return false; + } + return true; } } // namespace ppplugin