diff --git a/luprex/cpp/core/luastack.hpp b/luprex/cpp/core/luastack.hpp index f6c2fd70..1cb3ab44 100644 --- a/luprex/cpp/core/luastack.hpp +++ b/luprex/cpp/core/luastack.hpp @@ -147,6 +147,7 @@ #include "wrap-set.hpp" #include #include +#include #include "lua.h" #include "lauxlib.h" @@ -296,34 +297,45 @@ protected: int nvar; int nextra; constexpr Counts(int nr, int na, int nv, int ne) : nret(nr), narg(na), nvar(nv), nextra(ne) {} + constexpr Counts(Counts a, Counts b) + : nret(a.nret+b.nret), narg(a.narg+b.narg), nvar(a.nvar+b.nvar), nextra(a.nextra+b.nextra) {} }; - template - constexpr static Counts count_slots(LuaRet &v, SS & ... stackslots) + template + constexpr static Counts count_slots(LuaRet &v, SS && ... stackslots) { - return count_slots(stackslots...); + return Counts(Counts(1, 0, 0, 0), count_slots(stackslots...)); } - template - constexpr static Counts count_slots(LuaArg &v, SS & ... stackslots) + template + constexpr static Counts count_slots(LuaArg &v, SS && ... stackslots) { - return count_slots(stackslots...); + return Counts(Counts(0, 1, 0, 0), count_slots(stackslots...)); } - template - constexpr static Counts count_slots(LuaVar &v, SS & ... stackslots) + template + constexpr static Counts count_slots(LuaVar &v, SS && ... stackslots) { - return count_slots(stackslots...); + return Counts(Counts(0, 0, 1, 0), count_slots(stackslots...)); } - template - constexpr static Counts count_slots(LuaExtraArgs &v, SS & ... stackslots) + template + constexpr static Counts count_slots(LuaExtraArgs &v, SS && ... stackslots) { - return count_slots(stackslots...); + return Counts(Counts(0, 0, 0, 1), count_slots(stackslots...)); } - template constexpr static Counts count_slots() { - return Counts(NRET, NARG, NVAR, NEXTRA); + return Counts(0, 0, 0, 0); } -private: + + template + constexpr static int count_vars(LuaVar &v, SS && ... stackslots) + { + return 1 + count_vars(stackslots...); + } + constexpr static int count_vars() { + return 0; + } + + private: // Push any value on the stack, by type. void push_any_value(LuaNewTableMarker s) const { lua_newtable(L_); } void push_any_value(LuaNilMarker s) const { lua_pushnil(L_); } @@ -587,7 +599,7 @@ private: public: template inline LuaDefStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) { - constexpr Counts counts = count_slots<0, 0, 0, 0>(stackslots...); + Counts counts = count_slots(stackslots...); int nargs = lua_gettop(L); if (counts.nextra == 0) { if (nargs != counts.narg) { @@ -645,13 +657,10 @@ private: public: template LuaExtStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) { - constexpr Counts counts = count_slots<0, 0, 0, 0>(stackslots...); - static_assert(counts.narg == 0, "LuaExtStack doesn't allow LuaArg parameters"); - static_assert(counts.nret == 0, "LuaExtStack doesn't allow LuaRet parameters"); - static_assert(counts.nextra == 0, "LuaExtStack doesn't allow LuaExtraArgs parameters"); - lua_checkstack(L_, counts.nvar + 20); + int nvars = count_vars(stackslots...); + lua_checkstack(L_, nvars + 20); oldtop_ = lua_gettop(L_); - for (int i = 0; i < counts.nvar; i++) { + for (int i = 0; i < nvars; i++) { lua_pushnil(L_); } assign_slots(oldtop_ + 1, stackslots...);