From 6a0ffa6171030409a9bec8d344c7080ce3d37e06 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Sun, 28 Feb 2021 14:01:59 -0500 Subject: [PATCH] Doesn't compile with eris lua yet. Getting closer. --- luprex/core/cpp/luastack.cpp | 68 ++++++++++++++++++++---------------- luprex/core/cpp/luastack.hpp | 32 ++++------------- 2 files changed, 43 insertions(+), 57 deletions(-) diff --git a/luprex/core/cpp/luastack.cpp b/luprex/core/cpp/luastack.cpp index 4de275fe..5c93f9ef 100644 --- a/luprex/core/cpp/luastack.cpp +++ b/luprex/core/cpp/luastack.cpp @@ -6,7 +6,6 @@ LuaSpecial LuaRegistry(LUA_REGISTRYINDEX); LuaSpecial LuaGlobals(LUA_GLOBALSINDEX); LuaNilMarker LuaNil; LuaNewTableMarker LuaNewTable; -LuaDiscardMarker LuaDiscard; LuaFunctionReg::LuaFunctionReg(const char *m, const char *n, lua_CFunction f) { mode_ = m; @@ -27,6 +26,28 @@ LuaFunctionReg::List LuaFunctionReg::all() { LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry; +lua_Integer LuaStack::ckinteger(LuaSlot s) const { + luaL_checktype(L_, s, LUA_TNUMBER); + return lua_tointeger(L_, s); +} + +lua_Number LuaStack::cknumber(LuaSlot s) const { + luaL_checktype(L_, s, LUA_TNUMBER); + return lua_tonumber(L_, s); +} + +std::string LuaStack::ckstring(LuaSlot s) const { + luaL_checktype(L_, s, LUA_TSTRING); + size_t len; + const char *s = lua_tolstring(L_, s, &len); + return std::string(s, len); +} + +lua_State *LuaStack::ckthread(LuaSlot s) const { + luaL_checktype(L_, s, LUA_TTHREAD); + return lua_tothread(L_, s); +} + void LuaStack::count_slots_finalize(int narg, int nvar, int nret) { narg_ = narg; nret_ = nret; @@ -62,42 +83,27 @@ int LuaStack::result() { return nret_; } +void LuaStack::pop_any_value(lua_Integer &s) const { + luaL_checktype(L_, -1, LUA_TNUMBER); + s = lua_tointeger(L_, -1); + lua_pop(L_, 1); +} + +void LuaStack::pop_any_value(lua_Number &s) const { + luaL_checktype(L_, -1, LUA_TNUMBER); + s = lua_tonumber(L_, -1); + lua_pop(L_, 1); +} + + void LuaStack::pop_any_value(std::string &s) const { + luaL_checktype(L_, -1, LUA_TSTRING); size_t len; - const char *str = luaL_cklstring(L_, -1, &len); + const char *str = lua_tolstring(L_, -1, &len); s = std::string(str, len); lua_pop(L_, 1); } -void LuaStack::pop_any_value(LuaAcceptNilNumber &s) const { - if (lua_isnil(L_, -1)) { - s.v = 0.0; - } else { - s.v = luaL_cknumber(L_, -1); - } - lua_pop(L_, 1); -} - -void LuaStack::pop_any_value(LuaAcceptNilInteger &s) const { - if (lua_isnil(L_, -1)) { - s.v = 0; - } else { - s.v = luaL_ckinteger(L_, -1); - } - lua_pop(L_, 1); -} - -void LuaStack::pop_any_value(LuaAcceptNilString &s) const { - if (lua_isnil(L_, -1)) { - s.v = ""; - } else { - size_t len; - const char *str = luaL_cklstring(L_, -1, &len); - s.v = std::string(str, len); - } - lua_pop(L_, 1); -} - std::string LuaStack::ckstring(LuaSlot s) const { size_t len; const char *str = luaL_cklstring(L_, s, &len); diff --git a/luprex/core/cpp/luastack.hpp b/luprex/core/cpp/luastack.hpp index 619a1103..b1878346 100644 --- a/luprex/core/cpp/luastack.hpp +++ b/luprex/core/cpp/luastack.hpp @@ -207,17 +207,6 @@ extern LuaNilMarker LuaNil; class LuaNewTableMarker {}; extern LuaNewTableMarker LuaNewTable; -class LuaDiscardMarker {}; -extern LuaDiscardMarker LuaDiscard; - -struct LuaAcceptNilNumber { lua_Number v; }; -struct LuaAcceptNilInteger { lua_Integer v; }; -struct LuaAcceptNilString { std::string v; }; - -inline LuaAcceptNilNumber &LuaAcceptNil(lua_Number &x) { return *(LuaAcceptNilNumber *)(&x); } -inline LuaAcceptNilInteger &LuaAcceptNil(lua_Integer &x) { return *(LuaAcceptNilInteger *)(&x); } -inline LuaAcceptNilString &LuaAcceptNil(std::string &x) { return *(LuaAcceptNilString *)(&x); } - using LuaDeleterFn = void (*)(void *); using LuaTypeTag = lua_CFunction; @@ -303,13 +292,9 @@ private: // Pop any value off the stack, by type. void pop_any_value(LuaSlot &s) const { lua_replace(L_, s); } - void pop_any_value(lua_Integer &s) const { s = luaL_ckinteger(L_, -1); lua_pop(L_, 1); } - void pop_any_value(lua_Number &s) const { s = luaL_cknumber(L_, -1); lua_pop(L_, 1); } + void pop_any_value(lua_Integer &s) const; + void pop_any_value(lua_Number &s) const; void pop_any_value(std::string &s) const; - void pop_any_value(LuaAcceptNilNumber &s) const; - void pop_any_value(LuaAcceptNilInteger &s) const; - void pop_any_value(LuaAcceptNilString &s) const; - void pop_any_value(LuaDiscardMarker &s) const { lua_pop(L_, 1); } // Push multiple values on the stack, in order, by type. template @@ -342,11 +327,6 @@ private: pop_any_value(s); } template - void call_cfunction(int otop, LuaDiscardMarker &s, T... args) { - call_cfunction(otop, args...); - pop_any_value(s); - } - template void call_cfunction(int otop, lua_CFunction fn, T... args) { push_any_values(args...); int nret = fn(L_); @@ -397,11 +377,11 @@ public: void checkboolean(LuaSlot index) const { checktype(index, LUA_TBOOLEAN); } void checknil(LuaSlot index) const { checktype(index, LUA_TNIL); } - lua_Integer ckinteger(LuaSlot s) const { return luaL_ckinteger(L_, s); } - double cknumber(LuaSlot s) const { return luaL_cknumber(L_, s); } + lua_Integer ckinteger(LuaSlot s) const; + lua_Number cknumber(LuaSlot s) const; std::string ckstring(LuaSlot s) const; - lua_State *ckthread(LuaSlot s) const { return luaL_ckthread(L_, s); } - + lua_State *ckthread(LuaSlot s) const; + void clearmetatable(LuaSlot tab) const; void setmetatable(LuaSlot tab, LuaSlot mt) const; void getmetatable(LuaSlot mt, LuaSlot tab) const;