From 5ff9371ad9abc55de9094d9771a2210e0b87c267 Mon Sep 17 00:00:00 2001 From: Daid Date: Wed, 11 Sep 2024 11:26:36 +0200 Subject: [PATCH] Add code to allow capting all lua run results. --- src/script/conversion.h | 20 ++++++++++++++++++++ src/script/environment.h | 18 +++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/script/conversion.h b/src/script/conversion.h index bf2f44b..e99886f 100644 --- a/src/script/conversion.h +++ b/src/script/conversion.h @@ -9,6 +9,13 @@ namespace sp::script { +// Special class to capture all results of a run/call as a list of strings. +class CaptureAllResults +{ +public: + std::vector result; +}; + template struct Convert {}; template<> struct Convert { static int toLua(lua_State* L, bool value) { lua_pushboolean(L, value); return 1; } @@ -60,6 +67,19 @@ template struct Convert> { static std::optional fromLua(lua_State* L, int idx) { if (idx <= lua_gettop(L) && !lua_isnil(L, idx)) return Convert::fromLua(L, idx); return {}; } }; +template<> struct Convert { + static CaptureAllResults fromLua(lua_State* L, int idx) { + CaptureAllResults res; + int nres = lua_gettop(L); + for(int n=2; n<=nres; n++) { + auto s = luaL_tolstring(L, n, nullptr); + lua_pop(L, 1); + res.result.push_back(s); + } + return res; + } +}; + template struct Convert { using FT = RET(*)(ARGS...); static int toLua(lua_State* L, FT value) { diff --git a/src/script/environment.h b/src/script/environment.h index ced68ba..9d864c7 100644 --- a/src/script/environment.h +++ b/src/script/environment.h @@ -82,11 +82,12 @@ class Environment : NonCopyable private: template Result runImpl(const string& code, const string& name="=[string]") { + int stack_size = lua_gettop(L); lua_pushcfunction(L, luaErrorHandler); int result = luaL_loadbufferx(L, code.c_str(), code.length(), name.c_str(), "t"); if (result) { auto res = Result::makeError(luaL_checkstring(L, -1)); - lua_pop(L, 2); + lua_settop(L, stack_size); return res; } @@ -95,20 +96,23 @@ class Environment : NonCopyable //set the environment table it as 1st upvalue lua_setupvalue(L, -2, 1); - result = lua_pcall(L, 0, 1, -2); - if (result) - { + int result_count = 1; + if constexpr (std::is_same_v) { + result_count = LUA_MULTRET; + } + result = lua_pcall(L, 0, result_count, -2); + if (result) { auto result = Result::makeError(lua_tostring(L, -1)); - lua_pop(L, 2); + lua_settop(L, stack_size); return result; } if constexpr (!std::is_void_v) { auto return_value = Convert::fromLua(L, -1); - lua_pop(L, 2); + lua_settop(L, stack_size); return return_value; } else { - lua_pop(L, 2); + lua_settop(L, stack_size); return {}; } }