Some more error checking for class names and tangible IDs

This commit is contained in:
2024-02-28 14:19:59 -05:00
parent 32dae0df5a
commit 0c126e62d6
8 changed files with 77 additions and 21 deletions

View File

@@ -10,6 +10,10 @@ LuaSpecial LuaRegistry(LUA_REGISTRYINDEX);
LuaNilMarker LuaNil;
LuaNewTableMarker LuaNewTable;
inline bool ascii_islower(char c) { return (c >= 'a') && (c <= 'z'); }
inline bool ascii_isupper(char c) { return (c >= 'A') && (c <= 'Z'); }
inline bool ascii_isdigit(char c) { return (c >= '0') && (c <= '9'); }
LuaFunctionReg::LuaFunctionReg(const char *n, const char *a, const char *d, bool s, lua_CFunction f) {
name_ = n;
args_ = a;
@@ -220,12 +224,30 @@ lua_State *LuaCoreStack::newthread(LuaSlot target) const {
return result;
}
bool LuaCoreStack::validclassname(std::string_view cname) {
if (cname.empty()) return false;
if (cname == "_G") return false;
bool LuaCoreStack::valididentifier(std::string_view str) {
if (str.size() == 0) return false;
char c=str[0];
if ((!ascii_islower(c)) && (!ascii_isupper(c)) && (c!='_')) return false;
for (int i = 1; i < int(str.size()); i++) {
char c = str[i];
if ((!ascii_islower(c)) && (!ascii_isupper(c)) && (!ascii_isdigit(c)) && (c!='_')) return false;
}
return true;
}
bool LuaCoreStack::validint64(int64_t value) {
return (value <= MAXINT) && (value >= -MAXINT);
}
bool LuaCoreStack::validpositiveint64(int64_t value) {
return (value <= MAXINT) && (value >= 1);
}
bool LuaCoreStack::validclassname(std::string_view cname) {
if (cname == "_G") return false;
return valididentifier(cname);
}
bool LuaCoreStack::validclassname(LuaSlot slot) const {
if (!isstring(slot)) return false;
return validclassname(ckstring(slot));
@@ -352,6 +374,8 @@ void LuaCoreStack::makeclass(LuaSlot tab, std::string_view name) const {
}
void LuaCoreStack::maketan(LuaSlot tab, int64_t id) const {
assert(validpositiveint64(id));
LuaVar tangibles, metatab;
LuaExtStack LS(L_, tangibles, metatab);
@@ -412,6 +436,20 @@ int64_t LuaCoreStack::tanid(LuaSlot tab) const {
return result;
}
bool LuaCoreStack::tangetclass(LuaSlot classobj, LuaSlot tab) {
if (istable(tab) && (gettabletype(tab) == LUA_TT_TANGIBLE) && lua_getmetatable(L_, tab.index())) {
lua_pushstring(L_, "__index");
lua_rawget(L_, -2);
lua_replace(L_, classobj);
lua_pop(L_, 1);
if (istable(classobj) && (gettabletype(classobj) == LUA_TT_CLASS)) {
return true;
}
}
set(classobj, LuaNil);
return false;
}
bool LuaCoreStack::issortablekey(LuaSlot s) const {
int type = lua_type(L_, s);
return (type == LUA_TBOOLEAN) || (type == LUA_TNUMBER) || (type == LUA_TSTRING);