diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-31 11:08:27 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-31 11:08:27 -0300 |
| commit | 100bfec39a3de3029a97e645e7fe33877d7bbc2f (patch) | |
| tree | 01c2c9bb63f71d3fb186b8f5be0b7577978f8a19 /lapi.c | |
| parent | a290b84c670bbf0770d76429e7282c555a80058e (diff) | |
| download | lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.tar.gz lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.tar.bz2 lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.zip | |
new implementation for `next'
Diffstat (limited to '')
| -rw-r--r-- | lapi.c | 35 |
1 files changed, 16 insertions, 19 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.89 2000/08/29 14:52:27 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.90 2000/08/29 20:43:28 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 | */ |
| @@ -327,7 +327,7 @@ int lua_ref (lua_State *L, int lock) { | |||
| 327 | 327 | ||
| 328 | 328 | ||
| 329 | /* | 329 | /* |
| 330 | ** miscelaneous functions | 330 | ** miscellaneous functions |
| 331 | */ | 331 | */ |
| 332 | 332 | ||
| 333 | 333 | ||
| @@ -359,24 +359,21 @@ void lua_unref (lua_State *L, int ref) { | |||
| 359 | } | 359 | } |
| 360 | 360 | ||
| 361 | 361 | ||
| 362 | int luaA_next (lua_State *L, const Hash *t, int i) { | 362 | int lua_next (lua_State *L) { |
| 363 | int tsize = t->size; | 363 | const TObject *t = Index(L, -2); |
| 364 | for (; i<tsize; i++) { | 364 | Node *n; |
| 365 | Node *n = node(t, i); | ||
| 366 | if (ttype(val(n)) != TAG_NIL) { | ||
| 367 | luaA_pushobject(L, key(n)); | ||
| 368 | luaA_pushobject(L, val(n)); | ||
| 369 | return i+1; /* index to be used next time */ | ||
| 370 | } | ||
| 371 | } | ||
| 372 | return 0; /* no more elements */ | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | int lua_next (lua_State *L, int index, int i) { | ||
| 377 | const TObject *t = Index(L, index); | ||
| 378 | if (ttype(t) != TAG_TABLE) | 365 | if (ttype(t) != TAG_TABLE) |
| 379 | lua_error(L, "Lua API error - object is not a table in `lua_next'"); | 366 | lua_error(L, "Lua API error - object is not a table in `lua_next'"); |
| 380 | return luaA_next(L, hvalue(t), i); | 367 | n = luaH_next(L, hvalue(t), Index(L, -1)); |
| 368 | if (n) { | ||
| 369 | *(L->top-1) = *key(n); | ||
| 370 | *L->top = *val(n); | ||
| 371 | api_incr_top(L); | ||
| 372 | return 1; | ||
| 373 | } | ||
| 374 | else { /* no more elements */ | ||
| 375 | L->top -= 2; /* remove key and table */ | ||
| 376 | return 0; | ||
| 377 | } | ||
| 381 | } | 378 | } |
| 382 | 379 | ||
