diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-20 17:41:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-20 17:41:46 -0300 |
| commit | 3941af53adee868e2cccfb9b85783aba9ac311c1 (patch) | |
| tree | 8b6295c8aefacf286c13c7d78b1d2a91421ce2b1 /lapi.c | |
| parent | 5610fdd776edd0de71864f79d50b4526df861d75 (diff) | |
| download | lua-3941af53adee868e2cccfb9b85783aba9ac311c1.tar.gz lua-3941af53adee868e2cccfb9b85783aba9ac311c1.tar.bz2 lua-3941af53adee868e2cccfb9b85783aba9ac311c1.zip | |
first implementation of independent global table per function
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 62 |
1 files changed, 51 insertions, 11 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.199 2002/06/13 13:44:50 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.200 2002/06/18 15:19:27 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 | */ |
| @@ -487,6 +487,29 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
| 487 | } | 487 | } |
| 488 | 488 | ||
| 489 | 489 | ||
| 490 | static LClosure *getfunc (lua_State *L, int level) { | ||
| 491 | CallInfo *ci; | ||
| 492 | TObject *f; | ||
| 493 | if (L->ci - L->base_ci < level) ci = L->base_ci; | ||
| 494 | else ci = L->ci - level; | ||
| 495 | f = ci->base - 1; | ||
| 496 | if (isLfunction(f)) | ||
| 497 | return &clvalue(f)->l; | ||
| 498 | else | ||
| 499 | return NULL; | ||
| 500 | } | ||
| 501 | |||
| 502 | |||
| 503 | LUA_API void lua_getglobals (lua_State *L, int level) { | ||
| 504 | LClosure *f; | ||
| 505 | lua_lock(L); | ||
| 506 | f = getfunc(L, level); | ||
| 507 | setobj(L->top, (f ? &f->g : gt(L))); | ||
| 508 | api_incr_top(L); | ||
| 509 | lua_unlock(L); | ||
| 510 | } | ||
| 511 | |||
| 512 | |||
| 490 | /* | 513 | /* |
| 491 | ** set functions (stack -> Lua) | 514 | ** set functions (stack -> Lua) |
| 492 | */ | 515 | */ |
| @@ -527,27 +550,44 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) { | |||
| 527 | } | 550 | } |
| 528 | 551 | ||
| 529 | 552 | ||
| 530 | LUA_API void lua_setmetatable (lua_State *L, int objindex) { | 553 | LUA_API int lua_setmetatable (lua_State *L, int objindex) { |
| 531 | StkId obj, mt; | 554 | TObject *obj, *mt; |
| 555 | int res = 1; | ||
| 532 | lua_lock(L); | 556 | lua_lock(L); |
| 533 | api_checknelems(L, 1); | 557 | api_checknelems(L, 1); |
| 534 | obj = luaA_index(L, objindex); | 558 | obj = luaA_index(L, objindex); |
| 535 | mt = --L->top; | 559 | mt = (ttype(L->top - 1) != LUA_TNIL) ? L->top - 1 : defaultmeta(L); |
| 536 | if (ttype(mt) == LUA_TNIL) | ||
| 537 | mt = defaultmeta(L); | ||
| 538 | api_check(L, ttype(mt) == LUA_TTABLE); | 560 | api_check(L, ttype(mt) == LUA_TTABLE); |
| 539 | switch (ttype(obj)) { | 561 | switch (ttype(obj)) { |
| 540 | case LUA_TTABLE: | 562 | case LUA_TTABLE: { |
| 541 | hvalue(obj)->metatable = hvalue(mt); | 563 | hvalue(obj)->metatable = hvalue(mt); |
| 542 | break; | 564 | break; |
| 543 | case LUA_TUSERDATA: | 565 | } |
| 566 | case LUA_TUSERDATA: { | ||
| 544 | uvalue(obj)->uv.metatable = hvalue(mt); | 567 | uvalue(obj)->uv.metatable = hvalue(mt); |
| 545 | break; | 568 | break; |
| 546 | default: | 569 | } |
| 547 | luaG_runerror(L, "cannot change the meta table of a %s", | 570 | default: { |
| 548 | luaT_typenames[ttype(obj)]); | 571 | res = 0; /* cannot set */ |
| 572 | break; | ||
| 573 | } | ||
| 549 | } | 574 | } |
| 575 | L->top--; | ||
| 576 | lua_unlock(L); | ||
| 577 | return res; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | LUA_API int lua_setglobals (lua_State *L, int level) { | ||
| 582 | LClosure *f; | ||
| 583 | lua_lock(L); | ||
| 584 | api_checknelems(L, 1); | ||
| 585 | f = getfunc(L, level); | ||
| 586 | L->top--; | ||
| 587 | api_check(L, ttype(L->top) == LUA_TTABLE); | ||
| 588 | if (f) f->g = *(L->top); | ||
| 550 | lua_unlock(L); | 589 | lua_unlock(L); |
| 590 | return (f != NULL); | ||
| 551 | } | 591 | } |
| 552 | 592 | ||
| 553 | 593 | ||
