aboutsummaryrefslogtreecommitdiff
path: root/src/compat.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-07-29 18:07:16 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-07-29 18:07:16 +0200
commitbb11006f635a69dd9244e09e4359ed02eff8fe84 (patch)
tree7fda9eda216b1683ff3dd954b6300c13bfaf2b14 /src/compat.h
parent34b2b5e712ea1cc59004ca48f79f54af162993a5 (diff)
downloadlanes-bb11006f635a69dd9244e09e4359ed02eff8fe84.tar.gz
lanes-bb11006f635a69dd9244e09e4359ed02eff8fe84.tar.bz2
lanes-bb11006f635a69dd9244e09e4359ed02eff8fe84.zip
Internal refactorization to properly handle lua_resume idiosyncrasies
Diffstat (limited to 'src/compat.h')
-rw-r--r--src/compat.h25
1 files changed, 15 insertions, 10 deletions
diff --git a/src/compat.h b/src/compat.h
index 9e15230..2690a41 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -237,11 +237,13 @@ template <typename LUA_RESUME>
237concept RequiresLuaResume51 = requires(LUA_RESUME f_) { { f_(nullptr, 0) } -> std::same_as<int>; }; 237concept RequiresLuaResume51 = requires(LUA_RESUME f_) { { f_(nullptr, 0) } -> std::same_as<int>; };
238 238
239template <RequiresLuaResume51 LUA_RESUME> 239template <RequiresLuaResume51 LUA_RESUME>
240static inline int WrapLuaResume(LUA_RESUME const f_, lua_State* const L_, [[maybe_unused]] lua_State* const from_, int const nargs_, int* const nresults_) 240static inline int WrapLuaResume(LUA_RESUME const lua_resume_, lua_State* const L_, [[maybe_unused]] lua_State* const from_, int const nargs_, int* const nresults_)
241{ 241{
242 int const _resultsStart{ lua_gettop(L_) - nargs_ - 1 }; 242 // lua_resume is supposed to be called from a "clean" stack:
243 int const _rc{ f_(L_, nargs_) }; 243 // it should only contain the function and its initial arguments on first call, or the resume arguments on subsequent invocations
244 *nresults_ = lua_gettop(L_) - _resultsStart; 244 int const _rc{ lua_resume_(L_, nargs_) };
245 // after resuming, the stack should only contain the yielded values
246 *nresults_ = lua_gettop(L_);
245 return _rc; 247 return _rc;
246} 248}
247 249
@@ -251,11 +253,13 @@ template <typename LUA_RESUME>
251concept RequiresLuaResume52 = requires(LUA_RESUME f_) { { f_(nullptr, nullptr, 0) } -> std::same_as<int>; }; 253concept RequiresLuaResume52 = requires(LUA_RESUME f_) { { f_(nullptr, nullptr, 0) } -> std::same_as<int>; };
252 254
253template <RequiresLuaResume52 LUA_RESUME> 255template <RequiresLuaResume52 LUA_RESUME>
254static inline int WrapLuaResume(LUA_RESUME const f_, lua_State* const L_, lua_State* const from_, int const nargs_, [[maybe_unused]] int* const nresults_) 256static inline int WrapLuaResume(LUA_RESUME const lua_resume_, lua_State* const L_, lua_State* const from_, int const nargs_, [[maybe_unused]] int* const nresults_)
255{ 257{
256 int const _resultsStart{ lua_gettop(L_) - nargs_ - 1 }; 258 // lua_resume is supposed to be called from a "clean" stack:
257 int const _rc{ f_(L_, from_, nargs_) }; 259 // it should only contain the function and its initial arguments on first call, or the resume arguments on subsequent invocations
258 *nresults_ = lua_gettop(L_) - _resultsStart; 260 int const _rc{ lua_resume_(L_, from_, nargs_) };
261 // after resuming, the stack should only contain the yielded values
262 *nresults_ = lua_gettop(L_);
259 return _rc; 263 return _rc;
260} 264}
261 265
@@ -265,9 +269,10 @@ template <typename LUA_RESUME>
265concept RequiresLuaResume54 = requires(LUA_RESUME f_) { { f_(nullptr, nullptr, 0, nullptr) } -> std::same_as<int>; }; 269concept RequiresLuaResume54 = requires(LUA_RESUME f_) { { f_(nullptr, nullptr, 0, nullptr) } -> std::same_as<int>; };
266 270
267template <RequiresLuaResume54 LUA_RESUME> 271template <RequiresLuaResume54 LUA_RESUME>
268static inline int WrapLuaResume(LUA_RESUME const f_, lua_State* const L_, lua_State* const from_, int const nargs_, int* const nresults_) 272static inline int WrapLuaResume(LUA_RESUME const lua_resume_, lua_State* const L_, lua_State* const from_, int const nargs_, int* const nresults_)
269{ 273{
270 return f_(L_, from_, nargs_, nresults_); 274 // starting with Lua 5.4, the stack can contain stuff below the actual yielded values, but lua_resume tells us the correct nresult
275 return lua_resume_(L_, from_, nargs_, nresults_);
271} 276}
272 277
273// ------------------------------------------------------------------------------------------------- 278// -------------------------------------------------------------------------------------------------