Start refactoring LuaStack

This commit is contained in:
2023-04-07 12:58:05 -04:00
parent 7f000bc0fd
commit 44c5a56172
6 changed files with 52 additions and 53 deletions

View File

@@ -171,7 +171,7 @@ void AnimStep::config_store_number(lua_State *L, int idx, float *target, float o
void AnimStep::configure(LuaKeywordParser &kp, const AnimStep &aqback) {
lua_State *L = kp.state();
LuaVar value;
LuaOldStack LS(L, value);
LuaExtStack LS(L, value);
if (kp.parse(value, "action")) {
config_store_string(L, value.index(), &action_, 0, "action");

View File

@@ -59,7 +59,7 @@ eng::string Gui::menu_debug_string() const {
LuaDefine(gui_menu_item, "action,label", "add a menu item to the current gui") {
Gui *gui = Gui::fetch_global_pointer(L);
LuaArg laction, llabel;
LuaOldStack LS(L, laction, llabel);
LuaDefStack LS(L, laction, llabel);
eng::string action = LS.ckstring(laction);
eng::string label = LS.ckstring(llabel);
if (!sv::has_prefix(action, "cb_")) {

View File

@@ -683,7 +683,7 @@ void HttpClientRequest::set_params(LuaCoreStack &LS0, LuaSlot tab) {
return;
}
LuaVar key, val;
LuaOldStack LS(LS0.state(), key, val);
LuaDefStack LS(LS0.state(), key, val);
LS.set(key, LuaNil);
while (LS.next(tab, key, val)) {
set_param(LS, key, val);
@@ -736,7 +736,7 @@ void HttpClientRequest::set_defaults() {
void HttpClientRequest::configure(LuaKeywordParser &kp) {
LuaVar val;
LuaOldStack LS(kp.state(), val);
LuaExtStack LS(kp.state(), val);
if (kp.parse(val, "method")) {
set_method(LS, val);
}
@@ -1032,7 +1032,7 @@ void HttpServerResponse::set_jsonvalue(LuaCoreStack &LS, LuaSlot val) {
void HttpServerResponse::configure(LuaKeywordParser &kp) {
LuaVar val;
LuaOldStack LS(kp.state(), val);
LuaExtStack LS(kp.state(), val);
if (kp.parse(val, "status")) {
set_status(LS, val);
}
@@ -1470,7 +1470,7 @@ bool HttpParser::parse_content(std::string_view &view, bool closed) {
void HttpParser::store(LuaCoreStack &LS0, LuaSlot tab) const {
LuaVar ptab, djson;
LuaOldStack LS(LS0.state(), ptab, djson);
LuaExtStack LS(LS0.state(), ptab, djson);
LS.newtable(tab);
if (!is_request_) {
@@ -1659,7 +1659,7 @@ void HttpClientRequestMap::deserialize(StreamBuffer *sb) {
LuaDefine(http_fixurl, "url", "validate URL and repair minor flaws in the URL syntax") {
LuaArg url;
LuaRet fixed;
LuaOldStack LS(L, url, fixed);
LuaDefStack LS(L, url, fixed);
ParsedURL parsed(LS.ckstring(url));
if (!parsed.valid) {
luaL_error(L, "invalid URL, not fixable");
@@ -1715,7 +1715,7 @@ LuaDefine(http_clientrequest, "request",
"|that would be sent.") {
LuaArg tab;
LuaRet str;
LuaOldStack LS(L, tab, str);
LuaDefStack LS(L, tab, str);
LuaKeywordParser kp(LS, tab);
HttpClientRequest req;
req.configure(kp);
@@ -1774,7 +1774,7 @@ LuaDefine(http_clientresponse, "response",
"|an actual HTTP response string. This is for debugging only.") {
LuaArg text;
LuaRet tab;
LuaOldStack LS(L, text, tab);
LuaDefStack LS(L, text, tab);
HttpParser parser;
parser.parse_response(LS.ckstring(text), true, "GET");
parser.store(LS, tab);
@@ -1829,7 +1829,7 @@ LuaDefine(http_serverrequest, "request",
"|an actual HTTP request string. This is for debugging only.") {
LuaArg text;
LuaRet tab;
LuaOldStack LS(L, text, tab);
LuaDefStack LS(L, text, tab);
HttpParser parser;
parser.parse_request(LS.ckstring(text), true);
parser.store(LS, tab);
@@ -1887,7 +1887,7 @@ LuaDefine(http_serverresponse, "response",
"|that would be sent.") {
LuaArg tab;
LuaRet str;
LuaOldStack LS(L, tab, str);
LuaDefStack LS(L, tab, str);
LuaKeywordParser kp(LS, tab);
HttpServerResponse resp;
resp.configure(kp);
@@ -1901,7 +1901,7 @@ LuaDefine(http_serverresponse, "response",
LuaDefine(http_validmime, "(mt)", "") {
LuaArg str;
LuaRet ok;
LuaOldStack LS(L, str, ok);
LuaDefStack LS(L, str, ok);
LS.set(ok, valid_mime_type(LS.ckstring(str)));
return LS.result();
}
@@ -1909,7 +1909,7 @@ LuaDefine(http_validmime, "(mt)", "") {
LuaDefine(http_statusstring, "(statuscode)", "Convert a 3-digit status code to a string") {
LuaArg code;
LuaRet str;
LuaOldStack LS(L, code, str);
LuaDefStack LS(L, code, str);
int icode = LS.ckint(code);
LS.set(str, status_code_to_string(icode));
return LS.result();
@@ -1918,7 +1918,7 @@ LuaDefine(http_statusstring, "(statuscode)", "Convert a 3-digit status code to a
LuaDefine(http_statuscode, "(statusstring)", "Convert a string to a 3-digit status code") {
LuaArg str;
LuaRet code;
LuaOldStack LS(L, code, str);
LuaDefStack LS(L, code, str);
eng::string sstr = LS.ckstring(str);
LS.set(code, status_code_from_string(sstr));
int iresult = LS.result();

View File

@@ -311,7 +311,7 @@ static bool decode_number(lua_State *L, std::string_view &v) {
// is OK.
if (sv::valid_number(n, true, true, false, false)) {
int64_t i = sv::to_int64(n);
if (!LuaOldStack::int64_storable(i)) return false;
if (!LuaCoreStack::int64_storable(i)) return false;
lua_pushnumber(L, double(i));
return true;
} else {
@@ -361,7 +361,7 @@ static bool decode_int_string(lua_State *L, std::string_view &v) {
// Make sure the number fits in a lua double,
// and push it on the stack.
int64_t i = sv::to_int64(n);
if (!LuaOldStack::int64_storable(i)) {
if (!LuaCoreStack::int64_storable(i)) {
return false;
}
lua_pushnumber(L, double(i));
@@ -661,7 +661,7 @@ LuaDefine(json_encode, "data, indent, maxlen",
"|") {
LuaArg data, indent, maxlen;
LuaRet encoded;
LuaOldStack LS(L, data, indent, maxlen, encoded);
LuaDefStack LS(L, data, indent, maxlen, encoded);
eng::string out;
eng::string error = json::encode(LS, data, out, LS.ckboolean(indent), LS.ckint(maxlen));
if (!error.empty()) {
@@ -695,7 +695,7 @@ LuaDefine(json_decode, "data",
"|") {
LuaArg encoded;
LuaRet data;
LuaOldStack LS(L, encoded, data);
LuaDefStack LS(L, encoded, data);
std::string_view v = LS.ckstringview(encoded);
bool ok = json::decode(LS, data, v);
if (!ok) {
@@ -707,7 +707,7 @@ LuaDefine(json_decode, "data",
// LuaDefine(base64_encode, "data", "") {
// LuaArg str;
// LuaRet ret;
// LuaOldStack LS(L, str, ret);
// LuaDefStack LS(L, str, ret);
// eng::string cstr = LS.ckstring(str);
// eng::ostringstream oss;
// util::base64_encode(cstr, &oss);
@@ -718,7 +718,7 @@ LuaDefine(json_decode, "data",
// LuaDefine(base64_decode, "data", "") {
// LuaArg str;
// LuaRet ret;
// LuaOldStack LS(L, str, ret);
// LuaDefStack LS(L, str, ret);
// eng::string cstr = LS.ckstring(str);
// eng::ostringstream oss;
// util::base64_decode(cstr, &oss);

View File

@@ -181,6 +181,7 @@ public:
friend class LuaCoreStack;
friend class LuaOldStack;
friend class LuaDefStack;
friend class LuaExtStack;
};
@@ -275,6 +276,8 @@ class LuaCoreStack : public eng::nevernew {
protected:
lua_State *L_;
LuaCoreStack(lua_State *L) : L_(L) {}
private:
// Push any value on the stack, by type.
void push_any_value(LuaNewTableMarker s) const { lua_newtable(L_); }
@@ -549,8 +552,7 @@ private:
public:
template<class... SS>
LuaOldStack(lua_State *L, SS & ... stackslots) {
L_ = L;
LuaOldStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
count_slots<0, 0, 0>(stackslots...);
if (lua_gettop(L) < narg_) {
luaL_error(L, "not enough arguments to function");
@@ -624,8 +626,7 @@ private:
public:
template<class... SS>
LuaDefStack(lua_State *L, SS & ... stackslots) {
L_ = L;
LuaDefStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
count_slots<0, 0, 0>(stackslots...);
if (lua_gettop(L_) != narg_) {
luaL_error(L_, "function expects exactly %d arguments", narg_);
@@ -642,11 +643,12 @@ public:
assign_slots(1, 1 + nret_, 1 + nret_ + narg_, stackslots...);
}
~LuaDefStack() {
if (!lua_isthrowing(L_)) {
int result() {
lua_settop(L_, nret_);
};
return nret_;
}
~LuaDefStack() { }
};
class LuaExtStack : public LuaCoreStack {
@@ -673,8 +675,7 @@ private:
public:
template<class... SS>
LuaExtStack(lua_State *L, SS & ... stackslots) {
L_ = L;
LuaExtStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
count_slots<0>(stackslots...);
lua_checkstack(L_, nvar_ + 20);
oldtop_ = lua_gettop(L_);
@@ -685,8 +686,7 @@ public:
}
template<class... SS>
LuaExtStack(const LuaCoreStack &LS0, SS & ... stackslots) {
L_ = LS0.state();
LuaExtStack(const LuaCoreStack &LS0, SS & ... stackslots) : LuaCoreStack(LS0.state()) {
count_slots<0>(stackslots...);
lua_checkstack(L_, nvar_ + 20);
oldtop_ = lua_gettop(L_);

View File

@@ -33,7 +33,7 @@ bool table_equal(LuaCoreStack &LS, LuaSlot t1, LuaSlot t2) {
LuaDefine(table_equal, "table1,table2", "return true if two tables contain the same keys and values") {
LuaArg t1, t2;
LuaRet eql;
LuaOldStack LS(L, t1, t2, eql);
LuaDefStack LS(L, t1, t2, eql);
LS.set(eql, table_equal(LS, t1, t2));
return LS.result();
}
@@ -121,7 +121,7 @@ LuaDefine(table_count, "table", "return the number of keys in table") {
LuaDefine(table_clear, "table,metaflag", "clear all keys, and optionally the metatable") {
LuaArg tab, clearmeta;
LuaVar metatable, metafield;
LuaOldStack LS(L, tab, clearmeta, metatable, metafield);
LuaDefStack LS(L, tab, clearmeta, metatable, metafield);
LS.checktable(tab, "table");
if (LS.ckboolean(clearmeta)) {
LS.getmetatable(metatable, tab);
@@ -142,7 +142,7 @@ LuaDefine(table_clear, "table,metaflag", "clear all keys, and optionally the met
LuaDefine(table_getflagbits, "table", "get the table's flag bits (debugging only)") {
LuaArg tab;
LuaRet bits;
LuaOldStack LS(L, tab, bits);
LuaDefStack LS(L, tab, bits);
uint16_t ubits = lua_getflagbits(L, tab.index());
LS.set(bits, ubits);
return LS.result();
@@ -150,7 +150,7 @@ LuaDefine(table_getflagbits, "table", "get the table's flag bits (debugging only
LuaDefine(table_setflagbits, "table,bits", "set the table's flag bits (debugging only)") {
LuaArg tab, bits;
LuaOldStack LS(L, tab, bits);
LuaDefStack LS(L, tab, bits);
uint16_t ubits = LS.ckinteger(bits);
lua_setflagbits(L, tab.index(), ubits);
return LS.result();
@@ -232,7 +232,7 @@ int deque_make_room(lua_State *L, int deque, int left, int fill, int max) {
LuaDefine(deque_create, "", "create a deque") {
LuaRet rdeque;
LuaVar classobj;
LuaOldStack LS(L, rdeque, classobj);
LuaDefStack LS(L, rdeque, classobj);
const int imax = 4;
eng::string err = LS.getclass(classobj, "deque");
if (err != "") {
@@ -251,7 +251,7 @@ LuaDefine(deque_create, "", "create a deque") {
LuaDefine(deque_pushl, "deque,value", "push onto the left end of a deque") {
LuaArg deque, elt;
LuaOldStack LS(L, deque, elt);
LuaDefStack LS(L, deque, elt);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
max = deque_make_room(L, deque.index(), left, fill, max);
@@ -265,7 +265,7 @@ LuaDefine(deque_pushl, "deque,value", "push onto the left end of a deque") {
LuaDefine(deque_pushr, "deque,value", "push onto the right end of a deque") {
LuaArg deque, elt;
LuaOldStack LS(L, deque, elt);
LuaDefStack LS(L, deque, elt);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
max = deque_make_room(L, deque.index(), left, fill, max);
@@ -279,7 +279,7 @@ LuaDefine(deque_pushr, "deque,value", "push onto the right end of a deque") {
LuaDefine(deque_popl, "deque", "pop the left end of a deque") {
LuaArg deque;
LuaRet result;
LuaOldStack LS(L, deque, result);
LuaDefStack LS(L, deque, result);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
if (fill == 0) {
@@ -297,7 +297,7 @@ LuaDefine(deque_popl, "deque", "pop the left end of a deque") {
LuaDefine(deque_popr, "deque", "pop the right end of a deque") {
LuaArg deque;
LuaRet result;
LuaOldStack LS(L, deque, result);
LuaDefStack LS(L, deque, result);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
if (fill == 0) {
@@ -315,7 +315,7 @@ LuaDefine(deque_popr, "deque", "pop the right end of a deque") {
LuaDefine(deque_nthl, "deque,n", "return the nth item from the left end of a deque") {
LuaArg deque, nn;
LuaRet result;
LuaOldStack LS(L, deque, nn, result);
LuaDefStack LS(L, deque, nn, result);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
int n = LS.ckint(nn);
@@ -331,7 +331,7 @@ LuaDefine(deque_nthl, "deque,n", "return the nth item from the left end of a deq
LuaDefine(deque_nthr, "deque,n", "return the nth item from the right end of a deque") {
LuaArg deque, nn;
LuaRet result;
LuaOldStack LS(L, deque, nn, result);
LuaDefStack LS(L, deque, nn, result);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
int n = LS.ckint(nn);
@@ -346,7 +346,7 @@ LuaDefine(deque_nthr, "deque,n", "return the nth item from the right end of a de
LuaDefine(deque_setl, "deque,n,value", "set the nth item from the left end of a deque") {
LuaArg deque, nn, val;
LuaOldStack LS(L, deque, nn, val);
LuaDefStack LS(L, deque, nn, val);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
int n = LS.ckint(nn);
@@ -361,7 +361,7 @@ LuaDefine(deque_setl, "deque,n,value", "set the nth item from the left end of a
LuaDefine(deque_setr, "deque,n,value", "set the nth item from the right end of a deque") {
LuaArg deque, nn, val;
LuaOldStack LS(L, deque, nn, val);
LuaDefStack LS(L, deque, nn, val);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
int n = LS.ckint(nn);
@@ -378,7 +378,7 @@ LuaDefine(deque_findl, "deque,value", "find the first occurence of value in dequ
LuaArg deque, val;
LuaRet pos;
LuaVar check;
LuaOldStack LS(L, deque, val, pos, check);
LuaDefStack LS(L, deque, val, pos, check);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
for (int i = 0; i < fill; i++) {
@@ -397,7 +397,7 @@ LuaDefine(deque_findr, "deque,value", "find the first occurrence of value in deq
LuaArg deque, val;
LuaRet pos;
LuaVar check;
LuaOldStack LS(L, deque, val, pos, check);
LuaDefStack LS(L, deque, val, pos, check);
int left, fill, max;
deque_get_info(L, deque.index(), &left, &fill, &max);
int base = left + fill - 1;
@@ -416,7 +416,7 @@ LuaDefine(deque_findr, "deque,value", "find the first occurrence of value in deq
LuaDefine(deque_size, "deque", "return the number of items in the deque") {
LuaArg deque;
LuaRet size;
LuaOldStack LS(L, deque, size);
LuaDefStack LS(L, deque, size);
LS.checktable(deque, "deque");
LS.rawget(size, deque, DEQUE_FILL);
LS.checknumber(size, "deque size");
@@ -537,7 +537,7 @@ static void auxsort (lua_State *L, int tab, int l, int u) {
bool table_getpairs(LuaCoreStack &LS0, LuaSlot tab, LuaSlot pairs, bool sort) {
lua_State *L = LS0.state();
LuaVar key, value;
LuaOldStack LS(L, key, value);
LuaExtStack LS(L, key, value);
bool sorted = true;
// Create the table, store the initial 1.
int total = lua_nkeys(L, tab.index());
@@ -558,7 +558,6 @@ bool table_getpairs(LuaCoreStack &LS0, LuaSlot tab, LuaSlot pairs, bool sort) {
if (sort) {
auxsort(L, pairs.index(), 1, total);
}
LS.result();
return sorted;
}
@@ -602,7 +601,7 @@ LuaDefine(table_sortedpairs, "table",
"|") {
LuaArg tab;
LuaRet closure, rtab, key;
LuaOldStack LS(L, tab, closure, rtab, key);
LuaDefStack LS(L, tab, closure, rtab, key);
bool sorted = table_getpairs(LS, tab, rtab, true);
if (!sorted) {
luaL_error(L, "Cannot sort the table keys");
@@ -625,7 +624,7 @@ LuaDefine(table_semisortedpairs, "table",
"|") {
LuaArg tab;
LuaRet closure, rtab, key;
LuaOldStack LS(L, tab, closure, rtab, key);
LuaDefStack LS(L, tab, closure, rtab, key);
table_getpairs(LS, tab, rtab, true);
LS.set(closure, lfn_table_nextsortedpair);
LS.set(key, LuaNil);
@@ -665,7 +664,7 @@ LuaDefine(genlt, "obj1,obj2",
"|") {
LuaArg o1,o2;
LuaRet lt;
LuaOldStack LS(L, o1, o2, lt);
LuaDefStack LS(L, o1, o2, lt);
int ltf = lua_genlt(L, o1.index(), o2.index());
LS.set(lt, ltf ? true:false);
return LS.result();