diff --git a/luprex/cpp/core/luastack.hpp b/luprex/cpp/core/luastack.hpp index 53ac2555..f6c2fd70 100644 --- a/luprex/cpp/core/luastack.hpp +++ b/luprex/cpp/core/luastack.hpp @@ -172,7 +172,6 @@ public: } friend class LuaCoreStack; - friend class LuaOldStack; friend class LuaDefStack; friend class LuaExtStack; }; @@ -529,125 +528,15 @@ public: 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 - void assign_slots(int argp, int varp, int retp, LuaArg &v, SS & ... stackslots) { - v.index_ = argp; - assign_slots(argp + 1, varp, retp, stackslots...); - } - - template - void assign_slots(int argp, int varp, int retp, LuaVar &v, SS & ... stackslots) { - v.index_ = varp; - assign_slots(argp, varp+1, retp, stackslots...); - } - - template - 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 - void count_slotsx(LuaArg &v, SS & ... stackslots) - { - count_slotsx(stackslots...); - } - template - void count_slotsx(LuaVar &v, SS & ... stackslots) - { - count_slotsx(stackslots...); - } - template - void count_slotsx(LuaRet &v, SS & ... stackslots) - { - count_slotsx(stackslots...); - } - - template - 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 - 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 // -// This version of LuaStack is meant to be used at the top level of a LuaDefine. -// It can assign stack slots to LuaArg, LuaRet, and LuaVar locals. It arranges -// for the arguments to be in the LuaArg variables, and it arranges for the -// LuaRet variables to be returned. It also makes sure that the function has -// the correct number of arguments. +// This version of LuaStack should only be used inside a LuaDefine. It can +// assign stack slots to LuaArg, LuaRet, LuaVar, and LuaExtraArgs. It +// arranges for the arguments to be in the LuaArg variables, and it arranges for +// the LuaRet variables to be returned. It also makes sure that the function +// has the correct number of arguments. // // 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 @@ -659,11 +548,11 @@ public: // The lua interpreter will clean up after an error or yield. // // Implementation note: LuaDefStack doesn't have a destructor to deallocate -// stack slots. That's deliberate: you shouldn't expect this class to clean -// up its stack frame, because after all, it has to leave return values on -// the 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 -// class doesn't clean up its stack frame. +// stack slots. That's deliberate: you shouldn't expect this class to clean up +// its stack frame, because after all, it has to leave return values on the +// 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 class +// doesn't clean up its stack frame. // //////////////////////////////////////////////////////////////////// @@ -742,7 +631,6 @@ public: // //////////////////////////////////////////////////////////////////// - class LuaExtStack : public LuaCoreStack { private: int oldtop_; diff --git a/luprex/cpp/core/world-core.cpp b/luprex/cpp/core/world-core.cpp index 403282a6..8c30c5f0 100644 --- a/luprex/cpp/core/world-core.cpp +++ b/luprex/cpp/core/world-core.cpp @@ -248,7 +248,7 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) { } LuaVar closure; - LuaOldStack LS(L, closure); + LuaExtStack LS(L, closure); // create the compiled closure. 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. // This should normally not happen: LuaConsole should filter // out syntax errors. - LS.result(); return ""; } @@ -287,10 +286,6 @@ eng::string World::probe_lua(int64_t actor_id, const eng::string &lua) { lthread_prints_.reset(); close_lthread_state(); - - // And we're done. - LS.result(); - assert(stack_is_clear()); return result; } @@ -300,31 +295,27 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) { lua_State *L = state(); 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. LS.rawget(tangibles, LuaRegistry, "tangibles"); LS.rawget(actor, tangibles, actor_id); LS.rawget(place, tangibles, place_id); if (!LS.istable(actor) || !LS.istable(place)) { - LS.result(); return; } // Get the interface closure. LS.getmetatable(mt, place); if (!LS.istable(mt)) { - LS.result(); return; } LS.rawget(index, mt, "__index"); if (!LS.istable(index)) { - LS.result(); return; } LS.rawget(func, index, "interface"); if (!LS.isfunction(func)) { - LS.result(); return; } @@ -340,13 +331,8 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) { if (!msg.empty()) { gui->clear(0); util::dprint(msg); - LS.result(); return; } - - // And we're done. - LS.result(); - assert(stack_is_clear()); } void World::update_source(const util::LuaSourcePtr &source) { @@ -398,42 +384,41 @@ void World::http_response(const HttpParser &response) { HttpClientRequest request = iter->second; http_requests_.erase(iter); - // Get the place and thread as lua objects. - LuaVar tangibles, place, mt, threads, thinfo, thread; - LuaOldStack LS(state(), tangibles, place, mt, threads, thinfo, thread); - LS.rawget(tangibles, LuaRegistry, "tangibles"); - LS.rawget(place, tangibles, request.place_id()); - if (!LS.istable(place)) { - return; + lua_State *CO; + { + // Get the place and thread as lua objects. + LuaVar tangibles, place, mt, threads, thinfo, thread; + LuaExtStack LS(state(), tangibles, place, mt, threads, thinfo, thread); + LS.rawget(tangibles, LuaRegistry, "tangibles"); + 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. - LuaRet responsetable; - LuaOldStack LSCO(CO, responsetable); + lua_pushnil(CO); + LuaSpecial responsetable(lua_gettop(CO)); + LuaCoreStack LSCO(CO); response.store(LSCO, responsetable); - // Clean up lua stacks. - LSCO.result(); - LS.result(); - assert(stack_is_clear()); - // Awaken the thread, with its new return value. schedule(0, request.thread_id(), request.place_id()); run_scheduled_threads(); @@ -481,14 +466,13 @@ HttpServerResponse World::http_serve(const HttpParser &request) { lua_State *L = state(); 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, // return a 503 Service Unavailable to the client. eng::string err = LS.getclass(www, "www"); if (!err.empty()) { response.fail(503, "class www doesn't exist"); - LS.result(); return response; } @@ -497,7 +481,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) { LS.rawget(func, www, fn); if (!LS.isfunction(func)) { response.fail(404, util::ss("no such function: www.", fn)); - LS.result(); return response; } @@ -517,7 +500,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) { // a 500 Internal Server Error to the client. if (!msg.empty()) { response.fail(500, msg); - LS.result(); return response; } @@ -526,7 +508,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) { int newtop = lua_gettop(L); if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) { response.fail(500, util::ss("lua function www.", fn, " didn't return a table")); - LS.result(); return response; } @@ -538,7 +519,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) { if (!kperr.empty()) { response.fail(500, kperr); } - LS.result(); 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(); // Set up for lua manipulation. - lua_State *L = state(); - LuaVar func, tangibles, place, mt, thread, thinfo, threads; - LuaOldStack LS(L, func, tangibles, place, mt, thread, thinfo, threads); + { + lua_State *L = state(); + LuaVar func, tangibles, place, mt, thread, thinfo, threads; + LuaExtStack LS(L, func, tangibles, place, mt, thread, thinfo, threads); - // create the compiled closure. - int status = luaL_loadbuffer(L, action.c_str(), action.size(), "=invoke"); - lua_replace(L, func.index()); - if (status != LUA_OK) { - // The closure is actually an error message. Do nothing. - // This should normally not happen: LuaConsole should filter - // out syntax errors. - LS.result(); - return; + // create the compiled closure. + int status = luaL_loadbuffer(L, action.c_str(), action.size(), "=invoke"); + lua_replace(L, func.index()); + if (status != LUA_OK) { + // The closure is actually an error message. Do nothing. + // This should normally not happen: LuaConsole should filter + // out syntax errors. + 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); run_scheduled_threads(); assert(stack_is_clear()); @@ -692,67 +669,63 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const eng::string &a // pool in this case. int64_t tid = tactor->id_player_pool_.get_one(); - // Set up for Lua manipulation. - lua_State *L = state(); - LuaVar actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata; - LuaOldStack LS(L, actor, place, func, tangibles, mt, index, thread, threads, thinfo, message, invdata); + { + // Set up for Lua manipulation. + lua_State *L = state(); + 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. - LS.rawget(tangibles, LuaRegistry, "tangibles"); - LS.rawget(actor, tangibles, actor_id); - LS.rawget(place, tangibles, place_id); - if (!LS.istable(actor) || !LS.istable(place)) { - LS.result(); - return; - } + // Get the actor and place. + LS.rawget(tangibles, LuaRegistry, "tangibles"); + LS.rawget(actor, tangibles, actor_id); + LS.rawget(place, tangibles, place_id); + if (!LS.istable(actor) || !LS.istable(place)) { + return; + } - // Get the action closure. - LS.getmetatable(mt, place); - if (!LS.istable(mt)) { - LS.result(); - return; - } - LS.rawget(index, mt, "__index"); - if (!LS.istable(index)) { - LS.result(); - return; - } - LS.rawget(func, index, action); - if (!LS.isfunction(func)) { - LS.result(); - return; - } + // Get the action closure. + LS.getmetatable(mt, place); + if (!LS.istable(mt)) { + return; + } + LS.rawget(index, mt, "__index"); + if (!LS.istable(index)) { + return; + } + LS.rawget(func, index, action); + if (!LS.isfunction(func)) { + return; + } - // Convert the InvocationData into a lua table. - LS.newtable(invdata); - for (const auto &p : data) { - LS.rawset(invdata, p.first, p.second); - } + // Convert the InvocationData into a lua table. + LS.newtable(invdata); + for (const auto &p : data) { + 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 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; + // 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); } - LS.rawset(threads, tid, thinfo); - LS.result(); // Push the thread's ID into the runnable thread queue, // then run the thread queue. @@ -800,7 +773,7 @@ void World::guard_blockable(lua_State *L, const char *fn) { void World::guard_nopredict(lua_State *L, const char *fn) { // Caution: this code must be equivalent to the - // code in LuaOldStack::guard_nopredict. + // code in LuaCoreStack::guard_nopredict. if (lthread_thread_id_ == 0) { return; } @@ -821,7 +794,7 @@ void World::run_scheduled_threads() { assert(stack_is_clear()); lua_State *L = state(); 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"); while (thread_sched_.ready(clock_)) { @@ -871,7 +844,7 @@ void World::run_scheduled_threads() { // Remove from thread table. LS.rawget(print, thinfo, "print"); LS.rawset(threads, sched.thread_id(), LuaNil); - LuaOldStack LSCO(CO); + LuaCoreStack LSCO(CO); if (LS.ckboolean(print)) { for (int i = 1; i <= lua_gettop(CO); i++) { pprint(LSCO, LuaSpecial(i), PrettyPrintOptions(), ostream); @@ -900,9 +873,6 @@ void World::run_scheduled_threads() { } close_lthread_state(); } - - LS.result(); - assert(stack_is_clear()); } int64_t World::alloc_id_predictable() {