lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2023-02-01 23:43, game frog wrote:
static TValue *index2value (lua_State *L, int idx) {
   .............
  else if (!ispseudo(idx)) {  /* negative index */
api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
    return s2v(L->top + idx);
  }
   .............
  }

Hi, I have some questions~~

When executing 'else if(!ispseudo(idx)', I found this comment /*
negative index */
If 'idx' equals 0 is also a negative index?

The first branch in index2value handles common indexes (positive), then the branch you showed handles the negative indexes. But all pseudo-indexes like LUA_REGISTRYINDEX are negative, so the second branch is executed when the index is neither a positive one nor a pesudo one. But 0 is never a valid index, so the following api_check() handles that.

As the comment says, the second branch does handle negative indexes given by the user, rather pseudo ones defined by Lua itself.

--
Ziyao