Finally phase out LuaOldStack entirely.

This commit is contained in:
2023-04-14 17:50:06 -04:00
parent 2c2dabc127
commit 669a5a50ab
2 changed files with 143 additions and 285 deletions

View File

@@ -172,7 +172,6 @@ public:
} }
friend class LuaCoreStack; friend class LuaCoreStack;
friend class LuaOldStack;
friend class LuaDefStack; friend class LuaDefStack;
friend class LuaExtStack; friend class LuaExtStack;
}; };
@@ -529,125 +528,15 @@ public:
static bool int64_storable(int64_t v) { return (v <= MAXINT) && (v >= -MAXINT); } static bool int64_storable(int64_t v) { return (v <= MAXINT) && (v >= -MAXINT); }
}; };
////////////////////////////////////////////////////////////////////
//
// LuaOldStack
//
// This class is deprecated, we're phasing it out. This was the first piece of
// code we wrote to allocate stack slots, and it has issues. It is
// still in use in the difference transmitter and the source database,
// and a few other small spots.
//
////////////////////////////////////////////////////////////////////
class LuaOldStack : public LuaCoreStack {
private:
int narg_;
int ngap_;
int nvar_;
int nret_;
int argpos_;
int gappos_;
int varpos_;
int retpos_;
int rettop_;
int finaltop_;
private:
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaArg &v, SS & ... stackslots) {
v.index_ = argp;
assign_slots(argp + 1, varp, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaVar &v, SS & ... stackslots) {
v.index_ = varp;
assign_slots(argp, varp+1, retp, stackslots...);
}
template<class... SS>
void assign_slots(int argp, int varp, int retp, LuaRet &v, SS & ... stackslots) {
v.index_ = retp;
assign_slots(argp, varp, retp+1, stackslots...);
}
void assign_slots(int argp, int varp, int retp) {}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slotsx(LuaArg &v, SS & ... stackslots)
{
count_slotsx<NARG+1, NVAR, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slotsx(LuaVar &v, SS & ... stackslots)
{
count_slotsx<NARG, NVAR+1, NRET>(stackslots...);
}
template<int NARG, int NVAR, int NRET, class... SS>
void count_slotsx(LuaRet &v, SS & ... stackslots)
{
count_slotsx<NARG, NVAR, NRET+1>(stackslots...);
}
template<int NARG, int NVAR, int NRET>
void count_slotsx() {
narg_ = NARG;
nret_ = NRET;
nvar_ = NVAR;
ngap_ = NRET - NVAR - NARG;
if (ngap_ < 0) ngap_ = 0;
int argtop = lua_gettop(L_);
argpos_ = argtop + 1 - NARG;
gappos_ = argpos_ + NARG;
varpos_ = gappos_ + ngap_;
retpos_ = varpos_ + NVAR;
rettop_ = retpos_ + NRET - 1;
finaltop_ = argpos_ + NRET - 1;
}
public:
template<class... SS>
LuaOldStack(lua_State *L, SS & ... stackslots) : LuaCoreStack(L) {
count_slotsx<0, 0, 0>(stackslots...);
if (lua_gettop(L) < narg_) {
luaL_error(L, "not enough arguments to function");
}
assign_slots(argpos_, varpos_, retpos_, stackslots...);
lua_settop(L_, varpos_ - 1);
for (int i = 0; i < nvar_ + nret_; i++) {
lua_pushnil(L_);
}
}
int result() {
lua_settop(L_, rettop_);
int i = finaltop_;
for (int j = 0; j < nret_; j++) {
lua_replace(L_, i);
i -= 1;
}
lua_settop(L_, finaltop_);
return nret_;
}
~LuaOldStack() {};
};
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// //
// LuaDefStack // LuaDefStack
// //
// This version of LuaStack is meant to be used at the top level of a LuaDefine. // This version of LuaStack should only be used inside a LuaDefine. It can
// It can assign stack slots to LuaArg, LuaRet, and LuaVar locals. It arranges // assign stack slots to LuaArg, LuaRet, LuaVar, and LuaExtraArgs. It
// for the arguments to be in the LuaArg variables, and it arranges for the // arranges for the arguments to be in the LuaArg variables, and it arranges for
// LuaRet variables to be returned. It also makes sure that the function has // the LuaRet variables to be returned. It also makes sure that the function
// the correct number of arguments. // has the correct number of arguments.
// //
// At the end of the LuaDefine function, you're supposed to return LS.result(). // At the end of the LuaDefine function, you're supposed to return LS.result().
// LS.result causes the allocated stack slots to be freed except for the LuaRet // LS.result causes the allocated stack slots to be freed except for the LuaRet
@@ -659,11 +548,11 @@ public:
// The lua interpreter will clean up after an error or yield. // The lua interpreter will clean up after an error or yield.
// //
// Implementation note: LuaDefStack doesn't have a destructor to deallocate // Implementation note: LuaDefStack doesn't have a destructor to deallocate
// stack slots. That's deliberate: you shouldn't expect this class to clean // stack slots. That's deliberate: you shouldn't expect this class to clean up
// up its stack frame, because after all, it has to leave return values on // its stack frame, because after all, it has to leave return values on the
// the stack. It would be deceptive to put a destructor, which then doesn't // stack. It would be deceptive to put a destructor, which then doesn't
// actually clean up anyway. Better to just let it be known that this // actually clean up anyway. Better to just let it be known that this class
// class doesn't clean up its stack frame. // doesn't clean up its stack frame.
// //
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
@@ -742,7 +631,6 @@ public:
// //
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
class LuaExtStack : public LuaCoreStack { class LuaExtStack : public LuaCoreStack {
private: private:
int oldtop_; int oldtop_;

View File

@@ -248,7 +248,7 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
} }
LuaVar closure; LuaVar closure;
LuaOldStack LS(L, closure); LuaExtStack LS(L, closure);
// create the compiled closure. // create the compiled closure.
int status = luaL_loadbuffer(L, lua.c_str(), lua.size(), "=probe"); int status = luaL_loadbuffer(L, lua.c_str(), lua.size(), "=probe");
@@ -257,7 +257,6 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
// The closure is actually an error message. Do nothing. // The closure is actually an error message. Do nothing.
// This should normally not happen: LuaConsole should filter // This should normally not happen: LuaConsole should filter
// out syntax errors. // out syntax errors.
LS.result();
return ""; return "";
} }
@@ -287,10 +286,6 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) {
lthread_prints_.reset(); lthread_prints_.reset();
close_lthread_state(); close_lthread_state();
// And we're done.
LS.result();
assert(stack_is_clear());
return result; return result;
} }
@@ -300,31 +295,27 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
lua_State *L = state(); lua_State *L = state();
LuaVar actor, place, ugui, func, tangibles, mt, index; LuaVar actor, place, ugui, func, tangibles, mt, index;
LuaOldStack LS(L, actor, place, ugui, func, tangibles, mt, index); LuaExtStack LS(L, actor, place, ugui, func, tangibles, mt, index);
// Get the actor and place. // Get the actor and place.
LS.rawget(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(actor, tangibles, actor_id); LS.rawget(actor, tangibles, actor_id);
LS.rawget(place, tangibles, place_id); LS.rawget(place, tangibles, place_id);
if (!LS.istable(actor) || !LS.istable(place)) { if (!LS.istable(actor) || !LS.istable(place)) {
LS.result();
return; return;
} }
// Get the interface closure. // Get the interface closure.
LS.getmetatable(mt, place); LS.getmetatable(mt, place);
if (!LS.istable(mt)) { if (!LS.istable(mt)) {
LS.result();
return; return;
} }
LS.rawget(index, mt, "__index"); LS.rawget(index, mt, "__index");
if (!LS.istable(index)) { if (!LS.istable(index)) {
LS.result();
return; return;
} }
LS.rawget(func, index, "interface"); LS.rawget(func, index, "interface");
if (!LS.isfunction(func)) { if (!LS.isfunction(func)) {
LS.result();
return; return;
} }
@@ -340,13 +331,8 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
if (!msg.empty()) { if (!msg.empty()) {
gui->clear(0); gui->clear(0);
util::dprint(msg); util::dprint(msg);
LS.result();
return; return;
} }
// And we're done.
LS.result();
assert(stack_is_clear());
} }
void World::update_source(const util::LuaSourcePtr &source) { void World::update_source(const util::LuaSourcePtr &source) {
@@ -398,42 +384,41 @@ void World::http_response(const HttpParser &response) {
HttpClientRequest request = iter->second; HttpClientRequest request = iter->second;
http_requests_.erase(iter); http_requests_.erase(iter);
// Get the place and thread as lua objects. lua_State *CO;
LuaVar tangibles, place, mt, threads, thinfo, thread; {
LuaOldStack LS(state(), tangibles, place, mt, threads, thinfo, thread); // Get the place and thread as lua objects.
LS.rawget(tangibles, LuaRegistry, "tangibles"); LuaVar tangibles, place, mt, threads, thinfo, thread;
LS.rawget(place, tangibles, request.place_id()); LuaExtStack LS(state(), tangibles, place, mt, threads, thinfo, thread);
if (!LS.istable(place)) { LS.rawget(tangibles, LuaRegistry, "tangibles");
return; LS.rawget(place, tangibles, request.place_id());
if (!LS.istable(place)) {
return;
}
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
return;
}
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
return;
}
LS.rawget(thinfo, threads, request.thread_id());
if (!LS.istable(thinfo)) {
return;
}
LS.rawget(thread, thinfo, "thread");
if (!LS.isthread(thread)) {
return;
}
CO = LS.ckthread(thread);
} }
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
return;
}
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
return;
}
LS.rawget(thinfo, threads, request.thread_id());
if (!LS.istable(thinfo)) {
return;
}
LS.rawget(thread, thinfo, "thread");
if (!LS.isthread(thread)) {
return;
}
lua_State *CO = LS.ckthread(thread);
// Push the response onto the awakening thread. // Push the response onto the awakening thread.
LuaRet responsetable; lua_pushnil(CO);
LuaOldStack LSCO(CO, responsetable); LuaSpecial responsetable(lua_gettop(CO));
LuaCoreStack LSCO(CO);
response.store(LSCO, responsetable); response.store(LSCO, responsetable);
// Clean up lua stacks.
LSCO.result();
LS.result();
assert(stack_is_clear());
// Awaken the thread, with its new return value. // Awaken the thread, with its new return value.
schedule(0, request.thread_id(), request.place_id()); schedule(0, request.thread_id(), request.place_id());
run_scheduled_threads(); run_scheduled_threads();
@@ -481,14 +466,13 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
lua_State *L = state(); lua_State *L = state();
LuaVar www, func, reqtab; LuaVar www, func, reqtab;
LuaOldStack LS(L, www, func, reqtab); LuaExtStack LS(L, www, func, reqtab);
// Get the www class. If there's no such class, // Get the www class. If there's no such class,
// return a 503 Service Unavailable to the client. // return a 503 Service Unavailable to the client.
eng::string err = LS.getclass(www, "www"); eng::string err = LS.getclass(www, "www");
if (!err.empty()) { if (!err.empty()) {
response.fail(503, "class www doesn't exist"); response.fail(503, "class www doesn't exist");
LS.result();
return response; return response;
} }
@@ -497,7 +481,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
LS.rawget(func, www, fn); LS.rawget(func, www, fn);
if (!LS.isfunction(func)) { if (!LS.isfunction(func)) {
response.fail(404, util::ss("no such function: www.", fn)); response.fail(404, util::ss("no such function: www.", fn));
LS.result();
return response; return response;
} }
@@ -517,7 +500,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
// a 500 Internal Server Error to the client. // a 500 Internal Server Error to the client.
if (!msg.empty()) { if (!msg.empty()) {
response.fail(500, msg); response.fail(500, msg);
LS.result();
return response; return response;
} }
@@ -526,7 +508,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
int newtop = lua_gettop(L); int newtop = lua_gettop(L);
if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) { if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) {
response.fail(500, util::ss("lua function www.", fn, " didn't return a table")); response.fail(500, util::ss("lua function www.", fn, " didn't return a table"));
LS.result();
return response; return response;
} }
@@ -538,7 +519,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) {
if (!kperr.empty()) { if (!kperr.empty()) {
response.fail(500, kperr); response.fail(500, kperr);
} }
LS.result();
return response; return response;
} }
@@ -609,58 +589,55 @@ void World::invoke_lua(int64_t actor_id, int64_t place_id, const eng::string &ac
int64_t tid = tplace->id_player_pool_.get_one(); int64_t tid = tplace->id_player_pool_.get_one();
// Set up for lua manipulation. // Set up for lua manipulation.
lua_State *L = state(); {
LuaVar func, tangibles, place, mt, thread, thinfo, threads; lua_State *L = state();
LuaOldStack LS(L, func, tangibles, place, mt, thread, thinfo, threads); LuaVar func, tangibles, place, mt, thread, thinfo, threads;
LuaExtStack LS(L, func, tangibles, place, mt, thread, thinfo, threads);
// create the compiled closure. // create the compiled closure.
int status = luaL_loadbuffer(L, action.c_str(), action.size(), "=invoke"); int status = luaL_loadbuffer(L, action.c_str(), action.size(), "=invoke");
lua_replace(L, func.index()); lua_replace(L, func.index());
if (status != LUA_OK) { if (status != LUA_OK) {
// The closure is actually an error message. Do nothing. // The closure is actually an error message. Do nothing.
// This should normally not happen: LuaConsole should filter // This should normally not happen: LuaConsole should filter
// out syntax errors. // out syntax errors.
LS.result(); return;
return; }
// Get the place.
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, place_id);
if (!LS.istable(place)) {
return;
}
// Get the place's metatable.
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
return;
}
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_xmove(L, CO, 1);
// Create the thread info table.
LS.newtable(thinfo);
LS.rawset(thinfo, "thread", thread);
LS.rawset(thinfo, "actorid", actor_id);
LS.rawset(thinfo, "isnew", true);
LS.rawset(thinfo, "useppool", true);
LS.rawset(thinfo, "print", true);
// Store the thread into place's thread table.
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
return;
}
LS.rawset(threads, tid, thinfo);
} }
// Get the place.
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, place_id);
if (!LS.istable(place)) {
LS.result();
return;
}
// Get the place's metatable.
LS.getmetatable(mt, place);
if (!LS.istable(mt)) {
LS.result();
return;
}
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_xmove(L, CO, 1);
// Create the thread info table.
LS.newtable(thinfo);
LS.rawset(thinfo, "thread", thread);
LS.rawset(thinfo, "actorid", actor_id);
LS.rawset(thinfo, "isnew", true);
LS.rawset(thinfo, "useppool", true);
LS.rawset(thinfo, "print", true);
// Store the thread into place's thread table.
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
LS.result();
return;
}
LS.rawset(threads, tid, thinfo);
LS.result();
schedule(0, tid, place_id); schedule(0, tid, place_id);
run_scheduled_threads(); run_scheduled_threads();
assert(stack_is_clear()); assert(stack_is_clear());
@@ -692,68 +669,64 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const eng::string &a
// pool in this case. // pool in this case.
int64_t tid = tactor->id_player_pool_.get_one(); int64_t tid = tactor->id_player_pool_.get_one();
// Set up for Lua manipulation. {
lua_State *L = state(); // Set up for Lua manipulation.
LuaVar actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata; lua_State *L = state();
LuaOldStack LS(L, actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata); LuaVar actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata;
LuaExtStack LS(L, actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata);
// Get the actor and place. // Get the actor and place.
LS.rawget(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(actor, tangibles, actor_id); LS.rawget(actor, tangibles, actor_id);
LS.rawget(place, tangibles, place_id); LS.rawget(place, tangibles, place_id);
if (!LS.istable(actor) || !LS.istable(place)) { if (!LS.istable(actor) || !LS.istable(place)) {
LS.result(); return;
return; }
}
// Get the action closure. // Get the action closure.
LS.getmetatable(mt, place); LS.getmetatable(mt, place);
if (!LS.istable(mt)) { if (!LS.istable(mt)) {
LS.result(); return;
return; }
} LS.rawget(index, mt, "__index");
LS.rawget(index, mt, "__index"); if (!LS.istable(index)) {
if (!LS.istable(index)) { return;
LS.result(); }
return; LS.rawget(func, index, action);
} if (!LS.isfunction(func)) {
LS.rawget(func, index, action); return;
if (!LS.isfunction(func)) { }
LS.result();
return;
}
// Convert the InvocationData into a lua table. // Convert the InvocationData into a lua table.
LS.newtable(invdata); LS.newtable(invdata);
for (const auto &p : data) { for (const auto &p : data) {
LS.rawset(invdata, p.first, p.second); LS.rawset(invdata, p.first, p.second);
}
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
lua_pushvalue(L, invdata.index());
lua_xmove(L, CO, 4);
// Create the thread info table.
LS.newtable(thinfo);
LS.rawset(thinfo, "thread", thread);
LS.rawset(thinfo, "actorid", actor_id);
LS.rawset(thinfo, "isnew", true);
LS.rawset(thinfo, "useppool", true);
LS.rawset(thinfo, "print", false);
// Store the thread into place's thread table.
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
return;
}
LS.rawset(threads, tid, thinfo);
} }
// Create a new thread, set up function and parameters.
lua_State *CO = LS.newthread(thread);
lua_pushvalue(L, func.index());
lua_pushvalue(L, actor.index());
lua_pushvalue(L, place.index());
lua_pushvalue(L, invdata.index());
lua_xmove(L, CO, 4);
// Create the thread info table.
LS.newtable(thinfo);
LS.rawset(thinfo, "thread", thread);
LS.rawset(thinfo, "actorid", actor_id);
LS.rawset(thinfo, "isnew", true);
LS.rawset(thinfo, "useppool", true);
LS.rawset(thinfo, "print", false);
// Store the thread into place's thread table.
LS.rawget(threads, mt, "threads");
if (!LS.istable(threads)) {
LS.result();
return;
}
LS.rawset(threads, tid, thinfo);
LS.result();
// Push the thread's ID into the runnable thread queue, // Push the thread's ID into the runnable thread queue,
// then run the thread queue. // then run the thread queue.
schedule(0, tid, place_id); schedule(0, tid, place_id);
@@ -800,7 +773,7 @@ void World::guard_blockable(lua_State *L, const char *fn) {
void World::guard_nopredict(lua_State *L, const char *fn) { void World::guard_nopredict(lua_State *L, const char *fn) {
// Caution: this code must be equivalent to the // Caution: this code must be equivalent to the
// code in LuaOldStack::guard_nopredict. // code in LuaCoreStack::guard_nopredict.
if (lthread_thread_id_ == 0) { if (lthread_thread_id_ == 0) {
return; return;
} }
@@ -821,7 +794,7 @@ void World::run_scheduled_threads() {
assert(stack_is_clear()); assert(stack_is_clear());
lua_State *L = state(); lua_State *L = state();
LuaVar tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print; LuaVar tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print;
LuaOldStack LS(L, tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print); LuaExtStack LS(L, tangibles, place, mt, threads, thinfo, actorid, isnew, useppool, thread, print);
LS.rawget(tangibles, LuaRegistry, "tangibles"); LS.rawget(tangibles, LuaRegistry, "tangibles");
while (thread_sched_.ready(clock_)) { while (thread_sched_.ready(clock_)) {
@@ -871,7 +844,7 @@ void World::run_scheduled_threads() {
// Remove from thread table. // Remove from thread table.
LS.rawget(print, thinfo, "print"); LS.rawget(print, thinfo, "print");
LS.rawset(threads, sched.thread_id(), LuaNil); LS.rawset(threads, sched.thread_id(), LuaNil);
LuaOldStack LSCO(CO); LuaCoreStack LSCO(CO);
if (LS.ckboolean(print)) { if (LS.ckboolean(print)) {
for (int i = 1; i <= lua_gettop(CO); i++) { for (int i = 1; i <= lua_gettop(CO); i++) {
pprint(LSCO, LuaSpecial(i), PrettyPrintOptions(), ostream); pprint(LSCO, LuaSpecial(i), PrettyPrintOptions(), ostream);
@@ -900,9 +873,6 @@ void World::run_scheduled_threads() {
} }
close_lthread_state(); close_lthread_state();
} }
LS.result();
assert(stack_is_clear());
} }
int64_t World::alloc_id_predictable() { int64_t World::alloc_id_predictable() {