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

@@ -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() {