[Bug] Possible bug of index2addr
frank-zsy opened this issue · comments
There is a possible bug of index2addr
, I am not sure why the first check is if (idx > 0)
but not if (idx >= 0)
, because the later check will assert idx !== 0 && -idx <= L.top
, so it makes the function always throws if I pass 0 as idx into the function.
The situation is that when I try to get a value from the stack, it may involve recursive get because of embedded table, so I transfer negative to positive index by idx = lua.lua_gettop(L) + index + 1
, but if there is only one value on the stack, the idx will be 0 and it will throw. So how can I do this.
It is the same in the lua C implementation: https://www.lua.org/source/5.3/lapi.c.html#index2addr
@daurnimator Thx, but how can I get idx 0 from the stack, right now I have no method to get the item from the stack position 0. Is this by design?
Why do you expect anything at index 0?
In Lua stack, when I try to get the item at idx
and if the idx
is negative, like -1, I can use lua.lua_gettop(L) + idx + 1
to change it to a positive index which will always points to the same item while -1 will always point to the top item. So if the stack changed, -1 index will point to another item.
And when there is only one item in the stack, when I try to get the top item use -1 index, the lua.lua_gettop(L) + idx + 1
will return 0.
if the
idx
is negative, like -1, I can uselua.lua_gettop(L) + idx + 1
to change it to a positive index which will always points to the same item while -1 will always point to the top item.
Use lua_absindex
for that.
And when there is only one item in the stack, when I try to get the top item use -1 index, the
lua.lua_gettop(L) + idx + 1
will return 0.
If there is one item on the stack then lua_gettop
will return 1. 1 - 1 + 1
== 1
. I don't know how you get 0
.
Thx, I will try that, I don't know why is that too 😢
@daurnimator Thank you, after called do_string, I try to get result from stack, but if the stack is empty, I will get index 0, so if I check the stack top first, the problem solved.