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",
|
"panel": "shared",
|
||||||
"revealProblems": "never"
|
"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.";
|
return "in animation key-value pairs, key must be a string.";
|
||||||
}
|
}
|
||||||
eng::string name = LS.ckstring(key);
|
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.";
|
return "in animation key-value pairs, key must be a valid lua identifier.";
|
||||||
}
|
}
|
||||||
AnimValue parsedvalue = parse_anim_value(LS, val, tmp);
|
AnimValue parsedvalue = parse_anim_value(LS, val, tmp);
|
||||||
|
|||||||
@@ -85,6 +85,9 @@ lua_State *LuaCoreStack::newstate (lua_Alloc allocf) {
|
|||||||
lua_pushstring(L, "classes");
|
lua_pushstring(L, "classes");
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||||
|
lua_pushstring(L, "classnames");
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||||
lua_pushstring(L, "tangibles");
|
lua_pushstring(L, "tangibles");
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lua_rawset(L, LUA_REGISTRYINDEX);
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
||||||
@@ -333,112 +336,113 @@ lua_State *LuaCoreStack::newthread(LuaSlot target) const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaCoreStack::valididentifier(std::string_view str) {
|
eng::string LuaCoreStack::classname(LuaSlot input) const {
|
||||||
return sv::is_lua_id(str);
|
LuaVar lookup, classtab, classname;
|
||||||
}
|
LuaExtStack LS(L_, lookup, classtab, classname);
|
||||||
|
|
||||||
bool LuaCoreStack::validclassname(std::string_view cname) {
|
int xt = xtype(input);
|
||||||
if (cname == "_G") return false;
|
if (xt == LUA_TSTRING) {
|
||||||
return valididentifier(cname);
|
LS.rawget(lookup, LuaRegistry, "classes");
|
||||||
}
|
LS.rawget(classtab, lookup, input);
|
||||||
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
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 "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
eng::string LuaCoreStack::getclass(LuaSlot classtab, LuaSlot classname) const {
|
static eng::string nonexistentclass(eng::string name) {
|
||||||
lua_checkstack(L_, 20);
|
eng::string err;
|
||||||
LuaVar globtab, cname;
|
if (!sv::is_lua_classname(name)) {
|
||||||
LuaExtStack LS(L_, globtab, cname);
|
err = "invalid class name: " + name;
|
||||||
LS.getglobaltable(globtab);
|
} else {
|
||||||
|
err = "not a class: " + name;
|
||||||
if (LS.isstring(classname)) {
|
}
|
||||||
if (!validclassname(LS.ckstring(classname))) {
|
|
||||||
eng::string err = "invalid class name: " + LS.ckstring(classname);
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
LS.rawget(classtab, globtab, classname);
|
|
||||||
if (!LS.istable(classtab)) {
|
eng::string LuaCoreStack::getclass(LuaSlot classtab, LuaSlot input) const {
|
||||||
eng::string err = "not a class: " + LS.ckstring(classname);
|
LuaVar lookup;
|
||||||
return err;
|
LuaExtStack LS(L_, lookup);
|
||||||
}
|
|
||||||
LS.rawget(cname, classtab, "__class");
|
int xt = xtype(input);
|
||||||
if (!LS.rawequal(cname, classname)) {
|
if (xt == LUA_TSTRING) {
|
||||||
eng::string err = "not a valid class: " + LS.ckstring(classname);
|
LS.rawget(lookup, LuaRegistry, "classes");
|
||||||
return err;
|
LS.rawget(classtab, lookup, input);
|
||||||
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||||
|
eng::string classname = LS.ckstring(input);
|
||||||
|
return nonexistentclass(LS.ckstring(input));
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
} else if (LS.istable(classname)) {
|
} else if (xt == LUA_TT_TANGIBLE) {
|
||||||
LS.rawget(cname, classname, "__class");
|
if (!LS.getmetatable(lookup, input)) {
|
||||||
if (!LS.isstring(cname)) {
|
eng::string err = "inactive tangible has no class";
|
||||||
eng::string err = "table is not a class.";
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if (!validclassname(LS.ckstring(cname))) {
|
LS.rawget(classtab, lookup, "__index");
|
||||||
eng::string err = "invalid class name: " + LS.ckstring(cname);
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||||
return err;
|
eng::string err = "tangible has invalid class";
|
||||||
}
|
|
||||||
LS.rawget(classtab, globtab, cname);
|
|
||||||
if (!LS.rawequal(classtab, classname)) {
|
|
||||||
eng::string err = "not a valid class: " + LS.ckstring(cname);
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
|
} else if (xt == LUA_TT_CLASS) {
|
||||||
|
LS.set(classtab, input);
|
||||||
|
return "";
|
||||||
} else {
|
} 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;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eng::string LuaCoreStack::getclass(LuaSlot tab, std::string_view name) const {
|
eng::string LuaCoreStack::getclass(LuaSlot classtab, std::string_view classname) const {
|
||||||
push_any_value(name);
|
LuaVar lookup;
|
||||||
LuaSpecial classname(lua_gettop(L_));
|
LuaExtStack LS(L_, lookup);
|
||||||
eng::string err = getclass(tab, classname);
|
LS.rawget(lookup, LuaRegistry, "classes");
|
||||||
lua_pop(L_, 1);
|
LS.rawget(classtab, lookup, classname);
|
||||||
return err;
|
if (xtype(classtab) != LUA_TT_CLASS) {
|
||||||
|
return nonexistentclass(eng::string(classname));
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
||||||
LuaVar classes, globtab, cname;
|
LuaVar classes, classnames, globtab, cname;
|
||||||
LuaExtStack LS(L_, classes, globtab, cname);
|
LuaExtStack LS(L_, classes, classnames, globtab, cname);
|
||||||
|
|
||||||
// Validate the class name.
|
// 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.
|
// Fetch the classes table from the registry.
|
||||||
LS.rawget(classes, LuaRegistry, "classes");
|
LS.rawget(classes, LuaRegistry, "classes");
|
||||||
assert(LS.istable(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.
|
// Fetch the global environment from the registry.
|
||||||
LS.getglobaltable(globtab);
|
LS.getglobaltable(globtab);
|
||||||
|
|
||||||
@@ -449,6 +453,7 @@ void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
|||||||
if (!LS.istable(classtab)) {
|
if (!LS.istable(classtab)) {
|
||||||
LS.newtable(classtab);
|
LS.newtable(classtab);
|
||||||
LS.rawset(classes, classname, classtab);
|
LS.rawset(classes, classname, classtab);
|
||||||
|
LS.rawset(classnames, classtab, classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the table into the global environment.
|
// Put the table into the global environment.
|
||||||
@@ -456,7 +461,6 @@ void LuaCoreStack::makeclass(LuaSlot classtab, LuaSlot classname) const {
|
|||||||
|
|
||||||
// Repair the special fields.
|
// Repair the special fields.
|
||||||
LS.settabletype(classtab, LUA_TT_CLASS);
|
LS.settabletype(classtab, LUA_TT_CLASS);
|
||||||
LS.rawset(classtab, "__class", classname);
|
|
||||||
LS.rawset(classtab, "__index", classtab);
|
LS.rawset(classtab, "__index", classtab);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -308,9 +308,6 @@
|
|||||||
// both add functions to class 'deque', they can both makeclass 'deque',
|
// both add functions to class 'deque', they can both makeclass 'deque',
|
||||||
// and no conflict will occur.
|
// 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
|
// Class tables have a distinct LuaTableType: LUA_TT_CLASS. That
|
||||||
// way, it is easy to tell that the table is intended as a class.
|
// 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.
|
// 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); }
|
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.
|
// Get the class name given a class table.
|
||||||
//
|
//
|
||||||
// Return the class name if x is a valid class table. Otherwise, returns
|
// Return the class name if x is a valid class table. Otherwise, returns
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ class Deserializer {
|
|||||||
}
|
}
|
||||||
case LUA_TT_CLASS: {
|
case LUA_TT_CLASS: {
|
||||||
eng::string name = sb_->read_string();
|
eng::string name = sb_->read_string();
|
||||||
if (!LS_.validclassname(name)) {
|
if (!sv::is_lua_classname(name)) {
|
||||||
throw DeserializeError();
|
throw DeserializeError();
|
||||||
}
|
}
|
||||||
LS_.makeclass(target, name);
|
LS_.makeclass(target, name);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ LuaDefine(makeclass, "classname", "create a class if it doesn't already exist")
|
|||||||
LuaArg classname;
|
LuaArg classname;
|
||||||
LuaRet classtab;
|
LuaRet classtab;
|
||||||
LuaDefStack LS(L, classname, 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());
|
luaL_error(L, "invalid class name: %s", LS.ckstring(classname).c_str());
|
||||||
};
|
};
|
||||||
LS.makeclass(classtab, classname);
|
LS.makeclass(classtab, classname);
|
||||||
|
|||||||
@@ -15,15 +15,13 @@
|
|||||||
// a table and stores it in the global environment. The new table is meant to be
|
// 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:
|
// 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
|
// __index --> points back to the class. Makes it convenient to use the
|
||||||
// class as a metatable.
|
// class as a metatable.
|
||||||
//
|
//
|
||||||
// action --> a subtable of additional methods.
|
// action --> a subtable of additional methods.
|
||||||
//
|
//
|
||||||
// If you invoke 'makeclass' on a class that already exists, the existing table
|
// 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
|
// subtable, if not present, is recreated. If there are already functions or
|
||||||
// constants inside the class, they are not affected.
|
// constants inside the class, they are not affected.
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -188,6 +188,11 @@ bool is_lua_id(string_view str) {
|
|||||||
return true;
|
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) {
|
bool is_lua_comment(string_view s) {
|
||||||
int start = 0;
|
int start = 0;
|
||||||
while ((start < int(s.size())) && ((s[start]==' ') || (s[start]=='\t'))) start++;
|
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.
|
// Return true if the string is a lua identifier.
|
||||||
bool is_lua_id(string_view s);
|
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.
|
// Return true if the line of code is a lua comment.
|
||||||
bool is_lua_comment(string_view s);
|
bool is_lua_comment(string_view s);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user