Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can imagine the stack as growing from the bottom, with the bottom (i.e., the first pushed) element having index 1, then you push another element (index 2), then another one (index 3), etc.. So you have this situation:</p> <pre><code>+-----------------------+ | element with index 6 | &lt;-- top ("relative" index -1) +-----------------------+ | element with index 5 | &lt;-- -2 +-----------------------+ | element with index 4 | &lt;-- -3 +-----------------------+ | element with index 3 | &lt;-- -4 +-----------------------+ | element with index 2 | &lt;-- -5 +-----------------------+ | element with index 1 | &lt;-- bottom ("relative" index -6 ) +-----------------------+ </code></pre> <p>You could also say that the "normal index" (the one indexing from the bottom) is the absolute index of the element (like that of an array in C, besides starting from 1). Instead the negative index is "relative" to the top of stack.<code>lua_gettop</code> gives you the absolute index of the stack top (which has always relative index <code>-1</code>). </p> <p>Why are there two ways of indexing the stack, then? Because sometimes it is useful to access the elements like an array (using an absolute index) and sometimes you only need to access the last pushed elements (so indexing from the top). </p> <p>BTW, I usually visualize the Lua stack reversed: starting from above and growing downwards (i.e. the stack top is at the bottom of my mental representation). I find this mental model more useful because I interpret the index -1 as "<em>step back in code (upwards, therefore) until you find the the first push</em>". In this fashion, index -2 would be "<em>step back in code until you find the second push</em>", etc.. All this helps me quickly identify where I pushed what.</p> <p>However, to avoid confusion, here I used a more classical representation, where the stack top is drawn really at the top!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload