Improve implementation of LS.getclass and LS.classname, make it more consistent.
This commit is contained in:
2
luprex/.vscode/tasks.json
vendored
2
luprex/.vscode/tasks.json
vendored
@@ -25,6 +25,6 @@
|
||||
"panel": "shared",
|
||||
"revealProblems": "never"
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -244,7 +244,7 @@ eng::string AnimState::from_lua(LuaCoreStack &LS0, LuaSlot tab, bool persistent,
|
||||
return "in animation key-value pairs, key must be a string.";
|
||||
}
|
||||
eng::string name = LS.ckstring(key);
|
||||
if (!LS.valididentifier(name)) {
|
||||
if (!sv::is_lua_id(name)) {
|
||||
return "in animation key-value pairs, key must be a valid lua identifier.";
|
||||
}
|
||||
AnimValue parsedvalue = parse_anim_value(LS, val, tmp);
|
||||
|
||||
@@ -85,6 +85,9 @@ lua_State *LuaCoreStack::newstate (lua_Alloc allocf) {
|
||||
lua_pushstring(L, "classes");
|
||||
lua_newtable(L);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
lua_pushstring(L, "classnames");
|
||||
lua_newtable(L);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
lua_pushstring(L, "tangibles");
|
||||
lua_newtable(L);
|
||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||
@@ -333,112 +336,113 @@ lua_State *LuaCoreStack::newthread(LuaSlot target) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LuaCoreStack::valididentifier(std::string_view str) {
|
||||
return sv::is_lua_id(str);
|
||||
}
|
||||
eng::string LuaCoreStack::classname(LuaSlot input) const {
|
||||
LuaVar lookup, classtab, classname;
|
||||
LuaExtStack LS(L_, lookup, classtab, classname);
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
eng::string LuaCoreStack::classname(LuaSlot tab) const {
|
||||
eng::string result;
|
||||
if ((istable(tab)) && (gettabletype(tab) == LUA_TT_CLASS)) {
|
||||
LuaVar classes, name, dup;
|
||||
LuaExtStack LS(L_, classes, name, dup);
|
||||
// Get the classes table from the registry.
|
||||
LS.rawget(classes, LuaRegistry, "classes");
|
||||
|
||||
// Try the efficient approach: get the class name from
|
||||
// the class, and confirm it by checking the classes table.
|
||||
LS.rawget(name, tab, "__class");
|
||||
if (LS.isstring(name)) {
|
||||
LS.rawget(dup, classes, name);
|
||||
if (LS.rawequal(dup, tab)) {
|
||||
return LS.ckstring(name);
|
||||
}
|
||||
int xt = xtype(input);
|
||||
if (xt == LUA_TSTRING) {
|
||||
LS.rawget(lookup, LuaRegistry, "classes");
|
||||
LS.rawget(classtab, lookup, input);
|
||||
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Do it the brute force way: scan the classes table.
|
||||
LS.set(name, LuaNil);
|
||||
while (LS.next(classes, name, dup)) {
|
||||
if (LS.rawequal(dup, tab)) {
|
||||
return LS.ckstring(name);
|
||||
}
|
||||
return LS.ckstring(input);
|
||||
} else if (xt == LUA_TT_TANGIBLE) {
|
||||
if (!LS.getmetatable(lookup, input)) {
|
||||
return "";
|
||||
}
|
||||
LS.rawget(classtab, lookup, "__index");
|
||||
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||
return "";
|
||||
}
|
||||
LS.rawget(lookup, LuaRegistry, "classnames");
|
||||
LS.rawget(classname, lookup, classtab);
|
||||
if (!LS.isstring(classname)) {
|
||||
return "";
|
||||
}
|
||||
return LS.ckstring(classname);
|
||||
} else if (xt == LUA_TT_CLASS) {
|
||||
LS.rawget(lookup, LuaRegistry, "classnames");
|
||||
LS.rawget(classname, lookup, input);
|
||||
if (!LS.isstring(classname)) {
|
||||
return "";
|
||||
}
|
||||
return LS.ckstring(classname);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
eng::string LuaCoreStack::getclass(LuaSlot classtab, LuaSlot classname) const {
|
||||
lua_checkstack(L_, 20);
|
||||
LuaVar globtab, cname;
|
||||
LuaExtStack LS(L_, globtab, cname);
|
||||
LS.getglobaltable(globtab);
|
||||
static eng::string nonexistentclass(eng::string name) {
|
||||
eng::string err;
|
||||
if (!sv::is_lua_classname(name)) {
|
||||
err = "invalid class name: " + name;
|
||||
} else {
|
||||
err = "not a class: " + name;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
if (LS.isstring(classname)) {
|
||||
if (!validclassname(LS.ckstring(classname))) {
|
||||
eng::string err = "invalid class name: " + LS.ckstring(classname);
|
||||
return err;
|
||||
}
|
||||
LS.rawget(classtab, globtab, classname);
|
||||
if (!LS.istable(classtab)) {
|
||||
eng::string err = "not a class: " + LS.ckstring(classname);
|
||||
return err;
|
||||
}
|
||||
LS.rawget(cname, classtab, "__class");
|
||||
if (!LS.rawequal(cname, classname)) {
|
||||
eng::string err = "not a valid class: " + LS.ckstring(classname);
|
||||
return err;
|
||||
eng::string LuaCoreStack::getclass(LuaSlot classtab, LuaSlot input) const {
|
||||
LuaVar lookup;
|
||||
LuaExtStack LS(L_, lookup);
|
||||
|
||||
int xt = xtype(input);
|
||||
if (xt == LUA_TSTRING) {
|
||||
LS.rawget(lookup, LuaRegistry, "classes");
|
||||
LS.rawget(classtab, lookup, input);
|
||||
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||
eng::string classname = LS.ckstring(input);
|
||||
return nonexistentclass(LS.ckstring(input));
|
||||
}
|
||||
return "";
|
||||
} else if (LS.istable(classname)) {
|
||||
LS.rawget(cname, classname, "__class");
|
||||
if (!LS.isstring(cname)) {
|
||||
eng::string err = "table is not a class.";
|
||||
} else if (xt == LUA_TT_TANGIBLE) {
|
||||
if (!LS.getmetatable(lookup, input)) {
|
||||
eng::string err = "inactive tangible has no class";
|
||||
return err;
|
||||
}
|
||||
if (!validclassname(LS.ckstring(cname))) {
|
||||
eng::string err = "invalid class name: " + LS.ckstring(cname);
|
||||
return err;
|
||||
}
|
||||
LS.rawget(classtab, globtab, cname);
|
||||
if (!LS.rawequal(classtab, classname)) {
|
||||
eng::string err = "not a valid class: " + LS.ckstring(cname);
|
||||
return err;
|
||||
LS.rawget(classtab, lookup, "__index");
|
||||
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||
eng::string err = "tangible has invalid class";
|
||||
}
|
||||
return "";
|
||||
} else if (xt == LUA_TT_CLASS) {
|
||||
LS.set(classtab, input);
|
||||
return "";
|
||||
} else {
|
||||
eng::string err = "getclass expects a string or a classtab";
|
||||
eng::string err = "getclass must be passed a string, class, or tangible";
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
eng::string LuaCoreStack::getclass(LuaSlot tab, std::string_view name) const {
|
||||
push_any_value(name);
|
||||
LuaSpecial classname(lua_gettop(L_));
|
||||
eng::string err = getclass(tab, classname);
|
||||
lua_pop(L_, 1);
|
||||
return err;
|
||||
eng::string LuaCoreStack::getclass(LuaSlot classtab, std::string_view classname) const {
|
||||
LuaVar lookup;
|
||||
LuaExtStack LS(L_, lookup);
|
||||
LS.rawget(lookup, LuaRegistry, "classes");
|
||||
LS.rawget(classtab, lookup, classname);
|
||||
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||
return nonexistentclass(eng::string(classname));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||
LuaVar classes, globtab, cname;
|
||||
LuaExtStack LS(L_, classes, globtab, cname);
|
||||
LuaVar classes, classnames, globtab, cname;
|
||||
LuaExtStack LS(L_, classes, classnames, globtab, cname);
|
||||
|
||||
// Validate the class name.
|
||||
assert(LS.validclassname(classname));
|
||||
assert(LS.isstring(classname));
|
||||
assert(sv::is_lua_classname(LS.ckstring(classname)));
|
||||
|
||||
// Fetch the classes table from the registry.
|
||||
LS.rawget(classes, LuaRegistry, "classes");
|
||||
assert(LS.istable(classes));
|
||||
|
||||
// Fetch the classnames table from the registry.
|
||||
LS.rawget(classnames, LuaRegistry, "classnames");
|
||||
assert(LS.istable(classnames));
|
||||
|
||||
// Fetch the global environment from the registry.
|
||||
LS.getglobaltable(globtab);
|
||||
|
||||
@@ -449,6 +453,7 @@ void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||
if (!LS.istable(classtab)) {
|
||||
LS.newtable(classtab);
|
||||
LS.rawset(classes, classname, classtab);
|
||||
LS.rawset(classnames, classtab, classname);
|
||||
}
|
||||
|
||||
// Put the table into the global environment.
|
||||
@@ -456,7 +461,6 @@ void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||
|
||||
// Repair the special fields.
|
||||
LS.settabletype(classtab, LUA_TT_CLASS);
|
||||
LS.rawset(classtab, "__class", classname);
|
||||
LS.rawset(classtab, "__index", classtab);
|
||||
}
|
||||
|
||||
|
||||
@@ -308,9 +308,6 @@
|
||||
// both add functions to class 'deque', they can both makeclass 'deque',
|
||||
// and no conflict will occur.
|
||||
//
|
||||
// Class tables contain their own classname. This is primarily for
|
||||
// ease of debugging. C.__class = "deque"
|
||||
//
|
||||
// Class tables have a distinct LuaTableType: LUA_TT_CLASS. That
|
||||
// way, it is easy to tell that the table is intended as a class.
|
||||
// Class tables are treated uniquely by our engine in several ways.
|
||||
@@ -769,21 +766,6 @@ public:
|
||||
//
|
||||
static bool validpositiveinteger(int64_t value) { return (value <= MAXINT) && (value >= 1); }
|
||||
|
||||
// Return true if the string is a valid lua identifier.
|
||||
//
|
||||
// Lua identifiers can contain ascii uppercase, ascii lowercase,
|
||||
// digits, and underscores. They may not start with a digit.
|
||||
//
|
||||
static bool valididentifier(std::string_view id);
|
||||
|
||||
// Return true if the classname is legal.
|
||||
//
|
||||
// Returns true if value is a string, and is a valid lua identifier,
|
||||
// and is not "_G".
|
||||
//
|
||||
bool validclassname(LuaSlot value) const;
|
||||
static bool validclassname(std::string_view cname);
|
||||
|
||||
// Get the class name given a class table.
|
||||
//
|
||||
// Return the class name if x is a valid class table. Otherwise, returns
|
||||
|
||||
@@ -89,7 +89,7 @@ class Deserializer {
|
||||
}
|
||||
case LUA_TT_CLASS: {
|
||||
eng::string name = sb_->read_string();
|
||||
if (!LS_.validclassname(name)) {
|
||||
if (!sv::is_lua_classname(name)) {
|
||||
throw DeserializeError();
|
||||
}
|
||||
LS_.makeclass(target, name);
|
||||
|
||||
@@ -21,7 +21,7 @@ LuaDefine(makeclass, "classname", "create a class if it doesn't already exist")
|
||||
LuaArg classname;
|
||||
LuaRet classtab;
|
||||
LuaDefStack LS(L, classname, classtab);
|
||||
if (!LS.validclassname(classname)) {
|
||||
if (!sv::is_lua_classname(LS.ckstring(classname))) {
|
||||
luaL_error(L, "invalid class name: %s", LS.ckstring(classname).c_str());
|
||||
};
|
||||
LS.makeclass(classtab, classname);
|
||||
|
||||
@@ -15,15 +15,13 @@
|
||||
// a table and stores it in the global environment. The new table is meant to be
|
||||
// used as a class. Three fields are initially created:
|
||||
//
|
||||
// __class --> the name of the class as a string
|
||||
//
|
||||
// __index --> points back to the class. Makes it convenient to use the
|
||||
// class as a metatable.
|
||||
//
|
||||
// action --> a subtable of additional methods.
|
||||
//
|
||||
// If you invoke 'makeclass' on a class that already exists, the existing table
|
||||
// is "repaired" - the __class and __index fields are restored and the action
|
||||
// is "repaired" - __index field is restored and the action
|
||||
// subtable, if not present, is recreated. If there are already functions or
|
||||
// constants inside the class, they are not affected.
|
||||
//
|
||||
|
||||
@@ -188,6 +188,11 @@ bool is_lua_id(string_view str) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_lua_classname(string_view s) {
|
||||
if (s == "_G") return false;
|
||||
return is_lua_id(s);
|
||||
}
|
||||
|
||||
bool is_lua_comment(string_view s) {
|
||||
int start = 0;
|
||||
while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
|
||||
|
||||
@@ -104,6 +104,9 @@ int common_prefix_length(string_view a, string_view b);
|
||||
// Return true if the string is a lua identifier.
|
||||
bool is_lua_id(string_view s);
|
||||
|
||||
// Return true if the string is a valid lua classname.
|
||||
bool is_lua_classname(string_view s);
|
||||
|
||||
// Return true if the line of code is a lua comment.
|
||||
bool is_lua_comment(string_view s);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user