aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-26 16:28:44 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-06-26 16:28:44 -0300
commitf67ccfbdeb9595b0549a733d229a02a7c3205ddc (patch)
treeb59a5afc174ac338e23ad58dc564640324ba6b19
parentcfcf2008069fc2d5574e10303dbe4f1ea3188636 (diff)
downloadlua-f67ccfbdeb9595b0549a733d229a02a7c3205ddc.tar.gz
lua-f67ccfbdeb9595b0549a733d229a02a7c3205ddc.tar.bz2
lua-f67ccfbdeb9595b0549a733d229a02a7c3205ddc.zip
no more `lua_getn' function
-rw-r--r--lapi.c39
-rw-r--r--lauxlib.c17
-rw-r--r--lbaselib.c21
-rw-r--r--lua.h3
4 files changed, 27 insertions, 53 deletions
diff --git a/lapi.c b/lapi.c
index 84b3fe72..67fe9ddc 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.202 2002/06/24 13:08:45 roberto Exp roberto $ 2** $Id: lapi.c,v 1.203 2002/06/25 19:15:41 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*/
@@ -697,43 +697,6 @@ LUA_API int lua_next (lua_State *L, int index) {
697} 697}
698 698
699 699
700LUA_API int lua_getn (lua_State *L, int index) {
701 StkId t;
702 const TObject *value;
703 int n;
704 lua_lock(L);
705 t = luaA_index(L, index);
706 api_check(L, ttype(t) == LUA_TTABLE);
707 value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
708 if (ttype(value) == LUA_TNUMBER)
709 lua_number2int(n, nvalue(value));
710 else {
711 Node *nd;
712 Table *a = hvalue(t);
713 lua_Number max = 0;
714 int i;
715 i = sizearray(a);
716 while (i--) {
717 if (ttype(&a->array[i]) != LUA_TNIL)
718 break;
719 }
720 max = i+1;
721 i = sizenode(a);
722 nd = a->node;
723 while (i--) {
724 if (ttype(key(nd)) == LUA_TNUMBER &&
725 ttype(val(nd)) != LUA_TNIL &&
726 nvalue(key(nd)) > max)
727 max = nvalue(key(nd));
728 nd++;
729 }
730 lua_number2int(n, max);
731 }
732 lua_unlock(L);
733 return n;
734}
735
736
737LUA_API void lua_concat (lua_State *L, int n) { 700LUA_API void lua_concat (lua_State *L, int n) {
738 lua_lock(L); 701 lua_lock(L);
739 api_checknelems(L, n); 702 api_checknelems(L, n);
diff --git a/lauxlib.c b/lauxlib.c
index b090e9c5..26a81287 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.75 2002/06/18 15:19:27 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.76 2002/06/25 19:15:21 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*/
@@ -272,17 +272,20 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
272LUALIB_API int luaL_ref (lua_State *L, int t) { 272LUALIB_API int luaL_ref (lua_State *L, int t) {
273 int ref; 273 int ref;
274 lua_rawgeti(L, t, 0); /* get first free element */ 274 lua_rawgeti(L, t, 0); /* get first free element */
275 ref = (int)lua_tonumber(L, -1); 275 ref = (int)lua_tonumber(L, -1); /* ref = t[0] */
276 lua_pop(L, 1); /* remove it from stack */ 276 lua_pop(L, 1); /* remove it from stack */
277 if (ref != 0) { /* any free element? */ 277 if (ref != 0) { /* any free element? */
278 lua_rawgeti(L, t, ref); /* remove it from list */ 278 lua_rawgeti(L, t, ref); /* remove it from list */
279 lua_rawseti(L, t, 0); 279 lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */
280 } 280 }
281 else { /* no free elements */ 281 else { /* no free elements */
282 ref = lua_getn(L, t) + 1; /* use next `n' */
283 lua_pushliteral(L, "n"); 282 lua_pushliteral(L, "n");
283 lua_pushvalue(L, -1);
284 lua_rawget(L, t); /* get t.n */
285 ref = (int)lua_tonumber(L, -1) + 1; /* ref = t.n + 1 */
286 lua_pop(L, 1); /* pop t.n */
284 lua_pushnumber(L, ref); 287 lua_pushnumber(L, ref);
285 lua_rawset(L, t); /* n = n+1 */ 288 lua_rawset(L, t); /* t.n = t.n + 1 */
286 } 289 }
287 lua_rawseti(L, t, ref); 290 lua_rawseti(L, t, ref);
288 return ref; 291 return ref;
@@ -292,9 +295,9 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
292LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { 295LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
293 if (ref >= 0) { 296 if (ref >= 0) {
294 lua_rawgeti(L, t, 0); 297 lua_rawgeti(L, t, 0);
298 lua_rawseti(L, t, ref); /* t[ref] = t[0] */
295 lua_pushnumber(L, ref); 299 lua_pushnumber(L, ref);
296 lua_rawseti(L, t, 0); 300 lua_rawseti(L, t, 0); /* t[0] = ref */
297 lua_rawseti(L, t, ref);
298 } 301 }
299} 302}
300 303
diff --git a/lbaselib.c b/lbaselib.c
index aeb213b9..40567969 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.85 2002/06/25 19:19:33 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.86 2002/06/26 16:37:13 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -257,11 +257,20 @@ static int luaB_assert (lua_State *L) {
257static int luaB_unpack (lua_State *L) { 257static int luaB_unpack (lua_State *L) {
258 int n, i; 258 int n, i;
259 luaL_check_type(L, 1, LUA_TTABLE); 259 luaL_check_type(L, 1, LUA_TTABLE);
260 n = lua_getn(L, 1); 260 lua_pushliteral(L, "n");
261 luaL_check_stack(L, n+LUA_MINSTACK, "table too big to unpack"); 261 lua_rawget(L, 1);
262 for (i=1; i<=n; i++) /* push arg[1...n] */ 262 n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
263 lua_rawgeti(L, 1, i); 263 for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
264 return n; 264 luaL_check_stack(L, 1, "table too big to unpack");
265 lua_rawgeti(L, 1, i+1);
266 if (n == -1) { /* no explicit limit? */
267 if (lua_isnil(L, -1)) { /* stop at first `nil' element */
268 lua_pop(L, 1); /* remove `nil' */
269 break;
270 }
271 }
272 }
273 return i;
265} 274}
266 275
267 276
diff --git a/lua.h b/lua.h
index 3c650278..6cbf9497 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.142 2002/06/20 20:41:46 roberto Exp roberto $ 2** $Id: lua.h,v 1.143 2002/06/25 19:18:49 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -212,7 +212,6 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
212LUA_API int lua_error (lua_State *L); 212LUA_API int lua_error (lua_State *L);
213 213
214LUA_API int lua_next (lua_State *L, int index); 214LUA_API int lua_next (lua_State *L, int index);
215LUA_API int lua_getn (lua_State *L, int index);
216 215
217LUA_API void lua_concat (lua_State *L, int n); 216LUA_API void lua_concat (lua_State *L, int n);
218 217