new generic `for'

This commit is contained in:
Roberto Ierusalimschy 2002-02-14 19:46:43 -02:00
parent 57fb51f975
commit 1e602a61b3

25
lvm.c
View File

@ -56,7 +56,7 @@ int luaV_tostring (lua_State *L, TObject *obj) {
return 0; return 0;
else { else {
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
lua_number2str(s, nvalue(obj)); /* convert `s' to number */ lua_number2str(s, nvalue(obj));
setsvalue(obj, luaS_new(L, s)); setsvalue(obj, luaS_new(L, s));
return 1; return 1;
} }
@ -559,20 +559,23 @@ StkId luaV_execute (lua_State *L) {
break; break;
} }
case OP_TFORLOOP: { case OP_TFORLOOP: {
Table *t;
int n;
int j = GETARG_sBc(i); int j = GETARG_sBc(i);
int loop = 0;
pc += j; /* jump back before tests (for error messages) */ pc += j; /* jump back before tests (for error messages) */
if (ttype(ra) != LUA_TTABLE) if (ttype(ra) == LUA_TTABLE) {
luaD_error(L, "`for' table must be a table"); Table *t = hvalue(ra);
runtime_check(L, ttype(ra+1) == LUA_TNUMBER); loop = luaH_next(L, t, ra+1);
t = hvalue(ra); }
n = cast(int, nvalue(ra+1)); else if (ttype(ra) == LUA_TFUNCTION) {
n = luaH_nexti(t, n, ra+2); setobj(ra+1, ra);
if (n != -1) { /* repeat loop? */ L->top = ra+2; /* no arguments */
setnvalue(ra+1, n); /* index */ luaD_call(L, ra+1, 2);
L->top = L->ci->top;
loop = (ttype(ra+1) != LUA_TNIL);
} }
else else
luaD_error(L, "`for' value must be a table or function");
if (!loop)
pc -= j; /* undo jump */ pc -= j; /* undo jump */
break; break;
} }