From 7fccd662df48e5ddef0ec7e14a43289024bd27e1 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 28 Sep 2023 09:40:09 -0400 Subject: DRTVWR-589: Try to override Lua's built-in print() with print_info() so Lua print() output will go to the viewer log, instead of getting discarded and possibly causing failures when the buffer fills and there's no open stdout file handle. Also name each Lua C++ coroutine with the description we give the LuaState instance. --- indra/newview/llluamanager.cpp | 46 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'indra/newview') diff --git a/indra/newview/llluamanager.cpp b/indra/newview/llluamanager.cpp index c1e111f0ac..931faadf4b 100644 --- a/indra/newview/llluamanager.cpp +++ b/indra/newview/llluamanager.cpp @@ -226,6 +226,15 @@ public: } } + static lua_CFunction get(const std::string& key) + { + // use find() instead of subscripting to avoid creating an entry for + // unknown key + const auto& registry{ getRegistry() }; + auto found{ registry.find(key) }; + return (found == registry.end())? nullptr : found->second; + } + private: using Registry = std::map; static Registry& getRegistry() @@ -668,11 +677,6 @@ lua_function(listen_events) return 2; } -void initLUA(lua_State *L) -{ - LuaFunction::init(L); -} - /** * RAII class to manage the lifespan of a lua_State */ @@ -685,7 +689,9 @@ public: mState(luaL_newstate()) { luaL_openlibs(mState); - initLUA(mState); + LuaFunction::init(mState); + // Try to make print() write to our log. + lua_register(mState, "print", LuaFunction::get("print_info")); } LuaState(const LuaState&) = delete; @@ -747,9 +753,10 @@ private: void LLLUAmanager::runScriptFile(const std::string& filename, script_finished_fn cb) { - LLCoros::instance().launch("LUAScriptFileCoro", [filename, cb]() + std::string desc{ stringize("runScriptFile('", filename, "')") }; + LLCoros::instance().launch(desc, [desc, filename, cb]() { - LuaState L(stringize("runScriptFile('", filename, "')"), cb); + LuaState L(desc, cb); auto LUA_sleep_func = [](lua_State *L) { @@ -776,18 +783,19 @@ void LLLUAmanager::runScriptFile(const std::string& filename, script_finished_fn void LLLUAmanager::runScriptLine(const std::string& cmd, script_finished_fn cb) { - LLCoros::instance().launch("LUAScriptFileCoro", [cmd, cb]() + // find a suitable abbreviation for the cmd string + std::string_view shortcmd{ cmd }; + const size_t shortlen = 40; + std::string::size_type eol = shortcmd.find_first_of("\r\n"); + if (eol != std::string::npos) + shortcmd = shortcmd.substr(0, eol); + if (shortcmd.length() > shortlen) + shortcmd = stringize(shortcmd.substr(0, shortlen), "..."); + + std::string desc{ stringize("runScriptLine('", shortcmd, "')") }; + LLCoros::instance().launch(desc, [desc, cmd, cb]() { - // find a suitable abbreviation for the cmd string - std::string_view shortcmd{ cmd }; - const size_t shortlen = 40; - std::string::size_type eol = shortcmd.find_first_of("\r\n"); - if (eol != std::string::npos) - shortcmd = shortcmd.substr(0, eol); - if (shortcmd.length() > shortlen) - shortcmd = stringize(shortcmd.substr(0, shortlen), "..."); - - LuaState L(stringize("runScriptLine('", shortcmd, "')"), cb); + LuaState L(desc, cb); L.checkLua(luaL_dostring(L, cmd.c_str())); }); } -- cgit v1.2.3