From b23728f52550a40c4a1d19f28268fd49c1e6fbde Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Sun, 28 Feb 2021 14:24:59 -0500 Subject: [PATCH] still working on porting to lua 5.2 --- luprex/core/cpp/luaconsole.cpp | 2 +- luprex/core/cpp/luastack.cpp | 39 +++++++++++++++++----------------- luprex/core/cpp/luastack.hpp | 6 +++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/luprex/core/cpp/luaconsole.cpp b/luprex/core/cpp/luaconsole.cpp index 54956aec..1595dab8 100644 --- a/luprex/core/cpp/luaconsole.cpp +++ b/luprex/core/cpp/luaconsole.cpp @@ -9,7 +9,7 @@ static bool is_single_letter(const std::string &s) { } LuaConsole::LuaConsole() { - lua_state_ = lua_open(); + lua_state_ = luaL_newstate(); clear(); } diff --git a/luprex/core/cpp/luastack.cpp b/luprex/core/cpp/luastack.cpp index 5c93f9ef..a59c3991 100644 --- a/luprex/core/cpp/luastack.cpp +++ b/luprex/core/cpp/luastack.cpp @@ -3,7 +3,6 @@ #include LuaSpecial LuaRegistry(LUA_REGISTRYINDEX); -LuaSpecial LuaGlobals(LUA_GLOBALSINDEX); LuaNilMarker LuaNil; LuaNewTableMarker LuaNewTable; @@ -39,8 +38,8 @@ lua_Number LuaStack::cknumber(LuaSlot s) const { 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); + const char *str = lua_tolstring(L_, s, &len); + return std::string(str, len); } lua_State *LuaStack::ckthread(LuaSlot s) const { @@ -104,12 +103,6 @@ void LuaStack::pop_any_value(std::string &s) const { lua_pop(L_, 1); } -std::string LuaStack::ckstring(LuaSlot s) const { - size_t len; - const char *str = luaL_cklstring(L_, s, &len); - return std::string(str, len); -} - void LuaStack::clearmetatable(LuaSlot tab) const { lua_pushnil(L_); lua_setmetatable(L_, tab); @@ -156,29 +149,37 @@ lua_State *LuaStack::newthread(LuaSlot target) const { } void LuaStack::makeclass(LuaSlot classtab, LuaSlot classname) const { - checkstring(classname); + LuaVar globtab; + LuaStack LS(L_, globtab); - // Special case: if the classname is _G, detect and error. - if (equal(classname, "_G")) { + // Validate the class name. + LS.checkstring(classname); + if (LS.equal(classname, "_G")) { luaL_error(L_, "_G is explicitly not allowed as a class name"); } + + // Fetch the global environment from the registry. + LS.rawget(globtab, LuaRegistry, LUA_RIDX_GLOBALS); // Get the classtab from the global environment. // Create it if it doesn't exist. - rawget(classtab, LuaGlobals, classname); - if (isnil(classtab)) { - newtable(classtab); - rawset(LuaGlobals, classname, classtab); + LS.rawget(classtab, globtab, classname); + if (LS.isnil(classtab)) { + LS.newtable(classtab); + LS.rawset(globtab, classname, classtab); } // If the name isn't bound to a table, abort. - if (!istable(classtab)) { + if (!LS.istable(classtab)) { luaL_error(L_, "%s is not a class", ckstring(classname).c_str()); } // Repair the special fields. - rawset(classtab, "__index", classtab); - rawset(classtab, "__class", classname); + LS.rawset(classtab, "__index", classtab); + LS.rawset(classtab, "__class", classname); + + // Put the stack back. + LS.result(); } void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const { diff --git a/luprex/core/cpp/luastack.hpp b/luprex/core/cpp/luastack.hpp index b1878346..3e048688 100644 --- a/luprex/core/cpp/luastack.hpp +++ b/luprex/core/cpp/luastack.hpp @@ -156,6 +156,7 @@ extern "C" { #include "lua.h" +#include "lauxlib.h" } #include @@ -192,7 +193,6 @@ public: }; extern LuaSpecial LuaRegistry; -extern LuaSpecial LuaGlobals; class LuaUpvalue : public LuaSlot { public: @@ -404,12 +404,12 @@ public: } bool equal(LuaSlot v1, LuaSlot v2) const { - return lua_equal(L_, v1, v2); + return lua_compare(L_, v1, v2, LUA_OPEQ); } bool equal(LuaSlot v1, const char *name) const { push_any_value(name); - bool result = lua_equal(L_, v1, -1); + bool result = lua_compare(L_, v1, -1, LUA_OPEQ); lua_pop(L_, 1); return result; }