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; 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 { bool LuaStack::ckboolean(LuaSlot s) const {
luaL_checktype(L_, s, LUA_TBOOLEAN); luaL_checktype(L_, s, LUA_TBOOLEAN);
@@ -106,27 +102,6 @@ int LuaStack::result() {
return nret_; 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 { void LuaStack::clearmetatable(LuaSlot tab) const {
lua_pushnil(L_); lua_pushnil(L_);
lua_setmetatable(L_, tab); 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 { int LuaStack::next(LuaSlot tab, LuaSlot key, LuaSlot value) const {
lua_pushvalue(L_, key); lua_pushvalue(L_, key);
int ret = lua_next(L_, tab); int ret = lua_next(L_, tab);
@@ -359,6 +325,10 @@ int64_t LuaStack::tanid(LuaSlot tab) const {
return result; 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) { void LuaStack::movesortablekey(LuaSlot key, LuaStack &otherstack, LuaSlot otherslot) {
int type = lua_type(L_, key); int type = lua_type(L_, key);
@@ -379,25 +349,23 @@ void LuaStack::movesortablekey(LuaSlot key, LuaStack &otherstack, LuaSlot others
break; break;
} }
default: 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 { void LuaStack::makesubtable(LuaSlot sub, LuaSlot tab, const char *name) const {
assert(istable(tab));
rawget(sub, tab, name); rawget(sub, tab, name);
if (istable(sub)) { if (istable(sub)) {
return; return;
} else if (isnil(sub)) { } else {
newtable(sub); newtable(sub);
rawset(tab, name, sub); rawset(tab, name, sub);
return;
} else {
luaL_error(L_, "%s is not a table", name);
} }
} }
void LuaStack::cleartable(LuaSlot tab, bool clearmeta) const { void LuaStack::cleartable(LuaSlot tab, bool clearmeta) const {
checktable(tab); assert(istable(tab));
lua_pushnil(L_); lua_pushnil(L_);
while (lua_next(L_, tab.index()) != 0) { while (lua_next(L_, tab.index()) != 0) {
lua_pop(L_, 1); // Pop the old value. 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(lua_CFunction s) const { lua_pushcfunction(L_, s); }
void push_any_value(bool b) const { lua_pushboolean(L_, b ? 1:0); } 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. // Push multiple values on the stack, in order, by type.
template<typename T0, typename... T> template<typename T0, typename... T>
void push_any_values(T0 arg0, T... args) { 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 isboolean(LuaSlot s) const { return lua_type(L_, s) == LUA_TBOOLEAN; }
bool isnil(LuaSlot s) const { return lua_type(L_, s) == LUA_TNIL; } bool isnil(LuaSlot s) const { return lua_type(L_, s) == LUA_TNIL; }
bool iscfunction(LuaSlot s) const { return lua_iscfunction(L_, s) != 0; } 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 checktable(LuaSlot index) const { checktype(index, LUA_TTABLE); }
void checkstring(LuaSlot index) const { checktype(index, LUA_TSTRING); } void checkstring(LuaSlot index) const { checktype(index, LUA_TSTRING); }
@@ -373,7 +366,6 @@ public:
void clearmetatable(LuaSlot tab) const; void clearmetatable(LuaSlot tab) const;
void setmetatable(LuaSlot tab, LuaSlot mt) const; void setmetatable(LuaSlot tab, LuaSlot mt) const;
bool getmetatable(LuaSlot mt, LuaSlot tab) const; bool getmetatable(LuaSlot mt, LuaSlot tab) const;
void checknometa(LuaSlot index) const;
void newtable(LuaSlot target) const; void newtable(LuaSlot target) const;
void createtable(LuaSlot target, int narr, int nrec) const; void createtable(LuaSlot target, int narr, int nrec) const;
@@ -415,6 +407,12 @@ public:
// this module. // this module.
int64_t tanid(LuaSlot tab) const; 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); void movesortablekey(LuaSlot val, LuaStack &other, LuaSlot otherslot);
bool rawequal(LuaSlot v1, LuaSlot v2) const { bool rawequal(LuaSlot v1, LuaSlot v2) const {
@@ -428,23 +426,22 @@ public:
return result; return result;
} }
template<typename T1, typename T2> template<typename VT>
void set(T1 &target, T2 value) const { void set(LuaSlot target, VT value) const {
push_any_value(value); push_any_value(value);
pop_any_value(target); lua_replace(L_, target);
} }
template<typename RT, typename KT> template<typename KT>
void rawget(RT &target, LuaSlot tab, KT key) const { void rawget(LuaSlot target, LuaSlot tab, KT key) const {
push_any_value(key); push_any_value(key);
lua_rawget(L_, tab); lua_rawget(L_, tab);
pop_any_value(target); lua_replace(L_, target);
} }
template<typename RT> void rawget(LuaSlot target, LuaSlot tab, int key) const {
void rawget(RT &target, LuaSlot tab, int key) const {
lua_rawgeti(L_, tab, key); lua_rawgeti(L_, tab, key);
pop_any_value(target); lua_replace(L_, target);
} }
template<typename KT, typename VT> 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; LuaArg tab, clearmeta;
LuaVar metatable, metafield; LuaVar metatable, metafield;
LuaStack LS(L, tab, clearmeta, metatable, metafield); LuaStack LS(L, tab, clearmeta, metatable, metafield);
LS.checktable(tab);
if (LS.ckboolean(clearmeta)) { if (LS.ckboolean(clearmeta)) {
LS.getmetatable(metatable, tab); LS.getmetatable(metatable, tab);
if (LS.istable(metatable)) { if (LS.istable(metatable)) {