aboutsummaryrefslogtreecommitdiff
path: root/src/compat.cpp
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 14:26:18 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 14:26:18 +0200
commitc4aeac390daf21510575b6cad3163e9e6d34fd55 (patch)
tree370e01b7499f8a154ab5331d0da19f7015ba0734 /src/compat.cpp
parentea94590a2c7e3fb8a0f10f69a66864d79c8241d5 (diff)
downloadlanes-c4aeac390daf21510575b6cad3163e9e6d34fd55.tar.gz
lanes-c4aeac390daf21510575b6cad3163e9e6d34fd55.tar.bz2
lanes-c4aeac390daf21510575b6cad3163e9e6d34fd55.zip
Factorize uservalue extraction in luaG_getalluservalues
Diffstat (limited to 'src/compat.cpp')
-rw-r--r--src/compat.cpp28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/compat.cpp b/src/compat.cpp
index f0a3685..e1e7488 100644
--- a/src/compat.cpp
+++ b/src/compat.cpp
@@ -3,25 +3,37 @@
3 3
4#include "macros_and_utils.h" 4#include "macros_and_utils.h"
5 5
6
7// #################################################################################################
8// ###################################### Lua 5.1 / 5.2 / 5.3 ######################################
9// ################################################################################################# 6// #################################################################################################
10 7
8int luaG_getalluservalues(lua_State* const L_, int const idx_)
9{
10 STACK_CHECK_START_REL(L_, 0);
11 int const _idx{ luaG_absindex(L_, idx_) };
12 int _nuv{ 0 };
13 do {
14 // we don't know how many uservalues we are going to extract, there might be a lot...
15 STACK_GROW(L_, 1);
16 } while (lua_getiuservalue(L_, _idx, ++_nuv) != LUA_TNONE); // L_: ... [uv]* nil
17 // last call returned TNONE and pushed nil, that we don't need
18 lua_pop(L_, 1); // L_: ... [uv]*
19 --_nuv;
20 STACK_CHECK(L_, _nuv);
21 return _nuv;
22}
11 23
12// ################################################################################################# 24// #################################################################################################
13 25
14// a small helper to obtain a module's table from the registry instead of relying on the presence of _G["<name>"] 26// a small helper to obtain a module's table from the registry instead of relying on the presence of _G["<name>"]
15LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_) 27LuaType luaG_getmodule(lua_State* const L_, std::string_view const& name_)
16{ 28{
17 STACK_CHECK_START_REL(L_, 0); 29 STACK_CHECK_START_REL(L_, 0);
18 LuaType _type{ luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil 30 LuaType _type{ luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil
19 if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil 31 if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil
20 STACK_CHECK(L_, 1); 32 STACK_CHECK(L_, 1);
21 return _type; 33 return _type;
22 } 34 }
23 _type = luaG_getfield(L_, -1, name_); // L_: _R._LOADED {module}|nil 35 _type = luaG_getfield(L_, -1, name_); // L_: _R._LOADED {module}|nil
24 lua_remove(L_, -2); // L_: {module}|nil 36 lua_remove(L_, -2); // L_: {module}|nil
25 STACK_CHECK(L_, 1); 37 STACK_CHECK(L_, 1);
26 return _type; 38 return _type;
27} 39}