简体   繁体   English

Lua C API:lua_gettop()和-1之间有什么区别?

[英]Lua C API: what's the difference between lua_gettop() and -1?

I don't really understand stack exactly. 我并不完全理解堆栈。

lua_gettop()

Returns the index of the top element in the stack. 返回堆栈中顶部元素的索引。 Because indices start at 1, this result is equal to the number of elements in the stack (and so 0 means an empty stack). 因为索引从1开始,所以此结果等于堆栈中元素的数量(因此0表示空堆栈)。

so what's the difference between it and -1? 那么它与-1之间有什么区别?

lua_getglobal(L,"Foo");
if( lua_isfunction(L,lua_gettop(L)) ) {

lua_getglobal(L,"Foo");
if( lua_isfunction(L,-1) ) {

You can imagine the stack as growing from the bottom, with the bottom (ie, 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: 您可以想象堆栈从底部开始增长,底部(即第一个推送)元素具有索引1,然后您推送另一个元素(索引2),然后另一个元素(索引3)等等。所以你有这个情况:

+-----------------------+
| element with index 6  | <-- top ("relative" index -1)
+-----------------------+
| element with index 5  | <-- -2
+-----------------------+
| element with index 4  | <-- -3
+-----------------------+
| element with index 3  | <-- -4
+-----------------------+
| element with index 2  | <-- -5
+-----------------------+
| element with index 1  | <-- bottom ("relative" index -6 )
+-----------------------+

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). 您还可以说“正常索引”(从底部索引的索引)是元素的绝对索引(就像C中的数组一样,除了从1开始)。 Instead the negative index is "relative" to the top of stack. 相反,负指数与堆栈顶部“相对”。 lua_gettop gives you the absolute index of the stack top (which has always relative index -1 ). lua_gettop为您提供堆栈顶部的绝对索引(它始终具有相对索引-1 )。

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). 因为有时访问像数组这样的元素很有用(使用绝对索引),有时你只需要访问最后推送的元素(所以从顶部索引)。

BTW, I usually visualize the Lua stack reversed: starting from above and growing downwards (ie the stack top is at the bottom of my mental representation). 顺便说一句,我通常想象Lua堆栈的反转:从上面开始向下生长(即堆栈顶部位于我的心理表示的底部)。 I find this mental model more useful because I interpret the index -1 as " step back in code (upwards, therefore) until you find the the first push ". 我发现这个心智模型更有用,因为我将索引-1解释为“ 在代码中向后退(因此向上),直到找到第一个推送 ”。 In this fashion, index -2 would be " step back in code until you find the second push ", etc.. All this helps me quickly identify where I pushed what. 以这种方式,索引-2将“ 退回到代码中,直到找到第二次推送 ”等等。所有这些都有助于我快速确定我推送的内容。

However, to avoid confusion, here I used a more classical representation, where the stack top is drawn really at the top! 但是,为了避免混淆,我在这里使用了更经典的表示法,其中堆栈顶部真的在顶部绘制!

From PIL ( http://www.lua.org/pil/24.2.3.html ) 来自PIL( http://www.lua.org/pil/24.2.3.html

Notice that a negative index -x is equivalent to the positive index gettop - x + 1. 请注意,负索引-x等于正索引gettop - x + 1。

Therefore 因此

if( lua_isfunction(L,lua_gettop(L)) ) {

does the same than 做同样的事

if( lua_isfunction(L,-1) ) {

There's no difference if you immediately use it as an index like that. 如果您立即将其用作此类索引,则没有区别。 But you can do other things with it, like store the index and use it later, when it might no longer be the last index. 但是你可以用它来做其他事情,比如存储索引并在以后使用它时,它可能不再是最后一个索引。

As you've already stated, lua_gettop returns the index of the top element on the stack. 正如您已经说过的那样,lua_gettop返回堆栈顶部元素的索引。 If the stack is empty then there is no element on index -1. 如果堆栈为空,则索引-1上没有元素。 So the function lua_gettop gives you a boundary for the index you could use. 因此,函数lua_gettop为您提供了可以使用的索引的边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM