diff options
Diffstat (limited to '')
-rw-r--r-- | src/keeper.cpp | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index 58796b8..46f580b 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -82,10 +82,10 @@ class KeyUD | |||
82 | 82 | ||
83 | [[nodiscard]] bool changeLimit(LindaLimit limit_); | 83 | [[nodiscard]] bool changeLimit(LindaLimit limit_); |
84 | [[nodiscard]] static KeyUD* Create(KeeperState K_); | 84 | [[nodiscard]] static KeyUD* Create(KeeperState K_); |
85 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, int idx_); | 85 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, StackIndex idx_); |
86 | void peek(KeeperState K_, int count_) const; // keepercall_get | 86 | void peek(KeeperState K_, int count_) const; // keepercall_get |
87 | [[nodiscard]] int pop(KeeperState K_, int minCount_, int maxCount_); // keepercall_receive[_batched] | 87 | [[nodiscard]] int pop(KeeperState K_, int minCount_, int maxCount_); // keepercall_receive[_batched] |
88 | void prepareAccess(KeeperState K_, int idx_) const; | 88 | void prepareAccess(KeeperState K_, StackIndex idx_) const; |
89 | [[nodiscard]] bool push(KeeperState K_, int count_, bool enforceLimit_); // keepercall_send and keepercall_set | 89 | [[nodiscard]] bool push(KeeperState K_, int count_, bool enforceLimit_); // keepercall_send and keepercall_set |
90 | void pushFillStatus(KeeperState K_) const; | 90 | void pushFillStatus(KeeperState K_) const; |
91 | static void PushFillStatus(KeeperState K_, KeyUD const* key_); | 91 | static void PushFillStatus(KeeperState K_, KeyUD const* key_); |
@@ -116,14 +116,14 @@ KeyUD* KeyUD::Create(KeeperState const K_) | |||
116 | KeyUD* const _key{ new (K_) KeyUD{} }; | 116 | KeyUD* const _key{ new (K_) KeyUD{} }; |
117 | STACK_CHECK(K_, 1); | 117 | STACK_CHECK(K_, 1); |
118 | lua_newtable(K_); | 118 | lua_newtable(K_); |
119 | lua_setiuservalue(K_, -2, kContentsTableIndex); | 119 | lua_setiuservalue(K_, StackIndex{ -2 }, kContentsTableIndex); |
120 | STACK_CHECK(K_, 1); | 120 | STACK_CHECK(K_, 1); |
121 | return _key; | 121 | return _key; |
122 | } | 122 | } |
123 | 123 | ||
124 | // ################################################################################################# | 124 | // ################################################################################################# |
125 | 125 | ||
126 | KeyUD* KeyUD::GetPtr(KeeperState const K_, int idx_) | 126 | KeyUD* KeyUD::GetPtr(KeeperState const K_, StackIndex const idx_) |
127 | { | 127 | { |
128 | return luaG_tofulluserdata<KeyUD>(K_, idx_); | 128 | return luaG_tofulluserdata<KeyUD>(K_, idx_); |
129 | } | 129 | } |
@@ -137,7 +137,7 @@ KeyUD* KeyUD::GetPtr(KeeperState const K_, int idx_) | |||
137 | void KeyUD::peek(KeeperState const K_, int const count_) const | 137 | void KeyUD::peek(KeeperState const K_, int const count_) const |
138 | { | 138 | { |
139 | STACK_CHECK_START_ABS(K_, 1); | 139 | STACK_CHECK_START_ABS(K_, 1); |
140 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); // K_: KeyUD | 140 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, kIdxTop) == this); // K_: KeyUD |
141 | if (count <= 0) { // no data is available | 141 | if (count <= 0) { // no data is available |
142 | lua_pop(K_, 1); // K_: | 142 | lua_pop(K_, 1); // K_: |
143 | lua_pushinteger(K_, 0); // K_: 0 | 143 | lua_pushinteger(K_, 0); // K_: 0 |
@@ -145,7 +145,7 @@ void KeyUD::peek(KeeperState const K_, int const count_) const | |||
145 | } | 145 | } |
146 | 146 | ||
147 | // read <count_> value off the fifo, if possible | 147 | // read <count_> value off the fifo, if possible |
148 | prepareAccess(K_, -1); // K_: fifo | 148 | prepareAccess(K_, kIdxTop); // K_: fifo |
149 | int const _count{ std::min(count_, count) }; | 149 | int const _count{ std::min(count_, count) }; |
150 | lua_pushinteger(K_, _count); // K_: fifo _count | 150 | lua_pushinteger(K_, _count); // K_: fifo _count |
151 | lua_insert(K_, 1); // K_: _count fifo | 151 | lua_insert(K_, 1); // K_: _count fifo |
@@ -171,9 +171,9 @@ int KeyUD::pop(KeeperState const K_, int const minCount_, int const maxCount_) | |||
171 | return 0; | 171 | return 0; |
172 | } | 172 | } |
173 | int const _popCount{ std::min(count, maxCount_) }; | 173 | int const _popCount{ std::min(count, maxCount_) }; |
174 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); // K_: ... this | 174 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, kIdxTop) == this); // K_: ... this |
175 | prepareAccess(K_, -1); // K_: ... fifo | 175 | prepareAccess(K_, kIdxTop); // K_: ... fifo |
176 | int const _fifo_idx{ lua_gettop(K_) }; | 176 | StackIndex const _fifo_idx{ lua_gettop(K_) }; |
177 | // each iteration pushes a value on the stack! | 177 | // each iteration pushes a value on the stack! |
178 | STACK_GROW(K_, _popCount + 2); | 178 | STACK_GROW(K_, _popCount + 2); |
179 | // skip first item, we will push it last | 179 | // skip first item, we will push it last |
@@ -202,9 +202,9 @@ int KeyUD::pop(KeeperState const K_, int const minCount_, int const maxCount_) | |||
202 | 202 | ||
203 | // expects 'this' at the specified index | 203 | // expects 'this' at the specified index |
204 | // replaces it by its uservalue on the stack (the table holding the fifo values) | 204 | // replaces it by its uservalue on the stack (the table holding the fifo values) |
205 | void KeyUD::prepareAccess(KeeperState const K_, int const idx_) const | 205 | void KeyUD::prepareAccess(KeeperState const K_, StackIndex const idx_) const |
206 | { | 206 | { |
207 | int const _idx{ luaG_absindex(K_, idx_) }; | 207 | StackIndex const _idx{ luaG_absindex(K_, idx_) }; |
208 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, idx_) == this); | 208 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, idx_) == this); |
209 | // we can replace the key userdata in the stack without fear of it being GCed, there are other references around | 209 | // we can replace the key userdata in the stack without fear of it being GCed, there are other references around |
210 | lua_getiuservalue(K_, _idx, kContentsTableIndex); | 210 | lua_getiuservalue(K_, _idx, kContentsTableIndex); |
@@ -217,7 +217,7 @@ void KeyUD::prepareAccess(KeeperState const K_, int const idx_) const | |||
217 | // out: nothing, removes all pushed values from the stack | 217 | // out: nothing, removes all pushed values from the stack |
218 | bool KeyUD::push(KeeperState const K_, int const count_, bool const enforceLimit_) | 218 | bool KeyUD::push(KeeperState const K_, int const count_, bool const enforceLimit_) |
219 | { | 219 | { |
220 | int const _fifoIdx{ luaG_absindex(K_, -1 - count_) }; | 220 | StackIndex const _fifoIdx{ luaG_absindex(K_, StackIndex{ -1 - count_ }) }; |
221 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, _fifoIdx) == this); // K_: this val... | 221 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, _fifoIdx) == this); // K_: this val... |
222 | if (enforceLimit_ && (limit >= 0) && (count + count_ > limit)) { // not enough room | 222 | if (enforceLimit_ && (limit >= 0) && (count + count_ > limit)) { // not enough room |
223 | return false; | 223 | return false; |
@@ -270,13 +270,13 @@ void KeyUD::PushFillStatus(KeeperState const K_, KeyUD const* const key_) | |||
270 | // expects 'this' on top of the stack | 270 | // expects 'this' on top of the stack |
271 | bool KeyUD::reset(KeeperState const K_) | 271 | bool KeyUD::reset(KeeperState const K_) |
272 | { | 272 | { |
273 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); | 273 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, kIdxTop) == this); |
274 | STACK_CHECK_START_REL(K_, 0); | 274 | STACK_CHECK_START_REL(K_, 0); |
275 | bool const _wasFull{ (limit > 0) && (count >= limit) }; | 275 | bool const _wasFull{ (limit > 0) && (count >= limit) }; |
276 | // empty the KeyUD: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 276 | // empty the KeyUD: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
277 | // if we have an actual limit, use it to preconfigure the table | 277 | // if we have an actual limit, use it to preconfigure the table |
278 | lua_createtable(K_, (limit <= 0) ? 0 : limit, 0); // K_: KeysDB key val... KeyUD {} | 278 | lua_createtable(K_, (limit <= 0) ? 0 : limit, 0); // K_: KeysDB key val... KeyUD {} |
279 | lua_setiuservalue(K_, -2, kContentsTableIndex); // K_: KeysDB key val... KeyUD | 279 | lua_setiuservalue(K_, StackIndex{ -2 }, kContentsTableIndex); // K_: KeysDB key val... KeyUD |
280 | first = 1; | 280 | first = 1; |
281 | count = 0; | 281 | count = 0; |
282 | STACK_CHECK(K_, 0); | 282 | STACK_CHECK(K_, 0); |
@@ -290,20 +290,20 @@ bool KeyUD::reset(KeeperState const K_) | |||
290 | // out: the KeysDB table of the linda is pushed at the top of the stack | 290 | // out: the KeysDB table of the linda is pushed at the top of the stack |
291 | // xxh64 of string "kLindasRegKey" generated at https://www.pelock.com/products/hash-calculator | 291 | // xxh64 of string "kLindasRegKey" generated at https://www.pelock.com/products/hash-calculator |
292 | static constexpr RegistryUniqueKey kLindasRegKey{ 0x3AE0D5243A88B962ull }; | 292 | static constexpr RegistryUniqueKey kLindasRegKey{ 0x3AE0D5243A88B962ull }; |
293 | static void PushKeysDB(KeeperState const K_, int const idx_) | 293 | static void PushKeysDB(KeeperState const K_, StackIndex const idx_) |
294 | { | 294 | { |
295 | STACK_GROW(K_, 5); | 295 | STACK_GROW(K_, 5); |
296 | STACK_CHECK_START_REL(K_, 0); | 296 | STACK_CHECK_START_REL(K_, 0); |
297 | int const _idx{ luaG_absindex(K_, idx_) }; | 297 | StackIndex const _absidx{ luaG_absindex(K_, idx_) }; |
298 | kLindasRegKey.pushValue(K_); // K_: ... LindasDB | 298 | kLindasRegKey.pushValue(K_); // K_: ... LindasDB |
299 | lua_pushvalue(K_, _idx); // K_: ... LindasDB linda | 299 | lua_pushvalue(K_, _absidx); // K_: ... LindasDB linda |
300 | lua_rawget(K_, -2); // K_: ... LindasDB KeysDB | 300 | lua_rawget(K_, -2); // K_: ... LindasDB KeysDB |
301 | STACK_CHECK(K_, 2); | 301 | STACK_CHECK(K_, 2); |
302 | if (lua_isnil(K_, -1)) { | 302 | if (lua_isnil(K_, -1)) { |
303 | lua_pop(K_, 1); // K_: ... LindasDB | 303 | lua_pop(K_, 1); // K_: ... LindasDB |
304 | // add a new KeysDB table for this linda | 304 | // add a new KeysDB table for this linda |
305 | lua_newtable(K_); // K_: ... LindasDB KeysDB | 305 | lua_newtable(K_); // K_: ... LindasDB KeysDB |
306 | lua_pushvalue(K_, _idx); // K_: ... LindasDB KeysDB linda | 306 | lua_pushvalue(K_, _absidx); // K_: ... LindasDB KeysDB linda |
307 | lua_pushvalue(K_, -2); // K_: ... LindasDB KeysDB linda KeysDB | 307 | lua_pushvalue(K_, -2); // K_: ... LindasDB KeysDB linda KeysDB |
308 | lua_rawset(K_, -4); // K_: ... LindasDB KeysDB | 308 | lua_rawset(K_, -4); // K_: ... LindasDB KeysDB |
309 | } | 309 | } |
@@ -326,12 +326,12 @@ int keepercall_count(lua_State* const L_) | |||
326 | switch (lua_gettop(_K)) { | 326 | switch (lua_gettop(_K)) { |
327 | // no key is specified: return a table giving the count of all known keys | 327 | // no key is specified: return a table giving the count of all known keys |
328 | case 1: // _K: linda | 328 | case 1: // _K: linda |
329 | PushKeysDB(_K, 1); // _K: linda KeysDB | 329 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda KeysDB |
330 | lua_newtable(_K); // _K: linda KeysDB out | 330 | lua_newtable(_K); // _K: linda KeysDB out |
331 | lua_replace(_K, 1); // _K: out KeysDB | 331 | lua_replace(_K, 1); // _K: out KeysDB |
332 | lua_pushnil(_K); // _K: out KeysDB nil | 332 | lua_pushnil(_K); // _K: out KeysDB nil |
333 | while (lua_next(_K, 2)) { // _K: out KeysDB key KeyUD | 333 | while (lua_next(_K, 2)) { // _K: out KeysDB key KeyUD |
334 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 334 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
335 | lua_pop(_K, 1); // _K: out KeysDB key | 335 | lua_pop(_K, 1); // _K: out KeysDB key |
336 | lua_pushvalue(_K, -1); // _K: out KeysDB key key | 336 | lua_pushvalue(_K, -1); // _K: out KeysDB key key |
337 | lua_pushinteger(_K, _key->count); // _K: out KeysDB key key count | 337 | lua_pushinteger(_K, _key->count); // _K: out KeysDB key key count |
@@ -342,13 +342,13 @@ int keepercall_count(lua_State* const L_) | |||
342 | 342 | ||
343 | // 1 key is specified: return its count | 343 | // 1 key is specified: return its count |
344 | case 2: // _K: linda key | 344 | case 2: // _K: linda key |
345 | PushKeysDB(_K, 1); // _K: linda key KeysDB | 345 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda key KeysDB |
346 | lua_replace(_K, 1); // _K: KeysDB key | 346 | lua_replace(_K, 1); // _K: KeysDB key |
347 | lua_rawget(_K, -2); // _K: KeysDB KeyUD|nil | 347 | lua_rawget(_K, -2); // _K: KeysDB KeyUD|nil |
348 | if (lua_isnil(_K, -1)) { // the key is unknown // _K: KeysDB nil | 348 | if (lua_isnil(_K, -1)) { // the key is unknown // _K: KeysDB nil |
349 | lua_remove(_K, -2); // _K: nil | 349 | lua_remove(_K, -2); // _K: nil |
350 | } else { // the key is known // _K: KeysDB KeyUD | 350 | } else { // the key is known // _K: KeysDB KeyUD |
351 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 351 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
352 | lua_pushinteger(_K, _key->count); // _K: KeysDB KeyUD count | 352 | lua_pushinteger(_K, _key->count); // _K: KeysDB KeyUD count |
353 | lua_replace(_K, -3); // _K: count KeyUD | 353 | lua_replace(_K, -3); // _K: count KeyUD |
354 | lua_pop(_K, 1); // _K: count | 354 | lua_pop(_K, 1); // _K: count |
@@ -358,14 +358,14 @@ int keepercall_count(lua_State* const L_) | |||
358 | // a variable number of keys is specified: return a table of their counts | 358 | // a variable number of keys is specified: return a table of their counts |
359 | default: // _K: linda keys... key#1 | 359 | default: // _K: linda keys... key#1 |
360 | lua_pushvalue(_K, 2); // duplicate the first key of the list // _K: linda keys... key#1 | 360 | lua_pushvalue(_K, 2); // duplicate the first key of the list // _K: linda keys... key#1 |
361 | PushKeysDB(_K, 1); // _K: linda keys... key#1 KeysDB | 361 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda keys... key#1 KeysDB |
362 | lua_newtable(_K); // _K: linda keys... key#1 KeysDB out | 362 | lua_newtable(_K); // _K: linda keys... key#1 KeysDB out |
363 | lua_replace(_K, 1); // _K: out keys... key#1 KeysDB | 363 | lua_replace(_K, 1); // _K: out keys... key#1 KeysDB |
364 | lua_replace(_K, 2); // the list of keys is the same, but for key#1 moved at the end // _K: out KeysDB keys... | 364 | lua_replace(_K, 2); // the list of keys is the same, but for key#1 moved at the end // _K: out KeysDB keys... |
365 | while (lua_gettop(_K) > 2) { | 365 | while (lua_gettop(_K) > 2) { |
366 | lua_pushvalue(_K, -1); // _K: out KeysDB keys... key | 366 | lua_pushvalue(_K, -1); // _K: out KeysDB keys... key |
367 | lua_rawget(_K, 2); // _K: out KeysDB keys... KeyUD|nil | 367 | lua_rawget(_K, 2); // _K: out KeysDB keys... KeyUD|nil |
368 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 368 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
369 | lua_pop(_K, 1); // _K: out KeysDB keys... | 369 | lua_pop(_K, 1); // _K: out KeysDB keys... |
370 | if (_key != nullptr) { // the key is known | 370 | if (_key != nullptr) { // the key is known |
371 | lua_pushinteger(_K, _key->count); // _K: out KeysDB keys... count | 371 | lua_pushinteger(_K, _key->count); // _K: out KeysDB keys... count |
@@ -410,11 +410,11 @@ int keepercall_get(lua_State* const L_) | |||
410 | _count = static_cast<int>(lua_tointeger(_K, 3)); // linda:get() made sure _count >= 1 | 410 | _count = static_cast<int>(lua_tointeger(_K, 3)); // linda:get() made sure _count >= 1 |
411 | lua_pop(_K, 1); // _K: linda key | 411 | lua_pop(_K, 1); // _K: linda key |
412 | } | 412 | } |
413 | PushKeysDB(_K, 1); // _K: linda key KeysDB | 413 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda key KeysDB |
414 | lua_replace(_K, 1); // _K: KeysDB key | 414 | lua_replace(_K, 1); // _K: KeysDB key |
415 | lua_rawget(_K, 1); // _K: KeysDB KeyUD | 415 | lua_rawget(_K, 1); // _K: KeysDB KeyUD |
416 | lua_remove(_K, 1); // _K: KeyUD | 416 | lua_remove(_K, 1); // _K: KeyUD |
417 | KeyUD const* const _key{ KeyUD::GetPtr(_K, -1) }; | 417 | KeyUD const* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
418 | if (_key != nullptr) { | 418 | if (_key != nullptr) { |
419 | _key->peek(_K, _count); // _K: N val... | 419 | _key->peek(_K, _count); // _K: N val... |
420 | } else { | 420 | } else { |
@@ -438,11 +438,11 @@ int keepercall_limit(lua_State* const L_) | |||
438 | bool const _reading{ lua_gettop(_K) == 2 }; | 438 | bool const _reading{ lua_gettop(_K) == 2 }; |
439 | LindaLimit const _limit{ static_cast<LindaLimit::type>(luaL_optinteger(_K, 3, -1)) }; // -1 if we read nil because the argument is absent | 439 | LindaLimit const _limit{ static_cast<LindaLimit::type>(luaL_optinteger(_K, 3, -1)) }; // -1 if we read nil because the argument is absent |
440 | lua_settop(_K, 2); // _K: linda key | 440 | lua_settop(_K, 2); // _K: linda key |
441 | PushKeysDB(_K, 1); // _K: linda key KeysDB | 441 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda key KeysDB |
442 | lua_replace(_K, 1); // _K: KeysDB key | 442 | lua_replace(_K, 1); // _K: KeysDB key |
443 | lua_pushvalue(_K, -1); // _K: KeysDB key key | 443 | lua_pushvalue(_K, -1); // _K: KeysDB key key |
444 | lua_rawget(_K, -3); // _K: KeysDB key KeyUD|nil | 444 | lua_rawget(_K, -3); // _K: KeysDB key KeyUD|nil |
445 | KeyUD* _key{ KeyUD::GetPtr(_K, -1) }; | 445 | KeyUD* _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
446 | if (_reading) { | 446 | if (_reading) { |
447 | // remove any clutter on the stack | 447 | // remove any clutter on the stack |
448 | lua_settop(_K, 0); // _K: | 448 | lua_settop(_K, 0); // _K: |
@@ -476,14 +476,14 @@ int keepercall_limit(lua_State* const L_) | |||
476 | int keepercall_receive(lua_State* const L_) | 476 | int keepercall_receive(lua_State* const L_) |
477 | { | 477 | { |
478 | KeeperState const _K{ L_ }; | 478 | KeeperState const _K{ L_ }; |
479 | int const _top{ lua_gettop(_K) }; | 479 | StackIndex const _top{ lua_gettop(_K) }; |
480 | PushKeysDB(_K, 1); // _K: linda keys... KeysDB | 480 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda keys... KeysDB |
481 | lua_replace(_K, 1); // _K: KeysDB keys... | 481 | lua_replace(_K, 1); // _K: KeysDB keys... |
482 | 482 | ||
483 | for (int const _keyIdx : std::ranges::iota_view{ 2, _top + 1 }) { | 483 | for (StackIndex const _keyIdx : std::ranges::iota_view{ StackIndex{ 2 }, _top + 1 }) { |
484 | lua_pushvalue(_K, _keyIdx); // _K: KeysDB keys... key[i] | 484 | lua_pushvalue(_K, _keyIdx); // _K: KeysDB keys... key[i] |
485 | lua_rawget(_K, 1); // _K: KeysDB keys... KeyUD | 485 | lua_rawget(_K, 1); // _K: KeysDB keys... KeyUD |
486 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 486 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
487 | if (_key != nullptr) { // it's fine to attempt a read on a key that wasn't yet written to | 487 | if (_key != nullptr) { // it's fine to attempt a read on a key that wasn't yet written to |
488 | int const _popped{ _key->pop(_K, 1, 1) }; // _K: KeysDB keys... val | 488 | int const _popped{ _key->pop(_K, 1, 1) }; // _K: KeysDB keys... val |
489 | if (_popped > 0) { | 489 | if (_popped > 0) { |
@@ -514,12 +514,12 @@ int keepercall_receive_batched(lua_State* const L_) | |||
514 | int const _max_count{ static_cast<int>(luaL_optinteger(_K, 4, _min_count)) }; | 514 | int const _max_count{ static_cast<int>(luaL_optinteger(_K, 4, _min_count)) }; |
515 | lua_settop(_K, 2); // _K: linda key | 515 | lua_settop(_K, 2); // _K: linda key |
516 | lua_insert(_K, 1); // _K: key linda | 516 | lua_insert(_K, 1); // _K: key linda |
517 | PushKeysDB(_K, 2); // _K: key linda KeysDB | 517 | PushKeysDB(_K, StackIndex{ 2 }); // _K: key linda KeysDB |
518 | lua_remove(_K, 2); // _K: key KeysDB | 518 | lua_remove(_K, 2); // _K: key KeysDB |
519 | lua_pushvalue(_K, 1); // _K: key KeysDB key | 519 | lua_pushvalue(_K, 1); // _K: key KeysDB key |
520 | lua_rawget(_K, 2); // _K: key KeysDB KeyUD | 520 | lua_rawget(_K, 2); // _K: key KeysDB KeyUD |
521 | lua_remove(_K, 2); // _K: key KeyUD | 521 | lua_remove(_K, 2); // _K: key KeyUD |
522 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 522 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
523 | if (_key == nullptr || _key->pop(_K, _min_count, _max_count) == 0) { // _K: [key val...]|crap | 523 | if (_key == nullptr || _key->pop(_K, _min_count, _max_count) == 0) { // _K: [key val...]|crap |
524 | // Lua will adjust the stack for us when we return | 524 | // Lua will adjust the stack for us when we return |
525 | return 0; | 525 | return 0; |
@@ -538,7 +538,7 @@ int keepercall_send(lua_State* const L_) | |||
538 | KeeperState const _K{ L_ }; | 538 | KeeperState const _K{ L_ }; |
539 | int const _n{ lua_gettop(_K) - 2 }; | 539 | int const _n{ lua_gettop(_K) - 2 }; |
540 | STACK_CHECK_START_REL(_K, 0); // _K: linda key val... | 540 | STACK_CHECK_START_REL(_K, 0); // _K: linda key val... |
541 | PushKeysDB(_K, 1); // _K: linda key val... KeysDB | 541 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda key val... KeysDB |
542 | // get the fifo associated to this key in this linda, create it if it doesn't exist | 542 | // get the fifo associated to this key in this linda, create it if it doesn't exist |
543 | lua_pushvalue(_K, 2); // _K: linda key val... KeysDB key | 543 | lua_pushvalue(_K, 2); // _K: linda key val... KeysDB key |
544 | lua_rawget(_K, -2); // _K: linda key val... KeysDB KeyUD|nil | 544 | lua_rawget(_K, -2); // _K: linda key val... KeysDB KeyUD|nil |
@@ -553,7 +553,7 @@ int keepercall_send(lua_State* const L_) | |||
553 | lua_replace(_K, 2); // _K: linda KeyUD val... KeysDB | 553 | lua_replace(_K, 2); // _K: linda KeyUD val... KeysDB |
554 | lua_pop(_K, 1); // _K: linda KeyUD val... | 554 | lua_pop(_K, 1); // _K: linda KeyUD val... |
555 | STACK_CHECK(_K, 0); | 555 | STACK_CHECK(_K, 0); |
556 | KeyUD* const _key{ KeyUD::GetPtr(_K, 2) }; | 556 | KeyUD* const _key{ KeyUD::GetPtr(_K, StackIndex{ 2 }) }; |
557 | if (_key && _key->push(_K, _n, true)) { // not enough room? | 557 | if (_key && _key->push(_K, _n, true)) { // not enough room? |
558 | lua_settop(_K, 0); // _K: | 558 | lua_settop(_K, 0); // _K: |
559 | lua_pushboolean(_K, 1); // _K: true | 559 | lua_pushboolean(_K, 1); // _K: true |
@@ -576,11 +576,11 @@ int keepercall_set(lua_State* const L_) | |||
576 | STACK_GROW(_K, 6); | 576 | STACK_GROW(_K, 6); |
577 | 577 | ||
578 | // retrieve KeysDB associated with the linda | 578 | // retrieve KeysDB associated with the linda |
579 | PushKeysDB(_K, 1); // _K: linda key val... KeysDB | 579 | PushKeysDB(_K, StackIndex{ 1 }); // _K: linda key val... KeysDB |
580 | lua_replace(_K, 1); // _K: KeysDB key val... | 580 | lua_replace(_K, 1); // _K: KeysDB key val... |
581 | lua_pushvalue(_K, 2); // _K: KeysDB key val... key | 581 | lua_pushvalue(_K, 2); // _K: KeysDB key val... key |
582 | lua_rawget(_K, 1); // _K: KeysDB key val KeyUD|nil | 582 | lua_rawget(_K, 1); // _K: KeysDB key val KeyUD|nil |
583 | KeyUD* _key{ KeyUD::GetPtr(_K, -1) }; | 583 | KeyUD* _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
584 | 584 | ||
585 | if (lua_gettop(_K) == 3) { // no value to set // _K: KeysDB key KeyUD|nil | 585 | if (lua_gettop(_K) == 3) { // no value to set // _K: KeysDB key KeyUD|nil |
586 | // empty the KeyUD for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! | 586 | // empty the KeyUD for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! |
@@ -633,11 +633,11 @@ int keepercall_set(lua_State* const L_) | |||
633 | * | 633 | * |
634 | * Returns: number of return values (pushed to 'L'), unset in case of error | 634 | * Returns: number of return values (pushed to 'L'), unset in case of error |
635 | */ | 635 | */ |
636 | KeeperCallResult keeper_call(KeeperState K_, keeper_api_t func_, lua_State* L_, Linda* linda_, int starting_index_) | 636 | KeeperCallResult keeper_call(KeeperState const K_, keeper_api_t const func_, lua_State* const L_, Linda* const linda_, StackIndex const starting_index_) |
637 | { | 637 | { |
638 | KeeperCallResult _result; | 638 | KeeperCallResult _result; |
639 | int const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: | 639 | StackIndex const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: |
640 | int const _top_K{ lua_gettop(K_) }; | 640 | StackIndex const _top_K{ lua_gettop(K_) }; |
641 | // if we didn't do anything wrong, the keeper stack should be clean | 641 | // if we didn't do anything wrong, the keeper stack should be clean |
642 | LUA_ASSERT(L_, _top_K == 0); | 642 | LUA_ASSERT(L_, _top_K == 0); |
643 | 643 | ||
@@ -743,8 +743,8 @@ int Keeper::PushLindaStorage(Linda& linda_, DestState const L_) | |||
743 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; | 743 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; |
744 | lua_pushnil(_K); // _K: KeysDB nil L_: out | 744 | lua_pushnil(_K); // _K: KeysDB nil L_: out |
745 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out | 745 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out |
746 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; | 746 | KeyUD* const _key{ KeyUD::GetPtr(_K, kIdxTop) }; |
747 | _key->prepareAccess(_K, -1); // _K: KeysDB key fifo L_: out | 747 | _key->prepareAccess(_K, kIdxTop); // _K: KeysDB key fifo L_: out |
748 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out | 748 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out |
749 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key fifo L_: out key | 749 | if (_c.interMove(1) != InterCopyResult::Success) { // _K: KeysDB key fifo L_: out key |
750 | raise_luaL_error(L_, "Internal error reading Keeper contents"); | 750 | raise_luaL_error(L_, "Internal error reading Keeper contents"); |
@@ -928,7 +928,7 @@ void Keepers::initialize(Universe& U_, lua_State* L_, int const nbKeepers_, int | |||
928 | // copy package.path and package.cpath from the source state | 928 | // copy package.path and package.cpath from the source state |
929 | if (luaG_getmodule(L, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package _K: | 929 | if (luaG_getmodule(L, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package _K: |
930 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately | 930 | // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately |
931 | InterCopyContext _c{ U, DestState{ _K }, SourceState{ L }, {}, SourceIndex{ luaG_absindex(L, -1) }, {}, LookupMode::ToKeeper, {} }; | 931 | InterCopyContext _c{ U, DestState{ _K }, SourceState{ L }, {}, SourceIndex{ luaG_absindex(L, kIdxTop) }, {}, LookupMode::ToKeeper, {} }; |
932 | if (_c.interCopyPackage() != InterCopyResult::Success) { // L_: settings ... error_msg _K: | 932 | if (_c.interCopyPackage() != InterCopyResult::Success) { // L_: settings ... error_msg _K: |
933 | // if something went wrong, the error message is at the top of the stack | 933 | // if something went wrong, the error message is at the top of the stack |
934 | lua_remove(L, -2); // L_: settings error_msg | 934 | lua_remove(L, -2); // L_: settings error_msg |