Browse Source

new API function 'lua_absindex'

pull/9/head
Roberto Ierusalimschy 15 years ago
parent
commit
e924a7f9ea
  1. 16
      lapi.c
  2. 13
      lauxlib.c
  3. 3
      lua.h

16
lapi.c

@ -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));
}

13
lauxlib.c

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.208 2010/04/14 15:14:21 roberto Exp roberto $
** $Id: lauxlib.c,v 1.209 2010/05/10 15:25:02 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -24,11 +24,6 @@
#include "lauxlib.h"
/* convert a stack index to positive */
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
lua_gettop(L) + (i) + 1)
/*
** {======================================================
** Traceback
@ -442,7 +437,7 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
LUALIB_API int luaL_ref (lua_State *L, int t) {
int ref;
t = abs_index(L, t);
t = lua_absindex(L, t);
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* remove from stack */
return LUA_REFNIL; /* `nil' has a unique fixed reference */
@ -463,7 +458,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
if (ref >= 0) {
t = abs_index(L, t);
t = lua_absindex(L, t);
lua_getfield(L, t, freelist);
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
lua_pushinteger(L, ref);
@ -600,7 +595,7 @@ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
obj = abs_index(L, obj);
obj = lua_absindex(L, obj);
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
return 0;
lua_pushvalue(L, obj);

3
lua.h

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.268 2010/04/14 15:14:21 roberto Exp roberto $
** $Id: lua.h,v 1.269 2010/05/10 13:50:20 roberto Exp roberto $
** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@ -129,6 +129,7 @@ LUA_API const lua_Number *(lua_version) (lua_State *L);
/*
** basic stack manipulation
*/
LUA_API int (lua_absindex) (lua_State *L, int idx);
LUA_API int (lua_gettop) (lua_State *L);
LUA_API void (lua_settop) (lua_State *L, int idx);
LUA_API void (lua_pushvalue) (lua_State *L, int idx);

Loading…
Cancel
Save