Implement probe_lua and add it to lpxclient/lpxserver

This commit is contained in:
2021-11-26 13:56:24 -05:00
parent c02109699e
commit 1e93533246
10 changed files with 134 additions and 35 deletions

View File

@@ -257,6 +257,65 @@ int64_t World::create_login_actor() {
return tan->id();
}
std::string World::probe_lua(int64_t actor_id, const std::string &lua) {
assert(stack_is_clear());
lua_State *L = state();
Tangible *actor = tangible_get(actor_id);
if (actor == nullptr) {
return "";
}
LuaVar closure;
LuaStack LS(L, closure);
// create the compiled closure.
int status = luaL_loadbuffer(L, lua.c_str(), lua.size(), "=probe");
lua_replace(L, closure.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 "";
}
// Call the closure.
int top = lua_gettop(L);
lua_pushvalue(L, closure.index());
open_lthread_state(actor_id, actor_id, false, true);
status = traceback_pcall(L, 0, LUA_MULTRET);
// If there's an error message, print it.
// Otherwise, pretty-print the results.
std::ostream *ostream = lthread_print_stream();
if (status == LUA_OK) {
for (int i = top + 1; i <= lua_gettop(L); i++) {
LuaSpecial root(i);
pprint(LS, root, true, ostream);
(*ostream) << std::endl;
}
} else {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) {
msg = "(error object is not a string)";
}
(*ostream) << msg << std::endl;
}
// Collect the lthread_prints (and also make sure they
// don't go into the printbuffer).
std::string result = lthread_prints_->str();
lthread_prints_.reset();
close_lthread_state();
// And we're done.
LS.result();
assert(stack_is_clear());
return result;
}
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
assert(stack_is_clear());
gui->clear(place_id);
@@ -391,6 +450,8 @@ void World::invoke_lua(int64_t actor_id, int64_t place_id, const std::string &ac
lua_replace(L, closure.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;
}