Skip to content

Commit

Permalink
lua: Make LuaState Lua 5.2 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
taminob committed Mar 20, 2024
1 parent 6b432be commit b9d2d78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/lua/lua_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LuaScript {
if (auto function = state_.top<ReturnValue(Args...)>()) {
return (*function)(std::forward<Args>(args)...);
}
return { CallError::Code::unknown };
return CallError { CallError::Code::unknown, "Symbol does not match given type" };
}
return { CallError::Code::symbolNotFound };
}
Expand Down
5 changes: 5 additions & 0 deletions include/lua/lua_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
15 changes: 12 additions & 3 deletions src/lua_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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

0 comments on commit b9d2d78

Please sign in to comment.