Add LuaDefineAlias

This commit is contained in:
2023-04-14 14:52:44 -04:00
parent 8ea6c47e4c
commit 47c868876a
4 changed files with 53 additions and 41 deletions

View File

@@ -683,7 +683,7 @@ void HttpClientRequest::set_params(LuaCoreStack &LS0, LuaSlot tab) {
return; return;
} }
LuaVar key, val; LuaVar key, val;
LuaDefStack LS(LS0.state(), key, val); LuaExtStack LS(LS0.state(), key, val);
LS.set(key, LuaNil); LS.set(key, LuaNil);
while (LS.next(tab, key, val)) { while (LS.next(tab, key, val)) {
set_param(LS, key, val); set_param(LS, key, val);

View File

@@ -411,7 +411,7 @@ public:
int rawlen(LuaSlot val) const; int rawlen(LuaSlot val) const;
int nkeys(LuaSlot tab) const; int nkeys(LuaSlot tab) const;
int next(LuaSlot tab, LuaSlot key, LuaSlot value) const; int next(LuaSlot tab, LuaSlot key, LuaSlot value) const;
// Return true if the classname is legal. // Return true if the classname is legal.
@@ -902,13 +902,13 @@ public:
#define LuaDefine(name, args, docs) \ #define LuaDefine(name, args, docs) \
int lfn_##name(lua_State *L); \ int lfn_##name(lua_State *L); \
LuaFunctionReg reg_##name(#name, args, docs, false, lfn_##name); \ const char *lfnarg_##name = args; \
const char *lfndoc_##name = docs; \
LuaFunctionReg reg_##name(#name, lfnarg_##name, lfndoc_##name, false, lfn_##name); \
int lfn_##name(lua_State *L) int lfn_##name(lua_State *L)
#define LuaSandbox(name, args, docs) \ #define LuaDefineAlias(name1, name2) \
int lfn_##name(lua_State *L); \ LuaFunctionReg reg_##name1(#name1, lfnarg_##name2, lfndoc_##name2, false, lfn_##name2); \
LuaFunctionReg reg_##name(#name, args, docs, true, lfn_##name); \
int lfn_##name(lua_State *L)
#define LuaDefineBuiltin(name, args, docs) \ #define LuaDefineBuiltin(name, args, docs) \
LuaFunctionReg reg_##name(#name, args, docs, false, nullptr); LuaFunctionReg reg_##name(#name, args, docs, false, nullptr);

View File

@@ -320,25 +320,9 @@ LuaDefine(string_print, "obj",
"|Concise print the specified object into a string" "|Concise print the specified object into a string"
"|" "|"
"|This prints a concise representation of obj into a string. Tables" "|This prints a concise representation of obj into a string. Tables"
"|are not expanded: for that, use string.pprintx. The functions" "|are not expanded: for that, use string.pprint or string.pprintx."
"|tostring and string.print are identical."
"|") {
LuaArg val;
LuaRet result;
LuaDefStack LS(L, val, result);
eng::ostringstream oss;
atomic_print(LS, val, false, &oss);
LS.set(result, oss.str());
return LS.result();
}
LuaDefine(tostring, "obj",
"|Concise print the specified object into a string"
"|" "|"
"|This prints a concise representation of obj into a string. Tables" "|The functions string.print and tostring are identical."
"|are not expanded: for that, use string.pprint. The functions"
"|tostring and string.print are identical."
"|") { "|") {
LuaArg val; LuaArg val;
LuaRet result; LuaRet result;
@@ -349,6 +333,8 @@ LuaDefine(tostring, "obj",
return LS.result(); return LS.result();
} }
LuaDefineAlias(tostring, string_print);
LuaDefine(string_isidentifier, "str", "return true if the string is a valid lua identifier") { LuaDefine(string_isidentifier, "str", "return true if the string is a valid lua identifier") {
LuaArg str; LuaArg str;
LuaRet result; LuaRet result;

View File

@@ -60,7 +60,16 @@ LuaDefine(table_equal, "table1,table2",
return LS.result(); return LS.result();
} }
static int check_isvector(lua_State *L) { LuaDefine(table_isvector, "table",
"|Return true if the table is a valid vector."
"|"
"|A vector is a table that has keys starting with 1, and no gaps."
"|The empty table also counts as a valid vector. This function"
"|scans the entire vector to verify its validity, so it takes O(N)"
"|time. See also table.isvectorq"
"|"
"|The functions vector.isvector and table.isvector are identical."
"|") {
LuaArg table; LuaArg table;
LuaRet result; LuaRet result;
LuaVar tmp; LuaVar tmp;
@@ -81,27 +90,44 @@ static int check_isvector(lua_State *L) {
return LS.result(); return LS.result();
} }
LuaDefine(table_isvector, "table", LuaDefine(table_isvectorq, "table",
"|Return true if the table is a valid vector." "|Return true if the table is probably a valid vector."
"|" "|"
"|A vector is a table that has keys starting with 1, and no gaps." "|A vector is a table that has keys starting with 1, and no gaps."
"|The empty table also counts as a valid vector." "|The empty table also counts as a valid vector. This function"
"|does a constant-time heuristic check: it gets the number of keys"
"|in the table, NKEYS. Then it verifies that table[NKEYS] is not"
"|nil and that table[NKEYS+1] is nil. If these things are both"
"|true, the table is very likely a valid vector."
"|" "|"
"|This function is identical to vector.isvector" "|The functions vector.isvectorq and table.isvectorq are identical."
"|") { "|") {
return check_isvector(L); LuaArg table;
LuaRet result;
LuaVar tmp;
LuaDefStack LS(L, table, result, tmp);
if (!LS.istable(table)) {
LS.set(result, false);
return LS.result();
}
int nkeys = LS.nkeys(table);
if (nkeys > 0) {
LS.rawget(tmp, table, nkeys);
if (LS.isnil(tmp)) {
LS.set(result, false);
return LS.result();
}
LS.rawget(tmp, table, nkeys + 1);
if (!LS.isnil(tmp)) {
LS.set(result, false);
return LS.result();
}
}
LS.set(result, true);
return LS.result();
} }
LuaDefine(vector_isvector, "table", LuaDefineAlias(vector_isvector, table_isvector);
"|Return true if the table is a valid vector."
"|"
"|A vector is a table that has keys starting with 1, and no gaps."
"|The empty table also counts as a valid vector."
"|"
"|This function is identical to table.isvector"
"|") {
return check_isvector(L);
}
LuaDefine(vector_removeall, "vector,value", LuaDefine(vector_removeall, "vector,value",
"|Remove all occurrences of value from vector." "|Remove all occurrences of value from vector."