diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-24 14:53:09 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-24 14:53:09 +0200 |
commit | 4044e86ea4197535b70bc434634faf90cd2317d0 (patch) | |
tree | 522af1b44e3dda30ff66ed2245aad54ceeb7e1b2 | |
parent | 958ed4f1e29418e90f3dd3de6779e30e05f70448 (diff) | |
download | lanes-4044e86ea4197535b70bc434634faf90cd2317d0.tar.gz lanes-4044e86ea4197535b70bc434634faf90cd2317d0.tar.bz2 lanes-4044e86ea4197535b70bc434634faf90cd2317d0.zip |
Strengthen more code with UserValueIndex
-rw-r--r-- | deep_test/deep_test.cpp | 16 | ||||
-rw-r--r-- | src/compat.cpp | 8 | ||||
-rw-r--r-- | src/compat.h | 4 | ||||
-rw-r--r-- | src/keeper.cpp | 2 | ||||
-rw-r--r-- | src/lane.cpp | 18 | ||||
-rw-r--r-- | src/lanes.cpp | 2 | ||||
-rw-r--r-- | src/nameof.cpp | 2 |
7 files changed, 26 insertions, 26 deletions
diff --git a/deep_test/deep_test.cpp b/deep_test/deep_test.cpp index 974709f..43df188 100644 --- a/deep_test/deep_test.cpp +++ b/deep_test/deep_test.cpp | |||
@@ -70,7 +70,7 @@ void MyDeepFactory::deleteDeepObjectInternal(lua_State* const L_, DeepPrelude* c | |||
70 | { | 70 | { |
71 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) }; | 71 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) }; |
72 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); | 72 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); |
73 | int _uv = (int) luaL_optinteger(L, 2, 1); | 73 | UserValueIndex const _uv{ static_cast<UserValueIndex::type>(luaL_optinteger(L, 2, 1)) }; |
74 | lua_getiuservalue(L, StackIndex{ 1 }, _uv); | 74 | lua_getiuservalue(L, StackIndex{ 1 }, _uv); |
75 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); | 75 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); |
76 | return 1; | 76 | return 1; |
@@ -106,7 +106,7 @@ void MyDeepFactory::deleteDeepObjectInternal(lua_State* const L_, DeepPrelude* c | |||
106 | { | 106 | { |
107 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) }; | 107 | MyDeepUserdata* const _self{ static_cast<MyDeepUserdata*>(MyDeepFactory::Instance.toDeep(L, StackIndex{ 1 })) }; |
108 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); | 108 | _self->inUse.fetch_add(1, std::memory_order_seq_cst); |
109 | int _uv = (int) luaL_optinteger(L, 2, 1); | 109 | UserValueIndex const _uv{ static_cast<UserValueIndex::type>(luaL_optinteger(L, 2, 1)) }; |
110 | lua_settop(L, 3); | 110 | lua_settop(L, 3); |
111 | lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, _uv) != 0); | 111 | lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, _uv) != 0); |
112 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); | 112 | _self->inUse.fetch_sub(1, std::memory_order_seq_cst); |
@@ -157,10 +157,10 @@ struct MyClonableUserdata | |||
157 | 157 | ||
158 | [[nodiscard]] static int clonable_setuv(lua_State* L) | 158 | [[nodiscard]] static int clonable_setuv(lua_State* L) |
159 | { | 159 | { |
160 | MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); | 160 | MyClonableUserdata* const _self{ static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)) }; |
161 | int uv = (int) luaL_optinteger(L, 2, 1); | 161 | UserValueIndex const _uv{ static_cast<UserValueIndex::type>(luaL_optinteger(L, 2, 1)) }; |
162 | lua_settop(L, 3); | 162 | lua_settop(L, 3); |
163 | lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, uv) != 0); | 163 | lua_pushboolean(L, lua_setiuservalue(L, StackIndex{ 1 }, _uv) != 0); |
164 | return 1; | 164 | return 1; |
165 | } | 165 | } |
166 | 166 | ||
@@ -168,9 +168,9 @@ struct MyClonableUserdata | |||
168 | 168 | ||
169 | [[nodiscard]] static int clonable_getuv(lua_State* L) | 169 | [[nodiscard]] static int clonable_getuv(lua_State* L) |
170 | { | 170 | { |
171 | MyClonableUserdata* self = static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)); | 171 | MyClonableUserdata* const _self{ static_cast<MyClonableUserdata*>(lua_touserdata(L, 1)) }; |
172 | int uv = (int) luaL_optinteger(L, 2, 1); | 172 | UserValueIndex const _uv{ static_cast<UserValueIndex::type>(luaL_optinteger(L, 2, 1)) }; |
173 | lua_getiuservalue(L, StackIndex{ 1 }, uv); | 173 | lua_getiuservalue(L, StackIndex{ 1 }, _uv); |
174 | return 1; | 174 | return 1; |
175 | } | 175 | } |
176 | 176 | ||
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_) | |||
9 | { | 9 | { |
10 | STACK_CHECK_START_REL(L_, 0); | 10 | STACK_CHECK_START_REL(L_, 0); |
11 | StackIndex const _idx{ luaG_absindex(L_, idx_) }; | 11 | StackIndex const _idx{ luaG_absindex(L_, idx_) }; |
12 | UserValueCount _nuv{ 0 }; | 12 | UserValueIndex _nuv{ 0 }; |
13 | do { | 13 | do { |
14 | // we don't know how many uservalues we are going to extract, there might be a lot... | 14 | // we don't know how many uservalues we are going to extract, there might be a lot... |
15 | STACK_GROW(L_, 1); | 15 | STACK_GROW(L_, 1); |
@@ -18,7 +18,7 @@ UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) | |||
18 | lua_pop(L_, 1); // L_: ... [uv]* | 18 | lua_pop(L_, 1); // L_: ... [uv]* |
19 | --_nuv; | 19 | --_nuv; |
20 | STACK_CHECK(L_, _nuv); | 20 | STACK_CHECK(L_, _nuv); |
21 | return _nuv; | 21 | return UserValueCount{ _nuv.value() }; |
22 | } | 22 | } |
23 | 23 | ||
24 | // ################################################################################################# | 24 | // ################################################################################################# |
@@ -92,7 +92,7 @@ void* lua_newuserdatauv(lua_State* const L_, size_t const sz_, [[maybe_unused]] | |||
92 | // ################################################################################################# | 92 | // ################################################################################################# |
93 | 93 | ||
94 | // push on stack uservalue #n of full userdata at idx | 94 | // push on stack uservalue #n of full userdata at idx |
95 | int lua_getiuservalue(lua_State* const L_, StackIndex const idx_, int const n_) | 95 | int lua_getiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex const n_) |
96 | { | 96 | { |
97 | STACK_CHECK_START_REL(L_, 0); | 97 | STACK_CHECK_START_REL(L_, 0); |
98 | // full userdata can have only 1 uservalue before 5.4 | 98 | // 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_) | |||
129 | 129 | ||
130 | // 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. | 130 | // 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. |
131 | // Returns 0 if the userdata does not have that value. | 131 | // Returns 0 if the userdata does not have that value. |
132 | int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, int const n_) | 132 | int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex const n_) |
133 | { | 133 | { |
134 | if (n_ > 1 | 134 | if (n_ > 1 |
135 | #if LUA_VERSION_NUM == 501 | 135 | #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_) | |||
81 | #if LUA_VERSION_NUM < 504 | 81 | #if LUA_VERSION_NUM < 504 |
82 | 82 | ||
83 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, UserValueCount nuvalue_); | 83 | void* lua_newuserdatauv(lua_State* L_, size_t sz_, UserValueCount nuvalue_); |
84 | int lua_getiuservalue(lua_State* L_, StackIndex idx_, int n_); | 84 | int lua_getiuservalue(lua_State* L_, StackIndex idx_, UserValueIndex n_); |
85 | int lua_setiuservalue(lua_State* L_, StackIndex idx_, int n_); | 85 | int lua_setiuservalue(lua_State* L_, StackIndex idx_, UserValueIndex n_); |
86 | 86 | ||
87 | #define LUA_GNAME "_G" | 87 | #define LUA_GNAME "_G" |
88 | 88 | ||
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 { | |||
63 | class KeyUD | 63 | class KeyUD |
64 | { | 64 | { |
65 | private: | 65 | private: |
66 | static constexpr int kContentsTableIndex{ 1 }; | 66 | static constexpr UserValueIndex kContentsTableIndex{ 1 }; |
67 | 67 | ||
68 | public: | 68 | public: |
69 | static constexpr std::string_view kUnder{ "under" }; | 69 | 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) | |||
150 | if (_stored == 0) { | 150 | if (_stored == 0) { |
151 | 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"); | 151 | 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"); |
152 | } | 152 | } |
153 | lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} | 153 | lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} |
154 | for (int _i = 2; _i <= _stored; ++_i) { | 154 | for (int _i = 2; _i <= _stored; ++_i) { |
155 | lua_rawgeti(L_, 2, _i); // L_: lane {uv} results2...N | 155 | lua_rawgeti(L_, 2, _i); // L_: lane {uv} results2...N |
156 | } | 156 | } |
@@ -163,7 +163,7 @@ static LUAG_FUNC(thread_join) | |||
163 | case Lane::Error: | 163 | case Lane::Error: |
164 | { | 164 | { |
165 | LUA_ASSERT(L_, _stored == 2 || _stored == 3); | 165 | LUA_ASSERT(L_, _stored == 2 || _stored == 3); |
166 | lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} | 166 | lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} |
167 | lua_rawgeti(L_, 2, 2); // L_: lane {uv} <error> | 167 | lua_rawgeti(L_, 2, 2); // L_: lane {uv} <error> |
168 | lua_rawgeti(L_, 2, 3); // L_: lane {uv} <error> <trace>|nil | 168 | lua_rawgeti(L_, 2, 3); // L_: lane {uv} <error> <trace>|nil |
169 | if (lua_isnil(L_, -1)) { | 169 | if (lua_isnil(L_, -1)) { |
@@ -178,7 +178,7 @@ static LUAG_FUNC(thread_join) | |||
178 | 178 | ||
179 | case Lane::Cancelled: | 179 | case Lane::Cancelled: |
180 | LUA_ASSERT(L_, _stored == 2); | 180 | LUA_ASSERT(L_, _stored == 2); |
181 | lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane {uv} | 181 | lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane {uv} |
182 | lua_rawgeti(L_, 2, 2); // L_: lane {uv} cancel_error | 182 | lua_rawgeti(L_, 2, 2); // L_: lane {uv} cancel_error |
183 | lua_rawgeti(L_, 2, 1); // L_: lane {uv} cancel_error nil | 183 | lua_rawgeti(L_, 2, 1); // L_: lane {uv} cancel_error nil |
184 | lua_replace(L_, -3); // L_: lane nil cancel_error | 184 | lua_replace(L_, -3); // L_: lane nil cancel_error |
@@ -828,7 +828,7 @@ static LUAG_FUNC(lane_gc) | |||
828 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: ud | 828 | Lane* const _lane{ ToLane(L_, StackIndex{ 1 }) }; // L_: ud |
829 | 829 | ||
830 | // if there a gc callback? | 830 | // if there a gc callback? |
831 | lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: ud uservalue | 831 | lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: ud uservalue |
832 | kLaneGC.pushKey(L_); // L_: ud uservalue __gc | 832 | kLaneGC.pushKey(L_); // L_: ud uservalue __gc |
833 | lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil | 833 | lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil |
834 | if (!lua_isnil(L_, -1)) { | 834 | if (!lua_isnil(L_, -1)) { |
@@ -1070,7 +1070,7 @@ void Lane::pushIndexedResult(lua_State* const L_, int const key_) const | |||
1070 | LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); // L_: lane ... | 1070 | LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); // L_: lane ... |
1071 | STACK_GROW(L_, 3); | 1071 | STACK_GROW(L_, 3); |
1072 | 1072 | ||
1073 | lua_getiuservalue(L_, kIdxSelf, 1); // L_: lane ... {uv} | 1073 | lua_getiuservalue(L_, kIdxSelf, UserValueIndex{ 1 }); // L_: lane ... {uv} |
1074 | if (status != Lane::Error) { | 1074 | if (status != Lane::Error) { |
1075 | lua_rawgeti(L_, -1, key_); // L_: lane ... {uv} uv[i] | 1075 | lua_rawgeti(L_, -1, key_); // L_: lane ... {uv} uv[i] |
1076 | lua_remove(L_, -2); // L_: lane ... uv[i] | 1076 | lua_remove(L_, -2); // L_: lane ... uv[i] |
@@ -1138,7 +1138,7 @@ void Lane::resetResultsStorage(lua_State* const L_, StackIndex const self_idx_) | |||
1138 | // create the new table | 1138 | // create the new table |
1139 | lua_newtable(L_); // L_: ... self ... {} | 1139 | lua_newtable(L_); // L_: ... self ... {} |
1140 | // get the current table | 1140 | // get the current table |
1141 | lua_getiuservalue(L_, _self_idx, 1); // L_: ... self ... {} {uv} | 1141 | lua_getiuservalue(L_, _self_idx, UserValueIndex{ 1 }); // L_: ... self ... {} {uv} |
1142 | LUA_ASSERT(L_, lua_istable(L_, -1)); | 1142 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
1143 | // read gc_cb from the current table | 1143 | // read gc_cb from the current table |
1144 | kLaneGC.pushKey(L_); // L_: ... self ... {} {uv} kLaneGC | 1144 | kLaneGC.pushKey(L_); // L_: ... self ... {} {uv} kLaneGC |
@@ -1149,7 +1149,7 @@ void Lane::resetResultsStorage(lua_State* const L_, StackIndex const self_idx_) | |||
1149 | // we can forget the old table | 1149 | // we can forget the old table |
1150 | lua_pop(L_, 1); // L_: ... self ... {} | 1150 | lua_pop(L_, 1); // L_: ... self ... {} |
1151 | // and store the new one | 1151 | // and store the new one |
1152 | lua_setiuservalue(L_, _self_idx, 1); // L_: ... self ... | 1152 | lua_setiuservalue(L_, _self_idx, UserValueIndex{ 1 }); // L_: ... self ... |
1153 | STACK_CHECK(L_, 0); | 1153 | STACK_CHECK(L_, 0); |
1154 | } | 1154 | } |
1155 | 1155 | ||
@@ -1161,7 +1161,7 @@ void Lane::securizeDebugName(lua_State* const L_) | |||
1161 | STACK_CHECK_START_REL(L_, 0); | 1161 | STACK_CHECK_START_REL(L_, 0); |
1162 | STACK_GROW(L_, 3); | 1162 | STACK_GROW(L_, 3); |
1163 | // a Lane's uservalue should be a table | 1163 | // a Lane's uservalue should be a table |
1164 | lua_getiuservalue(L_, StackIndex{ 1 }, 1); // L_: lane ... {uv} | 1164 | lua_getiuservalue(L_, StackIndex{ 1 }, UserValueIndex{ 1 }); // L_: lane ... {uv} |
1165 | LUA_ASSERT(L_, lua_istable(L_, -1)); | 1165 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
1166 | // we don't care about the actual key, so long as it's unique and can't collide with anything. | 1166 | // we don't care about the actual key, so long as it's unique and can't collide with anything. |
1167 | lua_newtable(L_); // L_: lane ... {uv} {} | 1167 | lua_newtable(L_); // L_: lane ... {uv} {} |
@@ -1195,7 +1195,7 @@ int Lane::storeResults(lua_State* const L_) | |||
1195 | LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); | 1195 | LUA_ASSERT(L_, ToLane(L_, kIdxSelf) == this); |
1196 | 1196 | ||
1197 | STACK_CHECK_START_REL(L_, 0); | 1197 | STACK_CHECK_START_REL(L_, 0); |
1198 | lua_getiuservalue(L_, kIdxSelf, 1); // L_: lane ... {uv} | 1198 | lua_getiuservalue(L_, kIdxSelf, UserValueIndex{ 1 }); // L_: lane ... {uv} |
1199 | StackIndex const _tidx{ lua_gettop(L_) }; | 1199 | StackIndex const _tidx{ lua_gettop(L_) }; |
1200 | 1200 | ||
1201 | int _stored{}; | 1201 | 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) | |||
350 | } | 350 | } |
351 | STACK_CHECK(L, 2); | 351 | STACK_CHECK(L, 2); |
352 | // store the uservalue in the Lane full userdata | 352 | // store the uservalue in the Lane full userdata |
353 | lua_setiuservalue(L, StackIndex{ -2 }, 1); // L: ... lane | 353 | lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane |
354 | 354 | ||
355 | lua_State* const _L2{ lane->L }; | 355 | lua_State* const _L2{ lane->L }; |
356 | STACK_CHECK_START_REL(_L2, 0); | 356 | 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. | |||
125 | STACK_CHECK(L_, 2); | 125 | STACK_CHECK(L_, 2); |
126 | // search in the object's uservalues | 126 | // search in the object's uservalues |
127 | { | 127 | { |
128 | int _uvi{ 1 }; | 128 | UserValueIndex _uvi{ 1 }; |
129 | while (lua_getiuservalue(L_, kIdxTop, _uvi) != LUA_TNONE) { // L_: o "r" {c} {fqn} ... {?} k U {u} | 129 | while (lua_getiuservalue(L_, kIdxTop, _uvi) != LUA_TNONE) { // L_: o "r" {c} {fqn} ... {?} k U {u} |
130 | if (lua_istable(L_, -1)) { // if it is a table, look inside | 130 | if (lua_istable(L_, -1)) { // if it is a table, look inside |
131 | ++depth_; | 131 | ++depth_; |