diff --git a/indra/llcommon/llapp.cpp b/indra/llcommon/llapp.cpp index b99166991f1..905db9e4913 100644 --- a/indra/llcommon/llapp.cpp +++ b/indra/llcommon/llapp.cpp @@ -429,6 +429,7 @@ void LLApp::setStatus(EAppStatus status) statsd = LLSD::Integer(status); } LLEventPumps::instance().obtain("LLApp").post(llsd::map("status", statsd)); + LLEventPumps::instance().obtain("LLLua").post(llsd::map("status", "close_all")); } } diff --git a/indra/llcommon/lualistener.cpp b/indra/llcommon/lualistener.cpp index 82e32860db7..d4bd73a9fb6 100644 --- a/indra/llcommon/lualistener.cpp +++ b/indra/llcommon/lualistener.cpp @@ -33,18 +33,20 @@ std::ostream& operator<<(std::ostream& out, const LuaListener& self) LuaListener::LuaListener(lua_State* L): super(getUniqueKey()), + mCoroName(LLCoros::getName()), mListener(new LLLeapListener( "LuaListener", [this](const std::string& pump, const LLSD& data) { return queueEvent(pump, data); })), // Listen for shutdown events on the "LLApp" LLEventPump. mShutdownConnection( - LLEventPumps::instance().obtain("LLApp").listen( + LLEventPumps::instance().obtain("LLLua").listen( LLEventPump::inventName("LuaState"), [this](const LLSD& status) { - const auto& statsd = status["status"]; - if (statsd.asString() != "running") + auto coro_name = status["coro"].asString(); + auto statsd = status["status"].asString(); + if ((statsd == "close_all") || ((statsd == "close") && (coro_name == mCoroName))) { // If a Lua script is still blocked in getNext() during // viewer shutdown, close the queue to wake up getNext(). diff --git a/indra/llcommon/lualistener.h b/indra/llcommon/lualistener.h index 40ccfba8fec..d349ee23fd5 100644 --- a/indra/llcommon/lualistener.h +++ b/indra/llcommon/lualistener.h @@ -75,6 +75,8 @@ class LuaListener: public LLInstanceTracker std::unique_ptr mListener; LLTempBoundListener mShutdownConnection; + + std::string mCoroName; }; #endif /* ! defined(LL_LUALISTENER_H) */ diff --git a/indra/newview/llfloaterluascripts.cpp b/indra/newview/llfloaterluascripts.cpp index bd845a97d60..30353a72105 100644 --- a/indra/newview/llfloaterluascripts.cpp +++ b/indra/newview/llfloaterluascripts.cpp @@ -43,7 +43,20 @@ LLFloaterLUAScripts::LLFloaterLUAScripts(const LLSD &key) { mCommitCallbackRegistrar.add("Script.OpenFolder", [this](LLUICtrl*, const LLSD &userdata) { - gViewerWindow->getWindow()->openFolder(mTargetFolderPath); + if (mScriptList->hasSelectedItem()) + { + std::string target_folder_path = std::filesystem::path((mScriptList->getFirstSelected()->getColumn(1)->getValue().asString())).parent_path().string(); + gViewerWindow->getWindow()->openFolder(target_folder_path); + } + }); + mCommitCallbackRegistrar.add("Script.Terminate", [this](LLUICtrl*, const LLSD &userdata) + { + if (mScriptList->hasSelectedItem()) + { + std::string coro_name = mScriptList->getSelectedValue(); + LLEventPumps::instance().obtain("LLLua").post(llsd::map("status", "close", "coro", coro_name)); + LLLUAmanager::terminateScript(coro_name); + } }); } @@ -112,7 +125,6 @@ void LLFloaterLUAScripts::onScrollListRightClicked(LLUICtrl *ctrl, S32 x, S32 y) auto menu = mContextMenuHandle.get(); if (menu) { - mTargetFolderPath = std::filesystem::path((item->getColumn(1)->getValue().asString())).parent_path().string(); menu->show(x, y); LLMenuGL::showPopup(this, menu, x, y); } diff --git a/indra/newview/llfloaterluascripts.h b/indra/newview/llfloaterluascripts.h index 548bbd10f63..932c5c78dd8 100644 --- a/indra/newview/llfloaterluascripts.h +++ b/indra/newview/llfloaterluascripts.h @@ -46,7 +46,6 @@ class LLFloaterLUAScripts : std::unique_ptr mUpdateTimer; LLScrollListCtrl* mScriptList; - std::string mTargetFolderPath; LLHandle mContextMenuHandle; }; diff --git a/indra/newview/llluamanager.cpp b/indra/newview/llluamanager.cpp index 1d55313813f..853ed5a6344 100644 --- a/indra/newview/llluamanager.cpp +++ b/indra/newview/llluamanager.cpp @@ -49,6 +49,7 @@ #include std::map LLLUAmanager::sScriptNames; +std::set LLLUAmanager::sTerminationList; lua_function(sleep, "sleep(seconds): pause the running coroutine") { @@ -188,6 +189,20 @@ void LLLUAmanager::runScriptFile(const std::string &filename, script_result_fn r // A script_finished_fn is used to initialize the LuaState. // It will be called when the LuaState is destroyed. LuaState L(finished_cb); + + lua_callbacks(L)->interrupt = [](lua_State *L, int gc) + { + // skip if we're interrupting only for garbage collection + if (gc >= 0) + return; + + auto it = sTerminationList.find(LLCoros::getName()); + if (it != sTerminationList.end()) + { + sTerminationList.erase(it); + lluau::error(L, "Script was terminated"); + } + }; std::string text{std::istreambuf_iterator(in_file), {}}; auto [count, result] = L.expr(filename, text); if (result_cb) diff --git a/indra/newview/llluamanager.h b/indra/newview/llluamanager.h index fe4db22fca9..d671719bc4d 100644 --- a/indra/newview/llluamanager.h +++ b/indra/newview/llluamanager.h @@ -85,9 +85,12 @@ class LLLUAmanager static void runScriptOnLogin(); static const std::map getScriptNames() { return sScriptNames; } + static std::set getTerminationList() { return sTerminationList; } + static void terminateScript(std::string& coro_name) { sTerminationList.insert(coro_name); } private: - static std::map sScriptNames; + static std::map sScriptNames; + static std::set sTerminationList; }; class LLRequireResolver diff --git a/indra/newview/skins/default/xui/en/menu_lua_scripts.xml b/indra/newview/skins/default/xui/en/menu_lua_scripts.xml index 8f718abe175..645fee405df 100644 --- a/indra/newview/skins/default/xui/en/menu_lua_scripts.xml +++ b/indra/newview/skins/default/xui/en/menu_lua_scripts.xml @@ -8,4 +8,12 @@ + + + +