diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-13 16:26:02 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-13 18:15:46 +0200 |
commit | 2e61dc33af885974a2a3a3f8a504061abe91bd71 (patch) | |
tree | 95194cdbf3b5352d23ae5a2618ac08358ede1130 /src | |
parent | 0b7c49b9a93ab153ff1f6d50189a4afee2056ae2 (diff) | |
download | lanes-2e61dc33af885974a2a3a3f8a504061abe91bd71.tar.gz lanes-2e61dc33af885974a2a3a3f8a504061abe91bd71.tar.bz2 lanes-2e61dc33af885974a2a3a3f8a504061abe91bd71.zip |
Progressively applying the coding rules
Diffstat (limited to 'src')
-rw-r--r-- | src/cancel.cpp | 70 | ||||
-rw-r--r-- | src/compat.cpp | 12 | ||||
-rw-r--r-- | src/deep.cpp | 64 | ||||
-rw-r--r-- | src/intercopycontext.cpp | 399 | ||||
-rw-r--r-- | src/keeper.cpp | 328 | ||||
-rw-r--r-- | src/macros_and_utils.h | 10 |
6 files changed, 442 insertions, 441 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp index 8356169..b297c85 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp | |||
@@ -51,9 +51,9 @@ THE SOFTWARE. | |||
51 | */ | 51 | */ |
52 | [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) | 52 | [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) |
53 | { | 53 | { |
54 | Lane* const lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; | 54 | Lane* const _lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) }; |
55 | // 'lane' is nullptr for the original main state (and no-one can cancel that) | 55 | // 'lane' is nullptr for the original main state (and no-one can cancel that) |
56 | return lane ? lane->cancelRequest : CancelRequest::None; | 56 | return _lane ? _lane->cancelRequest : CancelRequest::None; |
57 | } | 57 | } |
58 | 58 | ||
59 | // ################################################################################################# | 59 | // ################################################################################################# |
@@ -66,8 +66,8 @@ THE SOFTWARE. | |||
66 | // | 66 | // |
67 | LUAG_FUNC(cancel_test) | 67 | LUAG_FUNC(cancel_test) |
68 | { | 68 | { |
69 | CancelRequest test{ cancel_test(L_) }; | 69 | CancelRequest _test{ cancel_test(L_) }; |
70 | lua_pushboolean(L_, test != CancelRequest::None); | 70 | lua_pushboolean(L_, _test != CancelRequest::None); |
71 | return 1; | 71 | return 1; |
72 | } | 72 | } |
73 | 73 | ||
@@ -110,9 +110,9 @@ LUAG_FUNC(cancel_test) | |||
110 | lane_->cancelRequest = CancelRequest::Soft; // it's now signaled to stop | 110 | lane_->cancelRequest = CancelRequest::Soft; // it's now signaled to stop |
111 | // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own | 111 | // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own |
112 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired | 112 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired |
113 | std::condition_variable* const waiting_on{ lane_->waiting_on }; | 113 | std::condition_variable* const _waiting_on{ lane_->waiting_on }; |
114 | if (lane_->status == Lane::Waiting && waiting_on != nullptr) { | 114 | if (lane_->status == Lane::Waiting && _waiting_on != nullptr) { |
115 | waiting_on->notify_all(); | 115 | _waiting_on->notify_all(); |
116 | } | 116 | } |
117 | } | 117 | } |
118 | 118 | ||
@@ -126,9 +126,9 @@ LUAG_FUNC(cancel_test) | |||
126 | lane_->cancelRequest = CancelRequest::Hard; // it's now signaled to stop | 126 | lane_->cancelRequest = CancelRequest::Hard; // it's now signaled to stop |
127 | // lane_->thread.get_stop_source().request_stop(); | 127 | // lane_->thread.get_stop_source().request_stop(); |
128 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired | 128 | if (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired |
129 | std::condition_variable* waiting_on = lane_->waiting_on; | 129 | std::condition_variable* const _waiting_on{ lane_->waiting_on }; |
130 | if (lane_->status == Lane::Waiting && waiting_on != nullptr) { | 130 | if (lane_->status == Lane::Waiting && _waiting_on != nullptr) { |
131 | waiting_on->notify_all(); | 131 | _waiting_on->notify_all(); |
132 | } | 132 | } |
133 | } | 133 | } |
134 | 134 | ||
@@ -163,21 +163,21 @@ CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, std::chron | |||
163 | 163 | ||
164 | CancelOp which_cancel_op(char const* opString_) | 164 | CancelOp which_cancel_op(char const* opString_) |
165 | { | 165 | { |
166 | CancelOp op{ CancelOp::Invalid }; | 166 | CancelOp _op{ CancelOp::Invalid }; |
167 | if (strcmp(opString_, "hard") == 0) { | 167 | if (strcmp(opString_, "hard") == 0) { |
168 | op = CancelOp::Hard; | 168 | _op = CancelOp::Hard; |
169 | } else if (strcmp(opString_, "soft") == 0) { | 169 | } else if (strcmp(opString_, "soft") == 0) { |
170 | op = CancelOp::Soft; | 170 | _op = CancelOp::Soft; |
171 | } else if (strcmp(opString_, "call") == 0) { | 171 | } else if (strcmp(opString_, "call") == 0) { |
172 | op = CancelOp::MaskCall; | 172 | _op = CancelOp::MaskCall; |
173 | } else if (strcmp(opString_, "ret") == 0) { | 173 | } else if (strcmp(opString_, "ret") == 0) { |
174 | op = CancelOp::MaskRet; | 174 | _op = CancelOp::MaskRet; |
175 | } else if (strcmp(opString_, "line") == 0) { | 175 | } else if (strcmp(opString_, "line") == 0) { |
176 | op = CancelOp::MaskLine; | 176 | _op = CancelOp::MaskLine; |
177 | } else if (strcmp(opString_, "count") == 0) { | 177 | } else if (strcmp(opString_, "count") == 0) { |
178 | op = CancelOp::MaskCount; | 178 | _op = CancelOp::MaskCount; |
179 | } | 179 | } |
180 | return op; | 180 | return _op; |
181 | } | 181 | } |
182 | 182 | ||
183 | // ################################################################################################# | 183 | // ################################################################################################# |
@@ -185,13 +185,13 @@ CancelOp which_cancel_op(char const* opString_) | |||
185 | [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) | 185 | [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) |
186 | { | 186 | { |
187 | if (lua_type(L_, idx_) == LUA_TSTRING) { | 187 | if (lua_type(L_, idx_) == LUA_TSTRING) { |
188 | char const* const str{ lua_tostring(L_, idx_) }; | 188 | char const* const _str{ lua_tostring(L_, idx_) }; |
189 | CancelOp op{ which_cancel_op(str) }; | 189 | CancelOp _op{ which_cancel_op(_str) }; |
190 | lua_remove(L_, idx_); // argument is processed, remove it | 190 | lua_remove(L_, idx_); // argument is processed, remove it |
191 | if (op == CancelOp::Invalid) { | 191 | if (_op == CancelOp::Invalid) { |
192 | raise_luaL_error(L_, "invalid hook option %s", str); | 192 | raise_luaL_error(L_, "invalid hook option %s", _str); |
193 | } | 193 | } |
194 | return op; | 194 | return _op; |
195 | } | 195 | } |
196 | return CancelOp::Hard; | 196 | return CancelOp::Hard; |
197 | } | 197 | } |
@@ -201,23 +201,23 @@ CancelOp which_cancel_op(char const* opString_) | |||
201 | // bool[,reason] = lane_h:cancel( [mode, hookcount] [, timeout] [, wake_lane]) | 201 | // bool[,reason] = lane_h:cancel( [mode, hookcount] [, timeout] [, wake_lane]) |
202 | LUAG_FUNC(thread_cancel) | 202 | LUAG_FUNC(thread_cancel) |
203 | { | 203 | { |
204 | Lane* const lane{ ToLane(L_, 1) }; | 204 | Lane* const _lane{ ToLane(L_, 1) }; |
205 | CancelOp const op{ which_cancel_op(L_, 2) }; // this removes the op string from the stack | 205 | CancelOp const _op{ which_cancel_op(L_, 2) }; // this removes the op string from the stack |
206 | 206 | ||
207 | int hook_count{ 0 }; | 207 | int _hook_count{ 0 }; |
208 | if (static_cast<int>(op) > static_cast<int>(CancelOp::Soft)) { // hook is requested | 208 | if (static_cast<int>(_op) > static_cast<int>(CancelOp::Soft)) { // hook is requested |
209 | hook_count = static_cast<int>(luaL_checkinteger(L_, 2)); | 209 | _hook_count = static_cast<int>(luaL_checkinteger(L_, 2)); |
210 | lua_remove(L_, 2); // argument is processed, remove it | 210 | lua_remove(L_, 2); // argument is processed, remove it |
211 | if (hook_count < 1) { | 211 | if (_hook_count < 1) { |
212 | raise_luaL_error(L_, "hook count cannot be < 1"); | 212 | raise_luaL_error(L_, "hook count cannot be < 1"); |
213 | } | 213 | } |
214 | } | 214 | } |
215 | 215 | ||
216 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | 216 | std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; |
217 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion | 217 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion |
218 | lua_Duration const duration{ lua_tonumber(L_, 2) }; | 218 | lua_Duration const duration{ lua_tonumber(L_, 2) }; |
219 | if (duration.count() >= 0.0) { | 219 | if (duration.count() >= 0.0) { |
220 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); | 220 | _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); |
221 | } else { | 221 | } else { |
222 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); | 222 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); |
223 | } | 223 | } |
@@ -227,16 +227,16 @@ LUAG_FUNC(thread_cancel) | |||
227 | } | 227 | } |
228 | 228 | ||
229 | // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired | 229 | // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired |
230 | bool wake_lane{ op != CancelOp::Soft }; | 230 | bool _wake_lane{ _op != CancelOp::Soft }; |
231 | if (lua_gettop(L_) >= 2) { | 231 | if (lua_gettop(L_) >= 2) { |
232 | if (!lua_isboolean(L_, 2)) { | 232 | if (!lua_isboolean(L_, 2)) { |
233 | raise_luaL_error(L_, "wake_lindas parameter is not a boolean"); | 233 | raise_luaL_error(L_, "wake_lindas parameter is not a boolean"); |
234 | } | 234 | } |
235 | wake_lane = lua_toboolean(L_, 2); | 235 | _wake_lane = lua_toboolean(L_, 2); |
236 | lua_remove(L_, 2); // argument is processed, remove it | 236 | lua_remove(L_, 2); // argument is processed, remove it |
237 | } | 237 | } |
238 | STACK_CHECK_START_REL(L_, 0); | 238 | STACK_CHECK_START_REL(L_, 0); |
239 | switch (thread_cancel(lane, op, hook_count, until, wake_lane)) { | 239 | switch (thread_cancel(_lane, _op, _hook_count, _until, _wake_lane)) { |
240 | default: // should never happen unless we added a case and forgot to handle it | 240 | default: // should never happen unless we added a case and forgot to handle it |
241 | LUA_ASSERT(L_, false); | 241 | LUA_ASSERT(L_, false); |
242 | break; | 242 | break; |
@@ -248,7 +248,7 @@ LUAG_FUNC(thread_cancel) | |||
248 | 248 | ||
249 | case CancelResult::Cancelled: | 249 | case CancelResult::Cancelled: |
250 | lua_pushboolean(L_, 1); // true | 250 | lua_pushboolean(L_, 1); // true |
251 | lane->pushThreadStatus(L_); // true status | 251 | _lane->pushThreadStatus(L_); // true status |
252 | break; | 252 | break; |
253 | } | 253 | } |
254 | STACK_CHECK(L_, 2); | 254 | STACK_CHECK(L_, 2); |
diff --git a/src/compat.cpp b/src/compat.cpp index 4e8025e..b45cce2 100644 --- a/src/compat.cpp +++ b/src/compat.cpp | |||
@@ -14,15 +14,15 @@ | |||
14 | LuaType luaG_getmodule(lua_State* L_, char const* name_) | 14 | LuaType luaG_getmodule(lua_State* L_, char const* name_) |
15 | { | 15 | { |
16 | STACK_CHECK_START_REL(L_, 0); | 16 | STACK_CHECK_START_REL(L_, 0); |
17 | LuaType type{ static_cast<LuaType>(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil | 17 | LuaType _type{ static_cast<LuaType>(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil |
18 | if (type != LuaType::TABLE) { // L_: _R._LOADED|nil | 18 | if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil |
19 | STACK_CHECK(L_, 1); | 19 | STACK_CHECK(L_, 1); |
20 | return type; | 20 | return _type; |
21 | } | 21 | } |
22 | type = static_cast<LuaType>(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil | 22 | _type = static_cast<LuaType>(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil |
23 | lua_remove(L_, -2); // L_: {module}|nil | 23 | lua_remove(L_, -2); // L_: {module}|nil |
24 | STACK_CHECK(L_, 1); | 24 | STACK_CHECK(L_, 1); |
25 | return type; | 25 | return _type; |
26 | } | 26 | } |
27 | 27 | ||
28 | // ################################################################################################# | 28 | // ################################################################################################# |
diff --git a/src/deep.cpp b/src/deep.cpp index e0c2a39..9474666 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -59,6 +59,8 @@ static constexpr RegistryUniqueKey kDeepLookupRegKey{ 0xC6788345703C6059ull }; | |||
59 | */ | 59 | */ |
60 | static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull }; | 60 | static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull }; |
61 | 61 | ||
62 | // ################################################################################################# | ||
63 | |||
62 | /* | 64 | /* |
63 | * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. | 65 | * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. |
64 | * Pops the both values off the stack. | 66 | * Pops the both values off the stack. |
@@ -106,9 +108,9 @@ static void LookupDeep(lua_State* L_) | |||
106 | { | 108 | { |
107 | // when looking inside a keeper, we are 100% sure the object is a deep userdata | 109 | // when looking inside a keeper, we are 100% sure the object is a deep userdata |
108 | if (mode_ == LookupMode::FromKeeper) { | 110 | if (mode_ == LookupMode::FromKeeper) { |
109 | DeepPrelude* const proxy{ *lua_tofulluserdata<DeepPrelude*>(L_, index_) }; | 111 | DeepPrelude* const _proxy{ *lua_tofulluserdata<DeepPrelude*>(L_, index_) }; |
110 | // we can (and must) cast and fetch the internally stored factory | 112 | // we can (and must) cast and fetch the internally stored factory |
111 | return &proxy->factory; | 113 | return &_proxy->factory; |
112 | } else { | 114 | } else { |
113 | // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database | 115 | // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database |
114 | // it is the only way to ensure that the userdata is indeed a deep userdata! | 116 | // it is the only way to ensure that the userdata is indeed a deep userdata! |
@@ -123,10 +125,10 @@ static void LookupDeep(lua_State* L_) | |||
123 | // replace metatable with the factory pointer, if it is actually a deep userdata | 125 | // replace metatable with the factory pointer, if it is actually a deep userdata |
124 | LookupDeep(L_); // L_: deep ... factory|nil | 126 | LookupDeep(L_); // L_: deep ... factory|nil |
125 | 127 | ||
126 | DeepFactory* const ret{ lua_tolightuserdata<DeepFactory>(L_, -1) }; // nullptr if not a userdata | 128 | DeepFactory* const _ret{ lua_tolightuserdata<DeepFactory>(L_, -1) }; // nullptr if not a userdata |
127 | lua_pop(L_, 1); | 129 | lua_pop(L_, 1); |
128 | STACK_CHECK(L_, 0); | 130 | STACK_CHECK(L_, 0); |
129 | return ret; | 131 | return _ret; |
130 | } | 132 | } |
131 | } | 133 | } |
132 | 134 | ||
@@ -149,12 +151,12 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
149 | */ | 151 | */ |
150 | [[nodiscard]] static int deep_userdata_gc(lua_State* L_) | 152 | [[nodiscard]] static int deep_userdata_gc(lua_State* L_) |
151 | { | 153 | { |
152 | DeepPrelude* const* const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, 1) }; | 154 | DeepPrelude* const* const _proxy{ lua_tofulluserdata<DeepPrelude*>(L_, 1) }; |
153 | DeepPrelude* const p{ *proxy }; | 155 | DeepPrelude* const _p{ *_proxy }; |
154 | 156 | ||
155 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded | 157 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded |
156 | // in that case, we are not multithreaded and locking isn't necessary anyway | 158 | // in that case, we are not multithreaded and locking isn't necessary anyway |
157 | bool const isLastRef{ p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; | 159 | bool const isLastRef{ _p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; |
158 | 160 | ||
159 | if (isLastRef) { | 161 | if (isLastRef) { |
160 | // retrieve wrapped __gc | 162 | // retrieve wrapped __gc |
@@ -166,7 +168,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) | |||
166 | // need an empty stack in case we are GC_ing from a Keeper, so that empty stack checks aren't triggered | 168 | // need an empty stack in case we are GC_ing from a Keeper, so that empty stack checks aren't triggered |
167 | lua_pop(L_, 2); // L_: | 169 | lua_pop(L_, 2); // L_: |
168 | } | 170 | } |
169 | DeepFactory::DeleteDeepObject(L_, p); | 171 | DeepFactory::DeleteDeepObject(L_, _p); |
170 | } | 172 | } |
171 | return 0; | 173 | return 0; |
172 | } | 174 | } |
@@ -219,9 +221,9 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int | |||
219 | STACK_CHECK_START_REL(L_, 0); | 221 | STACK_CHECK_START_REL(L_, 0); |
220 | 222 | ||
221 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) | 223 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) |
222 | DeepPrelude** const proxy{ lua_newuserdatauv<DeepPrelude*>(L_, nuv_) }; // L_: DPC proxy | 224 | DeepPrelude** const _proxy{ lua_newuserdatauv<DeepPrelude*>(L_, nuv_) }; // L_: DPC proxy |
223 | LUA_ASSERT(L_, proxy); | 225 | LUA_ASSERT(L_, _proxy); |
224 | *proxy = prelude_; | 226 | *_proxy = prelude_; |
225 | prelude_->refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data | 227 | prelude_->refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data |
226 | 228 | ||
227 | // Get/create metatable for 'factory' (in this state) | 229 | // Get/create metatable for 'factory' (in this state) |
@@ -231,13 +233,13 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int | |||
231 | 233 | ||
232 | if (lua_isnil(L_, -1)) { // No metatable yet. | 234 | if (lua_isnil(L_, -1)) { // No metatable yet. |
233 | lua_pop(L_, 1); // L_: DPC proxy | 235 | lua_pop(L_, 1); // L_: DPC proxy |
234 | int const oldtop{ lua_gettop(L_) }; | 236 | int const _oldtop{ lua_gettop(L_) }; |
235 | // 1 - make one and register it | 237 | // 1 - make one and register it |
236 | if (mode_ != LookupMode::ToKeeper) { | 238 | if (mode_ != LookupMode::ToKeeper) { |
237 | factory.createMetatable(L_); // L_: DPC proxy metatable | 239 | factory.createMetatable(L_); // L_: DPC proxy metatable |
238 | if (lua_gettop(L_) - oldtop != 1 || !lua_istable(L_, -1)) { | 240 | if (lua_gettop(L_) - _oldtop != 1 || !lua_istable(L_, -1)) { |
239 | // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR! | 241 | // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR! |
240 | lua_settop(L_, oldtop); // L_: DPC proxy X | 242 | lua_settop(L_, _oldtop); // L_: DPC proxy X |
241 | lua_pop(L_, 3); // L_: | 243 | lua_pop(L_, 3); // L_: |
242 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; | 244 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; |
243 | } | 245 | } |
@@ -262,12 +264,12 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int | |||
262 | factory.storeDeepLookup(L_); | 264 | factory.storeDeepLookup(L_); |
263 | 265 | ||
264 | // 2 - cause the target state to require the module that exported the factory | 266 | // 2 - cause the target state to require the module that exported the factory |
265 | if (char const* const modname{ factory.moduleName() }; modname) { // we actually got a module name | 267 | if (char const* const _modname{ factory.moduleName() }; _modname) { // we actually got a module name |
266 | // L.registry._LOADED exists without having registered the 'package' library. | 268 | // L.registry._LOADED exists without having registered the 'package' library. |
267 | lua_getglobal(L_, "require"); // DPC proxy metatable require() | 269 | lua_getglobal(L_, "require"); // DPC proxy metatable require() |
268 | // check that the module is already loaded (or being loaded, we are happy either way) | 270 | // check that the module is already loaded (or being loaded, we are happy either way) |
269 | if (lua_isfunction(L_, -1)) { | 271 | if (lua_isfunction(L_, -1)) { |
270 | lua_pushstring(L_, modname); // L_: DPC proxy metatable require() "module" | 272 | lua_pushstring(L_, _modname); // L_: DPC proxy metatable require() "module" |
271 | lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // L_: DPC proxy metatable require() "module" _R._LOADED | 273 | lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // L_: DPC proxy metatable require() "module" _R._LOADED |
272 | if (lua_istable(L_, -1)) { | 274 | if (lua_istable(L_, -1)) { |
273 | lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" | 275 | lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" |
@@ -280,7 +282,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int | |||
280 | require_result = lua_pcall(L_, 1, 0, 0); // L_: DPC proxy metatable error? | 282 | require_result = lua_pcall(L_, 1, 0, 0); // L_: DPC proxy metatable error? |
281 | if (require_result != LUA_OK) { | 283 | if (require_result != LUA_OK) { |
282 | // failed, return the error message | 284 | // failed, return the error message |
283 | lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); | 285 | lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", _modname); |
284 | lua_insert(L_, -2); // L_: DPC proxy metatable prefix error | 286 | lua_insert(L_, -2); // L_: DPC proxy metatable prefix error |
285 | lua_concat(L_, 2); // L_: DPC proxy metatable error | 287 | lua_concat(L_, 2); // L_: DPC proxy metatable error |
286 | return lua_tostring(L_, -1); | 288 | return lua_tostring(L_, -1); |
@@ -334,30 +336,30 @@ int DeepFactory::pushDeepUserdata(DestState L_, int nuv_) const | |||
334 | { | 336 | { |
335 | STACK_GROW(L_, 1); | 337 | STACK_GROW(L_, 1); |
336 | STACK_CHECK_START_REL(L_, 0); | 338 | STACK_CHECK_START_REL(L_, 0); |
337 | int const oldtop{ lua_gettop(L_) }; | 339 | int const _oldtop{ lua_gettop(L_) }; |
338 | DeepPrelude* const prelude{ newDeepObjectInternal(L_) }; | 340 | DeepPrelude* const _prelude{ newDeepObjectInternal(L_) }; |
339 | if (prelude == nullptr) { | 341 | if (_prelude == nullptr) { |
340 | raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); | 342 | raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); |
341 | } | 343 | } |
342 | 344 | ||
343 | if (prelude->magic != kDeepVersion) { | 345 | if (_prelude->magic != kDeepVersion) { |
344 | // just in case, don't leak the newly allocated deep userdata object | 346 | // just in case, don't leak the newly allocated deep userdata object |
345 | deleteDeepObjectInternal(L_, prelude); | 347 | deleteDeepObjectInternal(L_, _prelude); |
346 | raise_luaL_error(L_, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); | 348 | raise_luaL_error(L_, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); |
347 | } | 349 | } |
348 | 350 | ||
349 | LUA_ASSERT(L_, prelude->refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 | 351 | LUA_ASSERT(L_, _prelude->refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 |
350 | LUA_ASSERT(L_, &prelude->factory == this); | 352 | LUA_ASSERT(L_, &_prelude->factory == this); |
351 | 353 | ||
352 | if (lua_gettop(L_) - oldtop != 0) { | 354 | if (lua_gettop(L_) - _oldtop != 0) { |
353 | // just in case, don't leak the newly allocated deep userdata object | 355 | // just in case, don't leak the newly allocated deep userdata object |
354 | deleteDeepObjectInternal(L_, prelude); | 356 | deleteDeepObjectInternal(L_, _prelude); |
355 | raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); | 357 | raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); |
356 | } | 358 | } |
357 | 359 | ||
358 | char const* const errmsg{ DeepFactory::PushDeepProxy(L_, prelude, nuv_, LookupMode::LaneBody) }; // proxy | 360 | char const* const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy |
359 | if (errmsg != nullptr) { | 361 | if (_err != nullptr) { |
360 | raise_luaL_error(L_, errmsg); | 362 | raise_luaL_error(L_, _err); |
361 | } | 363 | } |
362 | STACK_CHECK(L_, 1); | 364 | STACK_CHECK(L_, 1); |
363 | return 1; | 365 | return 1; |
@@ -380,6 +382,6 @@ DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index_) const | |||
380 | } | 382 | } |
381 | STACK_CHECK(L_, 0); | 383 | STACK_CHECK(L_, 0); |
382 | 384 | ||
383 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, index_) }; | 385 | DeepPrelude** const _proxy{ lua_tofulluserdata<DeepPrelude*>(L_, index_) }; |
384 | return *proxy; | 386 | return *_proxy; |
385 | } | 387 | } |
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 07fcd77..10d620e 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp | |||
@@ -37,11 +37,11 @@ THE SOFTWARE. | |||
37 | // luckily, this also works with earlier Lua versions | 37 | // luckily, this also works with earlier Lua versions |
38 | [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) | 38 | [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) |
39 | { | 39 | { |
40 | luaL_Buffer* const B{ static_cast<luaL_Buffer*>(ud_) }; | 40 | luaL_Buffer* const _B{ static_cast<luaL_Buffer*>(ud_) }; |
41 | if (!B->L) { | 41 | if (!_B->L) { |
42 | luaL_buffinit(L_, B); | 42 | luaL_buffinit(L_, _B); |
43 | } | 43 | } |
44 | luaL_addlstring(B, static_cast<char const*>(b_), size_); | 44 | luaL_addlstring(_B, static_cast<char const*>(b_), size_); |
45 | return 0; | 45 | return 0; |
46 | } | 46 | } |
47 | 47 | ||
@@ -78,12 +78,12 @@ THE SOFTWARE. | |||
78 | STACK_CHECK_START_REL(L_, 0); | 78 | STACK_CHECK_START_REL(L_, 0); |
79 | STACK_GROW(L_, 3); // up to 3 slots are necessary on error | 79 | STACK_GROW(L_, 3); // up to 3 slots are necessary on error |
80 | if (mode_ == LookupMode::FromKeeper) { | 80 | if (mode_ == LookupMode::FromKeeper) { |
81 | lua_CFunction f = lua_tocfunction(L_, i_); // should *always* be one of the function sentinels | 81 | lua_CFunction const _f{ lua_tocfunction(L_, i_) }; // should *always* be one of the function sentinels |
82 | if (f == func_lookup_sentinel || f == table_lookup_sentinel || f == userdata_clone_sentinel) { | 82 | if (_f == func_lookup_sentinel || _f == table_lookup_sentinel || _f == userdata_clone_sentinel) { |
83 | lua_getupvalue(L_, i_, 1); // L_: ... v ... "f.q.n" | 83 | lua_getupvalue(L_, i_, 1); // L_: ... v ... "f.q.n" |
84 | } else { | 84 | } else { |
85 | // if this is not a sentinel, this is some user-created table we wanted to lookup | 85 | // if this is not a sentinel, this is some user-created table we wanted to lookup |
86 | LUA_ASSERT(L_, nullptr == f && lua_istable(L_, i_)); | 86 | LUA_ASSERT(L_, nullptr == _f && lua_istable(L_, i_)); |
87 | // push anything that will convert to nullptr string | 87 | // push anything that will convert to nullptr string |
88 | lua_pushnil(L_); // L_: ... v ... nil | 88 | lua_pushnil(L_); // L_: ... v ... nil |
89 | } | 89 | } |
@@ -95,13 +95,13 @@ THE SOFTWARE. | |||
95 | lua_pushvalue(L_, i_); // L_: ... v ... {} v | 95 | lua_pushvalue(L_, i_); // L_: ... v ... {} v |
96 | lua_rawget(L_, -2); // L_: ... v ... {} "f.q.n" | 96 | lua_rawget(L_, -2); // L_: ... v ... {} "f.q.n" |
97 | } | 97 | } |
98 | char const* fqn{ lua_tolstring(L_, -1, len_) }; | 98 | char const* _fqn{ lua_tolstring(L_, -1, len_) }; |
99 | DEBUGSPEW_CODE(Universe* const U = universe_get(L_)); | 99 | DEBUGSPEW_CODE(Universe* const U = universe_get(L_)); |
100 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), fqn)); | 100 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), _fqn)); |
101 | // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database | 101 | // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database |
102 | lua_pop(L_, (mode_ == LookupMode::FromKeeper) ? 1 : 2); // L_: ... v ... | 102 | lua_pop(L_, (mode_ == LookupMode::FromKeeper) ? 1 : 2); // L_: ... v ... |
103 | STACK_CHECK(L_, 0); | 103 | STACK_CHECK(L_, 0); |
104 | if (nullptr == fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) | 104 | if (nullptr == _fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) |
105 | *len_ = 0; // just in case | 105 | *len_ = 0; // just in case |
106 | // try to discover the name of the function we want to send | 106 | // try to discover the name of the function we want to send |
107 | lua_getglobal(L_, "decoda_name"); // L_: ... v ... decoda_name | 107 | lua_getglobal(L_, "decoda_name"); // L_: ... v ... decoda_name |
@@ -125,7 +125,7 @@ THE SOFTWARE. | |||
125 | raise_luaL_error(L_, "%s%s '%s' not found in %s origin transfer database.%s", typewhat, gotchaA, what, from ? from : "main", gotchaB); | 125 | raise_luaL_error(L_, "%s%s '%s' not found in %s origin transfer database.%s", typewhat, gotchaA, what, from ? from : "main", gotchaB); |
126 | } | 126 | } |
127 | STACK_CHECK(L_, 0); | 127 | STACK_CHECK(L_, 0); |
128 | return fqn; | 128 | return _fqn; |
129 | } | 129 | } |
130 | 130 | ||
131 | // ################################################################################################# | 131 | // ################################################################################################# |
@@ -147,26 +147,26 @@ static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; | |||
147 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} | 147 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} |
148 | lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? | 148 | lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? |
149 | 149 | ||
150 | lua_Integer id{ lua_tointeger(L_, -1) }; // 0 for nil | 150 | lua_Integer _id{ lua_tointeger(L_, -1) }; // 0 for nil |
151 | lua_pop(L_, 1); // L_: ... _R[kMtIdRegKey] | 151 | lua_pop(L_, 1); // L_: ... _R[kMtIdRegKey] |
152 | STACK_CHECK(L_, 1); | 152 | STACK_CHECK(L_, 1); |
153 | 153 | ||
154 | if (id == 0) { | 154 | if (_id == 0) { |
155 | id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); | 155 | _id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); |
156 | 156 | ||
157 | // Create two-way references: id_uint <-> table | 157 | // Create two-way references: id_uint <-> table |
158 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} | 158 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} |
159 | lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] {mt} id | 159 | lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] {mt} id |
160 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] | 160 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] |
161 | 161 | ||
162 | lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] id | 162 | lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] id |
163 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] id {mt} | 163 | lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] id {mt} |
164 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] | 164 | lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] |
165 | } | 165 | } |
166 | lua_pop(L_, 1); // L_: ... | 166 | lua_pop(L_, 1); // L_: ... |
167 | STACK_CHECK(L_, 0); | 167 | STACK_CHECK(L_, 0); |
168 | 168 | ||
169 | return id; | 169 | return _id; |
170 | } | 170 | } |
171 | 171 | ||
172 | // ################################################################################################# | 172 | // ################################################################################################# |
@@ -181,8 +181,8 @@ void InterCopyContext::copy_func() const | |||
181 | 181 | ||
182 | // 'lua_dump()' needs the function at top of stack | 182 | // 'lua_dump()' needs the function at top of stack |
183 | // if already on top of the stack, no need to push again | 183 | // if already on top of the stack, no need to push again |
184 | bool const needToPush{ L1_i != lua_gettop(L1) }; | 184 | bool const _needToPush{ L1_i != lua_gettop(L1) }; |
185 | if (needToPush) { | 185 | if (_needToPush) { |
186 | lua_pushvalue(L1, L1_i); // L1: ... f | 186 | lua_pushvalue(L1, L1_i); // L1: ... f |
187 | } | 187 | } |
188 | 188 | ||
@@ -191,8 +191,7 @@ void InterCopyContext::copy_func() const | |||
191 | // to the writer" (and we only return 0) | 191 | // to the writer" (and we only return 0) |
192 | // not sure this could ever fail but for memory shortage reasons | 192 | // not sure this could ever fail but for memory shortage reasons |
193 | // last parameter is Lua 5.4-specific (no stripping) | 193 | // last parameter is Lua 5.4-specific (no stripping) |
194 | luaL_Buffer B; | 194 | luaL_Buffer B{}; |
195 | B.L = nullptr; | ||
196 | if (lua504_dump(L1, buf_writer, &B, 0) != 0) { | 195 | if (lua504_dump(L1, buf_writer, &B, 0) != 0) { |
197 | raise_luaL_error(getErrL(), "internal error: function dump failed."); | 196 | raise_luaL_error(getErrL(), "internal error: function dump failed."); |
198 | } | 197 | } |
@@ -201,7 +200,7 @@ void InterCopyContext::copy_func() const | |||
201 | luaL_pushresult(&B); // L1: ... f b | 200 | luaL_pushresult(&B); // L1: ... f b |
202 | 201 | ||
203 | // if not pushed, no need to pop | 202 | // if not pushed, no need to pop |
204 | if (needToPush) { | 203 | if (_needToPush) { |
205 | lua_remove(L1, -2); // L1: ... b | 204 | lua_remove(L1, -2); // L1: ... b |
206 | } | 205 | } |
207 | 206 | ||
@@ -214,18 +213,18 @@ void InterCopyContext::copy_func() const | |||
214 | // stack and start the what string with the character '>'." | 213 | // stack and start the what string with the character '>'." |
215 | // | 214 | // |
216 | { | 215 | { |
217 | lua_Debug ar; | 216 | lua_Debug _ar; |
218 | lua_pushvalue(L1, L1_i); // L1: ... b f | 217 | lua_pushvalue(L1, L1_i); // L1: ... b f |
219 | // fills 'fname' 'namewhat' and 'linedefined', pops function | 218 | // fills 'fname' 'namewhat' and 'linedefined', pops function |
220 | lua_getinfo(L1, ">nS", &ar); // L1: ... b | 219 | lua_getinfo(L1, ">nS", &_ar); // L1: ... b |
221 | fname = ar.namewhat; | 220 | fname = _ar.namewhat; |
222 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), ar.short_src, ar.linedefined)); // just gives nullptr | 221 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), _ar.short_src, _ar.linedefined)); // just gives nullptr |
223 | } | 222 | } |
224 | #endif // LOG_FUNC_INFO | 223 | #endif // LOG_FUNC_INFO |
225 | { | 224 | { |
226 | size_t sz; | 225 | size_t _sz; |
227 | char const* s = lua_tolstring(L1, -1, &sz); // L1: ... b | 226 | char const* _s{ lua_tolstring(L1, -1, &_sz) }; // L1: ... b |
228 | LUA_ASSERT(L1, s && sz); | 227 | LUA_ASSERT(L1, _s && _sz); |
229 | STACK_GROW(L2, 2); | 228 | STACK_GROW(L2, 2); |
230 | // Note: Line numbers seem to be taken precisely from the | 229 | // Note: Line numbers seem to be taken precisely from the |
231 | // original function. 'fname' is not used since the chunk | 230 | // original function. 'fname' is not used since the chunk |
@@ -233,7 +232,7 @@ void InterCopyContext::copy_func() const | |||
233 | // | 232 | // |
234 | // TBD: Can we get the function's original name through, as well? | 233 | // TBD: Can we get the function's original name through, as well? |
235 | // | 234 | // |
236 | if (luaL_loadbuffer(L2, s, sz, fname) != 0) { // L2: ... {cache} ... p function | 235 | if (luaL_loadbuffer(L2, _s, _sz, fname) != 0) { // L2: ... {cache} ... p function |
237 | // chunk is precompiled so only LUA_ERRMEM can happen | 236 | // chunk is precompiled so only LUA_ERRMEM can happen |
238 | // "Otherwise, it pushes an error message" | 237 | // "Otherwise, it pushes an error message" |
239 | // | 238 | // |
@@ -259,15 +258,15 @@ void InterCopyContext::copy_func() const | |||
259 | */ | 258 | */ |
260 | int n{ 0 }; | 259 | int n{ 0 }; |
261 | { | 260 | { |
262 | InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; | 261 | InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; |
263 | #if LUA_VERSION_NUM >= 502 | 262 | #if LUA_VERSION_NUM >= 502 |
264 | // Starting with Lua 5.2, each Lua function gets its environment as one of its upvalues (named LUA_ENV, aka "_ENV" by default) | 263 | // Starting with Lua 5.2, each Lua function gets its environment as one of its upvalues (named LUA_ENV, aka "_ENV" by default) |
265 | // Generally this is LUA_RIDX_GLOBALS, which we don't want to copy from the source to the destination state... | 264 | // Generally this is LUA_RIDX_GLOBALS, which we don't want to copy from the source to the destination state... |
266 | // -> if we encounter an upvalue equal to the global table in the source, bind it to the destination's global table | 265 | // -> if we encounter an upvalue equal to the global table in the source, bind it to the destination's global table |
267 | lua_pushglobaltable(L1); // L1: ... _G | 266 | lua_pushglobaltable(L1); // L1: ... _G |
268 | #endif // LUA_VERSION_NUM | 267 | #endif // LUA_VERSION_NUM |
269 | for (n = 0; (c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] | 268 | for (n = 0; (_c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] |
270 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, c.name)); | 269 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, _c.name)); |
271 | #if LUA_VERSION_NUM >= 502 | 270 | #if LUA_VERSION_NUM >= 502 |
272 | if (lua_rawequal(L1, -1, -2)) { // is the upvalue equal to the global table? | 271 | if (lua_rawequal(L1, -1, -2)) { // is the upvalue equal to the global table? |
273 | DEBUGSPEW_CODE(fprintf(stderr, "pushing destination global scope\n")); | 272 | DEBUGSPEW_CODE(fprintf(stderr, "pushing destination global scope\n")); |
@@ -276,8 +275,8 @@ void InterCopyContext::copy_func() const | |||
276 | #endif // LUA_VERSION_NUM | 275 | #endif // LUA_VERSION_NUM |
277 | { | 276 | { |
278 | DEBUGSPEW_CODE(fprintf(stderr, "copying value\n")); | 277 | DEBUGSPEW_CODE(fprintf(stderr, "copying value\n")); |
279 | c.L1_i = SourceIndex{ lua_gettop(L1) }; | 278 | _c.L1_i = SourceIndex{ lua_gettop(L1) }; |
280 | if (!c.inter_copy_one()) { // L2: ... {cache} ... function <upvalues> | 279 | if (!_c.inter_copy_one()) { // L2: ... {cache} ... function <upvalues> |
281 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 280 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
282 | } | 281 | } |
283 | } | 282 | } |
@@ -292,13 +291,13 @@ void InterCopyContext::copy_func() const | |||
292 | STACK_CHECK(L1, 0); | 291 | STACK_CHECK(L1, 0); |
293 | 292 | ||
294 | // Set upvalues (originally set to 'nil' by 'lua_load') | 293 | // Set upvalues (originally set to 'nil' by 'lua_load') |
295 | for (int const func_index{ lua_gettop(L2) - n }; n > 0; --n) { | 294 | for (int const _func_index{ lua_gettop(L2) - n }; n > 0; --n) { |
296 | char const* rc{ lua_setupvalue(L2, func_index, n) }; // L2: ... {cache} ... function | 295 | char const* _rc{ lua_setupvalue(L2, _func_index, n) }; // L2: ... {cache} ... function |
297 | // | 296 | // |
298 | // "assigns the value at the top of the stack to the upvalue and returns its name. | 297 | // "assigns the value at the top of the stack to the upvalue and returns its name. |
299 | // It also pops the value from the stack." | 298 | // It also pops the value from the stack." |
300 | 299 | ||
301 | LUA_ASSERT(L1, rc); // not having enough slots? | 300 | LUA_ASSERT(L1, _rc); // not having enough slots? |
302 | } | 301 | } |
303 | // once all upvalues have been set we are left | 302 | // once all upvalues have been set we are left |
304 | // with the function at the top of the stack // L2: ... {cache} ... function | 303 | // with the function at the top of the stack // L2: ... {cache} ... function |
@@ -312,8 +311,8 @@ void InterCopyContext::copy_func() const | |||
312 | void InterCopyContext::lookup_native_func() const | 311 | void InterCopyContext::lookup_native_func() const |
313 | { | 312 | { |
314 | // get the name of the function we want to send | 313 | // get the name of the function we want to send |
315 | size_t len; | 314 | size_t _len; |
316 | char const* const fqn{ find_lookup_name(L1, L1_i, mode, name, &len) }; | 315 | char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; |
317 | // push the equivalent function in the destination's stack, retrieved from the lookup table | 316 | // push the equivalent function in the destination's stack, retrieved from the lookup table |
318 | STACK_CHECK_START_REL(L2, 0); | 317 | STACK_CHECK_START_REL(L2, 0); |
319 | STACK_GROW(L2, 3); // up to 3 slots are necessary on error | 318 | STACK_GROW(L2, 3); // up to 3 slots are necessary on error |
@@ -324,7 +323,7 @@ void InterCopyContext::lookup_native_func() const | |||
324 | 323 | ||
325 | case LookupMode::ToKeeper: | 324 | case LookupMode::ToKeeper: |
326 | // push a sentinel closure that holds the lookup name as upvalue | 325 | // push a sentinel closure that holds the lookup name as upvalue |
327 | lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: "f.q.n" | 326 | lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: "f.q.n" |
328 | lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f | 327 | lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f |
329 | break; | 328 | break; |
330 | 329 | ||
@@ -333,25 +332,25 @@ void InterCopyContext::lookup_native_func() const | |||
333 | kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} | 332 | kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} |
334 | STACK_CHECK(L2, 1); | 333 | STACK_CHECK(L2, 1); |
335 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 334 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
336 | lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: {} "f.q.n" | 335 | lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: {} "f.q.n" |
337 | lua_rawget(L2, -2); // L1: ... f ... L2: {} f | 336 | lua_rawget(L2, -2); // L1: ... f ... L2: {} f |
338 | // nil means we don't know how to transfer stuff: user should do something | 337 | // nil means we don't know how to transfer stuff: user should do something |
339 | // anything other than function or table should not happen! | 338 | // anything other than function or table should not happen! |
340 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { | 339 | if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { |
341 | lua_getglobal(L1, "decoda_name"); // L1: ... f ... decoda_name | 340 | lua_getglobal(L1, "decoda_name"); // L1: ... f ... decoda_name |
342 | char const* const from{ lua_tostring(L1, -1) }; | 341 | char const* const _from{ lua_tostring(L1, -1) }; |
343 | lua_pop(L1, 1); // L1: ... f ... | 342 | lua_pop(L1, 1); // L1: ... f ... |
344 | lua_getglobal(L2, "decoda_name"); // L1: ... f ... L2: {} f decoda_name | 343 | lua_getglobal(L2, "decoda_name"); // L1: ... f ... L2: {} f decoda_name |
345 | char const* const to{ lua_tostring(L2, -1) }; | 344 | char const* const _to{ lua_tostring(L2, -1) }; |
346 | lua_pop(L2, 1); // L2: {} f | 345 | lua_pop(L2, 1); // L2: {} f |
347 | // when mode_ == LookupMode::FromKeeper, L is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error | 346 | // when mode_ == LookupMode::FromKeeper, L is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error |
348 | raise_luaL_error( | 347 | raise_luaL_error( |
349 | getErrL(), | 348 | getErrL(), |
350 | "%s%s: function '%s' not found in %s destination transfer database.", | 349 | "%s%s: function '%s' not found in %s destination transfer database.", |
351 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", | 350 | lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", |
352 | from ? from : "main", | 351 | _from ? _from : "main", |
353 | fqn, | 352 | _fqn, |
354 | to ? to : "main"); | 353 | _to ? _to : "main"); |
355 | return; | 354 | return; |
356 | } | 355 | } |
357 | lua_remove(L2, -2); // L2: f | 356 | lua_remove(L2, -2); // L2: f |
@@ -381,10 +380,10 @@ void InterCopyContext::lookup_native_func() const | |||
381 | // Always pushes a function to 'L2'. | 380 | // Always pushes a function to 'L2'. |
382 | void InterCopyContext::copy_cached_func() const | 381 | void InterCopyContext::copy_cached_func() const |
383 | { | 382 | { |
384 | FuncSubType const funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; | 383 | FuncSubType const _funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; |
385 | if (funcSubType == FuncSubType::Bytecode) { | 384 | if (_funcSubType == FuncSubType::Bytecode) { |
386 | void* const aspointer = const_cast<void*>(lua_topointer(L1, L1_i)); | 385 | void* const _aspointer{ const_cast<void*>(lua_topointer(L1, L1_i)) }; |
387 | // TBD: Merge this and same code for tables | 386 | // TODO: Merge this and same code for tables |
388 | LUA_ASSERT(L1, L2_cache_i != 0); | 387 | LUA_ASSERT(L1, L2_cache_i != 0); |
389 | 388 | ||
390 | STACK_GROW(L2, 2); | 389 | STACK_GROW(L2, 2); |
@@ -397,7 +396,7 @@ void InterCopyContext::copy_cached_func() const | |||
397 | // is only for the duration of a copy (both states are locked). | 396 | // is only for the duration of a copy (both states are locked). |
398 | 397 | ||
399 | // push a light userdata uniquely representing the function | 398 | // push a light userdata uniquely representing the function |
400 | lua_pushlightuserdata(L2, aspointer); // L2: ... {cache} ... p | 399 | lua_pushlightuserdata(L2, _aspointer); // L2: ... {cache} ... p |
401 | 400 | ||
402 | // fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); | 401 | // fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); |
403 | 402 | ||
@@ -430,9 +429,9 @@ void InterCopyContext::copy_cached_func() const | |||
430 | [[nodiscard]] bool InterCopyContext::lookup_table() const | 429 | [[nodiscard]] bool InterCopyContext::lookup_table() const |
431 | { | 430 | { |
432 | // get the name of the table we want to send | 431 | // get the name of the table we want to send |
433 | size_t len; | 432 | size_t _len; |
434 | char const* fqn = find_lookup_name(L1, L1_i, mode, name, &len); | 433 | char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; |
435 | if (nullptr == fqn) { // name not found, it is some user-created table | 434 | if (nullptr == _fqn) { // name not found, it is some user-created table |
436 | return false; | 435 | return false; |
437 | } | 436 | } |
438 | // push the equivalent table in the destination's stack, retrieved from the lookup table | 437 | // push the equivalent table in the destination's stack, retrieved from the lookup table |
@@ -445,7 +444,7 @@ void InterCopyContext::copy_cached_func() const | |||
445 | 444 | ||
446 | case LookupMode::ToKeeper: | 445 | case LookupMode::ToKeeper: |
447 | // push a sentinel closure that holds the lookup name as upvalue | 446 | // push a sentinel closure that holds the lookup name as upvalue |
448 | lua_pushlstring(L2, fqn, len); // L1: ... t ... L2: "f.q.n" | 447 | lua_pushlstring(L2, _fqn, _len); // L1: ... t ... L2: "f.q.n" |
449 | lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f | 448 | lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f |
450 | break; | 449 | break; |
451 | 450 | ||
@@ -454,7 +453,7 @@ void InterCopyContext::copy_cached_func() const | |||
454 | kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} | 453 | kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} |
455 | STACK_CHECK(L2, 1); | 454 | STACK_CHECK(L2, 1); |
456 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 455 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
457 | lua_pushlstring(L2, fqn, len); // L2: {} "f.q.n" | 456 | lua_pushlstring(L2, _fqn, _len); // L2: {} "f.q.n" |
458 | lua_rawget(L2, -2); // L2: {} t | 457 | lua_rawget(L2, -2); // L2: {} t |
459 | // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) | 458 | // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) |
460 | // but not when we extract something out of a keeper, as there is nothing to clone! | 459 | // but not when we extract something out of a keeper, as there is nothing to clone! |
@@ -473,7 +472,7 @@ void InterCopyContext::copy_cached_func() const | |||
473 | getErrL(), | 472 | getErrL(), |
474 | "%s: source table '%s' found as %s in %s destination transfer database.", | 473 | "%s: source table '%s' found as %s in %s destination transfer database.", |
475 | from ? from : "main", | 474 | from ? from : "main", |
476 | fqn, | 475 | _fqn, |
477 | lua_typename(L2, lua_type_as_enum(L2, -1)), | 476 | lua_typename(L2, lua_type_as_enum(L2, -1)), |
478 | to ? to : "main"); | 477 | to ? to : "main"); |
479 | } | 478 | } |
@@ -488,12 +487,12 @@ void InterCopyContext::copy_cached_func() const | |||
488 | 487 | ||
489 | void InterCopyContext::inter_copy_keyvaluepair() const | 488 | void InterCopyContext::inter_copy_keyvaluepair() const |
490 | { | 489 | { |
491 | SourceIndex const val_i{ lua_gettop(L1) }; | 490 | SourceIndex const _val_i{ lua_gettop(L1) }; |
492 | SourceIndex const key_i{ val_i - 1 }; | 491 | SourceIndex const _key_i{ _val_i - 1 }; |
493 | 492 | ||
494 | // For the key, only basic key types are copied over. others ignored | 493 | // For the key, only basic key types are copied over. others ignored |
495 | InterCopyContext c{ U, L2, L1, L2_cache_i, key_i, VT::KEY, mode, name }; | 494 | InterCopyContext _c{ U, L2, L1, L2_cache_i, _key_i, VT::KEY, mode, name }; |
496 | if (!c.inter_copy_one()) { | 495 | if (!_c.inter_copy_one()) { |
497 | return; | 496 | return; |
498 | // we could raise an error instead of ignoring the table entry, like so: | 497 | // we could raise an error instead of ignoring the table entry, like so: |
499 | // raise_luaL_error(L1, "Unable to copy %s key '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", name, luaL_typename(L1, key_i)); | 498 | // raise_luaL_error(L1, "Unable to copy %s key '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", name, luaL_typename(L1, key_i)); |
@@ -503,44 +502,44 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
503 | char* valPath{ nullptr }; | 502 | char* valPath{ nullptr }; |
504 | if (U->verboseErrors) { | 503 | if (U->verboseErrors) { |
505 | // for debug purposes, let's try to build a useful name | 504 | // for debug purposes, let's try to build a useful name |
506 | if (lua_type(L1, key_i) == LUA_TSTRING) { | 505 | if (lua_type(L1, _key_i) == LUA_TSTRING) { |
507 | char const* key{ lua_tostring(L1, key_i) }; | 506 | char const* key{ lua_tostring(L1, _key_i) }; |
508 | size_t const keyRawLen = lua_rawlen(L1, key_i); | 507 | size_t const keyRawLen = lua_rawlen(L1, _key_i); |
509 | size_t const bufLen = strlen(name) + keyRawLen + 2; | 508 | size_t const bufLen = strlen(name) + keyRawLen + 2; |
510 | valPath = (char*) alloca(bufLen); | 509 | valPath = (char*) alloca(bufLen); |
511 | sprintf(valPath, "%s.%*s", name, (int) keyRawLen, key); | 510 | sprintf(valPath, "%s.%*s", name, (int) keyRawLen, key); |
512 | key = nullptr; | 511 | key = nullptr; |
513 | } | 512 | } |
514 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 513 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
515 | else if (lua_isinteger(L1, key_i)) { | 514 | else if (lua_isinteger(L1, _key_i)) { |
516 | lua_Integer const key{ lua_tointeger(L1, key_i) }; | 515 | lua_Integer const key{ lua_tointeger(L1, _key_i) }; |
517 | valPath = (char*) alloca(strlen(name) + 32 + 3); | 516 | valPath = (char*) alloca(strlen(name) + 32 + 3); |
518 | sprintf(valPath, "%s[" LUA_INTEGER_FMT "]", name, key); | 517 | sprintf(valPath, "%s[" LUA_INTEGER_FMT "]", name, key); |
519 | } | 518 | } |
520 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 519 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
521 | else if (lua_type(L1, key_i) == LUA_TNUMBER) { | 520 | else if (lua_type(L1, _key_i) == LUA_TNUMBER) { |
522 | lua_Number const key{ lua_tonumber(L1, key_i) }; | 521 | lua_Number const key{ lua_tonumber(L1, _key_i) }; |
523 | valPath = (char*) alloca(strlen(name) + 32 + 3); | 522 | valPath = (char*) alloca(strlen(name) + 32 + 3); |
524 | sprintf(valPath, "%s[" LUA_NUMBER_FMT "]", name, key); | 523 | sprintf(valPath, "%s[" LUA_NUMBER_FMT "]", name, key); |
525 | } else if (lua_type(L1, key_i) == LUA_TLIGHTUSERDATA) { | 524 | } else if (lua_type(L1, _key_i) == LUA_TLIGHTUSERDATA) { |
526 | void* const key{ lua_touserdata(L1, key_i) }; | 525 | void* const key{ lua_touserdata(L1, _key_i) }; |
527 | valPath = (char*) alloca(strlen(name) + 16 + 5); | 526 | valPath = (char*) alloca(strlen(name) + 16 + 5); |
528 | sprintf(valPath, "%s[U:%p]", name, key); | 527 | sprintf(valPath, "%s[U:%p]", name, key); |
529 | } else if (lua_type(L1, key_i) == LUA_TBOOLEAN) { | 528 | } else if (lua_type(L1, _key_i) == LUA_TBOOLEAN) { |
530 | int const key{ lua_toboolean(L1, key_i) }; | 529 | int const key{ lua_toboolean(L1, _key_i) }; |
531 | valPath = (char*) alloca(strlen(name) + 8); | 530 | valPath = (char*) alloca(strlen(name) + 8); |
532 | sprintf(valPath, "%s[%s]", name, key ? "true" : "false"); | 531 | sprintf(valPath, "%s[%s]", name, key ? "true" : "false"); |
533 | } | 532 | } |
534 | } | 533 | } |
535 | c.L1_i = SourceIndex{ val_i }; | 534 | _c.L1_i = SourceIndex{ _val_i }; |
536 | // Contents of metatables are copied with cache checking. important to detect loops. | 535 | // Contents of metatables are copied with cache checking. important to detect loops. |
537 | c.vt = VT::NORMAL; | 536 | _c.vt = VT::NORMAL; |
538 | c.name = valPath ? valPath : name; | 537 | _c.name = valPath ? valPath : name; |
539 | if (c.inter_copy_one()) { | 538 | if (_c.inter_copy_one()) { |
540 | LUA_ASSERT(L1, lua_istable(L2, -3)); | 539 | LUA_ASSERT(L1, lua_istable(L2, -3)); |
541 | lua_rawset(L2, -3); // add to table (pops key & val) | 540 | lua_rawset(L2, -3); // add to table (pops key & val) |
542 | } else { | 541 | } else { |
543 | raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, val_i)); | 542 | raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, _val_i)); |
544 | } | 543 | } |
545 | } | 544 | } |
546 | 545 | ||
@@ -555,13 +554,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
555 | } | 554 | } |
556 | STACK_CHECK(L1, 1); | 555 | STACK_CHECK(L1, 1); |
557 | 556 | ||
558 | lua_Integer const mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable | 557 | lua_Integer const _mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable |
559 | 558 | ||
560 | STACK_CHECK_START_REL(L2, 0); | 559 | STACK_CHECK_START_REL(L2, 0); |
561 | STACK_GROW(L2, 4); | 560 | STACK_GROW(L2, 4); |
562 | // do we already know this metatable? | 561 | // do we already know this metatable? |
563 | std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey] | 562 | std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey] |
564 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] id | 563 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] id |
565 | lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil | 564 | lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil |
566 | STACK_CHECK(L2, 2); | 565 | STACK_CHECK(L2, 2); |
567 | 566 | ||
@@ -574,13 +573,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
574 | 573 | ||
575 | STACK_CHECK(L2, 2); // L2: _R[kMtIdRegKey] mt | 574 | STACK_CHECK(L2, 2); // L2: _R[kMtIdRegKey] mt |
576 | // mt_id -> metatable | 575 | // mt_id -> metatable |
577 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt id | 576 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt id |
578 | lua_pushvalue(L2, -2); // L2: _R[kMtIdRegKey] mt id mt | 577 | lua_pushvalue(L2, -2); // L2: _R[kMtIdRegKey] mt id mt |
579 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt | 578 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt |
580 | 579 | ||
581 | // metatable -> mt_id | 580 | // metatable -> mt_id |
582 | lua_pushvalue(L2, -1); // L2: _R[kMtIdRegKey] mt mt | 581 | lua_pushvalue(L2, -1); // L2: _R[kMtIdRegKey] mt mt |
583 | lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt mt id | 582 | lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt mt id |
584 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt | 583 | lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt |
585 | STACK_CHECK(L2, 2); | 584 | STACK_CHECK(L2, 2); |
586 | } | 585 | } |
@@ -600,7 +599,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
600 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. | 599 | // Returns true if the table was cached (no need to fill it!); false if it's a virgin. |
601 | [[nodiscard]] bool InterCopyContext::push_cached_table() const | 600 | [[nodiscard]] bool InterCopyContext::push_cached_table() const |
602 | { | 601 | { |
603 | void const* p{ lua_topointer(L1, L1_i) }; | 602 | void const* const _p{ lua_topointer(L1, L1_i) }; |
604 | 603 | ||
605 | LUA_ASSERT(L1, L2_cache_i != 0); | 604 | LUA_ASSERT(L1, L2_cache_i != 0); |
606 | STACK_GROW(L2, 3); | 605 | STACK_GROW(L2, 3); |
@@ -609,37 +608,37 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
609 | // We don't need to use the from state ('L1') in ID since the life span | 608 | // We don't need to use the from state ('L1') in ID since the life span |
610 | // is only for the duration of a copy (both states are locked). | 609 | // is only for the duration of a copy (both states are locked). |
611 | // push a light userdata uniquely representing the table | 610 | // push a light userdata uniquely representing the table |
612 | lua_pushlightuserdata(L2, const_cast<void*>(p)); // L1: ... t ... L2: ... p | 611 | lua_pushlightuserdata(L2, const_cast<void*>(_p)); // L1: ... t ... L2: ... p |
613 | 612 | ||
614 | // fprintf(stderr, "<< ID: %s >>\n", lua_tostring(L2, -1)); | 613 | // fprintf(stderr, "<< ID: %s >>\n", lua_tostring(L2, -1)); |
615 | 614 | ||
616 | lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} | 615 | lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} |
617 | bool const not_found_in_cache{ lua_isnil(L2, -1) }; | 616 | bool const _not_found_in_cache{ lua_isnil(L2, -1) }; |
618 | if (not_found_in_cache) { | 617 | if (_not_found_in_cache) { |
619 | // create a new entry in the cache | 618 | // create a new entry in the cache |
620 | lua_pop(L2, 1); // L1: ... t ... L2: ... | 619 | lua_pop(L2, 1); // L1: ... t ... L2: ... |
621 | lua_newtable(L2); // L1: ... t ... L2: ... {} | 620 | lua_newtable(L2); // L1: ... t ... L2: ... {} |
622 | lua_pushlightuserdata(L2, const_cast<void*>(p)); // L1: ... t ... L2: ... {} p | 621 | lua_pushlightuserdata(L2, const_cast<void*>(_p)); // L1: ... t ... L2: ... {} p |
623 | lua_pushvalue(L2, -2); // L1: ... t ... L2: ... {} p {} | 622 | lua_pushvalue(L2, -2); // L1: ... t ... L2: ... {} p {} |
624 | lua_rawset(L2, L2_cache_i); // L1: ... t ... L2: ... {} | 623 | lua_rawset(L2, L2_cache_i); // L1: ... t ... L2: ... {} |
625 | } | 624 | } |
626 | STACK_CHECK(L2, 1); | 625 | STACK_CHECK(L2, 1); |
627 | LUA_ASSERT(L1, lua_istable(L2, -1)); | 626 | LUA_ASSERT(L1, lua_istable(L2, -1)); |
628 | return !not_found_in_cache; | 627 | return !_not_found_in_cache; |
629 | } | 628 | } |
630 | 629 | ||
631 | // ################################################################################################# | 630 | // ################################################################################################# |
632 | 631 | ||
633 | [[nodiscard]] bool InterCopyContext::tryCopyClonable() const | 632 | [[nodiscard]] bool InterCopyContext::tryCopyClonable() const |
634 | { | 633 | { |
635 | SourceIndex const L1i{ lua_absindex(L1, L1_i) }; | 634 | SourceIndex const _L1_i{ lua_absindex(L1, L1_i) }; |
636 | void* const source{ lua_touserdata(L1, L1i) }; | 635 | void* const _source{ lua_touserdata(L1, _L1_i) }; |
637 | 636 | ||
638 | STACK_CHECK_START_REL(L1, 0); | 637 | STACK_CHECK_START_REL(L1, 0); |
639 | STACK_CHECK_START_REL(L2, 0); | 638 | STACK_CHECK_START_REL(L2, 0); |
640 | 639 | ||
641 | // Check if the source was already cloned during this copy | 640 | // Check if the source was already cloned during this copy |
642 | lua_pushlightuserdata(L2, source); // L2: ... source | 641 | lua_pushlightuserdata(L2, _source); // L2: ... source |
643 | lua_rawget(L2, L2_cache_i); // L2: ... clone? | 642 | lua_rawget(L2, L2_cache_i); // L2: ... clone? |
644 | if (!lua_isnil(L2, -1)) { | 643 | if (!lua_isnil(L2, -1)) { |
645 | STACK_CHECK(L2, 1); | 644 | STACK_CHECK(L2, 1); |
@@ -650,7 +649,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
650 | STACK_CHECK(L2, 0); | 649 | STACK_CHECK(L2, 0); |
651 | 650 | ||
652 | // no metatable? -> not clonable | 651 | // no metatable? -> not clonable |
653 | if (!lua_getmetatable(L1, L1i)) { // L1: ... mt? | 652 | if (!lua_getmetatable(L1, _L1_i)) { // L1: ... mt? |
654 | STACK_CHECK(L1, 0); | 653 | STACK_CHECK(L1, 0); |
655 | return false; | 654 | return false; |
656 | } | 655 | } |
@@ -666,18 +665,18 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
666 | // we need to copy over the uservalues of the userdata as well | 665 | // we need to copy over the uservalues of the userdata as well |
667 | { | 666 | { |
668 | int const mt{ lua_absindex(L1, -2) }; // L1: ... mt __lanesclone | 667 | int const mt{ lua_absindex(L1, -2) }; // L1: ... mt __lanesclone |
669 | size_t const userdata_size{ lua_rawlen(L1, L1i) }; | 668 | size_t const userdata_size{ lua_rawlen(L1, _L1_i) }; |
670 | // extract all the uservalues, but don't transfer them yet | 669 | // extract all the uservalues, but don't transfer them yet |
671 | int uvi = 0; | 670 | int _uvi{ 0 }; |
672 | while (lua_getiuservalue(L1, L1i, ++uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil | 671 | while (lua_getiuservalue(L1, _L1_i, ++_uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil |
673 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now | 672 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now |
674 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]+ | 673 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]+ |
675 | --uvi; | 674 | --_uvi; |
676 | // create the clone userdata with the required number of uservalue slots | 675 | // create the clone userdata with the required number of uservalue slots |
677 | void* const clone{ lua_newuserdatauv(L2, userdata_size, uvi) }; // L2: ... u | 676 | void* const _clone{ lua_newuserdatauv(L2, userdata_size, _uvi) }; // L2: ... u |
678 | // copy the metatable in the target state, and give it to the clone we put there | 677 | // copy the metatable in the target state, and give it to the clone we put there |
679 | InterCopyContext c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; | 678 | InterCopyContext _c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; |
680 | if (c.inter_copy_one()) { // L2: ... u mt|sentinel | 679 | if (_c.inter_copy_one()) { // L2: ... u mt|sentinel |
681 | if (LookupMode::ToKeeper == mode) { // L2: ... u sentinel | 680 | if (LookupMode::ToKeeper == mode) { // L2: ... u sentinel |
682 | LUA_ASSERT(L1, lua_tocfunction(L2, -1) == table_lookup_sentinel); | 681 | LUA_ASSERT(L1, lua_tocfunction(L2, -1) == table_lookup_sentinel); |
683 | // we want to create a new closure with a 'clone sentinel' function, where the upvalues are the userdata and the metatable fqn | 682 | // we want to create a new closure with a 'clone sentinel' function, where the upvalues are the userdata and the metatable fqn |
@@ -694,7 +693,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
694 | raise_luaL_error(getErrL(), "Error copying a metatable"); | 693 | raise_luaL_error(getErrL(), "Error copying a metatable"); |
695 | } | 694 | } |
696 | // first, add the entry in the cache (at this point it is either the actual userdata or the keeper sentinel | 695 | // first, add the entry in the cache (at this point it is either the actual userdata or the keeper sentinel |
697 | lua_pushlightuserdata(L2, source); // L2: ... u source | 696 | lua_pushlightuserdata(L2, _source); // L2: ... u source |
698 | lua_pushvalue(L2, -2); // L2: ... u source u | 697 | lua_pushvalue(L2, -2); // L2: ... u source u |
699 | lua_rawset(L2, L2_cache_i); // L2: ... u | 698 | lua_rawset(L2, L2_cache_i); // L2: ... u |
700 | // make sure we have the userdata now | 699 | // make sure we have the userdata now |
@@ -702,15 +701,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
702 | lua_getupvalue(L2, -1, 2); // L2: ... userdata_clone_sentinel u | 701 | lua_getupvalue(L2, -1, 2); // L2: ... userdata_clone_sentinel u |
703 | } | 702 | } |
704 | // assign uservalues | 703 | // assign uservalues |
705 | while (uvi > 0) { | 704 | while (_uvi > 0) { |
706 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 705 | _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
707 | if (!c.inter_copy_one()) { // L2: ... u uv | 706 | if (!_c.inter_copy_one()) { // L2: ... u uv |
708 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 707 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
709 | } | 708 | } |
710 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]* | 709 | lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]* |
711 | // this pops the value from the stack | 710 | // this pops the value from the stack |
712 | lua_setiuservalue(L2, -2, uvi); // L2: ... u | 711 | lua_setiuservalue(L2, -2, _uvi); // L2: ... u |
713 | --uvi; | 712 | --_uvi; |
714 | } | 713 | } |
715 | // when we are done, all uservalues are popped from the source stack, and we want only the single transferred value in the destination | 714 | // when we are done, all uservalues are popped from the source stack, and we want only the single transferred value in the destination |
716 | if (LookupMode::ToKeeper == mode) { // L2: ... userdata_clone_sentinel u | 715 | if (LookupMode::ToKeeper == mode) { // L2: ... userdata_clone_sentinel u |
@@ -719,8 +718,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
719 | STACK_CHECK(L2, 1); | 718 | STACK_CHECK(L2, 1); |
720 | STACK_CHECK(L1, 2); | 719 | STACK_CHECK(L1, 2); |
721 | // call cloning function in source state to perform the actual memory cloning | 720 | // call cloning function in source state to perform the actual memory cloning |
722 | lua_pushlightuserdata(L1, clone); // L1: ... mt __lanesclone clone | 721 | lua_pushlightuserdata(L1, _clone); // L1: ... mt __lanesclone clone |
723 | lua_pushlightuserdata(L1, source); // L1: ... mt __lanesclone clone source | 722 | lua_pushlightuserdata(L1, _source); // L1: ... mt __lanesclone clone source |
724 | lua_pushinteger(L1, static_cast<lua_Integer>(userdata_size)); // L1: ... mt __lanesclone clone source size | 723 | lua_pushinteger(L1, static_cast<lua_Integer>(userdata_size)); // L1: ... mt __lanesclone clone source size |
725 | lua_call(L1, 3, 0); // L1: ... mt | 724 | lua_call(L1, 3, 0); // L1: ... mt |
726 | STACK_CHECK(L1, 1); | 725 | STACK_CHECK(L1, 1); |
@@ -738,8 +737,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
738 | // Returns false if not a deep userdata, else true (unless an error occured) | 737 | // Returns false if not a deep userdata, else true (unless an error occured) |
739 | [[nodiscard]] bool InterCopyContext::tryCopyDeep() const | 738 | [[nodiscard]] bool InterCopyContext::tryCopyDeep() const |
740 | { | 739 | { |
741 | DeepFactory* const factory{ LookupFactory(L1, L1_i, mode) }; | 740 | DeepFactory* const _factory{ LookupFactory(L1, L1_i, mode) }; |
742 | if (factory == nullptr) { | 741 | if (_factory == nullptr) { |
743 | return false; // not a deep userdata | 742 | return false; // not a deep userdata |
744 | } | 743 | } |
745 | 744 | ||
@@ -747,33 +746,33 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
747 | STACK_CHECK_START_REL(L2, 0); | 746 | STACK_CHECK_START_REL(L2, 0); |
748 | 747 | ||
749 | // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail | 748 | // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail |
750 | int nuv = 0; | 749 | int _nuv = 0; |
751 | while (lua_getiuservalue(L1, L1_i, nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil | 750 | while (lua_getiuservalue(L1, L1_i, _nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil |
752 | ++nuv; | 751 | ++_nuv; |
753 | } | 752 | } |
754 | // last call returned TNONE and pushed nil, that we don't need | 753 | // last call returned TNONE and pushed nil, that we don't need |
755 | lua_pop(L1, 1); // L1: ... u [uv]* | 754 | lua_pop(L1, 1); // L1: ... u [uv]* |
756 | STACK_CHECK(L1, nuv); | 755 | STACK_CHECK(L1, _nuv); |
757 | 756 | ||
758 | DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) }; | 757 | DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) }; |
759 | char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, nuv, mode) }; // L1: ... u [uv]* L2: u | 758 | char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u |
760 | if (errmsg != nullptr) { | 759 | if (errmsg != nullptr) { |
761 | raise_luaL_error(getErrL(), errmsg); | 760 | raise_luaL_error(getErrL(), errmsg); |
762 | } | 761 | } |
763 | 762 | ||
764 | // transfer all uservalues of the source in the destination | 763 | // transfer all uservalues of the source in the destination |
765 | { | 764 | { |
766 | InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; | 765 | InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; |
767 | int const clone_i{ lua_gettop(L2) }; | 766 | int const _clone_i{ lua_gettop(L2) }; |
768 | while (nuv) { | 767 | while (_nuv) { |
769 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 768 | _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
770 | if (!c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv | 769 | if (!_c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv |
771 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 770 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
772 | } | 771 | } |
773 | lua_pop(L1, 1); // L1: ... u [uv]* | 772 | lua_pop(L1, 1); // L1: ... u [uv]* |
774 | // this pops the value from the stack | 773 | // this pops the value from the stack |
775 | lua_setiuservalue(L2, clone_i, nuv); // L2: u | 774 | lua_setiuservalue(L2, _clone_i, _nuv); // L2: u |
776 | --nuv; | 775 | --_nuv; |
777 | } | 776 | } |
778 | } | 777 | } |
779 | 778 | ||
@@ -787,9 +786,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
787 | 786 | ||
788 | [[nodiscard]] bool InterCopyContext::inter_copy_boolean() const | 787 | [[nodiscard]] bool InterCopyContext::inter_copy_boolean() const |
789 | { | 788 | { |
790 | int const v{ lua_toboolean(L1, L1_i) }; | 789 | int const _v{ lua_toboolean(L1, L1_i) }; |
791 | DEBUGSPEW_CODE(fprintf(stderr, "%s\n", v ? "true" : "false")); | 790 | DEBUGSPEW_CODE(fprintf(stderr, "%s\n", _v ? "true" : "false")); |
792 | lua_pushboolean(L2, v); | 791 | lua_pushboolean(L2, _v); |
793 | return true; | 792 | return true; |
794 | } | 793 | } |
795 | 794 | ||
@@ -810,8 +809,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
810 | 809 | ||
811 | // let's see if we already restored this userdata | 810 | // let's see if we already restored this userdata |
812 | lua_getupvalue(L1, L1_i, 2); // L1: ... u | 811 | lua_getupvalue(L1, L1_i, 2); // L1: ... u |
813 | void* source = lua_touserdata(L1, -1); | 812 | void* _source{ lua_touserdata(L1, -1) }; |
814 | lua_pushlightuserdata(L2, source); // L2: ... source | 813 | lua_pushlightuserdata(L2, _source); // L2: ... source |
815 | lua_rawget(L2, L2_cache_i); // L2: ... u? | 814 | lua_rawget(L2, L2_cache_i); // L2: ... u? |
816 | if (!lua_isnil(L2, -1)) { | 815 | if (!lua_isnil(L2, -1)) { |
817 | lua_pop(L1, 1); // L1: ... | 816 | lua_pop(L1, 1); // L1: ... |
@@ -829,22 +828,22 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
829 | } | 828 | } |
830 | // 'L1_i' slot was the proxy closure, but from now on we operate onthe actual userdata we extracted from it | 829 | // 'L1_i' slot was the proxy closure, but from now on we operate onthe actual userdata we extracted from it |
831 | SourceIndex const source_i{ lua_gettop(L1) }; | 830 | SourceIndex const source_i{ lua_gettop(L1) }; |
832 | source = lua_touserdata(L1, -1); | 831 | _source = lua_touserdata(L1, -1); |
833 | void* clone{ nullptr }; | 832 | void* _clone{ nullptr }; |
834 | // get the number of bytes to allocate for the clone | 833 | // get the number of bytes to allocate for the clone |
835 | size_t const userdata_size{ lua_rawlen(L1, -1) }; | 834 | size_t const userdata_size{ lua_rawlen(L1, -1) }; |
836 | { | 835 | { |
837 | // extract uservalues (don't transfer them yet) | 836 | // extract uservalues (don't transfer them yet) |
838 | int uvi = 0; | 837 | int _uvi = 0; |
839 | while (lua_getiuservalue(L1, source_i, ++uvi) != LUA_TNONE) {} // L1: ... u uv | 838 | while (lua_getiuservalue(L1, source_i, ++_uvi) != LUA_TNONE) {} // L1: ... u uv |
840 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now | 839 | // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now |
841 | lua_pop(L1, 1); // L1: ... u [uv]* | 840 | lua_pop(L1, 1); // L1: ... u [uv]* |
842 | --uvi; | 841 | --_uvi; |
843 | STACK_CHECK(L1, uvi + 1); | 842 | STACK_CHECK(L1, _uvi + 1); |
844 | // create the clone userdata with the required number of uservalue slots | 843 | // create the clone userdata with the required number of uservalue slots |
845 | clone = lua_newuserdatauv(L2, userdata_size, uvi); // L2: ... mt u | 844 | _clone = lua_newuserdatauv(L2, userdata_size, _uvi); // L2: ... mt u |
846 | // add it in the cache | 845 | // add it in the cache |
847 | lua_pushlightuserdata(L2, source); // L2: ... mt u source | 846 | lua_pushlightuserdata(L2, _source); // L2: ... mt u source |
848 | lua_pushvalue(L2, -2); // L2: ... mt u source u | 847 | lua_pushvalue(L2, -2); // L2: ... mt u source u |
849 | lua_rawset(L2, L2_cache_i); // L2: ... mt u | 848 | lua_rawset(L2, L2_cache_i); // L2: ... mt u |
850 | // set metatable | 849 | // set metatable |
@@ -852,15 +851,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
852 | lua_setmetatable(L2, -2); // L2: ... mt u | 851 | lua_setmetatable(L2, -2); // L2: ... mt u |
853 | // transfer and assign uservalues | 852 | // transfer and assign uservalues |
854 | InterCopyContext c{ *this }; | 853 | InterCopyContext c{ *this }; |
855 | while (uvi > 0) { | 854 | while (_uvi > 0) { |
856 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; | 855 | c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; |
857 | if (!c.inter_copy_one()) { // L2: ... mt u uv | 856 | if (!c.inter_copy_one()) { // L2: ... mt u uv |
858 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); | 857 | raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); |
859 | } | 858 | } |
860 | lua_pop(L1, 1); // L1: ... u [uv]* | 859 | lua_pop(L1, 1); // L1: ... u [uv]* |
861 | // this pops the value from the stack | 860 | // this pops the value from the stack |
862 | lua_setiuservalue(L2, -2, uvi); // L2: ... mt u | 861 | lua_setiuservalue(L2, -2, _uvi); // L2: ... mt u |
863 | --uvi; | 862 | --_uvi; |
864 | } | 863 | } |
865 | // when we are done, all uservalues are popped from the stack, we can pop the source as well | 864 | // when we are done, all uservalues are popped from the stack, we can pop the source as well |
866 | lua_pop(L1, 1); // L1: ... | 865 | lua_pop(L1, 1); // L1: ... |
@@ -868,12 +867,12 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
868 | STACK_CHECK(L2, 2); // L2: ... mt u | 867 | STACK_CHECK(L2, 2); // L2: ... mt u |
869 | } | 868 | } |
870 | // perform the custom cloning part | 869 | // perform the custom cloning part |
871 | lua_insert(L2, -2); // L2: ... u mt | 870 | lua_insert(L2, -2); // L2: ... u mt |
872 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with | 871 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with |
873 | lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone | 872 | lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone |
874 | lua_remove(L2, -2); // L2: ... u __lanesclone | 873 | lua_remove(L2, -2); // L2: ... u __lanesclone |
875 | lua_pushlightuserdata(L2, clone); // L2: ... u __lanesclone clone | 874 | lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone |
876 | lua_pushlightuserdata(L2, source); // L2: ... u __lanesclone clone source | 875 | lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source |
877 | lua_pushinteger(L2, userdata_size); // L2: ... u __lanesclone clone source size | 876 | lua_pushinteger(L2, userdata_size); // L2: ... u __lanesclone clone source size |
878 | // clone:__lanesclone(dest, source, size) | 877 | // clone:__lanesclone(dest, source, size) |
879 | lua_call(L2, 3, 0); // L2: ... u | 878 | lua_call(L2, 3, 0); // L2: ... u |
@@ -891,9 +890,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
891 | 890 | ||
892 | [[nodiscard]] bool InterCopyContext::inter_copy_lightuserdata() const | 891 | [[nodiscard]] bool InterCopyContext::inter_copy_lightuserdata() const |
893 | { | 892 | { |
894 | void* const p{ lua_touserdata(L1, L1_i) }; | 893 | void* const _p{ lua_touserdata(L1, L1_i) }; |
895 | DEBUGSPEW_CODE(fprintf(stderr, "%p\n", p)); | 894 | DEBUGSPEW_CODE(fprintf(stderr, "%p\n", _p)); |
896 | lua_pushlightuserdata(L2, p); | 895 | lua_pushlightuserdata(L2, _p); |
897 | return true; | 896 | return true; |
898 | } | 897 | } |
899 | 898 | ||
@@ -915,15 +914,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
915 | // LNUM patch support (keeping integer accuracy) | 914 | // LNUM patch support (keeping integer accuracy) |
916 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 915 | #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
917 | if (lua_isinteger(L1, L1_i)) { | 916 | if (lua_isinteger(L1, L1_i)) { |
918 | lua_Integer const v{ lua_tointeger(L1, L1_i) }; | 917 | lua_Integer const _v{ lua_tointeger(L1, L1_i) }; |
919 | DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", v)); | 918 | DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", _v)); |
920 | lua_pushinteger(L2, v); | 919 | lua_pushinteger(L2, _v); |
921 | } else | 920 | } else |
922 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 | 921 | #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 |
923 | { | 922 | { |
924 | lua_Number const v{ lua_tonumber(L1, L1_i) }; | 923 | lua_Number const _v{ lua_tonumber(L1, L1_i) }; |
925 | DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", v)); | 924 | DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", _v)); |
926 | lua_pushnumber(L2, v); | 925 | lua_pushnumber(L2, _v); |
927 | } | 926 | } |
928 | return true; | 927 | return true; |
929 | } | 928 | } |
@@ -932,10 +931,10 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
932 | 931 | ||
933 | [[nodiscard]] bool InterCopyContext::inter_copy_string() const | 932 | [[nodiscard]] bool InterCopyContext::inter_copy_string() const |
934 | { | 933 | { |
935 | size_t len; | 934 | size_t _len; |
936 | char const* const s{ lua_tolstring(L1, L1_i, &len) }; | 935 | char const* const _s{ lua_tolstring(L1, L1_i, &_len) }; |
937 | DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", s)); | 936 | DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", _s)); |
938 | lua_pushlstring(L2, s, len); | 937 | lua_pushlstring(L2, _s, _len); |
939 | return true; | 938 | return true; |
940 | } | 939 | } |
941 | 940 | ||
@@ -1029,8 +1028,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
1029 | 1028 | ||
1030 | // Not a deep or clonable full userdata | 1029 | // Not a deep or clonable full userdata |
1031 | if (U->demoteFullUserdata) { // attempt demotion to light userdata | 1030 | if (U->demoteFullUserdata) { // attempt demotion to light userdata |
1032 | void* const lud{ lua_touserdata(L1, L1_i) }; | 1031 | void* const _lud{ lua_touserdata(L1, L1_i) }; |
1033 | lua_pushlightuserdata(L2, lud); | 1032 | lua_pushlightuserdata(L2, _lud); |
1034 | } else { // raise an error | 1033 | } else { // raise an error |
1035 | raise_luaL_error(getErrL(), "can't copy non-deep full userdata across lanes"); | 1034 | raise_luaL_error(getErrL(), "can't copy non-deep full userdata across lanes"); |
1036 | } | 1035 | } |
@@ -1083,16 +1082,16 @@ static char const* vt_names[] = { | |||
1083 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END(U))); | 1082 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END(U))); |
1084 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1083 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1085 | 1084 | ||
1086 | LuaType val_type{ lua_type_as_enum(L1, L1_i) }; | 1085 | LuaType _val_type{ lua_type_as_enum(L1, L1_i) }; |
1087 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast<int>(val_type)], vt_names[static_cast<int>(vt)])); | 1086 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast<int>(_val_type)], vt_names[static_cast<int>(vt)])); |
1088 | 1087 | ||
1089 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } | 1088 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } |
1090 | if (((1 << static_cast<int>(val_type)) & kPODmask) == 0) { | 1089 | if (((1 << static_cast<int>(_val_type)) & kPODmask) == 0) { |
1091 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt | 1090 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt |
1092 | lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? | 1091 | lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? |
1093 | if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { | 1092 | if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { |
1094 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); | 1093 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); |
1095 | val_type = LuaType::NIL; | 1094 | _val_type = LuaType::NIL; |
1096 | } | 1095 | } |
1097 | lua_pop(L1, 2); // L1: ... | 1096 | lua_pop(L1, 2); // L1: ... |
1098 | } | 1097 | } |
@@ -1100,47 +1099,47 @@ static char const* vt_names[] = { | |||
1100 | STACK_CHECK(L1, 0); | 1099 | STACK_CHECK(L1, 0); |
1101 | 1100 | ||
1102 | // Lets push nil to L2 if the object should be ignored | 1101 | // Lets push nil to L2 if the object should be ignored |
1103 | bool ret{ true }; | 1102 | bool _ret{ true }; |
1104 | switch (val_type) { | 1103 | switch (_val_type) { |
1105 | // Basic types allowed both as values, and as table keys | 1104 | // Basic types allowed both as values, and as table keys |
1106 | case LuaType::BOOLEAN: | 1105 | case LuaType::BOOLEAN: |
1107 | ret = inter_copy_boolean(); | 1106 | _ret = inter_copy_boolean(); |
1108 | break; | 1107 | break; |
1109 | case LuaType::NUMBER: | 1108 | case LuaType::NUMBER: |
1110 | ret = inter_copy_number(); | 1109 | _ret = inter_copy_number(); |
1111 | break; | 1110 | break; |
1112 | case LuaType::STRING: | 1111 | case LuaType::STRING: |
1113 | ret = inter_copy_string(); | 1112 | _ret = inter_copy_string(); |
1114 | break; | 1113 | break; |
1115 | case LuaType::LIGHTUSERDATA: | 1114 | case LuaType::LIGHTUSERDATA: |
1116 | ret = inter_copy_lightuserdata(); | 1115 | _ret = inter_copy_lightuserdata(); |
1117 | break; | 1116 | break; |
1118 | 1117 | ||
1119 | // The following types are not allowed as table keys | 1118 | // The following types are not allowed as table keys |
1120 | case LuaType::USERDATA: | 1119 | case LuaType::USERDATA: |
1121 | ret = inter_copy_userdata(); | 1120 | _ret = inter_copy_userdata(); |
1122 | break; | 1121 | break; |
1123 | case LuaType::NIL: | 1122 | case LuaType::NIL: |
1124 | ret = inter_copy_nil(); | 1123 | _ret = inter_copy_nil(); |
1125 | break; | 1124 | break; |
1126 | case LuaType::FUNCTION: | 1125 | case LuaType::FUNCTION: |
1127 | ret = inter_copy_function(); | 1126 | _ret = inter_copy_function(); |
1128 | break; | 1127 | break; |
1129 | case LuaType::TABLE: | 1128 | case LuaType::TABLE: |
1130 | ret = inter_copy_table(); | 1129 | _ret = inter_copy_table(); |
1131 | break; | 1130 | break; |
1132 | 1131 | ||
1133 | // The following types cannot be copied | 1132 | // The following types cannot be copied |
1134 | case LuaType::CDATA: | 1133 | case LuaType::CDATA: |
1135 | [[fallthrough]]; | 1134 | [[fallthrough]]; |
1136 | case LuaType::THREAD: | 1135 | case LuaType::THREAD: |
1137 | ret = false; | 1136 | _ret = false; |
1138 | break; | 1137 | break; |
1139 | } | 1138 | } |
1140 | 1139 | ||
1141 | STACK_CHECK(L2, ret ? 1 : 0); | 1140 | STACK_CHECK(L2, _ret ? 1 : 0); |
1142 | STACK_CHECK(L1, 0); | 1141 | STACK_CHECK(L1, 0); |
1143 | return ret; | 1142 | return _ret; |
1144 | } | 1143 | } |
1145 | 1144 | ||
1146 | // ################################################################################################# | 1145 | // ################################################################################################# |
@@ -1189,30 +1188,30 @@ static char const* vt_names[] = { | |||
1189 | return InterCopyResult::Success; | 1188 | return InterCopyResult::Success; |
1190 | } | 1189 | } |
1191 | 1190 | ||
1192 | InterCopyResult result{ InterCopyResult::Success }; | 1191 | InterCopyResult _result{ InterCopyResult::Success }; |
1193 | // package.loaders is renamed package.searchers in Lua 5.2 | 1192 | // package.loaders is renamed package.searchers in Lua 5.2 |
1194 | // but don't copy it anyway, as the function names change depending on the slot index! | 1193 | // but don't copy it anyway, as the function names change depending on the slot index! |
1195 | // users should provide an on_state_create function to setup custom loaders instead | 1194 | // users should provide an on_state_create function to setup custom loaders instead |
1196 | // don't copy package.preload in keeper states (they don't know how to translate functions) | 1195 | // don't copy package.preload in keeper states (they don't know how to translate functions) |
1197 | char const* entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; | 1196 | char const* _entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; |
1198 | for (char const* const entry : entries) { | 1197 | for (char const* const _entry : _entries) { |
1199 | if (!entry) { | 1198 | if (!_entry) { |
1200 | continue; | 1199 | continue; |
1201 | } | 1200 | } |
1202 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), entry)); | 1201 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); |
1203 | lua_getfield(L1, L1_i, entry); | 1202 | lua_getfield(L1, L1_i, _entry); |
1204 | if (lua_isnil(L1, -1)) { | 1203 | if (lua_isnil(L1, -1)) { |
1205 | lua_pop(L1, 1); | 1204 | lua_pop(L1, 1); |
1206 | } else { | 1205 | } else { |
1207 | { | 1206 | { |
1208 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1207 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1209 | result = inter_move(1); // moves the entry to L2 | 1208 | _result = inter_move(1); // moves the entry to L2 |
1210 | STACK_CHECK(L1, 0); | 1209 | STACK_CHECK(L1, 0); |
1211 | } | 1210 | } |
1212 | if (result == InterCopyResult::Success) { | 1211 | if (_result == InterCopyResult::Success) { |
1213 | lua_setfield(L2, -2, entry); // set package[entry] | 1212 | lua_setfield(L2, -2, _entry); // set package[entry] |
1214 | } else { | 1213 | } else { |
1215 | lua_pushfstring(L1, "failed to copy package entry %s", entry); | 1214 | lua_pushfstring(L1, "failed to copy package entry %s", _entry); |
1216 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later | 1215 | // raise the error when copying from lane to lane, else just leave it on the stack to be raised later |
1217 | if (mode == LookupMode::LaneBody) { | 1216 | if (mode == LookupMode::LaneBody) { |
1218 | raise_lua_error(getErrL()); | 1217 | raise_lua_error(getErrL()); |
@@ -1223,7 +1222,7 @@ static char const* vt_names[] = { | |||
1223 | } | 1222 | } |
1224 | } | 1223 | } |
1225 | STACK_CHECK(L1, 0); | 1224 | STACK_CHECK(L1, 0); |
1226 | return result; | 1225 | return _result; |
1227 | } | 1226 | } |
1228 | 1227 | ||
1229 | // ################################################################################################# | 1228 | // ################################################################################################# |
@@ -1237,8 +1236,8 @@ static char const* vt_names[] = { | |||
1237 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy()\n" INDENT_END(U))); | 1236 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy()\n" INDENT_END(U))); |
1238 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1237 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1239 | 1238 | ||
1240 | int const top_L1{ lua_gettop(L1) }; | 1239 | int const _top_L1{ lua_gettop(L1) }; |
1241 | if (n_ > top_L1) { | 1240 | if (n_ > _top_L1) { |
1242 | // requesting to copy more than is available? | 1241 | // requesting to copy more than is available? |
1243 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END(U))); | 1242 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END(U))); |
1244 | return InterCopyResult::NotEnoughValues; | 1243 | return InterCopyResult::NotEnoughValues; |
@@ -1252,36 +1251,36 @@ static char const* vt_names[] = { | |||
1252 | * function entries, avoiding the same entries to be passed on as multiple | 1251 | * function entries, avoiding the same entries to be passed on as multiple |
1253 | * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! | 1252 | * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! |
1254 | */ | 1253 | */ |
1255 | int const top_L2{ lua_gettop(L2) }; // L2: ... | 1254 | int const _top_L2{ lua_gettop(L2) }; // L2: ... |
1256 | lua_newtable(L2); // L2: ... cache | 1255 | lua_newtable(L2); // L2: ... cache |
1257 | 1256 | ||
1258 | char tmpBuf[16]; | 1257 | char _tmpBuf[16]; |
1259 | char const* const pBuf{ U->verboseErrors ? tmpBuf : "?" }; | 1258 | char const* const _pBuf{ U->verboseErrors ? _tmpBuf : "?" }; |
1260 | InterCopyContext c{ U, L2, L1, CacheIndex{ top_L2 + 1 }, {}, VT::NORMAL, mode, pBuf }; | 1259 | InterCopyContext _c{ U, L2, L1, CacheIndex{ _top_L2 + 1 }, {}, VT::NORMAL, mode, _pBuf }; |
1261 | bool copyok{ true }; | 1260 | bool _copyok{ true }; |
1262 | STACK_CHECK_START_REL(L1, 0); | 1261 | STACK_CHECK_START_REL(L1, 0); |
1263 | for (int i{ top_L1 - n_ + 1 }, j{ 1 }; i <= top_L1; ++i, ++j) { | 1262 | for (int i{ _top_L1 - n_ + 1 }, j{ 1 }; i <= _top_L1; ++i, ++j) { |
1264 | if (U->verboseErrors) { | 1263 | if (U->verboseErrors) { |
1265 | sprintf(tmpBuf, "arg_%d", j); | 1264 | sprintf(_tmpBuf, "arg_%d", j); |
1266 | } | 1265 | } |
1267 | c.L1_i = SourceIndex{ i }; | 1266 | _c.L1_i = SourceIndex{ i }; |
1268 | copyok = c.inter_copy_one(); // L2: ... cache {}n | 1267 | _copyok = _c.inter_copy_one(); // L2: ... cache {}n |
1269 | if (!copyok) { | 1268 | if (!_copyok) { |
1270 | break; | 1269 | break; |
1271 | } | 1270 | } |
1272 | } | 1271 | } |
1273 | STACK_CHECK(L1, 0); | 1272 | STACK_CHECK(L1, 0); |
1274 | 1273 | ||
1275 | if (copyok) { | 1274 | if (_copyok) { |
1276 | STACK_CHECK(L2, n_ + 1); | 1275 | STACK_CHECK(L2, n_ + 1); |
1277 | // Remove the cache table. Persistent caching would cause i.e. multiple | 1276 | // Remove the cache table. Persistent caching would cause i.e. multiple |
1278 | // messages passed in the same table to use the same table also in receiving end. | 1277 | // messages passed in the same table to use the same table also in receiving end. |
1279 | lua_remove(L2, top_L2 + 1); | 1278 | lua_remove(L2, _top_L2 + 1); // L2: ... {}n |
1280 | return InterCopyResult::Success; | 1279 | return InterCopyResult::Success; |
1281 | } | 1280 | } |
1282 | 1281 | ||
1283 | // error -> pop everything from the target state stack | 1282 | // error -> pop everything from the target state stack |
1284 | lua_settop(L2, top_L2); | 1283 | lua_settop(L2, _top_L2); |
1285 | STACK_CHECK(L2, 0); | 1284 | STACK_CHECK(L2, 0); |
1286 | return InterCopyResult::Error; | 1285 | return InterCopyResult::Error; |
1287 | } | 1286 | } |
@@ -1290,7 +1289,7 @@ static char const* vt_names[] = { | |||
1290 | 1289 | ||
1291 | [[nodiscard]] InterCopyResult InterCopyContext::inter_move(int n_) const | 1290 | [[nodiscard]] InterCopyResult InterCopyContext::inter_move(int n_) const |
1292 | { | 1291 | { |
1293 | InterCopyResult const ret{ inter_copy(n_) }; | 1292 | InterCopyResult const _ret{ inter_copy(n_) }; |
1294 | lua_pop(L1, n_); | 1293 | lua_pop(L1, n_); |
1295 | return ret; | 1294 | return _ret; |
1296 | } | 1295 | } |
diff --git a/src/keeper.cpp b/src/keeper.cpp index 7367d0c..39d2e85 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -76,15 +76,15 @@ static constexpr int kContentsTableIndex{ 1 }; | |||
76 | // replaces the fifo ud by its uservalue on the stack | 76 | // replaces the fifo ud by its uservalue on the stack |
77 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) | 77 | [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) |
78 | { | 78 | { |
79 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, idx_) }; | 79 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, idx_) }; |
80 | if (fifo != nullptr) { | 80 | if (_fifo != nullptr) { |
81 | idx_ = lua_absindex(L_, idx_); | 81 | idx_ = lua_absindex(L_, idx_); |
82 | STACK_GROW(L_, 1); | 82 | STACK_GROW(L_, 1); |
83 | // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around | 83 | // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around |
84 | lua_getiuservalue(L_, idx_, kContentsTableIndex); | 84 | lua_getiuservalue(L_, idx_, kContentsTableIndex); |
85 | lua_replace(L_, idx_); | 85 | lua_replace(L_, idx_); |
86 | } | 86 | } |
87 | return fifo; | 87 | return _fifo; |
88 | } | 88 | } |
89 | 89 | ||
90 | // ################################################################################################# | 90 | // ################################################################################################# |
@@ -95,12 +95,12 @@ static constexpr int kContentsTableIndex{ 1 }; | |||
95 | { | 95 | { |
96 | STACK_GROW(L_, 2); | 96 | STACK_GROW(L_, 2); |
97 | STACK_CHECK_START_REL(L_, 0); | 97 | STACK_CHECK_START_REL(L_, 0); |
98 | keeper_fifo* const fifo{ new (L_) keeper_fifo{} }; | 98 | keeper_fifo* const _fifo{ new (L_) keeper_fifo{} }; |
99 | STACK_CHECK(L_, 1); | 99 | STACK_CHECK(L_, 1); |
100 | lua_newtable(L_); | 100 | lua_newtable(L_); |
101 | lua_setiuservalue(L_, -2, kContentsTableIndex); | 101 | lua_setiuservalue(L_, -2, kContentsTableIndex); |
102 | STACK_CHECK(L_, 1); | 102 | STACK_CHECK(L_, 1); |
103 | return fifo; | 103 | return _fifo; |
104 | } | 104 | } |
105 | 105 | ||
106 | // ################################################################################################# | 106 | // ################################################################################################# |
@@ -109,12 +109,12 @@ static constexpr int kContentsTableIndex{ 1 }; | |||
109 | // out: nothing, removes all pushed values from the stack | 109 | // out: nothing, removes all pushed values from the stack |
110 | static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) | 110 | static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) |
111 | { | 111 | { |
112 | int const idx{ lua_gettop(L_) - count_ }; | 112 | int const _idx{ lua_gettop(L_) - count_ }; |
113 | int const start{ fifo_->first + fifo_->count - 1 }; | 113 | int const _start{ fifo_->first + fifo_->count - 1 }; |
114 | // pop all additional arguments, storing them in the fifo | 114 | // pop all additional arguments, storing them in the fifo |
115 | for (int i = count_; i >= 1; --i) { | 115 | for (int _i = count_; _i >= 1; --_i) { |
116 | // store in the fifo the value at the top of the stack at the specified index, popping it from the stack | 116 | // store in the fifo the value at the top of the stack at the specified index, popping it from the stack |
117 | lua_rawseti(L_, idx, start + i); | 117 | lua_rawseti(L_, _idx, _start + _i); |
118 | } | 118 | } |
119 | fifo_->count += count_; | 119 | fifo_->count += count_; |
120 | } | 120 | } |
@@ -129,8 +129,8 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) | |||
129 | static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) | 129 | static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) |
130 | { | 130 | { |
131 | STACK_GROW(L_, count_); | 131 | STACK_GROW(L_, count_); |
132 | for (int i = 0; i < count_; ++i) { | 132 | for (int _i = 0; _i < count_; ++_i) { |
133 | lua_rawgeti(L_, 1, (fifo_->first + i)); | 133 | lua_rawgeti(L_, 1, (fifo_->first + _i)); |
134 | } | 134 | } |
135 | } | 135 | } |
136 | 136 | ||
@@ -141,32 +141,32 @@ static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) | |||
141 | static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) | 141 | static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) |
142 | { | 142 | { |
143 | LUA_ASSERT(L_, lua_istable(L_, -1)); | 143 | LUA_ASSERT(L_, lua_istable(L_, -1)); |
144 | int const fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl | 144 | int const _fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl |
145 | // each iteration pushes a value on the stack! | 145 | // each iteration pushes a value on the stack! |
146 | STACK_GROW(L_, count_ + 2); | 146 | STACK_GROW(L_, count_ + 2); |
147 | // skip first item, we will push it last | 147 | // skip first item, we will push it last |
148 | for (int i = 1; i < count_; ++i) { | 148 | for (int i = 1; i < count_; ++i) { |
149 | int const at{ fifo_->first + i }; | 149 | int const at{ fifo_->first + i }; |
150 | // push item on the stack | 150 | // push item on the stack |
151 | lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl val | 151 | lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl val |
152 | // remove item from the fifo | 152 | // remove item from the fifo |
153 | lua_pushnil(L_); // L_: ... fifotbl val nil | 153 | lua_pushnil(L_); // L_: ... fifotbl val nil |
154 | lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl val | 154 | lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl val |
155 | } | 155 | } |
156 | // now process first item | 156 | // now process first item |
157 | { | 157 | { |
158 | int const at{ fifo_->first }; | 158 | int const at{ fifo_->first }; |
159 | lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl vals val | 159 | lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl vals val |
160 | lua_pushnil(L_); // L_: ... fifotbl vals val nil | 160 | lua_pushnil(L_); // L_: ... fifotbl vals val nil |
161 | lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl vals val | 161 | lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl vals val |
162 | lua_replace(L_, fifo_idx); // L_: ... vals | 162 | lua_replace(L_, _fifo_idx); // L_: ... vals |
163 | } | 163 | } |
164 | 164 | ||
165 | // avoid ever-growing indexes by resetting each time we detect the fifo is empty | 165 | // avoid ever-growing indexes by resetting each time we detect the fifo is empty |
166 | { | 166 | { |
167 | int const new_count{ fifo_->count - count_ }; | 167 | int const _new_count{ fifo_->count - count_ }; |
168 | fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_); | 168 | fifo_->first = (_new_count == 0) ? 1 : (fifo_->first + count_); |
169 | fifo_->count = new_count; | 169 | fifo_->count = _new_count; |
170 | } | 170 | } |
171 | } | 171 | } |
172 | 172 | ||
@@ -202,34 +202,34 @@ static void push_table(lua_State* L_, int idx_) | |||
202 | // only used by linda:dump() and linda:__towatch() for debugging purposes | 202 | // only used by linda:dump() and linda:__towatch() for debugging purposes |
203 | int keeper_push_linda_storage(Linda& linda_, DestState L_) | 203 | int keeper_push_linda_storage(Linda& linda_, DestState L_) |
204 | { | 204 | { |
205 | Keeper* const K{ linda_.whichKeeper() }; | 205 | Keeper* const _K{ linda_.whichKeeper() }; |
206 | SourceState const KL{ K ? K->L : nullptr }; | 206 | SourceState const _KL{ _K ? _K->L : nullptr }; |
207 | if (KL == nullptr) | 207 | if (_KL == nullptr) |
208 | return 0; | 208 | return 0; |
209 | STACK_GROW(KL, 4); | 209 | STACK_GROW(_KL, 4); |
210 | STACK_CHECK_START_REL(KL, 0); | 210 | STACK_CHECK_START_REL(_KL, 0); |
211 | kFifosRegKey.pushValue(KL); // KL: fifos L_: | 211 | kFifosRegKey.pushValue(_KL); // KL: fifos L_: |
212 | lua_pushlightuserdata(KL, &linda_); // KL: fifos ud L_: | 212 | lua_pushlightuserdata(_KL, &linda_); // KL: fifos ud L_: |
213 | lua_rawget(KL, -2); // KL: fifos storage L_: | 213 | lua_rawget(_KL, -2); // KL: fifos storage L_: |
214 | lua_remove(KL, -2); // KL: storage L_: | 214 | lua_remove(_KL, -2); // KL: storage L_: |
215 | if (!lua_istable(KL, -1)) { | 215 | if (!lua_istable(_KL, -1)) { |
216 | lua_pop(KL, 1); // KL: L_: | 216 | lua_pop(_KL, 1); // KL: L_: |
217 | STACK_CHECK(KL, 0); | 217 | STACK_CHECK(_KL, 0); |
218 | return 0; | 218 | return 0; |
219 | } | 219 | } |
220 | // move data from keeper to destination state | 220 | // move data from keeper to destination state |
221 | STACK_GROW(L_, 5); | 221 | STACK_GROW(L_, 5); |
222 | STACK_CHECK_START_REL(L_, 0); | 222 | STACK_CHECK_START_REL(L_, 0); |
223 | lua_newtable(L_); // KL: storage L_: out | 223 | lua_newtable(L_); // KL: storage L_: out |
224 | InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; | 224 | InterCopyContext _c{ linda_.U, L_, _KL, {}, {}, {}, LookupMode::FromKeeper, {} }; |
225 | lua_pushnil(KL); // KL: storage nil L_: out | 225 | lua_pushnil(_KL); // KL: storage nil L_: out |
226 | while (lua_next(KL, -2)) { // KL: storage key fifo L_: out | 226 | while (lua_next(_KL, -2)) { // KL: storage key fifo L_: out |
227 | keeper_fifo* fifo = prepare_fifo_access(KL, -1); // KL: storage key fifotbl L_: out | 227 | keeper_fifo* fifo = prepare_fifo_access(_KL, -1); // KL: storage key fifotbl L_: out |
228 | lua_pushvalue(KL, -2); // KL: storage key fifotbl key L_: out | 228 | lua_pushvalue(_KL, -2); // KL: storage key fifotbl key L_: out |
229 | std::ignore = c.inter_move(1); // KL: storage key fifotbl L_: out key | 229 | std::ignore = _c.inter_move(1); // KL: storage key fifotbl L_: out key |
230 | STACK_CHECK(L_, 2); | 230 | STACK_CHECK(L_, 2); |
231 | lua_newtable(L_); // KL: storage key L_: out key keyout | 231 | lua_newtable(L_); // KL: storage key L_: out key keyout |
232 | std::ignore = c.inter_move(1); // KL: storage key L_: out key keyout fifotbl | 232 | std::ignore = _c.inter_move(1); // KL: storage key L_: out key keyout fifotbl |
233 | lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first | 233 | lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first |
234 | STACK_CHECK(L_, 5); | 234 | STACK_CHECK(L_, 5); |
235 | lua_setfield(L_, -3, "first"); // KL: storage key L_: out key keyout fifotbl | 235 | lua_setfield(L_, -3, "first"); // KL: storage key L_: out key keyout fifotbl |
@@ -244,8 +244,8 @@ int keeper_push_linda_storage(Linda& linda_, DestState L_) | |||
244 | STACK_CHECK(L_, 1); | 244 | STACK_CHECK(L_, 1); |
245 | } // KL_: storage L_: out | 245 | } // KL_: storage L_: out |
246 | STACK_CHECK(L_, 1); | 246 | STACK_CHECK(L_, 1); |
247 | lua_pop(KL, 1); // KL: L_: out | 247 | lua_pop(_KL, 1); // KL: L_: out |
248 | STACK_CHECK(KL, 0); | 248 | STACK_CHECK(_KL, 0); |
249 | return 1; | 249 | return 1; |
250 | } | 250 | } |
251 | 251 | ||
@@ -271,7 +271,7 @@ int keepercall_clear(lua_State* L_) | |||
271 | // out: true|false | 271 | // out: true|false |
272 | int keepercall_send(lua_State* L_) | 272 | int keepercall_send(lua_State* L_) |
273 | { | 273 | { |
274 | int const n{ lua_gettop(L_) - 2 }; | 274 | int const _n{ lua_gettop(L_) - 2 }; |
275 | push_table(L_, 1); // L_: ud key ... fifos | 275 | push_table(L_, 1); // L_: ud key ... fifos |
276 | // get the fifo associated to this key in this linda, create it if it doesn't exist | 276 | // get the fifo associated to this key in this linda, create it if it doesn't exist |
277 | lua_pushvalue(L_, 2); // L_: ud key ... fifos key | 277 | lua_pushvalue(L_, 2); // L_: ud key ... fifos key |
@@ -284,14 +284,14 @@ int keepercall_send(lua_State* L_) | |||
284 | lua_rawset(L_, -4); // L_: ud key ... fifos fifo | 284 | lua_rawset(L_, -4); // L_: ud key ... fifos fifo |
285 | } | 285 | } |
286 | lua_remove(L_, -2); // L_: ud key ... fifo | 286 | lua_remove(L_, -2); // L_: ud key ... fifo |
287 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 287 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
288 | if (fifo->limit >= 0 && fifo->count + n > fifo->limit) { | 288 | if (_fifo->limit >= 0 && _fifo->count + _n > _fifo->limit) { |
289 | lua_settop(L_, 0); // L_: | 289 | lua_settop(L_, 0); // L_: |
290 | lua_pushboolean(L_, 0); // L_:false | 290 | lua_pushboolean(L_, 0); // L_:false |
291 | } else { | 291 | } else { |
292 | fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl | 292 | _fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl |
293 | lua_replace(L_, 2); // L_: ud fifotbl ... | 293 | lua_replace(L_, 2); // L_: ud fifotbl ... |
294 | fifo_push(L_, fifo, n); // L_: ud fifotbl | 294 | fifo_push(L_, _fifo, _n); // L_: ud fifotbl |
295 | lua_settop(L_, 0); // L_: | 295 | lua_settop(L_, 0); // L_: |
296 | lua_pushboolean(L_, 1); // L_: true | 296 | lua_pushboolean(L_, 1); // L_: true |
297 | } | 297 | } |
@@ -304,19 +304,19 @@ int keepercall_send(lua_State* L_) | |||
304 | // out: (key, val) or nothing | 304 | // out: (key, val) or nothing |
305 | int keepercall_receive(lua_State* L_) | 305 | int keepercall_receive(lua_State* L_) |
306 | { | 306 | { |
307 | int const top{ lua_gettop(L_) }; | 307 | int const _top{ lua_gettop(L_) }; |
308 | push_table(L_, 1); // L_: ud keys fifos | 308 | push_table(L_, 1); // L_: ud keys fifos |
309 | lua_replace(L_, 1); // L_: fifos keys | 309 | lua_replace(L_, 1); // L_: fifos keys |
310 | for (int i = 2; i <= top; ++i) { | 310 | for (int _i = 2; _i <= _top; ++_i) { |
311 | lua_pushvalue(L_, i); // L_: fifos keys key[i] | 311 | lua_pushvalue(L_, _i); // L_: fifos keys key[i] |
312 | lua_rawget(L_, 1); // L_: fifos keys fifo | 312 | lua_rawget(L_, 1); // L_: fifos keys fifo |
313 | keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl | 313 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl |
314 | if (fifo != nullptr && fifo->count > 0) { | 314 | if (_fifo != nullptr && _fifo->count > 0) { |
315 | fifo_pop(L_, fifo, 1); // L_: fifos keys val | 315 | fifo_pop(L_, _fifo, 1); // L_: fifos keys val |
316 | if (!lua_isnil(L_, -1)) { | 316 | if (!lua_isnil(L_, -1)) { |
317 | lua_replace(L_, 1); // L_: val keys | 317 | lua_replace(L_, 1); // L_: val keys |
318 | lua_settop(L_, i); // L_: val keys key[i] | 318 | lua_settop(L_, _i); // L_: val keys key[i] |
319 | if (i != 2) { | 319 | if (_i != 2) { |
320 | lua_replace(L_, 2); // L_: val key keys | 320 | lua_replace(L_, 2); // L_: val key keys |
321 | lua_settop(L_, 2); // L_: val key | 321 | lua_settop(L_, 2); // L_: val key |
322 | } | 322 | } |
@@ -324,7 +324,7 @@ int keepercall_receive(lua_State* L_) | |||
324 | return 2; | 324 | return 2; |
325 | } | 325 | } |
326 | } | 326 | } |
327 | lua_settop(L_, top); // L_: data keys | 327 | lua_settop(L_, _top); // L_: data keys |
328 | } | 328 | } |
329 | // nothing to receive | 329 | // nothing to receive |
330 | return 0; | 330 | return 0; |
@@ -335,9 +335,9 @@ int keepercall_receive(lua_State* L_) | |||
335 | // in: linda_ud key mincount [maxcount] | 335 | // in: linda_ud key mincount [maxcount] |
336 | int keepercall_receive_batched(lua_State* L_) | 336 | int keepercall_receive_batched(lua_State* L_) |
337 | { | 337 | { |
338 | int const min_count{ static_cast<int>(lua_tointeger(L_, 3)) }; | 338 | int const _min_count{ static_cast<int>(lua_tointeger(L_, 3)) }; |
339 | if (min_count > 0) { | 339 | if (_min_count > 0) { |
340 | int const max_count{ static_cast<int>(luaL_optinteger(L_, 4, min_count)) }; | 340 | int const _max_count{ static_cast<int>(luaL_optinteger(L_, 4, _min_count)) }; |
341 | lua_settop(L_, 2); // L_: ud key | 341 | lua_settop(L_, 2); // L_: ud key |
342 | lua_insert(L_, 1); // L_: key ud | 342 | lua_insert(L_, 1); // L_: key ud |
343 | push_table(L_, 2); // L_: key ud fifos | 343 | push_table(L_, 2); // L_: key ud fifos |
@@ -345,9 +345,9 @@ int keepercall_receive_batched(lua_State* L_) | |||
345 | lua_pushvalue(L_, 1); // L_: key fifos key | 345 | lua_pushvalue(L_, 1); // L_: key fifos key |
346 | lua_rawget(L_, 2); // L_: key fifos fifo | 346 | lua_rawget(L_, 2); // L_: key fifos fifo |
347 | lua_remove(L_, 2); // L_: key fifo | 347 | lua_remove(L_, 2); // L_: key fifo |
348 | keeper_fifo* const fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl | 348 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl |
349 | if (fifo != nullptr && fifo->count >= min_count) { | 349 | if (_fifo != nullptr && _fifo->count >= _min_count) { |
350 | fifo_pop(L_, fifo, std::min(max_count, fifo->count)); // L_: key ... | 350 | fifo_pop(L_, _fifo, std::min(_max_count, _fifo->count)); // L_: key ... |
351 | } else { | 351 | } else { |
352 | lua_settop(L_, 0); // L_: | 352 | lua_settop(L_, 0); // L_: |
353 | } | 353 | } |
@@ -363,16 +363,16 @@ int keepercall_receive_batched(lua_State* L_) | |||
363 | // out: true or nil | 363 | // out: true or nil |
364 | int keepercall_limit(lua_State* L_) | 364 | int keepercall_limit(lua_State* L_) |
365 | { | 365 | { |
366 | int const limit{ static_cast<int>(lua_tointeger(L_, 3)) }; | 366 | int const _limit{ static_cast<int>(lua_tointeger(L_, 3)) }; |
367 | push_table(L_, 1); // L_: ud key n fifos | 367 | push_table(L_, 1); // L_: ud key n fifos |
368 | lua_replace(L_, 1); // L_: fifos key n | 368 | lua_replace(L_, 1); // L_: fifos key n |
369 | lua_pop(L_, 1); // L_: fifos key | 369 | lua_pop(L_, 1); // L_: fifos key |
370 | lua_pushvalue(L_, -1); // L_: fifos key key | 370 | lua_pushvalue(L_, -1); // L_: fifos key key |
371 | lua_rawget(L_, -3); // L_: fifos key fifo|nil | 371 | lua_rawget(L_, -3); // L_: fifos key fifo|nil |
372 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 372 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
373 | if (fifo == nullptr) { // L_: fifos key nil | 373 | if (_fifo == nullptr) { // L_: fifos key nil |
374 | lua_pop(L_, 1); // L_: fifos key | 374 | lua_pop(L_, 1); // L_: fifos key |
375 | fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo | 375 | _fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo |
376 | lua_rawset(L_, -3); // L_: fifos | 376 | lua_rawset(L_, -3); // L_: fifos |
377 | } | 377 | } |
378 | // remove any clutter on the stack | 378 | // remove any clutter on the stack |
@@ -380,13 +380,13 @@ int keepercall_limit(lua_State* L_) | |||
380 | // return true if we decide that blocked threads waiting to write on that key should be awakened | 380 | // return true if we decide that blocked threads waiting to write on that key should be awakened |
381 | // this is the case if we detect the key was full but it is no longer the case | 381 | // this is the case if we detect the key was full but it is no longer the case |
382 | if ( | 382 | if ( |
383 | ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit | 383 | ((_fifo->limit >= 0) && (_fifo->count >= _fifo->limit)) // the key was full if limited and count exceeded the previous limit |
384 | && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit | 384 | && ((_limit < 0) || (_fifo->count < _limit)) // the key is not full if unlimited or count is lower than the new limit |
385 | ) { | 385 | ) { |
386 | lua_pushboolean(L_, 1); // L_: true | 386 | lua_pushboolean(L_, 1); // L_: true |
387 | } | 387 | } |
388 | // set the new limit | 388 | // set the new limit |
389 | fifo->limit = limit; | 389 | _fifo->limit = _limit; |
390 | // return 0 or 1 value | 390 | // return 0 or 1 value |
391 | return lua_gettop(L_); | 391 | return lua_gettop(L_); |
392 | } | 392 | } |
@@ -397,7 +397,7 @@ int keepercall_limit(lua_State* L_) | |||
397 | // out: true if the linda was full but it's no longer the case, else nothing | 397 | // out: true if the linda was full but it's no longer the case, else nothing |
398 | int keepercall_set(lua_State* L_) | 398 | int keepercall_set(lua_State* L_) |
399 | { | 399 | { |
400 | bool should_wake_writers{ false }; | 400 | bool _should_wake_writers{ false }; |
401 | STACK_GROW(L_, 6); | 401 | STACK_GROW(L_, 6); |
402 | 402 | ||
403 | // retrieve fifos associated with the linda | 403 | // retrieve fifos associated with the linda |
@@ -409,28 +409,28 @@ int keepercall_set(lua_State* L_) | |||
409 | lua_pushvalue(L_, -1); // L_: fifos key key | 409 | lua_pushvalue(L_, -1); // L_: fifos key key |
410 | lua_rawget(L_, 1); // L_: fifos key fifo|nil | 410 | lua_rawget(L_, 1); // L_: fifos key fifo|nil |
411 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 411 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
412 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 412 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
413 | if (fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo | 413 | if (_fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo |
414 | if (fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it | 414 | if (_fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it |
415 | lua_pop(L_, 1); // L_: fifos key | 415 | lua_pop(L_, 1); // L_: fifos key |
416 | lua_pushnil(L_); // L_: fifos key nil | 416 | lua_pushnil(L_); // L_: fifos key nil |
417 | lua_rawset(L_, -3); // L_: fifos | 417 | lua_rawset(L_, -3); // L_: fifos |
418 | } else { | 418 | } else { |
419 | // we create room if the fifo was full but it is no longer the case | 419 | // we create room if the fifo was full but it is no longer the case |
420 | should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit); | 420 | _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit); |
421 | lua_remove(L_, -2); // L_: fifos fifo | 421 | lua_remove(L_, -2); // L_: fifos fifo |
422 | lua_newtable(L_); // L_: fifos fifo {} | 422 | lua_newtable(L_); // L_: fifos fifo {} |
423 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo | 423 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo |
424 | fifo->first = 1; | 424 | _fifo->first = 1; |
425 | fifo->count = 0; | 425 | _fifo->count = 0; |
426 | } | 426 | } |
427 | } | 427 | } |
428 | } else { // set/replace contents stored at the specified key? | 428 | } else { // set/replace contents stored at the specified key? |
429 | int const count{ lua_gettop(L_) - 2 }; // number of items we want to store | 429 | int const _count{ lua_gettop(L_) - 2 }; // number of items we want to store |
430 | lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key | 430 | lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key |
431 | lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil | 431 | lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil |
432 | keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; | 432 | keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; |
433 | if (fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil | 433 | if (_fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil |
434 | // no need to wake writers in that case, because a writer can't wait on an inexistent key | 434 | // no need to wake writers in that case, because a writer can't wait on an inexistent key |
435 | lua_pop(L_, 1); // L_: fifos key [val [, ...]] | 435 | lua_pop(L_, 1); // L_: fifos key [val [, ...]] |
436 | std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo | 436 | std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo |
@@ -440,19 +440,19 @@ int keepercall_set(lua_State* L_) | |||
440 | } else { // L_: fifos key [val [, ...]] fifo | 440 | } else { // L_: fifos key [val [, ...]] fifo |
441 | // the fifo exists, we just want to update its contents | 441 | // the fifo exists, we just want to update its contents |
442 | // we create room if the fifo was full but it is no longer the case | 442 | // we create room if the fifo was full but it is no longer the case |
443 | should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit) && (count < fifo->limit); | 443 | _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit) && (_count < _fifo->limit); |
444 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 444 | // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
445 | lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} | 445 | lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} |
446 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo | 446 | lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo |
447 | fifo->first = 1; | 447 | _fifo->first = 1; |
448 | fifo->count = 0; | 448 | _fifo->count = 0; |
449 | } | 449 | } |
450 | fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl | 450 | _fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl |
451 | // move the fifo below the values we want to store | 451 | // move the fifo below the values we want to store |
452 | lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] | 452 | lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] |
453 | fifo_push(L_, fifo, count); // L_: fifos key fifotbl | 453 | fifo_push(L_, _fifo, _count); // L_: fifos key fifotbl |
454 | } | 454 | } |
455 | return should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; | 455 | return _should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; |
456 | } | 456 | } |
457 | 457 | ||
458 | // ################################################################################################# | 458 | // ################################################################################################# |
@@ -461,21 +461,21 @@ int keepercall_set(lua_State* L_) | |||
461 | // out: at most <count> values | 461 | // out: at most <count> values |
462 | int keepercall_get(lua_State* L_) | 462 | int keepercall_get(lua_State* L_) |
463 | { | 463 | { |
464 | int count{ 1 }; | 464 | int _count{ 1 }; |
465 | if (lua_gettop(L_) == 3) { // L_: ud key count | 465 | if (lua_gettop(L_) == 3) { // L_: ud key count |
466 | count = static_cast<int>(lua_tointeger(L_, 3)); | 466 | _count = static_cast<int>(lua_tointeger(L_, 3)); |
467 | lua_pop(L_, 1); // L_: ud key | 467 | lua_pop(L_, 1); // L_: ud key |
468 | } | 468 | } |
469 | push_table(L_, 1); // L_: ud key fifos | 469 | push_table(L_, 1); // L_: ud key fifos |
470 | lua_replace(L_, 1); // L_: fifos key | 470 | lua_replace(L_, 1); // L_: fifos key |
471 | lua_rawget(L_, 1); // L_: fifos fifo | 471 | lua_rawget(L_, 1); // L_: fifos fifo |
472 | keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl | 472 | keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl |
473 | if (fifo != nullptr && fifo->count > 0) { | 473 | if (_fifo != nullptr && _fifo->count > 0) { |
474 | lua_remove(L_, 1); // L_: fifotbl | 474 | lua_remove(L_, 1); // L_: fifotbl |
475 | count = std::min(count, fifo->count); | 475 | _count = std::min(_count, _fifo->count); |
476 | // read <count> value off the fifo | 476 | // read <count> value off the fifo |
477 | fifo_peek(L_, fifo, count); // L_: fifotbl ... | 477 | fifo_peek(L_, _fifo, _count); // L_: fifotbl ... |
478 | return count; | 478 | return _count; |
479 | } | 479 | } |
480 | // no fifo was ever registered for this key, or it is empty | 480 | // no fifo was ever registered for this key, or it is empty |
481 | return 0; | 481 | return 0; |
@@ -494,10 +494,10 @@ int keepercall_count(lua_State* L_) | |||
494 | lua_replace(L_, 1); // L_: out fifos | 494 | lua_replace(L_, 1); // L_: out fifos |
495 | lua_pushnil(L_); // L_: out fifos nil | 495 | lua_pushnil(L_); // L_: out fifos nil |
496 | while (lua_next(L_, 2)) { // L_: out fifos key fifo | 496 | while (lua_next(L_, 2)) { // L_: out fifos key fifo |
497 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 497 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
498 | lua_pop(L_, 1); // L_: out fifos key | 498 | lua_pop(L_, 1); // L_: out fifos key |
499 | lua_pushvalue(L_, -1); // L_: out fifos key key | 499 | lua_pushvalue(L_, -1); // L_: out fifos key key |
500 | lua_pushinteger(L_, fifo->count); // L_: out fifos key key count | 500 | lua_pushinteger(L_, _fifo->count); // L_: out fifos key key count |
501 | lua_rawset(L_, -5); // L_: out fifos key | 501 | lua_rawset(L_, -5); // L_: out fifos key |
502 | } | 502 | } |
503 | lua_pop(L_, 1); // L_: out | 503 | lua_pop(L_, 1); // L_: out |
@@ -510,8 +510,8 @@ int keepercall_count(lua_State* L_) | |||
510 | if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil | 510 | if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil |
511 | lua_remove(L_, -2); // L_: nil | 511 | lua_remove(L_, -2); // L_: nil |
512 | } else { // the key is known // L_: fifos fifo | 512 | } else { // the key is known // L_: fifos fifo |
513 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 513 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
514 | lua_pushinteger(L_, fifo->count); // L_: fifos fifo count | 514 | lua_pushinteger(L_, _fifo->count); // L_: fifos fifo count |
515 | lua_replace(L_, -3); // L_: count fifo | 515 | lua_replace(L_, -3); // L_: count fifo |
516 | lua_pop(L_, 1); // L_: count | 516 | lua_pop(L_, 1); // L_: count |
517 | } | 517 | } |
@@ -526,10 +526,10 @@ int keepercall_count(lua_State* L_) | |||
526 | while (lua_gettop(L_) > 2) { | 526 | while (lua_gettop(L_) > 2) { |
527 | lua_pushvalue(L_, -1); // L_: out fifos keys... key | 527 | lua_pushvalue(L_, -1); // L_: out fifos keys... key |
528 | lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil | 528 | lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil |
529 | keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; | 529 | keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; |
530 | lua_pop(L_, 1); // L_: out fifos keys... | 530 | lua_pop(L_, 1); // L_: out fifos keys... |
531 | if (fifo != nullptr) { // L_: the key is known | 531 | if (_fifo != nullptr) { // L_: the key is known |
532 | lua_pushinteger(L_, fifo->count); // L_: out fifos keys... count | 532 | lua_pushinteger(L_, _fifo->count); // L_: out fifos keys... count |
533 | lua_rawset(L_, 1); // L_: out fifos keys... | 533 | lua_rawset(L_, 1); // L_: out fifos keys... |
534 | } else { // the key is unknown | 534 | } else { // the key is unknown |
535 | lua_pop(L_, 1); // L_: out fifos keys... | 535 | lua_pop(L_, 1); // L_: out fifos keys... |
@@ -557,27 +557,27 @@ int keepercall_count(lua_State* L_) | |||
557 | void close_keepers(Universe* U_) | 557 | void close_keepers(Universe* U_) |
558 | { | 558 | { |
559 | if (U_->keepers != nullptr) { | 559 | if (U_->keepers != nullptr) { |
560 | int nbKeepers{ U_->keepers->nb_keepers }; | 560 | int _nbKeepers{ U_->keepers->nb_keepers }; |
561 | // NOTE: imagine some keeper state N+1 currently holds a linda that uses another keeper N, and a _gc that will make use of it | 561 | // NOTE: imagine some keeper state N+1 currently holds a linda that uses another keeper N, and a _gc that will make use of it |
562 | // when keeper N+1 is closed, object is GCed, linda operation is called, which attempts to acquire keeper N, whose Lua state no longer exists | 562 | // when keeper N+1 is closed, object is GCed, linda operation is called, which attempts to acquire keeper N, whose Lua state no longer exists |
563 | // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success | 563 | // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success |
564 | // which is early-outed with a U->keepers->nbKeepers null-check | 564 | // which is early-outed with a U->keepers->nbKeepers null-check |
565 | U_->keepers->nb_keepers = 0; | 565 | U_->keepers->nb_keepers = 0; |
566 | for (int i = 0; i < nbKeepers; ++i) { | 566 | for (int _i = 0; _i < _nbKeepers; ++_i) { |
567 | lua_State* const K{ U_->keepers->keeper_array[i].L }; | 567 | lua_State* const K{ U_->keepers->keeper_array[_i].L }; |
568 | U_->keepers->keeper_array[i].L = KeeperState{ nullptr }; | 568 | U_->keepers->keeper_array[_i].L = KeeperState{ nullptr }; |
569 | if (K != nullptr) { | 569 | if (K != nullptr) { |
570 | lua_close(K); | 570 | lua_close(K); |
571 | } else { | 571 | } else { |
572 | // detected partial init: destroy only the mutexes that got initialized properly | 572 | // detected partial init: destroy only the mutexes that got initialized properly |
573 | nbKeepers = i; | 573 | _nbKeepers = _i; |
574 | } | 574 | } |
575 | } | 575 | } |
576 | for (int i = 0; i < nbKeepers; ++i) { | 576 | for (int _i = 0; _i < _nbKeepers; ++_i) { |
577 | U_->keepers->keeper_array[i].~Keeper(); | 577 | U_->keepers->keeper_array[_i].~Keeper(); |
578 | } | 578 | } |
579 | // free the keeper bookkeeping structure | 579 | // free the keeper bookkeeping structure |
580 | U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (nbKeepers - 1) * sizeof(Keeper)); | 580 | U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (_nbKeepers - 1) * sizeof(Keeper)); |
581 | U_->keepers = nullptr; | 581 | U_->keepers = nullptr; |
582 | } | 582 | } |
583 | } | 583 | } |
@@ -600,10 +600,10 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
600 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | 600 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); |
601 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 601 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
602 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers | 602 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers |
603 | int const nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; | 603 | int const _nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; |
604 | lua_pop(L_, 1); // L_: settings | 604 | lua_pop(L_, 1); // L_: settings |
605 | if (nb_keepers < 1) { | 605 | if (_nb_keepers < 1) { |
606 | raise_luaL_error(L_, "Bad number of keepers (%d)", nb_keepers); | 606 | raise_luaL_error(L_, "Bad number of keepers (%d)", _nb_keepers); |
607 | } | 607 | } |
608 | STACK_CHECK(L_, 0); | 608 | STACK_CHECK(L_, 0); |
609 | 609 | ||
@@ -614,51 +614,51 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
614 | 614 | ||
615 | // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states | 615 | // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states |
616 | { | 616 | { |
617 | size_t const bytes = sizeof(Keepers) + (nb_keepers - 1) * sizeof(Keeper); | 617 | size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); |
618 | U_->keepers = static_cast<Keepers*>(U_->internalAllocator.alloc(bytes)); | 618 | U_->keepers = static_cast<Keepers*>(U_->internalAllocator.alloc(bytes)); |
619 | if (U_->keepers == nullptr) { | 619 | if (U_->keepers == nullptr) { |
620 | raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); | 620 | raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); |
621 | } | 621 | } |
622 | U_->keepers->Keepers::Keepers(); | 622 | U_->keepers->Keepers::Keepers(); |
623 | U_->keepers->gc_threshold = keepers_gc_threshold; | 623 | U_->keepers->gc_threshold = keepers_gc_threshold; |
624 | U_->keepers->nb_keepers = nb_keepers; | 624 | U_->keepers->nb_keepers = _nb_keepers; |
625 | 625 | ||
626 | for (int i = 0; i < nb_keepers; ++i) { | 626 | for (int _i = 0; _i < _nb_keepers; ++_i) { |
627 | U_->keepers->keeper_array[i].Keeper::Keeper(); | 627 | U_->keepers->keeper_array[_i].Keeper::Keeper(); |
628 | } | 628 | } |
629 | } | 629 | } |
630 | for (int i = 0; i < nb_keepers; ++i) { | 630 | for (int _i = 0; _i < _nb_keepers; ++_i) { |
631 | // note that we will leak K if we raise an error later | 631 | // note that we will leak K if we raise an error later |
632 | KeeperState const K{ create_state(U_, L_) }; // L_: settings K: | 632 | KeeperState const _K{ create_state(U_, L_) }; // L_: settings K: |
633 | if (K == nullptr) { | 633 | if (_K == nullptr) { |
634 | raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); | 634 | raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); |
635 | } | 635 | } |
636 | 636 | ||
637 | U_->keepers->keeper_array[i].L = K; | 637 | U_->keepers->keeper_array[_i].L = _K; |
638 | 638 | ||
639 | if (U_->keepers->gc_threshold >= 0) { | 639 | if (U_->keepers->gc_threshold >= 0) { |
640 | lua_gc(K, LUA_GCSTOP, 0); | 640 | lua_gc(_K, LUA_GCSTOP, 0); |
641 | } | 641 | } |
642 | 642 | ||
643 | STACK_CHECK_START_ABS(K, 0); | 643 | STACK_CHECK_START_ABS(_K, 0); |
644 | 644 | ||
645 | // copy the universe pointer in the keeper itself | 645 | // copy the universe pointer in the keeper itself |
646 | universe_store(K, U_); | 646 | universe_store(_K, U_); |
647 | STACK_CHECK(K, 0); | 647 | STACK_CHECK(_K, 0); |
648 | 648 | ||
649 | // make sure 'package' is initialized in keeper states, so that we have require() | 649 | // make sure 'package' is initialized in keeper states, so that we have require() |
650 | // this because this is needed when transferring deep userdata object | 650 | // this because this is needed when transferring deep userdata object |
651 | luaL_requiref(K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package | 651 | luaL_requiref(_K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package |
652 | lua_pop(K, 1); // L_: settings K: | 652 | lua_pop(_K, 1); // L_: settings K: |
653 | STACK_CHECK(K, 0); | 653 | STACK_CHECK(_K, 0); |
654 | serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K); | 654 | serialize_require(DEBUGSPEW_PARAM_COMMA(U_) _K); |
655 | STACK_CHECK(K, 0); | 655 | STACK_CHECK(_K, 0); |
656 | 656 | ||
657 | // copy package.path and package.cpath from the source state | 657 | // copy package.path and package.cpath from the source state |
658 | if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: | 658 | if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: |
659 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately | 659 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately |
660 | InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; | 660 | InterCopyContext _c{ U_, DestState{ _K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; |
661 | if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: | 661 | if (_c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: |
662 | // if something went wrong, the error message is at the top of the stack | 662 | // if something went wrong, the error message is at the top of the stack |
663 | lua_remove(L_, -2); // L_: settings error_msg | 663 | lua_remove(L_, -2); // L_: settings error_msg |
664 | raise_lua_error(L_); | 664 | raise_lua_error(L_); |
@@ -666,19 +666,19 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
666 | } | 666 | } |
667 | lua_pop(L_, 1); // L_: settings K: | 667 | lua_pop(L_, 1); // L_: settings K: |
668 | STACK_CHECK(L_, 0); | 668 | STACK_CHECK(L_, 0); |
669 | STACK_CHECK(K, 0); | 669 | STACK_CHECK(_K, 0); |
670 | 670 | ||
671 | // attempt to call on_state_create(), if we have one and it is a C function | 671 | // attempt to call on_state_create(), if we have one and it is a C function |
672 | // (only support a C function because we can't transfer executable Lua code in keepers) | 672 | // (only support a C function because we can't transfer executable Lua code in keepers) |
673 | // will raise an error in L_ in case of problem | 673 | // will raise an error in L_ in case of problem |
674 | callOnStateCreate(U_, K, L_, LookupMode::ToKeeper); | 674 | callOnStateCreate(U_, _K, L_, LookupMode::ToKeeper); |
675 | 675 | ||
676 | // to see VM name in Decoda debugger | 676 | // to see VM name in Decoda debugger |
677 | lua_pushfstring(K, "Keeper #%d", i + 1); // L_: settings K: "Keeper #n" | 677 | lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" |
678 | lua_setglobal(K, "decoda_name"); // L_: settings K: | 678 | lua_setglobal(_K, "decoda_name"); // L_: settings K: |
679 | // create the fifos table in the keeper state | 679 | // create the fifos table in the keeper state |
680 | kFifosRegKey.setValue(K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: | 680 | kFifosRegKey.setValue(_K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: |
681 | STACK_CHECK(K, 0); | 681 | STACK_CHECK(_K, 0); |
682 | } | 682 | } |
683 | STACK_CHECK(L_, 0); | 683 | STACK_CHECK(L_, 0); |
684 | } | 684 | } |
@@ -687,12 +687,12 @@ void init_keepers(Universe* U_, lua_State* L_) | |||
687 | 687 | ||
688 | Keeper* Linda::acquireKeeper() const | 688 | Keeper* Linda::acquireKeeper() const |
689 | { | 689 | { |
690 | int const nbKeepers{ U->keepers->nb_keepers }; | 690 | int const _nbKeepers{ U->keepers->nb_keepers }; |
691 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) | 691 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) |
692 | if (nbKeepers) { | 692 | if (_nbKeepers) { |
693 | Keeper* const K{ &U->keepers->keeper_array[keeperIndex] }; | 693 | Keeper* const _K{ &U->keepers->keeper_array[keeperIndex] }; |
694 | K->mutex.lock(); | 694 | _K->mutex.lock(); |
695 | return K; | 695 | return _K; |
696 | } | 696 | } |
697 | return nullptr; | 697 | return nullptr; |
698 | } | 698 | } |
@@ -711,17 +711,17 @@ void Linda::releaseKeeper(Keeper* K_) const | |||
711 | 711 | ||
712 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) | 712 | void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) |
713 | { | 713 | { |
714 | int const n{ lua_gettop(L_) }; | 714 | int const _n{ lua_gettop(L_) }; |
715 | for (int i = start_; i <= n; ++i) { | 715 | for (int _i = start_; _i <= _n; ++_i) { |
716 | if (mode_ == LookupMode::ToKeeper) { | 716 | if (mode_ == LookupMode::ToKeeper) { |
717 | if (lua_isnil(L_, i)) { | 717 | if (lua_isnil(L_, _i)) { |
718 | kNilSentinel.pushKey(L_); | 718 | kNilSentinel.pushKey(L_); |
719 | lua_replace(L_, i); | 719 | lua_replace(L_, _i); |
720 | } | 720 | } |
721 | } else { | 721 | } else { |
722 | if (kNilSentinel.equals(L_, i)) { | 722 | if (kNilSentinel.equals(L_, _i)) { |
723 | lua_pushnil(L_); | 723 | lua_pushnil(L_); |
724 | lua_replace(L_, i); | 724 | lua_replace(L_, _i); |
725 | } | 725 | } |
726 | } | 726 | } |
727 | } | 727 | } |
@@ -740,21 +740,21 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mod | |||
740 | */ | 740 | */ |
741 | KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) | 741 | KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, lua_State* L_, void* linda_, int starting_index_) |
742 | { | 742 | { |
743 | KeeperCallResult result; | 743 | KeeperCallResult _result{}; |
744 | int const args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: | 744 | int const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: |
745 | int const top_K{ lua_gettop(K_) }; | 745 | int const _top_K{ lua_gettop(K_) }; |
746 | // if we didn't do anything wrong, the keeper stack should be clean | 746 | // if we didn't do anything wrong, the keeper stack should be clean |
747 | LUA_ASSERT(L_, top_K == 0); | 747 | LUA_ASSERT(L_, _top_K == 0); |
748 | 748 | ||
749 | STACK_GROW(K_, 2); | 749 | STACK_GROW(K_, 2); |
750 | PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ | 750 | PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ |
751 | lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda | 751 | lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda |
752 | if ( | 752 | if ( |
753 | (args == 0) || | 753 | (_args == 0) || |
754 | (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) | 754 | (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(_args) == InterCopyResult::Success) |
755 | ) { // L: ... args... K_: func_ linda args... | 755 | ) { // L: ... args... K_: func_ linda args... |
756 | lua_call(K_, 1 + args, LUA_MULTRET); // L: ... args... K_: result... | 756 | lua_call(K_, 1 + _args, LUA_MULTRET); // L: ... args... K_: result... |
757 | int const retvals{ lua_gettop(K_) - top_K }; | 757 | int const retvals{ lua_gettop(K_) - _top_K }; |
758 | // note that this can raise a lua error while the keeper state (and its mutex) is acquired | 758 | // note that this can raise a lua error while the keeper state (and its mutex) is acquired |
759 | // this may interrupt a lane, causing the destruction of the underlying OS thread | 759 | // this may interrupt a lane, causing the destruction of the underlying OS thread |
760 | // after this, another lane making use of this keeper can get an error code from the mutex-locking function | 760 | // after this, another lane making use of this keeper can get an error code from the mutex-locking function |
@@ -763,29 +763,29 @@ KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, l | |||
763 | (retvals == 0) || | 763 | (retvals == 0) || |
764 | (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) | 764 | (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) |
765 | ) { // L: ... args... result... K_: result... | 765 | ) { // L: ... args... result... K_: result... |
766 | result.emplace(retvals); | 766 | _result.emplace(retvals); |
767 | } | 767 | } |
768 | } | 768 | } |
769 | // whatever happens, restore the stack to where it was at the origin | 769 | // whatever happens, restore the stack to where it was at the origin |
770 | lua_settop(K_, top_K); // L: ... args... result... K_: | 770 | lua_settop(K_, _top_K); // L: ... args... result... K_: |
771 | 771 | ||
772 | // don't do this for this particular function, as it is only called during Linda destruction, and we don't want to raise an error, ever | 772 | // don't do this for this particular function, as it is only called during Linda destruction, and we don't want to raise an error, ever |
773 | if (func_ != KEEPER_API(clear)) [[unlikely]] { | 773 | if (func_ != KEEPER_API(clear)) [[unlikely]] { |
774 | // since keeper state GC is stopped, let's run a step once in a while if required | 774 | // since keeper state GC is stopped, let's run a step once in a while if required |
775 | int const gc_threshold{ U_->keepers->gc_threshold }; | 775 | int const _gc_threshold{ U_->keepers->gc_threshold }; |
776 | if (gc_threshold == 0) [[unlikely]] { | 776 | if (_gc_threshold == 0) [[unlikely]] { |
777 | lua_gc(K_, LUA_GCSTEP, 0); | 777 | lua_gc(K_, LUA_GCSTEP, 0); |
778 | } else if (gc_threshold > 0) [[likely]] { | 778 | } else if (_gc_threshold > 0) [[likely]] { |
779 | int const gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; | 779 | int const _gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; |
780 | if (gc_usage >= gc_threshold) { | 780 | if (_gc_usage >= _gc_threshold) { |
781 | lua_gc(K_, LUA_GCCOLLECT, 0); | 781 | lua_gc(K_, LUA_GCCOLLECT, 0); |
782 | int const gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; | 782 | int const _gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; |
783 | if (gc_usage_after > gc_threshold) [[unlikely]] { | 783 | if (_gc_usage_after > _gc_threshold) [[unlikely]] { |
784 | raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", gc_usage_after); | 784 | raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", _gc_usage_after); |
785 | } | 785 | } |
786 | } | 786 | } |
787 | } | 787 | } |
788 | } | 788 | } |
789 | 789 | ||
790 | return result; | 790 | return _result; |
791 | } | 791 | } |
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index b8f04b6..a8738a8 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -159,26 +159,26 @@ class StackChecker | |||
159 | }; | 159 | }; |
160 | 160 | ||
161 | #define STACK_CHECK_START_REL(L, offset_) \ | 161 | #define STACK_CHECK_START_REL(L, offset_) \ |
162 | StackChecker stackChecker_##L \ | 162 | StackChecker _stackChecker_##L \ |
163 | { \ | 163 | { \ |
164 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ | 164 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ |
165 | } | 165 | } |
166 | #define STACK_CHECK_START_ABS(L, offset_) \ | 166 | #define STACK_CHECK_START_ABS(L, offset_) \ |
167 | StackChecker stackChecker_##L \ | 167 | StackChecker _stackChecker_##L \ |
168 | { \ | 168 | { \ |
169 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | 169 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ |
170 | } | 170 | } |
171 | #define STACK_CHECK_RESET_REL(L, offset_) \ | 171 | #define STACK_CHECK_RESET_REL(L, offset_) \ |
172 | stackChecker_##L = StackChecker \ | 172 | _stackChecker_##L = StackChecker \ |
173 | { \ | 173 | { \ |
174 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ | 174 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ |
175 | } | 175 | } |
176 | #define STACK_CHECK_RESET_ABS(L, offset_) \ | 176 | #define STACK_CHECK_RESET_ABS(L, offset_) \ |
177 | stackChecker_##L = StackChecker \ | 177 | _stackChecker_##L = StackChecker \ |
178 | { \ | 178 | { \ |
179 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | 179 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ |
180 | } | 180 | } |
181 | #define STACK_CHECK(L, offset_) stackChecker_##L.check(offset_, __FILE__, __LINE__) | 181 | #define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__) |
182 | 182 | ||
183 | #endif // NDEBUG | 183 | #endif // NDEBUG |
184 | 184 | ||