From 4044e86ea4197535b70bc434634faf90cd2317d0 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 24 Oct 2024 14:53:09 +0200 Subject: Strengthen more code with UserValueIndex --- src/compat.cpp | 8 ++++---- src/compat.h | 4 ++-- src/keeper.cpp | 2 +- src/lane.cpp | 18 +++++++++--------- src/lanes.cpp | 2 +- src/nameof.cpp | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/compat.cpp b/src/compat.cpp index b661b28..04ac819 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -9,7 +9,7 @@ UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) { STACK_CHECK_START_REL(L_, 0); StackIndex const _idx{ luaG_absindex(L_, idx_) }; - UserValueCount _nuv{ 0 }; + UserValueIndex _nuv{ 0 }; do { // we don't know how many uservalues we are going to extract, there might be a lot... STACK_GROW(L_, 1); @@ -18,7 +18,7 @@ UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) lua_pop(L_, 1); // L_: ... [uv]* --_nuv; STACK_CHECK(L_, _nuv); - return _nuv; + return UserValueCount{ _nuv.value() }; } // ################################################################################################# @@ -92,7 +92,7 @@ void* lua_newuserdatauv(lua_State* const L_, size_t const sz_, [[maybe_unused]] // ################################################################################################# // push on stack uservalue #n of full userdata at idx -int lua_getiuservalue(lua_State* const L_, StackIndex const idx_, int const n_) +int lua_getiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex const n_) { STACK_CHECK_START_REL(L_, 0); // full userdata can have only 1 uservalue before 5.4 @@ -129,7 +129,7 @@ int lua_getiuservalue(lua_State* const L_, StackIndex const idx_, int const n_) // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. // Returns 0 if the userdata does not have that value. -int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, int const n_) +int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex const n_) { if (n_ > 1 #if LUA_VERSION_NUM == 501 diff --git a/src/compat.h b/src/compat.h index 0b6b12b..0ea35bf 100644 --- a/src/compat.h +++ b/src/compat.h @@ -81,8 +81,8 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) #if LUA_VERSION_NUM < 504 void* lua_newuserdatauv(lua_State* L_, size_t sz_, UserValueCount nuvalue_); -int lua_getiuservalue(lua_State* L_, StackIndex idx_, int n_); -int lua_setiuservalue(lua_State* L_, StackIndex idx_, int n_); +int lua_getiuservalue(lua_State* L_, StackIndex idx_, UserValueIndex n_); +int lua_setiuservalue(lua_State* L_, StackIndex idx_, UserValueIndex n_); #define LUA_GNAME "_G" diff --git a/src/keeper.cpp b/src/keeper.cpp index 7deb2b3..4b1ae3d 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -63,7 +63,7 @@ namespace { class KeyUD { private: - static constexpr int kContentsTableIndex{ 1 }; + static constexpr UserValueIndex kContentsTableIndex{ 1 }; public: static constexpr std::string_view kUnder{ "under" }; diff --git a/src/lane.cpp b/src/lane.cpp index 29ad8bb..65cb58d 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -150,7 +150,7 @@ static LUAG_FUNC(thread_join) if (_stored == 0) { raise_luaL_error(L_, _lane->L ? "First return value must be non-nil when using join()" : "Can't join() more than once or after indexing"); } - lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} + lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} for (int _i = 2; _i <= _stored; ++_i) { lua_rawgeti(L_, 2, _i); // L_: lane {uv} results2...N } @@ -163,7 +163,7 @@ static LUAG_FUNC(thread_join) case Lane::Error: { LUA_ASSERT(L_, _stored == 2 || _stored == 3); - lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} + lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} lua_rawgeti(L_, 2, 2); // L_: lane {uv} lua_rawgeti(L_, 2, 3); // L_: lane {uv} |nil if (lua_isnil(L_, -1)) { @@ -178,7 +178,7 @@ static LUAG_FUNC(thread_join) case Lane::Cancelled: LUA_ASSERT(L_, _stored == 2); - lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} + lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} lua_rawgeti(L_, 2, 2); // L_: lane {uv} cancel_error lua_rawgeti(L_, 2, 1); // L_: lane {uv} cancel_error nil lua_replace(L_, -3); // L_: lane nil cancel_error @@ -828,7 +828,7 @@ static LUAG_FUNC(lane_gc) Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: ud // if there a gc callback? - lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: ud uservalue + lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: ud uservalue kLaneGC.pushKey(L_); // L_: ud uservalue __gc lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil if (!lua_isnil(L_, -1)) { @@ -1070,7 +1070,7 @@ void Lane::pushIndexedResult(lua_State* const L_, int const key_) const LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); // L_: lane ... STACK_GROW(L_, 3); - lua_getiuservalue(L_, kIdxSelf, 1); // L_: lane ... {uv} + lua_getiuservalue(L_, kIdxSelf, UserValueIndex{ 1 }); // L_: lane ... {uv} if (status != Lane::Error) { lua_rawgeti(L_, -1, key_); // L_: lane ... {uv} uv[i] lua_remove(L_, -2); // L_: lane ... uv[i] @@ -1138,7 +1138,7 @@ void Lane::resetResultsStorage(lua_State* const L_, StackIndex const self_idx_) // create the new table lua_newtable(L_); // L_: ... self ... {} // get the current table - lua_getiuservalue(L_, _self_idx, 1); // L_: ... self ... {} {uv} + lua_getiuservalue(L_, _self_idx, UserValueIndex{ 1 }); // L_: ... self ... {} {uv} LUA_ASSERT(L_, lua_istable(L_, -1)); // read gc_cb from the current table kLaneGC.pushKey(L_); // L_: ... self ... {} {uv} kLaneGC @@ -1149,7 +1149,7 @@ void Lane::resetResultsStorage(lua_State* const L_, StackIndex const self_idx_) // we can forget the old table lua_pop(L_, 1); // L_: ... self ... {} // and store the new one - lua_setiuservalue(L_, _self_idx, 1); // L_: ... self ... + lua_setiuservalue(L_, _self_idx, UserValueIndex{ 1 }); // L_: ... self ... STACK_CHECK(L_, 0); } @@ -1161,7 +1161,7 @@ void Lane::securizeDebugName(lua_State* const L_) STACK_CHECK_START_REL(L_, 0); STACK_GROW(L_, 3); // a Lane's uservalue should be a table - lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane ... {uv} + lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane ... {uv} LUA_ASSERT(L_, lua_istable(L_, -1)); // we don't care about the actual key, so long as it's unique and can't collide with anything. lua_newtable(L_); // L_: lane ... {uv} {} @@ -1195,7 +1195,7 @@ int Lane::storeResults(lua_State* const L_) LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); STACK_CHECK_START_REL(L_, 0); - lua_getiuservalue(L_, kIdxSelf, 1); // L_: lane ... {uv} + lua_getiuservalue(L_, kIdxSelf, UserValueIndex{ 1 }); // L_: lane ... {uv} StackIndex const _tidx{ lua_gettop(L_) }; int _stored{}; diff --git a/src/lanes.cpp b/src/lanes.cpp index e545ca0..8c4ef61 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -350,7 +350,7 @@ LUAG_FUNC(lane_new) } STACK_CHECK(L, 2); // store the uservalue in the Lane full userdata - lua_setiuservalue(L, StackIndex{ -2 }, 1); // L: ... lane + lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane lua_State* const _L2{ lane->L }; STACK_CHECK_START_REL(_L2, 0); diff --git a/src/nameof.cpp b/src/nameof.cpp index fd31a28..ef03820 100644 --- a/src/nameof.cpp +++ b/src/nameof.cpp @@ -125,7 +125,7 @@ THE SOFTWARE. STACK_CHECK(L_, 2); // search in the object's uservalues { - int _uvi{ 1 }; + UserValueIndex _uvi{ 1 }; while (lua_getiuservalue(L_, kIdxTop, _uvi) != LUA_TNONE) { // L_: o "r" {c} {fqn} ... {?} k U {u} if (lua_istable(L_, -1)) { // if it is a table, look inside ++depth_; -- cgit v1.2.3-55-g6feb