summaryrefslogtreecommitdiff
path: root/indra/newview/llluamanager.h
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-02-29 12:09:59 -0500
committerNat Goodspeed <nat@lindenlab.com>2024-02-29 12:09:59 -0500
commit9dae3e96ee6b0cb4139f368488da85f9961d1d4f (patch)
treea9db7546a72f25e95cdd20bfa5e91c15096dd8f7 /indra/newview/llluamanager.h
parent0df7936ea50db2ee5680f75fa285f96fedf1f341 (diff)
Refactor require() to make it easier to reason about Lua stack usage.
Push throwing Lua errors down into LLRequireResolver::findModule() and findModuleImpl() so their callers don't have to handle the error case. That eliminates finishrequire(). require() itself now only retrieves (and pops) the passed module name and calls LLRequireResolver::resolveRequire() to do the actual work. resolveRequire() is now void. It only instantiates LLRequireResolver and calls its findModule(). findModule() is now also void. It's guaranteed to either push the loaded Lua module or throw a Lua error. In particular, when findPathImpl() cannot find the specified module, findModule() throws an error. That replaces ModuleStatus::NotFound. Since std::filesystem::path::append() aka operator/() detects when its right operand is absolute and, in that case, discards the left operand, we no longer need resolveAndStoreDefaultPaths(): we can just invoke that operation inline. When findModule() pushes _MODULES on the Lua stack, it uses LuaRemover (below) to ensure that _MODULES is removed again no matter how findModules() exits. findModuleImpl() now accepts the candidate pathname as its argument. That eliminates mAbsolutePath. findModuleImpl() now returns only bool: true means the module was found and loaded and pushed on the Lua stack, false means not found and nothing was pushed; no return means an error was reported. Push running a newly found module's source file down into findModuleImpl(). That eliminates the distinction between Cached and FileRead, which obviates ModuleStatus: a bool return means either "previously cached" or "we read it, compiled it, loaded it and ran it." That also eliminates the need to store the module's textual content in mSourceCode. Similarly, once loading the module succeeds, findModuleImpl() caches it in _MODULES right away. That eliminates ResolvedRequire since we need not pass the full pathname of the found module (or its contents) back up through the call chain. Move require() code that runs the new module into private runModule() method, called by findModuleImpl() in the not-cached case. runModule() is the only remaining method that can push either a string error message or the desired module, because of its funny stack manipulations. That means the check for a string error message on the stack top can move down to findModuleImpl(). Add LuaRemover class to ensure that on exit from some particular C++ block, the specified Lua stack entry will definitely be removed. This is different from LuaPopper in that it engages lua_remove() rather than lua_pop(). Also ditch obsolete await_event() Lua entry point.
Diffstat (limited to 'indra/newview/llluamanager.h')
-rw-r--r--indra/newview/llluamanager.h25
1 files changed, 4 insertions, 21 deletions
diff --git a/indra/newview/llluamanager.h b/indra/newview/llluamanager.h
index 43950ccee4..fb9f1b8141 100644
--- a/indra/newview/llluamanager.h
+++ b/indra/newview/llluamanager.h
@@ -87,24 +87,7 @@ public:
class LLRequireResolver
{
public:
- enum class ModuleStatus
- {
- Cached,
- FileRead,
- NotFound
- };
-
- struct ResolvedRequire
- {
- ModuleStatus status;
- std::string absolutePath;
- std::string sourceCode;
- };
-
- [[nodiscard]] ResolvedRequire static resolveRequire(lua_State *L, std::string path);
-
- std::string mAbsolutePath;
- std::string mSourceCode;
+ static void resolveRequire(lua_State *L, std::string path);
private:
std::string mPathToResolve;
@@ -112,10 +95,10 @@ class LLRequireResolver
LLRequireResolver(lua_State *L, const std::string& path);
- ModuleStatus findModule();
+ void findModule();
lua_State *L;
- void resolveAndStoreDefaultPaths();
- ModuleStatus findModuleImpl();
+ bool findModuleImpl(const std::string& absolutePath);
+ void runModule(const std::string& desc, const std::string& code);
};
#endif