From 7e41612eb28ff0fba282bd419781fcbc9bd94776 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 May 2004 14:50:51 -0300 Subject: [PATCH] code parameterized by LUA_FIRSTINDEX (first index of an array) --- lauxlib.c | 6 +++--- lbaselib.c | 9 +++++---- ldo.c | 4 ++-- ltablib.c | 17 +++++++++-------- ltests.c | 4 ++-- luaconf.h | 6 +++++- lvm.c | 11 ++++++----- 7 files changed, 32 insertions(+), 25 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 9a8fe02b..6ff2940f 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.110 2004/03/23 16:38:43 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.111 2004/04/30 20:13:38 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -308,13 +308,13 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { if ((n = checkint(L, 2)) >= 0) return n; lua_getfield(L, t, "n"); /* else try t.n */ if ((n = checkint(L, 1)) >= 0) return n; - for (n = 1; ; n++) { /* else must count elements */ + for (n = LUA_FIRSTINDEX; ; n++) { /* else must count elements */ lua_rawgeti(L, t, n); if (lua_isnil(L, -1)) break; lua_pop(L, 1); } lua_pop(L, 1); - return n - 1; + return n - LUA_FIRSTINDEX; } /* }====================================================== */ diff --git a/lbaselib.c b/lbaselib.c index 06c38da8..13e6c7b6 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.141 2004/03/26 13:25:17 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.142 2004/04/30 20:13:38 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -257,7 +257,7 @@ static int luaB_ipairs (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ lua_pushvalue(L, 1); /* state, */ - lua_pushinteger(L, 0); /* and initial value */ + lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */ return 3; } @@ -347,11 +347,12 @@ static int luaB_assert (lua_State *L) { static int luaB_unpack (lua_State *L) { - int i = luaL_optint(L, 2, 1); + int i = luaL_optint(L, 2, LUA_FIRSTINDEX); int e = luaL_optint(L, 3, -1); int n; luaL_checktype(L, 1, LUA_TTABLE); - if (e == -1) e = luaL_getn(L, 1); + if (e == -1) + e = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1; n = e - i + 1; /* number of elements */ if (n <= 0) return 0; /* empty range */ luaL_checkstack(L, n, "table too big to unpack"); diff --git a/ldo.c b/ldo.c index e13eec5a..f2682670 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 2.2 2004/03/23 17:02:58 roberto Exp roberto $ +** $Id: ldo.c,v 2.3 2004/04/30 20:13:38 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -193,7 +193,7 @@ static void adjust_varargs (lua_State *L, int nfixargs, StkId base) { actual -= nfixargs; /* number of extra arguments */ htab = luaH_new(L, actual, 1); /* create `arg' table */ for (i=0; itop - actual + i); + setobj2n(L, luaH_setnum(L, htab, i+LUA_FIRSTINDEX), L->top - actual + i); /* store counter in field `n' */ setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast(lua_Number, actual)); diff --git a/ltablib.c b/ltablib.c index 3ad650fe..3e35857e 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.22 2003/10/07 20:13:41 roberto Exp roberto $ +** $Id: ltablib.c,v 1.23 2004/04/30 20:13:38 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -23,7 +23,7 @@ static int luaB_foreachi (lua_State *L) { int i; int n = aux_getn(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); - for (i=1; i<=n; i++) { + for (i=LUA_FIRSTINDEX; i < n+LUA_FIRSTINDEX; i++) { lua_pushvalue(L, 2); /* function */ lua_pushinteger(L, i); /* 1st argument */ lua_rawgeti(L, 1, i); /* 2nd argument */ @@ -109,16 +109,17 @@ static int str_concat (lua_State *L) { luaL_Buffer b; size_t lsep; const char *sep = luaL_optlstring(L, 2, "", &lsep); - int i = luaL_optint(L, 3, 1); - int n = luaL_optint(L, 4, 0); + int i = luaL_optint(L, 3, LUA_FIRSTINDEX); + int last = luaL_optint(L, 4, -2); luaL_checktype(L, 1, LUA_TTABLE); - if (n == 0) n = luaL_getn(L, 1); + if (last == -2) + last = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1; luaL_buffinit(L, &b); - for (; i <= n; i++) { + for (; i <= last; i++) { lua_rawgeti(L, 1, i); luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); luaL_addvalue(&b); - if (i != n) + if (i != last) luaL_addlstring(&b, sep, lsep); } luaL_pushresult(&b); @@ -224,7 +225,7 @@ static int luaB_sort (lua_State *L) { if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ luaL_checktype(L, 2, LUA_TFUNCTION); lua_settop(L, 2); /* make sure there is two arguments */ - auxsort(L, 1, n); + auxsort(L, LUA_FIRSTINDEX, n + LUA_FIRSTINDEX - 1); return 0; } diff --git a/ltests.c b/ltests.c index b69d55ee..1d5cfa6b 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 2.4 2004/03/23 17:07:53 roberto Exp roberto $ +** $Id: ltests.c,v 2.5 2004/04/30 20:13:38 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -418,7 +418,7 @@ static int listk (lua_State *L) { lua_createtable(L, p->sizek, 0); for (i=0; isizek; i++) { luaA_pushobject(L, p->k+i); - lua_rawseti(L, -2, i+1); + lua_rawseti(L, -2, i+LUA_FIRSTINDEX); } return 1; } diff --git a/luaconf.h b/luaconf.h index 190c8620..ebf4d170 100644 --- a/luaconf.h +++ b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.1 2004/05/03 12:28:43 roberto Exp roberto $ +** $Id: luaconf.h,v 1.2 2004/05/10 13:58:26 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -51,6 +51,10 @@ /* buffer size used by lauxlib buffer system */ #define LUAL_BUFFERSIZE BUFSIZ + +/* first index for arrays */ +#define LUA_FIRSTINDEX 1 + /* }====================================================== */ diff --git a/lvm.c b/lvm.c index 4f176ca1..a7a4b3b3 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.3 2004/03/26 14:02:41 roberto Exp roberto $ +** $Id: lvm.c,v 2.4 2004/04/30 20:13:38 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -694,7 +694,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { case OP_SETLIST: case OP_SETLISTO: { int bc = GETARG_Bx(i); - int n; + int n, last; Table *h; runtime_check(L, ttistable(ra)); h = hvalue(ra); @@ -705,11 +705,12 @@ StkId luaV_execute (lua_State *L, int nexeccalls) { L->top = L->ci->top; } bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */ - if (bc+n > h->sizearray) /* needs more space? */ - luaH_resize(L, h, bc+n, h->lsizenode); /* pre-alloc it at once */ + last = bc + n + LUA_FIRSTINDEX - 1; + if (last > h->sizearray) /* needs more space? */ + luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */ for (; n > 0; n--) { TValue *val = ra+n; - setobj2t(L, luaH_setnum(L, h, bc+n), val); + setobj2t(L, luaH_setnum(L, h, last--), val); luaC_barrier(L, h, val); } break;