Browse Source

code parameterized by LUA_FIRSTINDEX (first index of an array)

v5-2
Roberto Ierusalimschy 21 years ago
parent
commit
7e41612eb2
  1. 6
      lauxlib.c
  2. 9
      lbaselib.c
  3. 4
      ldo.c
  4. 17
      ltablib.c
  5. 4
      ltests.c
  6. 6
      luaconf.h
  7. 11
      lvm.c

6
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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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; if ((n = checkint(L, 2)) >= 0) return n;
lua_getfield(L, t, "n"); /* else try t.n */ lua_getfield(L, t, "n"); /* else try t.n */
if ((n = checkint(L, 1)) >= 0) return 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); lua_rawgeti(L, t, n);
if (lua_isnil(L, -1)) break; if (lua_isnil(L, -1)) break;
lua_pop(L, 1); lua_pop(L, 1);
} }
lua_pop(L, 1); lua_pop(L, 1);
return n - 1; return n - LUA_FIRSTINDEX;
} }
/* }====================================================== */ /* }====================================================== */

9
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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -257,7 +257,7 @@ static int luaB_ipairs (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1); /* state, */
lua_pushinteger(L, 0); /* and initial value */ lua_pushinteger(L, LUA_FIRSTINDEX - 1); /* and initial value */
return 3; return 3;
} }
@ -347,11 +347,12 @@ static int luaB_assert (lua_State *L) {
static int luaB_unpack (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 e = luaL_optint(L, 3, -1);
int n; int n;
luaL_checktype(L, 1, LUA_TTABLE); 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 */ n = e - i + 1; /* number of elements */
if (n <= 0) return 0; /* empty range */ if (n <= 0) return 0; /* empty range */
luaL_checkstack(L, n, "table too big to unpack"); luaL_checkstack(L, n, "table too big to unpack");

4
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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** 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 */ actual -= nfixargs; /* number of extra arguments */
htab = luaH_new(L, actual, 1); /* create `arg' table */ htab = luaH_new(L, actual, 1); /* create `arg' table */
for (i=0; i<actual; i++) /* put extra arguments into `arg' table */ for (i=0; i<actual; i++) /* put extra arguments into `arg' table */
setobj2n(L, luaH_setnum(L, htab, i+1), L->top - actual + i); setobj2n(L, luaH_setnum(L, htab, i+LUA_FIRSTINDEX), L->top - actual + i);
/* store counter in field `n' */ /* store counter in field `n' */
setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")),
cast(lua_Number, actual)); cast(lua_Number, actual));

17
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 ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,7 +23,7 @@ static int luaB_foreachi (lua_State *L) {
int i; int i;
int n = aux_getn(L, 1); int n = aux_getn(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION); 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_pushvalue(L, 2); /* function */
lua_pushinteger(L, i); /* 1st argument */ lua_pushinteger(L, i); /* 1st argument */
lua_rawgeti(L, 1, i); /* 2nd argument */ lua_rawgeti(L, 1, i); /* 2nd argument */
@ -109,16 +109,17 @@ static int str_concat (lua_State *L) {
luaL_Buffer b; luaL_Buffer b;
size_t lsep; size_t lsep;
const char *sep = luaL_optlstring(L, 2, "", &lsep); const char *sep = luaL_optlstring(L, 2, "", &lsep);
int i = luaL_optint(L, 3, 1); int i = luaL_optint(L, 3, LUA_FIRSTINDEX);
int n = luaL_optint(L, 4, 0); int last = luaL_optint(L, 4, -2);
luaL_checktype(L, 1, LUA_TTABLE); 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); luaL_buffinit(L, &b);
for (; i <= n; i++) { for (; i <= last; i++) {
lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i);
luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
luaL_addvalue(&b); luaL_addvalue(&b);
if (i != n) if (i != last)
luaL_addlstring(&b, sep, lsep); luaL_addlstring(&b, sep, lsep);
} }
luaL_pushresult(&b); luaL_pushresult(&b);
@ -224,7 +225,7 @@ static int luaB_sort (lua_State *L) {
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
luaL_checktype(L, 2, LUA_TFUNCTION); luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2); /* make sure there is two arguments */ lua_settop(L, 2); /* make sure there is two arguments */
auxsort(L, 1, n); auxsort(L, LUA_FIRSTINDEX, n + LUA_FIRSTINDEX - 1);
return 0; return 0;
} }

4
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 ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -418,7 +418,7 @@ static int listk (lua_State *L) {
lua_createtable(L, p->sizek, 0); lua_createtable(L, p->sizek, 0);
for (i=0; i<p->sizek; i++) { for (i=0; i<p->sizek; i++) {
luaA_pushobject(L, p->k+i); luaA_pushobject(L, p->k+i);
lua_rawseti(L, -2, i+1); lua_rawseti(L, -2, i+LUA_FIRSTINDEX);
} }
return 1; return 1;
} }

6
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 ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -51,6 +51,10 @@
/* buffer size used by lauxlib buffer system */ /* buffer size used by lauxlib buffer system */
#define LUAL_BUFFERSIZE BUFSIZ #define LUAL_BUFFERSIZE BUFSIZ
/* first index for arrays */
#define LUA_FIRSTINDEX 1
/* }====================================================== */ /* }====================================================== */

11
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 ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -694,7 +694,7 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
case OP_SETLIST: case OP_SETLIST:
case OP_SETLISTO: { case OP_SETLISTO: {
int bc = GETARG_Bx(i); int bc = GETARG_Bx(i);
int n; int n, last;
Table *h; Table *h;
runtime_check(L, ttistable(ra)); runtime_check(L, ttistable(ra));
h = hvalue(ra); h = hvalue(ra);
@ -705,11 +705,12 @@ StkId luaV_execute (lua_State *L, int nexeccalls) {
L->top = L->ci->top; L->top = L->ci->top;
} }
bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */ bc &= ~(LFIELDS_PER_FLUSH-1); /* bc = bc - bc%FPF */
if (bc+n > h->sizearray) /* needs more space? */ last = bc + n + LUA_FIRSTINDEX - 1;
luaH_resize(L, h, bc+n, h->lsizenode); /* pre-alloc it at once */ if (last > h->sizearray) /* needs more space? */
luaH_resize(L, h, last, h->lsizenode); /* pre-alloc it at once */
for (; n > 0; n--) { for (; n > 0; n--) {
TValue *val = ra+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); luaC_barrier(L, h, val);
} }
break; break;

Loading…
Cancel
Save