no more `lua_getn' function

This commit is contained in:
Roberto Ierusalimschy 2002-06-26 16:28:44 -03:00
parent cfcf200806
commit f67ccfbdeb
4 changed files with 27 additions and 53 deletions

39
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.202 2002/06/24 13:08:45 roberto Exp roberto $
** $Id: lapi.c,v 1.203 2002/06/25 19:15:41 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -697,43 +697,6 @@ LUA_API int lua_next (lua_State *L, int index) {
}
LUA_API int lua_getn (lua_State *L, int index) {
StkId t;
const TObject *value;
int n;
lua_lock(L);
t = luaA_index(L, index);
api_check(L, ttype(t) == LUA_TTABLE);
value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
if (ttype(value) == LUA_TNUMBER)
lua_number2int(n, nvalue(value));
else {
Node *nd;
Table *a = hvalue(t);
lua_Number max = 0;
int i;
i = sizearray(a);
while (i--) {
if (ttype(&a->array[i]) != LUA_TNIL)
break;
}
max = i+1;
i = sizenode(a);
nd = a->node;
while (i--) {
if (ttype(key(nd)) == LUA_TNUMBER &&
ttype(val(nd)) != LUA_TNIL &&
nvalue(key(nd)) > max)
max = nvalue(key(nd));
nd++;
}
lua_number2int(n, max);
}
lua_unlock(L);
return n;
}
LUA_API void lua_concat (lua_State *L, int n) {
lua_lock(L);
api_checknelems(L, n);

View File

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.75 2002/06/18 15:19:27 roberto Exp roberto $
** $Id: lauxlib.c,v 1.76 2002/06/25 19:15:21 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -272,17 +272,20 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
LUALIB_API int luaL_ref (lua_State *L, int t) {
int ref;
lua_rawgeti(L, t, 0); /* get first free element */
ref = (int)lua_tonumber(L, -1);
ref = (int)lua_tonumber(L, -1); /* ref = t[0] */
lua_pop(L, 1); /* remove it from stack */
if (ref != 0) { /* any free element? */
lua_rawgeti(L, t, ref); /* remove it from list */
lua_rawseti(L, t, 0);
lua_rawseti(L, t, 0); /* (that is, t[0] = t[ref]) */
}
else { /* no free elements */
ref = lua_getn(L, t) + 1; /* use next `n' */
lua_pushliteral(L, "n");
lua_pushvalue(L, -1);
lua_rawget(L, t); /* get t.n */
ref = (int)lua_tonumber(L, -1) + 1; /* ref = t.n + 1 */
lua_pop(L, 1); /* pop t.n */
lua_pushnumber(L, ref);
lua_rawset(L, t); /* n = n+1 */
lua_rawset(L, t); /* t.n = t.n + 1 */
}
lua_rawseti(L, t, ref);
return ref;
@ -292,9 +295,9 @@ 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) {
lua_rawgeti(L, t, 0);
lua_rawseti(L, t, ref); /* t[ref] = t[0] */
lua_pushnumber(L, ref);
lua_rawseti(L, t, 0);
lua_rawseti(L, t, ref);
lua_rawseti(L, t, 0); /* t[0] = ref */
}
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.85 2002/06/25 19:19:33 roberto Exp roberto $
** $Id: lbaselib.c,v 1.86 2002/06/26 16:37:13 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@ -257,11 +257,20 @@ static int luaB_assert (lua_State *L) {
static int luaB_unpack (lua_State *L) {
int n, i;
luaL_check_type(L, 1, LUA_TTABLE);
n = lua_getn(L, 1);
luaL_check_stack(L, n+LUA_MINSTACK, "table too big to unpack");
for (i=1; i<=n; i++) /* push arg[1...n] */
lua_rawgeti(L, 1, i);
return n;
lua_pushliteral(L, "n");
lua_rawget(L, 1);
n = (lua_isnumber(L, -1)) ? (int)lua_tonumber(L, -1) : -1;
for (i=0; i<n || n==-1; i++) { /* push arg[1...n] */
luaL_check_stack(L, 1, "table too big to unpack");
lua_rawgeti(L, 1, i+1);
if (n == -1) { /* no explicit limit? */
if (lua_isnil(L, -1)) { /* stop at first `nil' element */
lua_pop(L, 1); /* remove `nil' */
break;
}
}
}
return i;
}

3
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.142 2002/06/20 20:41:46 roberto Exp roberto $
** $Id: lua.h,v 1.143 2002/06/25 19:18:49 roberto Exp roberto $
** Lua - An Extensible Extension Language
** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
** http://www.lua.org mailto:info@lua.org
@ -212,7 +212,6 @@ LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold);
LUA_API int lua_error (lua_State *L);
LUA_API int lua_next (lua_State *L, int index);
LUA_API int lua_getn (lua_State *L, int index);
LUA_API void lua_concat (lua_State *L, int n);