Skip to content

Commit

Permalink
Rewrite thunk to not use lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
Foereaper committed Aug 5, 2024
1 parent 0e736f1 commit a2bee45
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,13 @@ class ElunaTemplate
{
ElunaRegister<T>* l = static_cast<ElunaRegister<T>*>(lua_touserdata(L, lua_upvalueindex(1)));
Eluna* E = static_cast<Eluna*>(lua_touserdata(L, lua_upvalueindex(2)));
T* obj;

// determine if the method table functions are global or non-global
bool isGlobal = false;
std::visit([&](auto&& func)
{
using FuncType = std::decay_t<decltype(func)>;
if constexpr (std::is_same_v<FuncType, int(*)(Eluna*)>)
isGlobal = true;
}, l->mfunc);
constexpr bool isGlobal = std::is_same_v<T, void>;

// we only check self if the method is not a global
if (!isGlobal)
T* obj;
if constexpr (!isGlobal)
{
obj = E->CHECKOBJ<T>(1);
if (!obj)
Expand All @@ -439,15 +433,18 @@ class ElunaTemplate
int top = lua_gettop(L);

int expected = 0;
std::visit([&](auto&& func)
if constexpr (isGlobal)
{
using FuncType = std::decay_t<decltype(func)>;
if constexpr (std::is_same_v<FuncType, int(*)(Eluna*)>) // global method
expected = func(E);
else if constexpr (std::is_same_v<FuncType, int(*)(Eluna*, T*)>) // non-global method
expected = func(E, obj);

}, l->mfunc);
auto func = std::get_if<int(*)(Eluna*)>(&l->mfunc);
if (func)
expected = (*func)(E);
}
else
{
auto func = std::get_if<int(*)(Eluna*, T*)>(&l->mfunc);
if (func)
expected = (*func)(E, obj);
}

int args = lua_gettop(L) - top;
if (args < 0 || args > expected)
Expand Down

0 comments on commit a2bee45

Please sign in to comment.