diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-23 11:57:28 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-23 11:57:28 -0300 |
| commit | f04c83e0759b1059545ea0526c5999c35cb1793f (patch) | |
| tree | 97a483571b0ee31c0620a90b3ab9a0da485759a2 /lapi.c | |
| parent | c364e9f97ee825be833d50a04a621d58d71af684 (diff) | |
| download | lua-f04c83e0759b1059545ea0526c5999c35cb1793f.tar.gz lua-f04c83e0759b1059545ea0526c5999c35cb1793f.tar.bz2 lua-f04c83e0759b1059545ea0526c5999c35cb1793f.zip | |
new function "lua_next" (+ new implementation for "next")
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 64 |
1 files changed, 48 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.36 1999/02/12 19:23:02 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.37 1999/02/22 19:13:12 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 | */ |
| @@ -25,7 +25,7 @@ | |||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" | 27 | char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" |
| 28 | "$Autores: " LUA_AUTHORS " $"; | 28 | "$Authors: " LUA_AUTHORS " $"; |
| 29 | 29 | ||
| 30 | 30 | ||
| 31 | 31 | ||
| @@ -70,12 +70,8 @@ void luaA_packresults (void) | |||
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | int luaA_passresults (void) | 73 | int luaA_passresults (void) { |
| 74 | { | 74 | L->Cstack.base = L->Cstack.lua2C; /* position of first result */ |
| 75 | luaD_checkstack(L->Cstack.num); | ||
| 76 | memcpy(L->stack.top, L->Cstack.lua2C+L->stack.stack, | ||
| 77 | L->Cstack.num*sizeof(TObject)); | ||
| 78 | L->stack.top += L->Cstack.num; | ||
| 79 | return L->Cstack.num; | 75 | return L->Cstack.num; |
| 80 | } | 76 | } |
| 81 | 77 | ||
| @@ -87,24 +83,29 @@ static void checkCparams (int nParams) | |||
| 87 | } | 83 | } |
| 88 | 84 | ||
| 89 | 85 | ||
| 90 | static lua_Object put_luaObject (TObject *o) | 86 | static lua_Object put_luaObject (TObject *o) { |
| 91 | { | ||
| 92 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 87 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); |
| 93 | L->stack.stack[L->Cstack.base++] = *o; | 88 | L->stack.stack[L->Cstack.base++] = *o; |
| 94 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 89 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
| 95 | } | 90 | } |
| 96 | 91 | ||
| 97 | 92 | ||
| 98 | static lua_Object put_luaObjectonTop (void) | 93 | static lua_Object put_luaObjectonTop (void) { |
| 99 | { | ||
| 100 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); | 94 | luaD_openstack((L->stack.top-L->stack.stack)-L->Cstack.base); |
| 101 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); | 95 | L->stack.stack[L->Cstack.base++] = *(--L->stack.top); |
| 102 | return L->Cstack.base; /* this is +1 real position (see Ref) */ | 96 | return L->Cstack.base; /* this is +1 real position (see Ref) */ |
| 103 | } | 97 | } |
| 104 | 98 | ||
| 105 | 99 | ||
| 106 | lua_Object lua_pop (void) | 100 | static void top2LC (int n) { |
| 107 | { | 101 | /* Put the 'n' elements on the top as the Lua2C contents */ |
| 102 | L->Cstack.base = (L->stack.top-L->stack.stack); /* new base */ | ||
| 103 | L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */ | ||
| 104 | L->Cstack.num = n; /* number of results */ | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | lua_Object lua_pop (void) { | ||
| 108 | checkCparams(1); | 109 | checkCparams(1); |
| 109 | return put_luaObjectonTop(); | 110 | return put_luaObjectonTop(); |
| 110 | } | 111 | } |
| @@ -436,6 +437,11 @@ TaggedString *luaA_nextvar (TaggedString *g) { | |||
| 436 | } | 437 | } |
| 437 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ | 438 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ |
| 438 | g = (TaggedString *)g->head.next; | 439 | g = (TaggedString *)g->head.next; |
| 440 | if (g) { | ||
| 441 | ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g; | ||
| 442 | incr_top; | ||
| 443 | luaA_pushobject(&g->u.s.globalval); | ||
| 444 | } | ||
| 439 | return g; | 445 | return g; |
| 440 | } | 446 | } |
| 441 | 447 | ||
| @@ -444,11 +450,37 @@ char *lua_nextvar (char *varname) { | |||
| 444 | TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); | 450 | TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); |
| 445 | g = luaA_nextvar(g); | 451 | g = luaA_nextvar(g); |
| 446 | if (g) { | 452 | if (g) { |
| 447 | luaA_pushobject(&g->u.s.globalval); | 453 | top2LC(2); |
| 448 | return g->str; | 454 | return g->str; |
| 449 | } | 455 | } |
| 450 | else | 456 | else { |
| 457 | top2LC(0); | ||
| 451 | return NULL; | 458 | return NULL; |
| 459 | } | ||
| 460 | } | ||
| 461 | |||
| 462 | |||
| 463 | int luaA_next (Hash *t, int i) { | ||
| 464 | Node *n; | ||
| 465 | int tsize = nhash(t); | ||
| 466 | while (i < tsize && ttype(val(n=node(t, i))) == LUA_T_NIL) i++; | ||
| 467 | if (i >= tsize) | ||
| 468 | return 0; | ||
| 469 | else { | ||
| 470 | luaA_pushobject(ref(n)); | ||
| 471 | luaA_pushobject(val(n)); | ||
| 472 | return i+1; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | |||
| 477 | int lua_next (lua_Object o, int i) { | ||
| 478 | TObject *t = Address(o); | ||
| 479 | if (ttype(t) != LUA_T_ARRAY) | ||
| 480 | lua_error("API error: object is not a table in `lua_next'"); | ||
| 481 | i = luaA_next(avalue(t), i); | ||
| 482 | top2LC((i==0) ? 0 : 2); | ||
| 483 | return i; | ||
| 452 | } | 484 | } |
| 453 | 485 | ||
| 454 | 486 | ||
