diff options
Diffstat (limited to 'indra/llcommon')
-rw-r--r-- | indra/llcommon/lua_function.cpp | 39 | ||||
-rw-r--r-- | indra/llcommon/lua_function.h | 17 |
2 files changed, 48 insertions, 8 deletions
diff --git a/indra/llcommon/lua_function.cpp b/indra/llcommon/lua_function.cpp index 2557fd0cc9..014ff3c4c4 100644 --- a/indra/llcommon/lua_function.cpp +++ b/indra/llcommon/lua_function.cpp @@ -60,7 +60,8 @@ namespace namespace lluau { -int dostring(lua_State* L, const std::string& desc, const std::string& text) +int dostring(lua_State* L, const std::string& desc, const std::string& text, + const std::vector<std::string>& args) { auto r = loadstring(L, desc, text); if (r != LUA_OK) @@ -80,12 +81,22 @@ int dostring(lua_State* L, const std::string& desc, const std::string& text) // stack: compiled chunk, debug.traceback() lua_insert(L, -2); // stack: debug.traceback(), compiled chunk - LuaRemover cleanup(L, -2); + // capture absolute index of debug.traceback() + int traceback = lua_absindex(L, -2); + // remove it from stack on exit + LuaRemover cleanup(L, traceback); + + // push any args passed -- all strings -- script must handle any desired + // conversions + for (const auto& arg : args) + { + lua_pushstdstring(L, arg); + } // It's important to pass LUA_MULTRET as the expected number of return // values: if we pass any fixed number, we discard any returned values // beyond that number. - return lua_pcall(L, 0, LUA_MULTRET, -2); + return lua_pcall(L, int(args.size()), LUA_MULTRET, traceback); } int loadstring(lua_State *L, const std::string &desc, const std::string &text) @@ -804,7 +815,13 @@ bool LuaState::checkLua(const std::string& desc, int r) return true; } -std::pair<int, LLSD> LuaState::expr(const std::string& desc, const std::string& text) +std::pair<int, LLSD> LuaState::expr(const std::string& desc, const ScriptCommand& command) +{ + return expr(desc, , command.args); +} + +std::pair<int, LLSD> LuaState::expr(const std::string& desc, const std::string& text, + const std::vector<std::string>& args) { /*---------------------------- feature flag ----------------------------*/ if (! mFeature) @@ -827,7 +844,7 @@ std::pair<int, LLSD> LuaState::expr(const std::string& desc, const std::string& }; LL_INFOS("Lua") << desc << " run" << LL_ENDL; - if (! checkLua(desc, lluau::dostring(mState, desc, text))) + if (! checkLua(desc, lluau::dostring(mState, desc, text, args))) { LL_WARNS("Lua") << desc << " error: " << mError << LL_ENDL; return { -1, mError }; @@ -1050,6 +1067,18 @@ std::pair<LuaFunction::Registry&, LuaFunction::Lookup&> LuaFunction::getState() } /***************************************************************************** +* LuaCommand +*****************************************************************************/ +LuaCommand::LuaCommand(const std::string& command) +{ +} + +bool LuaCommand::found() const +{ + return ; +} + +/***************************************************************************** * source_path() *****************************************************************************/ lua_function(source_path, "source_path(): return the source path of the running Lua script") diff --git a/indra/llcommon/lua_function.h b/indra/llcommon/lua_function.h index 10c201c234..a5022db225 100644 --- a/indra/llcommon/lua_function.h +++ b/indra/llcommon/lua_function.h @@ -19,6 +19,7 @@ #include "fsyspath.h" #include "llerror.h" #include "llsd.h" +#include "scriptcommand.h" #include "stringize.h" #include <exception> // std::uncaught_exceptions() #include <memory> // std::shared_ptr @@ -26,6 +27,7 @@ #include <typeinfo> #include <unordered_map> #include <utility> // std::pair +#include <vector> class LuaListener; @@ -55,8 +57,10 @@ namespace lluau // luau removed lua_dostring(), but since we perform the equivalent luau // sequence in multiple places, encapsulate it. desc and text are strings // rather than string_views because dostring() needs pointers to nul- - // terminated char arrays. - int dostring(lua_State* L, const std::string& desc, const std::string& text); + // terminated char arrays. Any args are pushed to the Lua stack before + // calling the Lua chunk in text. + int dostring(lua_State* L, const std::string& desc, const std::string& text, + const std::vector<std::string>& args={}); int loadstring(lua_State* L, const std::string& desc, const std::string& text); fsyspath source_path(lua_State* L); @@ -92,13 +96,20 @@ public: // expr() is for when we want to capture any results left on the stack // by a Lua expression, possibly including multiple return values. + // Pass: + // desc = description used for logging et al. + // text = Lua chunk to execute, e.g. contents of a script file + // args = arguments, if any, to pass to script file + // Returns: // 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); + std::pair<int, LLSD> expr(const std::string& desc, const std::string& text, + const std::vector<std::string>& args={}); + std::pair<int, LLSD> expr(const std::string& desc, const ScriptCommand& command); operator lua_State*() const { return mState; } |