summaryrefslogtreecommitdiff
path: root/indra/llcommon/lua_function.h
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-02-07 12:50:26 -0500
committerNat Goodspeed <nat@lindenlab.com>2024-02-07 12:50:26 -0500
commit5b0404961e35ecca46148b90f2c27aed1bad607f (patch)
tree2ea3cd1c19e719536a57ff96aecb5e0347eb19c9 /indra/llcommon/lua_function.h
parentf664c2ea26fb63f162f3d988b6d00f1483be5d45 (diff)
Add machinery to capture result of running a Lua script or snippet.
Add LuaState::expr() that evaluates a Lua snippet and reports back any result (or error) left on the stack. Add LLLUAmanager::runScriptFile() and runScriptLine() overloads that accept a callback with an (int count, LLSD result) signature. The count disambiguates (error, no result, one result, array of results). Also add overloads that accept an existing LuaState instance. Also add waitScriptFile() and waitScriptLine() methods that pause the calling coroutine until the Lua script completes, and return its results. Instead of giving LuaState a description to use for all subsequent checkLua() calls, remove description from its constructor and data members. Move to expr() and checkLua() parameters: we want a description specific to each operation, rather than for the LuaState as a whole. This prepares for persistent LuaState instances. For now, the existing script_finished_fn semantics remain: the callback will be called only when the LuaState is destroyed. This may need to change as we migrate towards longer-lasting LuaState instances. Make lua_function(name) macro append suffixes to the name for both the LuaFunction subclass declaration and the instance declaration. This allows publishing a lua_function() name such as sleep(), which already has a different C++ declaration. Move the Lua sleep() entry point to a standalone lua_function(sleep), instead of a lambda in the body of runScriptFile().
Diffstat (limited to 'indra/llcommon/lua_function.h')
-rw-r--r--indra/llcommon/lua_function.h30
1 files changed, 20 insertions, 10 deletions
diff --git a/indra/llcommon/lua_function.h b/indra/llcommon/lua_function.h
index e2bd58e80a..ea9f2ebdf8 100644
--- a/indra/llcommon/lua_function.h
+++ b/indra/llcommon/lua_function.h
@@ -17,6 +17,7 @@
#include "luau/luaconf.h"
#include "luau/lualib.h"
#include "stringize.h"
+#include <utility> // std::pair
#define lua_register(L, n, f) (lua_pushcfunction(L, (f), n), lua_setglobal(L, (n)))
#define lua_rawlen lua_objlen
@@ -55,19 +56,28 @@ class LuaState
public:
typedef std::function<void(std::string msg)> script_finished_fn;
- LuaState(const std::string_view& desc, script_finished_fn cb);
+ LuaState(script_finished_fn cb={});
LuaState(const LuaState&) = delete;
LuaState& operator=(const LuaState&) = delete;
~LuaState();
- bool checkLua(int r);
+ bool checkLua(const std::string& desc, int r);
+
+ // expr() is for when we want to capture any results left on the stack
+ // by a Lua expression, possibly including multiple return values.
+ // int < 0 means error, and LLSD::asString() is the error message.
+ // int == 0 with LLSD::isUndefined() means the Lua expression returned no
+ // results.
+ // int == 1 means the Lua expression returned one result.
+ // int > 1 with LLSD::isArray() means the Lua expression returned
+ // multiple results, represented as the entries of the array.
+ std::pair<int, LLSD> expr(const std::string& desc, const std::string& text);
operator lua_State*() const { return mState; }
private:
- std::string mDesc;
script_finished_fn mCallback;
lua_State* mState;
std::string mError;
@@ -129,13 +139,13 @@ private:
* call() method definition header, to be followed by a method body enclosed
* in curly braces as usual.
*/
-#define lua_function(name) \
-static struct name##_ : public LuaFunction \
-{ \
- name##_(): LuaFunction(#name, &call) {} \
- static int call(lua_State* L); \
-} name; \
-int name##_::call(lua_State* L)
+#define lua_function(name) \
+static struct name##_luadecl : public LuaFunction \
+{ \
+ name##_luadecl(): LuaFunction(#name, &call) {} \
+ static int call(lua_State* L); \
+} name##_luadef; \
+int name##_luadecl::call(lua_State* L)
// {
// ... supply method body here, referencing 'L' ...
// }