new API function 'lua_absindex'

This commit is contained in:
Roberto Ierusalimschy
2010-05-12 11:09:20 -03:00
parent 4fd76b8148
commit e924a7f9ea
3 changed files with 19 additions and 13 deletions

16
lapi.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 2.126 2010/05/05 18:53:41 roberto Exp roberto $
** $Id: lapi.c,v 2.127 2010/05/07 18:10:01 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -61,8 +61,8 @@ static TValue *index2addr (lua_State *L, int idx) {
else {
Closure *func = clvalue(ci->func);
return (idx <= func->c.nupvalues)
? &func->c.upvalue[idx-1]
: cast(TValue *, luaO_nilobject);
? &func->c.upvalue[idx-1]
: cast(TValue *, luaO_nilobject);
}
}
}
@@ -136,6 +136,16 @@ LUA_API const lua_Number *lua_version (lua_State *L) {
*/
/*
** convert an acceptable stack index into an absolute index
*/
LUA_API int lua_absindex (lua_State *L, int idx) {
return (idx > 0 || idx <= LUA_REGISTRYINDEX)
? idx
: lua_gettop(L) + idx + 1;
}
LUA_API int lua_gettop (lua_State *L) {
return cast_int(L->top - (L->ci->func + 1));
}