diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 15:54:18 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-08-06 15:54:18 -0300 |
| commit | 4664f2e9270a956f6175481abe590ba4931b7477 (patch) | |
| tree | b697cf966fada0f46d7779ad620036d9d27e4127 /lapi.c | |
| parent | 2e38c6ae5a53cbf1a607a790460fca45ba3b9ca8 (diff) | |
| download | lua-4664f2e9270a956f6175481abe590ba4931b7477.tar.gz lua-4664f2e9270a956f6175481abe590ba4931b7477.tar.bz2 lua-4664f2e9270a956f6175481abe590ba4931b7477.zip | |
any Lua closure has a table of globals (not only active functions)
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 37 |
1 files changed, 14 insertions, 23 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.207 2002/08/06 15:32:22 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.208 2002/08/06 17:06:56 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 | */ |
| @@ -506,24 +506,11 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
| 506 | } | 506 | } |
| 507 | 507 | ||
| 508 | 508 | ||
| 509 | static LClosure *getfunc (lua_State *L, int level) { | 509 | LUA_API void lua_getglobals (lua_State *L, int index) { |
| 510 | CallInfo *ci; | 510 | StkId o; |
| 511 | TObject *f; | ||
| 512 | if (L->ci - L->base_ci < level) ci = L->base_ci; | ||
| 513 | else ci = L->ci - level; | ||
| 514 | f = ci->base - 1; | ||
| 515 | if (isLfunction(f)) | ||
| 516 | return &clvalue(f)->l; | ||
| 517 | else | ||
| 518 | return NULL; | ||
| 519 | } | ||
| 520 | |||
| 521 | |||
| 522 | LUA_API void lua_getglobals (lua_State *L, int level) { | ||
| 523 | LClosure *f; | ||
| 524 | lua_lock(L); | 511 | lua_lock(L); |
| 525 | f = getfunc(L, level); | 512 | o = luaA_index(L, index); |
| 526 | setobj(L->top, (f ? &f->g : gt(L))); | 513 | setobj(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L)); |
| 527 | api_incr_top(L); | 514 | api_incr_top(L); |
| 528 | lua_unlock(L); | 515 | lua_unlock(L); |
| 529 | } | 516 | } |
| @@ -608,16 +595,20 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { | |||
| 608 | } | 595 | } |
| 609 | 596 | ||
| 610 | 597 | ||
| 611 | LUA_API int lua_setglobals (lua_State *L, int level) { | 598 | LUA_API int lua_setglobals (lua_State *L, int index) { |
| 612 | LClosure *f; | 599 | StkId o; |
| 600 | int res = 0; | ||
| 613 | lua_lock(L); | 601 | lua_lock(L); |
| 614 | api_checknelems(L, 1); | 602 | api_checknelems(L, 1); |
| 615 | f = getfunc(L, level); | 603 | o = luaA_index(L, index); |
| 616 | L->top--; | 604 | L->top--; |
| 617 | api_check(L, ttistable(L->top)); | 605 | api_check(L, ttistable(L->top)); |
| 618 | if (f) f->g = *(L->top); | 606 | if (isLfunction(o)) { |
| 607 | res = 1; | ||
| 608 | clvalue(o)->l.g = *(L->top); | ||
| 609 | } | ||
| 619 | lua_unlock(L); | 610 | lua_unlock(L); |
| 620 | return (f != NULL); | 611 | return res; |
| 621 | } | 612 | } |
| 622 | 613 | ||
| 623 | 614 | ||
