Add class vector. Change probe_lua_call to handle multiple return values.

This commit is contained in:
2025-02-26 16:45:11 -05:00
parent 23f6401a93
commit 3da5e7534a
5 changed files with 73 additions and 9 deletions

View File

@@ -470,6 +470,7 @@ void World::probe_lua_call(int64_t place_id, int64_t actor_id, std::string_view
if (!LS.isfunction(lfunc)) return;
// Call the closure.
int calltop = lua_gettop(L);
lua_pushvalue(L, lfunc.index());
int nargs = 0;
try {
@@ -482,20 +483,35 @@ void World::probe_lua_call(int64_t place_id, int64_t actor_id, std::string_view
}
open_lthread_state(actor_id, place_id, 0, false, true);
eng::string msg = traceback_pcall(L, nargs, 1);
eng::string msg = traceback_pcall(L, nargs, LUA_MULTRET);
LuaExtraArgs returnvalues(calltop + 1, lua_gettop(L) - calltop);
// Send any prints to the console.
eng::string prints = lthread_prints_->str();
lthread_prints_.reset();
util::dprint(prints);
if (msg.empty()) {
lua_replace(L, retvec.index());
if (LS.istable(retvec)) {
for (int i = 1 ; ; i++) {
LS.rawget(retval, retvec, i);
bool ok = encode_simple_dynamic(LS, retval, retvals);
if (!ok) break;
bool ok = true;
for (int i = 0; ok && (i < returnvalues.size()); i++) {
LS.set(retvec, returnvalues[i]);
// If it's a general table without an MT, then
// treat it as a list of return values.
bool is_compound = false;
if (LS.xtype(retvec) == LUA_TT_GENERAL) {
LS.getmetatable(mt, retvec);
if (LS.isnil(mt)) is_compound = true;
}
if (is_compound) {
for (int j = 1; ok; j++) {
LS.rawget(retval, retvec, j);
if (LS.isnil(retval)) break;
ok = encode_simple_dynamic(LS, retval, retvals);
}
} else {
ok = encode_simple_dynamic(LS, retvec, retvals);
}
}
} else {