From 150a71433b8a3b6435a9b408aa1ef48d958fe0bb Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 27 Nov 2020 14:24:37 -0500 Subject: [PATCH] More LuaStack work. --- luprex/syscpp/luastack.cpp | 37 +++++++++++++++++- luprex/syscpp/luastack.hpp | 78 +++++++++++++------------------------- 2 files changed, 61 insertions(+), 54 deletions(-) diff --git a/luprex/syscpp/luastack.cpp b/luprex/syscpp/luastack.cpp index 73ab49ff..6126fa4b 100644 --- a/luprex/syscpp/luastack.cpp +++ b/luprex/syscpp/luastack.cpp @@ -78,15 +78,48 @@ void LuaStack::setnumber(LuaSlot target, double value) const { lua_replace(L_, target); } -std::string LuaStack::tostring(LuaSlot s) { +std::string LuaStack::tostring(LuaSlot s) const { size_t len; const char *str = lua_tolstring(L_, s, &len); return std::string(str, len); } -std::string LuaStack::checkstring(LuaSlot s) { +std::string LuaStack::checkstring(LuaSlot s) const { size_t len; const char *str = luaL_checklstring(L_, s, &len); return std::string(str, len); } +void LuaStack::clearmetatable(LuaSlot tab) const { + lua_pushnil(L_); + lua_setmetatable(L_, tab); +} + +void LuaStack::setmetatable(LuaSlot tab, LuaSlot mt) const { + lua_pushvalue(L_, mt); + lua_setmetatable(L_, tab); +} + +void LuaStack::checknometa(LuaSlot index) const { + if (lua_istable(L_, index)) { + if (!lua_getmetatable(L_, index)) { + return; + } + } + luaL_error(L_, "expected simple table with no metatable"); +} + +int LuaStack::next(LuaSlot tab, LuaSlot key, LuaSlot value) const { + lua_pushvalue(L_, key); + int ret = lua_next(L_, tab); + if (ret != 0) { + lua_replace(L_, value); + lua_replace(L_, key); + } + return ret; +} + +void LuaStack::newtable(LuaSlot target) const { + lua_newtable(L_); + lua_replace(L_, target); +} diff --git a/luprex/syscpp/luastack.hpp b/luprex/syscpp/luastack.hpp index a1a53a28..1c80aef1 100644 --- a/luprex/syscpp/luastack.hpp +++ b/luprex/syscpp/luastack.hpp @@ -168,38 +168,36 @@ public: void setstring(LuaSlot target, const std::string &str) const; void setnumber(LuaSlot target, double value) const; - bool isboolean(LuaSlot s) { return lua_isboolean(L_, s); } - bool iscfunction(LuaSlot s) { return lua_iscfunction(L_, s); } - bool isfunction(LuaSlot s) { return lua_isfunction(L_, s); } - bool islightuserdata(LuaSlot s) { return lua_islightuserdata(L_, s); } - bool isnil(LuaSlot s) { return lua_isnil(L_, s); } - bool isnumber(LuaSlot s) { return lua_isnumber(L_, s); } - bool isstring(LuaSlot s) { return lua_isstring(L_, s); } - bool istable(LuaSlot s) { return lua_istable(L_, s); } - bool isthread(LuaSlot s) { return lua_isthread(L_, s); } - bool isuserdata(LuaSlot s) { return lua_isuserdata(L_, s); } + bool isboolean(LuaSlot s) const { return lua_isboolean(L_, s); } + bool iscfunction(LuaSlot s) const { return lua_iscfunction(L_, s); } + bool isfunction(LuaSlot s) const { return lua_isfunction(L_, s); } + bool islightuserdata(LuaSlot s) const { return lua_islightuserdata(L_, s); } + bool isnil(LuaSlot s) const { return lua_isnil(L_, s); } + bool isnumber(LuaSlot s) const { return lua_type(L_, s) == LUA_TNUMBER; } + bool isstring(LuaSlot s) const { return lua_type(L_, s) == LUA_TSTRING; } + bool istable(LuaSlot s) const { return lua_istable(L_, s); } + bool isthread(LuaSlot s) const { return lua_isthread(L_, s); } + bool isuserdata(LuaSlot s) const { return lua_isuserdata(L_, s); } - bool toboolean(LuaSlot s) { return lua_toboolean(L_, s); } - int tointeger(LuaSlot s) { return lua_tointeger(L_, s); } - double tonumber(LuaSlot s) { return lua_tonumber(L_, s); } - std::string tostring(LuaSlot s); - lua_State *tothread(LuaSlot s) { return lua_tothread(L_, s); } + bool toboolean(LuaSlot s) const { return lua_toboolean(L_, s); } + int tointeger(LuaSlot s) const { return lua_tointeger(L_, s); } + double tonumber(LuaSlot s) const { return lua_tonumber(L_, s); } + std::string tostring(LuaSlot s) const; + lua_State *tothread(LuaSlot s) const { return lua_tothread(L_, s); } - int checkint(LuaSlot s) { return luaL_checkint(L_, s); } - long checklong(LuaSlot s) { return luaL_checklong(L_, s); } - double checknumber(LuaSlot s) { return luaL_checknumber(L_, s); } - std::string checkstring(LuaSlot s); - void checktype(LuaSlot s, int t) { return luaL_checktype(L_, s, t); } + int checkint(LuaSlot s) const { return luaL_checkint(L_, s); } + long checklong(LuaSlot s) const { return luaL_checklong(L_, s); } + double checknumber(LuaSlot s) const { return luaL_checknumber(L_, s); } + std::string checkstring(LuaSlot s) const; + void checktype(LuaSlot s, int t) const { return luaL_checktype(L_, s, t); } - void clearmetatable(LuaSlot tab) { - lua_pushnil(L_); - lua_setmetatable(L_, tab); - } + void clearmetatable(LuaSlot tab) const; + void setmetatable(LuaSlot tab, LuaSlot mt) const; + void checknometa(LuaSlot index) const; - void setmetatable(LuaSlot tab, LuaSlot mt) { - lua_pushvalue(L_, mt); - lua_setmetatable(L_, tab); - } + void newtable(LuaSlot target) const; + + int next(LuaSlot tab, LuaSlot key, LuaSlot value) const; void rawget(LuaSlot target, LuaSlot tab, LuaSlot key) const { lua_pushvalue(L_, key); @@ -213,18 +211,6 @@ public: lua_rawset(L_, tab); } - - int next(LuaSlot tab, LuaSlot key, LuaSlot value) const { - lua_pushvalue(L_, key); - int ret = lua_next(L_, tab); - if (ret != 0) { - lua_replace(L_, value); - lua_replace(L_, key); - } - return ret; - } - - void setfield(LuaSlot tab, const char *field, LuaSlot value) const { lua_pushvalue(L_, value); lua_setfield(L_, tab, field); @@ -245,19 +231,7 @@ public: lua_replace(L_, target); } - void newtable(LuaSlot target) const { - lua_newtable(L_); - lua_replace(L_, target); - } - void checknometa(LuaSlot index) const { - if (lua_istable(L_, index)) { - if (!lua_getmetatable(L_, index)) { - return; - } - } - luaL_error(L_, "expected simple table with no metatable"); - } };