aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-05-12 11:09:20 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-05-12 11:09:20 -0300
commite924a7f9ea495df3b2311eb1a0bcb176f34d0545 (patch)
tree8764c3c54c234c5a0ac2a79c9828b5d221020f99
parent4fd76b8148eae1a1be43a265588bd0fc3959c33b (diff)
downloadlua-e924a7f9ea495df3b2311eb1a0bcb176f34d0545.tar.gz
lua-e924a7f9ea495df3b2311eb1a0bcb176f34d0545.tar.bz2
lua-e924a7f9ea495df3b2311eb1a0bcb176f34d0545.zip
new API function 'lua_absindex'
-rw-r--r--lapi.c16
-rw-r--r--lauxlib.c13
-rw-r--r--lua.h3
3 files changed, 19 insertions, 13 deletions
diff --git a/lapi.c b/lapi.c
index a63e4fbd..e32f696d 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.126 2010/05/05 18:53:41 roberto Exp roberto $ 2** $Id: lapi.c,v 2.127 2010/05/07 18:10:01 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -61,8 +61,8 @@ static TValue *index2addr (lua_State *L, int idx) {
61 else { 61 else {
62 Closure *func = clvalue(ci->func); 62 Closure *func = clvalue(ci->func);
63 return (idx <= func->c.nupvalues) 63 return (idx <= func->c.nupvalues)
64 ? &func->c.upvalue[idx-1] 64 ? &func->c.upvalue[idx-1]
65 : cast(TValue *, luaO_nilobject); 65 : cast(TValue *, luaO_nilobject);
66 } 66 }
67 } 67 }
68} 68}
@@ -136,6 +136,16 @@ LUA_API const lua_Number *lua_version (lua_State *L) {
136*/ 136*/
137 137
138 138
139/*
140** convert an acceptable stack index into an absolute index
141*/
142LUA_API int lua_absindex (lua_State *L, int idx) {
143 return (idx > 0 || idx <= LUA_REGISTRYINDEX)
144 ? idx
145 : lua_gettop(L) + idx + 1;
146}
147
148
139LUA_API int lua_gettop (lua_State *L) { 149LUA_API int lua_gettop (lua_State *L) {
140 return cast_int(L->top - (L->ci->func + 1)); 150 return cast_int(L->top - (L->ci->func + 1));
141} 151}
diff --git a/lauxlib.c b/lauxlib.c
index 7d35124c..c3fa8be6 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.208 2010/04/14 15:14:21 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.209 2010/05/10 15:25:02 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -24,11 +24,6 @@
24#include "lauxlib.h" 24#include "lauxlib.h"
25 25
26 26
27/* convert a stack index to positive */
28#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
29 lua_gettop(L) + (i) + 1)
30
31
32/* 27/*
33** {====================================================== 28** {======================================================
34** Traceback 29** Traceback
@@ -442,7 +437,7 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
442 437
443LUALIB_API int luaL_ref (lua_State *L, int t) { 438LUALIB_API int luaL_ref (lua_State *L, int t) {
444 int ref; 439 int ref;
445 t = abs_index(L, t); 440 t = lua_absindex(L, t);
446 if (lua_isnil(L, -1)) { 441 if (lua_isnil(L, -1)) {
447 lua_pop(L, 1); /* remove from stack */ 442 lua_pop(L, 1); /* remove from stack */
448 return LUA_REFNIL; /* `nil' has a unique fixed reference */ 443 return LUA_REFNIL; /* `nil' has a unique fixed reference */
@@ -463,7 +458,7 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
463 458
464LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { 459LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
465 if (ref >= 0) { 460 if (ref >= 0) {
466 t = abs_index(L, t); 461 t = lua_absindex(L, t);
467 lua_getfield(L, t, freelist); 462 lua_getfield(L, t, freelist);
468 lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */ 463 lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
469 lua_pushinteger(L, ref); 464 lua_pushinteger(L, ref);
@@ -600,7 +595,7 @@ LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
600 595
601 596
602LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { 597LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
603 obj = abs_index(L, obj); 598 obj = lua_absindex(L, obj);
604 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ 599 if (!luaL_getmetafield(L, obj, event)) /* no metafield? */
605 return 0; 600 return 0;
606 lua_pushvalue(L, obj); 601 lua_pushvalue(L, obj);
diff --git a/lua.h b/lua.h
index 37525166..586c9388 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.268 2010/04/14 15:14:21 roberto Exp roberto $ 2** $Id: lua.h,v 1.269 2010/05/10 13:50:20 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -129,6 +129,7 @@ LUA_API const lua_Number *(lua_version) (lua_State *L);
129/* 129/*
130** basic stack manipulation 130** basic stack manipulation
131*/ 131*/
132LUA_API int (lua_absindex) (lua_State *L, int idx);
132LUA_API int (lua_gettop) (lua_State *L); 133LUA_API int (lua_gettop) (lua_State *L);
133LUA_API void (lua_settop) (lua_State *L, int idx); 134LUA_API void (lua_settop) (lua_State *L, int idx);
134LUA_API void (lua_pushvalue) (lua_State *L, int idx); 135LUA_API void (lua_pushvalue) (lua_State *L, int idx);