Remove more invalid uses of luaL_error and luaL_check

This commit is contained in:
2021-12-27 17:03:42 -05:00
parent 9517b3e004
commit 0c021dc93a
3 changed files with 24 additions and 58 deletions

View File

@@ -34,10 +34,6 @@ const LuaFunctionReg *LuaFunctionReg::lookup(lua_CFunction fn) {
LuaFunctionReg *LuaFunctionReg::LuaFunctionRegistry;
bool LuaStack::issortablekey(LuaSlot s) const {
int type = lua_type(L_, s);
return (type == LUA_TBOOLEAN) || (type == LUA_TNUMBER) || (type == LUA_TSTRING);
}
bool LuaStack::ckboolean(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TBOOLEAN);
@@ -106,27 +102,6 @@ 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 = lua_tolstring(L_, -1, &len);
s = std::string(str, len);
lua_pop(L_, 1);
}
void LuaStack::clearmetatable(LuaSlot tab) const {
lua_pushnil(L_);
lua_setmetatable(L_, tab);
@@ -148,15 +123,6 @@ bool LuaStack::getmetatable(LuaSlot mt, LuaSlot tab) const {
}
}
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);
@@ -359,6 +325,10 @@ int64_t LuaStack::tanid(LuaSlot tab) const {
return result;
}
bool LuaStack::issortablekey(LuaSlot s) const {
int type = lua_type(L_, s);
return (type == LUA_TBOOLEAN) || (type == LUA_TNUMBER) || (type == LUA_TSTRING);
}
void LuaStack::movesortablekey(LuaSlot key, LuaStack &otherstack, LuaSlot otherslot) {
int type = lua_type(L_, key);
@@ -379,25 +349,23 @@ void LuaStack::movesortablekey(LuaSlot key, LuaStack &otherstack, LuaSlot others
break;
}
default:
luaL_error(L_, "movesortablekey: not a sortable key");
assert(false && "movesortablekey: not a sortable key");
}
}
void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
assert(istable(tab));
rawget(sub, tab, name);
if (istable(sub)) {
return;
} else if (isnil(sub)) {
} else {
newtable(sub);
rawset(tab, name, sub);
return;
} else {
luaL_error(L_, "%s is not a table", name);
}
}
void LuaStack::cleartable(LuaSlot tab, bool clearmeta) const {
checktable(tab);
assert(istable(tab));
lua_pushnil(L_);
while (lua_next(L_, tab.index()) != 0) {
lua_pop(L_, 1); // Pop the old value.

View File

@@ -304,12 +304,6 @@ private:
void push_any_value(lua_CFunction s) const { lua_pushcfunction(L_, s); }
void push_any_value(bool b) const { lua_pushboolean(L_, b ? 1:0); }
// 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;
void pop_any_value(lua_Number &s) const;
void pop_any_value(std::string &s) const;
// Push multiple values on the stack, in order, by type.
template<typename T0, typename... T>
void push_any_values(T0 arg0, T... args) {
@@ -353,7 +347,6 @@ public:
bool isboolean(LuaSlot s) const { return lua_type(L_, s) == LUA_TBOOLEAN; }
bool isnil(LuaSlot s) const { return lua_type(L_, s) == LUA_TNIL; }
bool iscfunction(LuaSlot s) const { return lua_iscfunction(L_, s) != 0; }
bool issortablekey(LuaSlot s) const;
void checktable(LuaSlot index) const { checktype(index, LUA_TTABLE); }
void checkstring(LuaSlot index) const { checktype(index, LUA_TSTRING); }
@@ -373,7 +366,6 @@ public:
void clearmetatable(LuaSlot tab) const;
void setmetatable(LuaSlot tab, LuaSlot mt) const;
bool getmetatable(LuaSlot mt, LuaSlot tab) const;
void checknometa(LuaSlot index) const;
void newtable(LuaSlot target) const;
void createtable(LuaSlot target, int narr, int nrec) const;
@@ -415,6 +407,12 @@ public:
// this module.
int64_t tanid(LuaSlot tab) const;
// Return true if the value is a sortable key (string, number, or boolean).
bool issortablekey(LuaSlot s) const;
// Move a sortable key (string, number, or boolean) from one lua
// environment to another lua environment. WARNING: this assert-fails
// if the value is not a sortable key.
void movesortablekey(LuaSlot val, LuaStack &other, LuaSlot otherslot);
bool rawequal(LuaSlot v1, LuaSlot v2) const {
@@ -428,23 +426,22 @@ public:
return result;
}
template<typename T1, typename T2>
void set(T1 &target, T2 value) const {
template<typename VT>
void set(LuaSlot target, VT value) const {
push_any_value(value);
pop_any_value(target);
lua_replace(L_, target);
}
template<typename RT, typename KT>
void rawget(RT &target, LuaSlot tab, KT key) const {
template<typename KT>
void rawget(LuaSlot target, LuaSlot tab, KT key) const {
push_any_value(key);
lua_rawget(L_, tab);
pop_any_value(target);
lua_replace(L_, target);
}
template<typename RT>
void rawget(RT &target, LuaSlot tab, int key) const {
lua_rawgeti(L_, tab, key);
pop_any_value(target);
void rawget(LuaSlot target, LuaSlot tab, int key) const {
lua_rawgeti(L_, tab, key);
lua_replace(L_, target);
}
template<typename KT, typename VT>

View File

@@ -118,6 +118,7 @@ LuaDefine(table_clear, "table,metaflag", "clear all keys, and optionally the met
LuaArg tab, clearmeta;
LuaVar metatable, metafield;
LuaStack LS(L, tab, clearmeta, metatable, metafield);
LS.checktable(tab);
if (LS.ckboolean(clearmeta)) {
LS.getmetatable(metatable, tab);
if (LS.istable(metatable)) {