diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-17 19:12:57 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-10-17 19:12:57 -0200 |
| commit | 1e81da51bab87148981486a84b846399050f4ef2 (patch) | |
| tree | d2c94326ca096e032d1ae3fa75a5d0605f494cc6 | |
| parent | 7cd37142f404462634a5049a840f572e85c5762b (diff) | |
| download | lua-1e81da51bab87148981486a84b846399050f4ef2.tar.gz lua-1e81da51bab87148981486a84b846399050f4ef2.tar.bz2 lua-1e81da51bab87148981486a84b846399050f4ef2.zip | |
new API for registry and C upvalues + new implementation for references
| -rw-r--r-- | lapi.c | 120 | ||||
| -rw-r--r-- | lbaselib.c | 25 | ||||
| -rw-r--r-- | ldblib.c | 16 | ||||
| -rw-r--r-- | ldo.c | 13 | ||||
| -rw-r--r-- | lgc.c | 5 | ||||
| -rw-r--r-- | lstate.c | 7 | ||||
| -rw-r--r-- | lstate.h | 5 | ||||
| -rw-r--r-- | ltests.c | 21 | ||||
| -rw-r--r-- | lua.c | 4 | ||||
| -rw-r--r-- | lua.h | 18 |
10 files changed, 111 insertions, 123 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.152 2001/09/07 17:39:10 roberto Exp $ | 2 | ** $Id: lapi.c,v 1.154 2001/10/11 21:40:56 roberto Exp $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -40,15 +40,28 @@ const l_char lua_ident[] = | |||
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | static TObject *negindex (lua_State *L, int index) { | ||
| 44 | if (index > LUA_REGISTRYINDEX) { | ||
| 45 | api_check(L, index != 0 && -index <= L->top - L->ci->base); | ||
| 46 | return L->top+index; | ||
| 47 | } else if (index == LUA_REGISTRYINDEX) /* pseudo-indices */ | ||
| 48 | return &G(L)->registry; | ||
| 49 | else { | ||
| 50 | TObject *func = (L->ci->base - 1); | ||
| 51 | index = LUA_REGISTRYINDEX - index; | ||
| 52 | api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues); | ||
| 53 | return &clvalue(func)->c.upvalue[index-1]; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 43 | TObject *luaA_index (lua_State *L, int index) { | 58 | TObject *luaA_index (lua_State *L, int index) { |
| 44 | if (index > 0) { | 59 | if (index > 0) { |
| 45 | api_check(L, index <= L->top - L->ci->base); | 60 | api_check(L, index <= L->top - L->ci->base); |
| 46 | return L->ci->base+index-1; | 61 | return L->ci->base+index-1; |
| 47 | } | 62 | } |
| 48 | else { | 63 | else |
| 49 | api_check(L, index != 0 && -index <= L->top - L->ci->base); | 64 | return negindex(L, index); |
| 50 | return L->top+index; | ||
| 51 | } | ||
| 52 | } | 65 | } |
| 53 | 66 | ||
| 54 | 67 | ||
| @@ -59,10 +72,8 @@ static TObject *luaA_indexAcceptable (lua_State *L, int index) { | |||
| 59 | if (o >= L->top) return NULL; | 72 | if (o >= L->top) return NULL; |
| 60 | else return o; | 73 | else return o; |
| 61 | } | 74 | } |
| 62 | else { | 75 | else |
| 63 | api_check(L, index != 0 && -index <= L->top - L->ci->base); | 76 | return negindex(L, index); |
| 64 | return L->top+index; | ||
| 65 | } | ||
| 66 | } | 77 | } |
| 67 | 78 | ||
| 68 | 79 | ||
| @@ -378,24 +389,6 @@ LUA_API void lua_getglobals (lua_State *L) { | |||
| 378 | } | 389 | } |
| 379 | 390 | ||
| 380 | 391 | ||
| 381 | LUA_API int lua_getref (lua_State *L, int ref) { | ||
| 382 | int status; | ||
| 383 | lua_lock(L); | ||
| 384 | if (ref == LUA_REFNIL) { | ||
| 385 | setnilvalue(L->top); | ||
| 386 | status = 1; | ||
| 387 | } | ||
| 388 | else { | ||
| 389 | setobj(L->top, luaH_getnum(G(L)->weakregistry, ref)); | ||
| 390 | status = (ttype(L->top) != LUA_TNIL); | ||
| 391 | } | ||
| 392 | if (status) | ||
| 393 | api_incr_top(L); | ||
| 394 | lua_unlock(L); | ||
| 395 | return status; | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | LUA_API void lua_newtable (lua_State *L) { | 392 | LUA_API void lua_newtable (lua_State *L) { |
| 400 | lua_lock(L); | 393 | lua_lock(L); |
| 401 | sethvalue(L->top, luaH_new(L, 0)); | 394 | sethvalue(L->top, luaH_new(L, 0)); |
| @@ -404,22 +397,6 @@ LUA_API void lua_newtable (lua_State *L) { | |||
| 404 | } | 397 | } |
| 405 | 398 | ||
| 406 | 399 | ||
| 407 | LUA_API void lua_getregistry (lua_State *L) { | ||
| 408 | lua_lock(L); | ||
| 409 | sethvalue(L->top, G(L)->registry); | ||
| 410 | api_incr_top(L); | ||
| 411 | lua_unlock(L); | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | LUA_API void lua_getweakregistry (lua_State *L) { | ||
| 416 | lua_lock(L); | ||
| 417 | sethvalue(L->top, G(L)->weakregistry); | ||
| 418 | api_incr_top(L); | ||
| 419 | lua_unlock(L); | ||
| 420 | } | ||
| 421 | |||
| 422 | |||
| 423 | 400 | ||
| 424 | /* | 401 | /* |
| 425 | ** set functions (stack -> Lua) | 402 | ** set functions (stack -> Lua) |
| @@ -483,26 +460,25 @@ LUA_API void lua_setglobals (lua_State *L) { | |||
| 483 | 460 | ||
| 484 | LUA_API int lua_ref (lua_State *L, int lock) { | 461 | LUA_API int lua_ref (lua_State *L, int lock) { |
| 485 | int ref; | 462 | int ref; |
| 463 | if (lock == 0) lua_error(L, l_s("unlocked references are obsolete")); | ||
| 486 | if (lua_isnil(L, -1)) { | 464 | if (lua_isnil(L, -1)) { |
| 487 | lua_pop(L, 1); | 465 | lua_pop(L, 1); |
| 488 | ref = LUA_REFNIL; | 466 | return LUA_REFNIL; |
| 489 | } | 467 | } |
| 490 | else { | 468 | lua_rawgeti(L, LUA_REGISTRYINDEX, 0); /* get first free element */ |
| 491 | lua_getweakregistry(L); | 469 | ref = lua_tonumber(L, -1); |
| 492 | ref = lua_getn(L, -1) + 1; | 470 | lua_pop(L, 1); /* remove it from stack */ |
| 493 | lua_pushvalue(L, -2); | 471 | if (ref != 0) { /* some free element? */ |
| 494 | lua_rawseti(L, -2, ref); | 472 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); /* remove it from list */ |
| 495 | if (lock) { | 473 | lua_rawseti(L, LUA_REGISTRYINDEX, 0); |
| 496 | lua_getregistry(L); | 474 | } |
| 497 | lua_pushvalue(L, -3); | 475 | else { /* no free elements */ |
| 498 | lua_rawseti(L, -2, ref); | 476 | ref = lua_getn(L, LUA_REGISTRYINDEX) + 1; /* use next `n' */ |
| 499 | lua_pop(L, 1); /* remove registry */ | ||
| 500 | } | ||
| 501 | lua_pushliteral(L, l_s("n")); | 477 | lua_pushliteral(L, l_s("n")); |
| 502 | lua_pushnumber(L, ref); | 478 | lua_pushnumber(L, ref); |
| 503 | lua_settable(L, -3); | 479 | lua_settable(L, LUA_REGISTRYINDEX); /* n = n+1 */ |
| 504 | lua_pop(L, 2); | ||
| 505 | } | 480 | } |
| 481 | lua_rawseti(L, LUA_REGISTRYINDEX, ref); | ||
| 506 | return ref; | 482 | return ref; |
| 507 | } | 483 | } |
| 508 | 484 | ||
| @@ -661,13 +637,10 @@ LUA_API void lua_error (lua_State *L, const l_char *s) { | |||
| 661 | 637 | ||
| 662 | LUA_API void lua_unref (lua_State *L, int ref) { | 638 | LUA_API void lua_unref (lua_State *L, int ref) { |
| 663 | if (ref >= 0) { | 639 | if (ref >= 0) { |
| 664 | lua_getregistry(L); | 640 | lua_rawgeti(L, LUA_REGISTRYINDEX, 0); |
| 665 | lua_pushnil(L); | 641 | lua_pushnumber(L, ref); |
| 666 | lua_rawseti(L, -2, ref); | 642 | lua_rawseti(L, LUA_REGISTRYINDEX, 0); |
| 667 | lua_getweakregistry(L); | 643 | lua_rawseti(L, LUA_REGISTRYINDEX, ref); |
| 668 | lua_pushnil(L); | ||
| 669 | lua_rawseti(L, -2, ref); | ||
| 670 | lua_pop(L, 2); /* remove both registries */ | ||
| 671 | } | 644 | } |
| 672 | } | 645 | } |
| 673 | 646 | ||
| @@ -787,3 +760,22 @@ LUA_API void lua_setweakmode (lua_State *L, int mode) { | |||
| 787 | lua_unlock(L); | 760 | lua_unlock(L); |
| 788 | } | 761 | } |
| 789 | 762 | ||
| 763 | |||
| 764 | |||
| 765 | LUA_API void lua_pushupvalues (lua_State *L) { | ||
| 766 | TObject *func; | ||
| 767 | int n, i; | ||
| 768 | lua_lock(L); | ||
| 769 | func = (L->ci->base - 1); | ||
| 770 | api_check(L, iscfunction(func)); | ||
| 771 | n = clvalue(func)->c.nupvalues; | ||
| 772 | if (LUA_MINSTACK+n > lua_stackspace(L)) | ||
| 773 | luaD_error(L, l_s("stack overflow")); | ||
| 774 | for (i=0; i<n; i++) { | ||
| 775 | setobj(L->top, &clvalue(func)->c.upvalue[i]); | ||
| 776 | L->top++; | ||
| 777 | } | ||
| 778 | lua_unlock(L); | ||
| 779 | } | ||
| 780 | |||
| 781 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.41 2001/08/31 19:46:07 roberto Exp $ | 2 | ** $Id: lbaselib.c,v 1.43 2001/10/11 21:41:21 roberto Exp $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -363,18 +363,8 @@ static int luaB_require (lua_State *L) { | |||
| 363 | lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */ | 363 | lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */ |
| 364 | lua_setglobal(L, LUA_PATH); | 364 | lua_setglobal(L, LUA_PATH); |
| 365 | } | 365 | } |
| 366 | lua_getregistry(L); | 366 | lua_pushvalue(L, 1); /* check package's name in book-keeping table */ |
| 367 | lua_pushliteral(L, LUA_PATH); | 367 | lua_gettable(L, lua_upvalueindex(1)); |
| 368 | lua_gettable(L, 3); /* get book-keeping table */ | ||
| 369 | if (lua_isnil(L, 4)) { /* no book-keeping table? */ | ||
| 370 | lua_pop(L, 1); /* pop the `nil' */ | ||
| 371 | lua_newtable(L); /* create book-keeping table */ | ||
| 372 | lua_pushliteral(L, LUA_PATH); | ||
| 373 | lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */ | ||
| 374 | lua_settable(L, 3); /* store book-keeping table in registry */ | ||
| 375 | } | ||
| 376 | lua_pushvalue(L, 1); | ||
| 377 | lua_gettable(L, 4); /* check package's name in book-keeping table */ | ||
| 378 | if (!lua_isnil(L, -1)) /* is it there? */ | 368 | if (!lua_isnil(L, -1)) /* is it there? */ |
| 379 | return 0; /* package is already loaded */ | 369 | return 0; /* package is already loaded */ |
| 380 | else { /* must load it */ | 370 | else { /* must load it */ |
| @@ -385,7 +375,7 @@ static int luaB_require (lua_State *L) { | |||
| 385 | lua_pushvalue(L, 1); /* package name */ | 375 | lua_pushvalue(L, 1); /* package name */ |
| 386 | lua_concat(L, 2); /* concat directory with package name */ | 376 | lua_concat(L, 2); /* concat directory with package name */ |
| 387 | res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */ | 377 | res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */ |
| 388 | lua_settop(L, 4); /* pop string and eventual results from dofile */ | 378 | lua_settop(L, 2); /* pop string and eventual results from dofile */ |
| 389 | if (res == 0) break; /* ok; file done */ | 379 | if (res == 0) break; /* ok; file done */ |
| 390 | else if (res != LUA_ERRFILE) | 380 | else if (res != LUA_ERRFILE) |
| 391 | lua_error(L, NULL); /* error running package; propagate it */ | 381 | lua_error(L, NULL); /* error running package; propagate it */ |
| @@ -397,7 +387,7 @@ static int luaB_require (lua_State *L) { | |||
| 397 | } | 387 | } |
| 398 | lua_pushvalue(L, 1); | 388 | lua_pushvalue(L, 1); |
| 399 | lua_pushnumber(L, 1); | 389 | lua_pushnumber(L, 1); |
| 400 | lua_settable(L, 4); /* mark it as loaded */ | 390 | lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */ |
| 401 | return 0; | 391 | return 0; |
| 402 | } | 392 | } |
| 403 | 393 | ||
| @@ -713,7 +703,6 @@ static const luaL_reg base_funcs[] = { | |||
| 713 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ | 703 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ |
| 714 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ | 704 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ |
| 715 | {l_s("rawtype"), luaB_rawtype}, | 705 | {l_s("rawtype"), luaB_rawtype}, |
| 716 | {l_s("require"), luaB_require}, | ||
| 717 | {l_s("setglobal"), luaB_setglobal}, | 706 | {l_s("setglobal"), luaB_setglobal}, |
| 718 | {l_s("settag"), luaB_settype}, /* for compatibility 4.0 */ | 707 | {l_s("settag"), luaB_settype}, /* for compatibility 4.0 */ |
| 719 | {l_s("settype"), luaB_settype}, | 708 | {l_s("settype"), luaB_settype}, |
| @@ -737,6 +726,10 @@ LUALIB_API int lua_baselibopen (lua_State *L) { | |||
| 737 | luaL_openl(L, base_funcs); | 726 | luaL_openl(L, base_funcs); |
| 738 | lua_pushliteral(L, l_s(LUA_VERSION)); | 727 | lua_pushliteral(L, l_s(LUA_VERSION)); |
| 739 | lua_setglobal(L, l_s("_VERSION")); | 728 | lua_setglobal(L, l_s("_VERSION")); |
| 729 | /* `require' needs an empty table as upvalue */ | ||
| 730 | lua_newtable(L); | ||
| 731 | lua_pushcclosure(L, luaB_require, 1); | ||
| 732 | lua_setglobal(L, l_s("require")); | ||
| 740 | return 0; | 733 | return 0; |
| 741 | } | 734 | } |
| 742 | 735 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldblib.c,v 1.37 2001/06/06 18:00:19 roberto Exp $ | 2 | ** $Id: ldblib.c,v 1.38 2001/08/31 19:46:07 roberto Exp $ |
| 3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -116,16 +116,14 @@ static int setlocal (lua_State *L) { | |||
| 116 | 116 | ||
| 117 | 117 | ||
| 118 | static void hookf (lua_State *L, const l_char *key) { | 118 | static void hookf (lua_State *L, const l_char *key) { |
| 119 | lua_getregistry(L); | ||
| 120 | lua_pushstring(L, key); | 119 | lua_pushstring(L, key); |
| 121 | lua_gettable(L, -2); | 120 | lua_gettable(L, LUA_REGISTRYINDEX); |
| 122 | if (lua_isfunction(L, -1)) { | 121 | if (lua_isfunction(L, -1)) { |
| 123 | lua_pushvalue(L, -3); /* original argument (below table and function) */ | 122 | lua_pushvalue(L, -2); /* original argument (below function) */ |
| 124 | lua_rawcall(L, 1, 0); | 123 | lua_rawcall(L, 1, 0); |
| 125 | } | 124 | } |
| 126 | else | 125 | else |
| 127 | lua_pop(L, 1); /* pop result from gettable */ | 126 | lua_pop(L, 1); /* pop result from gettable */ |
| 128 | lua_pop(L, 1); /* pop table */ | ||
| 129 | } | 127 | } |
| 130 | 128 | ||
| 131 | 129 | ||
| @@ -150,13 +148,11 @@ static void sethook (lua_State *L, const l_char *key, lua_Hook hook, | |||
| 150 | (*sethookf)(L, hook); | 148 | (*sethookf)(L, hook); |
| 151 | else | 149 | else |
| 152 | luaL_argerror(L, 1, l_s("function expected")); | 150 | luaL_argerror(L, 1, l_s("function expected")); |
| 153 | lua_getregistry(L); | ||
| 154 | lua_pushstring(L, key); | 151 | lua_pushstring(L, key); |
| 155 | lua_pushvalue(L, -1); /* dup key */ | 152 | lua_gettable(L, LUA_REGISTRYINDEX); /* get old value */ |
| 156 | lua_gettable(L, -3); /* get old value */ | 153 | lua_pushstring(L, key); |
| 157 | lua_pushvalue(L, -2); /* key (again) */ | ||
| 158 | lua_pushvalue(L, 1); | 154 | lua_pushvalue(L, 1); |
| 159 | lua_settable(L, -5); /* set new value */ | 155 | lua_settable(L, LUA_REGISTRYINDEX); /* set new value */ |
| 160 | } | 156 | } |
| 161 | 157 | ||
| 162 | 158 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.141 2001/09/25 17:05:49 roberto Exp $ | 2 | ** $Id: ldo.c,v 1.142 2001/10/02 16:45:03 roberto Exp $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -44,7 +44,8 @@ void luaD_init (lua_State *L, int stacksize) { | |||
| 44 | stacksize += EXTRA_STACK; | 44 | stacksize += EXTRA_STACK; |
| 45 | L->stack = luaM_newvector(L, stacksize, TObject); | 45 | L->stack = luaM_newvector(L, stacksize, TObject); |
| 46 | L->stacksize = stacksize; | 46 | L->stacksize = stacksize; |
| 47 | L->basefunc.base = L->top = L->stack; | 47 | setnilvalue(L->stack); /* the `initial' function */ |
| 48 | L->top = L->basefunc.base = L->stack + 1; | ||
| 48 | restore_stack_limit(L); | 49 | restore_stack_limit(L); |
| 49 | } | 50 | } |
| 50 | 51 | ||
| @@ -119,12 +120,12 @@ static void luaD_callHook (lua_State *L, lua_Hook callhook, | |||
| 119 | 120 | ||
| 120 | 121 | ||
| 121 | static StkId callCclosure (lua_State *L, const struct CClosure *cl) { | 122 | static StkId callCclosure (lua_State *L, const struct CClosure *cl) { |
| 122 | int nup = cl->nupvalues; /* number of upvalues */ | ||
| 123 | int n; | 123 | int n; |
| 124 | luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ | 124 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
| 125 | for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ | ||
| 126 | setobj(L->top++, &cl->upvalue[n]); | ||
| 127 | lua_unlock(L); | 125 | lua_unlock(L); |
| 126 | #if LUA_COMPATUPVALUES | ||
| 127 | lua_pushupvalues(L); | ||
| 128 | #endif | ||
| 128 | n = (*cl->f)(L); /* do the actual call */ | 129 | n = (*cl->f)(L); /* do the actual call */ |
| 129 | lua_lock(L); | 130 | lua_lock(L); |
| 130 | return L->top - n; /* return index of first result */ | 131 | return L->top - n; /* return index of first result */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lgc.c,v 1.111 2001/09/07 17:39:10 roberto Exp $ | 2 | ** $Id: lgc.c,v 1.112 2001/10/02 16:45:03 roberto Exp $ |
| 3 | ** Garbage Collector | 3 | ** Garbage Collector |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -171,8 +171,7 @@ static void markall (lua_State *L) { | |||
| 171 | marktagmethods(G(L), &st); /* mark tag methods */ | 171 | marktagmethods(G(L), &st); /* mark tag methods */ |
| 172 | markstacks(L, &st); /* mark all stacks */ | 172 | markstacks(L, &st); /* mark all stacks */ |
| 173 | marktable(&st, G(L)->type2tag); | 173 | marktable(&st, G(L)->type2tag); |
| 174 | marktable(&st, G(L)->registry); | 174 | markobject(&st, &G(L)->registry); |
| 175 | marktable(&st, G(L)->weakregistry); | ||
| 176 | while (st.tmark) { /* mark tables */ | 175 | while (st.tmark) { /* mark tables */ |
| 177 | Hash *h = st.tmark; /* get first table from list */ | 176 | Hash *h = st.tmark; /* get first table from list */ |
| 178 | st.tmark = h->mark; /* remove it from list */ | 177 | st.tmark = h->mark; /* remove it from list */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 1.66 2001/07/17 17:54:46 roberto Exp $ | 2 | ** $Id: lstate.c,v 1.68 2001/09/07 17:39:10 roberto Exp $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -66,10 +66,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 66 | luaD_init(L, so->stacksize); /* init stack */ | 66 | luaD_init(L, so->stacksize); /* init stack */ |
| 67 | L->gt = luaH_new(L, 10); /* table of globals */ | 67 | L->gt = luaH_new(L, 10); /* table of globals */ |
| 68 | G(L)->type2tag = luaH_new(L, 10); | 68 | G(L)->type2tag = luaH_new(L, 10); |
| 69 | G(L)->registry = luaH_new(L, 0); | 69 | sethvalue(&G(L)->registry, luaH_new(L, 0)); |
| 70 | G(L)->weakregistry = luaH_new(L, 0); | ||
| 71 | /* make weakregistry weak */ | ||
| 72 | G(L)->weakregistry->weakmode = LUA_WEAK_KEY | LUA_WEAK_VALUE; | ||
| 73 | luaS_resize(L, MINPOWER2); | 70 | luaS_resize(L, MINPOWER2); |
| 74 | luaX_init(L); | 71 | luaX_init(L); |
| 75 | luaT_init(L); | 72 | luaT_init(L); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.h,v 1.59 2001/09/07 17:39:10 roberto Exp $ | 2 | ** $Id: lstate.h,v 1.60 2001/10/02 16:43:29 roberto Exp $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -58,8 +58,7 @@ typedef struct global_State { | |||
| 58 | size_t Mbuffsize; /* size of Mbuffer */ | 58 | size_t Mbuffsize; /* size of Mbuffer */ |
| 59 | stringtable strt; /* hash table for strings */ | 59 | stringtable strt; /* hash table for strings */ |
| 60 | Hash *type2tag; /* hash table from type names to tags */ | 60 | Hash *type2tag; /* hash table from type names to tags */ |
| 61 | Hash *registry; /* (strong) registry table */ | 61 | TObject registry; /* registry table */ |
| 62 | Hash *weakregistry; /* weakregistry table */ | ||
| 63 | struct TM *TMtable; /* table for tag methods */ | 62 | struct TM *TMtable; /* table for tag methods */ |
| 64 | int sizeTM; /* size of TMtable */ | 63 | int sizeTM; /* size of TMtable */ |
| 65 | int ntag; /* number of tags in TMtable */ | 64 | int ntag; /* number of tags in TMtable */ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltests.c,v 1.91 2001/09/07 17:39:10 roberto Exp $ | 2 | ** $Id: ltests.c,v 1.92 2001/10/02 16:45:03 roberto Exp $ |
| 3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -317,21 +317,16 @@ static int tref (lua_State *L) { | |||
| 317 | int level = lua_gettop(L); | 317 | int level = lua_gettop(L); |
| 318 | luaL_checkany(L, 1); | 318 | luaL_checkany(L, 1); |
| 319 | lua_pushvalue(L, 1); | 319 | lua_pushvalue(L, 1); |
| 320 | lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1))); | 320 | lua_pushnumber(L, lua_ref(L, 1)); |
| 321 | assert(lua_gettop(L) == level+1); /* +1 for result */ | 321 | assert(lua_gettop(L) == level+1); /* +1 for result */ |
| 322 | return 1; | 322 | return 1; |
| 323 | } | 323 | } |
| 324 | 324 | ||
| 325 | static int getref (lua_State *L) { | 325 | static int getref (lua_State *L) { |
| 326 | int level = lua_gettop(L); | 326 | int level = lua_gettop(L); |
| 327 | if (lua_getref(L, luaL_check_int(L, 1))) { | 327 | lua_getref(L, luaL_check_int(L, 1)); |
| 328 | assert(lua_gettop(L) == level+1); | 328 | assert(lua_gettop(L) == level+1); |
| 329 | return 1; | 329 | return 1; |
| 330 | } | ||
| 331 | else { | ||
| 332 | assert(lua_gettop(L) == level); | ||
| 333 | return 0; | ||
| 334 | } | ||
| 335 | } | 330 | } |
| 336 | 331 | ||
| 337 | static int unref (lua_State *L) { | 332 | static int unref (lua_State *L) { |
| @@ -570,6 +565,12 @@ static int testC (lua_State *L) { | |||
| 570 | else if EQ(l_s("pushvalue")) { | 565 | else if EQ(l_s("pushvalue")) { |
| 571 | lua_pushvalue(L, getnum); | 566 | lua_pushvalue(L, getnum); |
| 572 | } | 567 | } |
| 568 | else if EQ(l_s("pushcclosure")) { | ||
| 569 | lua_pushcclosure(L, testC, getnum); | ||
| 570 | } | ||
| 571 | else if EQ(l_s("pushupvalues")) { | ||
| 572 | lua_pushupvalues(L); | ||
| 573 | } | ||
| 573 | else if EQ(l_s("remove")) { | 574 | else if EQ(l_s("remove")) { |
| 574 | lua_remove(L, getnum); | 575 | lua_remove(L, getnum); |
| 575 | } | 576 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.c,v 1.69 2001/08/30 20:54:02 roberto Exp $ | 2 | ** $Id: lua.c,v 1.70 2001/09/25 17:06:34 roberto Exp $ |
| 3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -141,7 +141,7 @@ static void getargs (l_char *argv[]) { | |||
| 141 | 141 | ||
| 142 | 142 | ||
| 143 | static int l_getargs (lua_State *l) { | 143 | static int l_getargs (lua_State *l) { |
| 144 | l_char **argv = (l_char **)lua_touserdata(l, -1); | 144 | l_char **argv = (l_char **)lua_touserdata(l, lua_upvalueindex(1)); |
| 145 | getargs(argv); | 145 | getargs(argv); |
| 146 | return 1; | 146 | return 1; |
| 147 | } | 147 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lua.h,v 1.103 2001/08/31 19:46:07 roberto Exp $ | 2 | ** $Id: lua.h,v 1.104 2001/10/11 21:41:21 roberto Exp $ |
| 3 | ** Lua - An Extensible Extension Language | 3 | ** Lua - An Extensible Extension Language |
| 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil | 4 | ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil |
| 5 | ** e-mail: info@lua.org | 5 | ** e-mail: info@lua.org |
| @@ -35,6 +35,13 @@ | |||
| 35 | #define LUA_MULTRET (-1) | 35 | #define LUA_MULTRET (-1) |
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | /* pseudo-index for registry */ | ||
| 39 | #define LUA_REGISTRYINDEX (-10000) | ||
| 40 | |||
| 41 | /* pseudo-indices for upvalues */ | ||
| 42 | #define lua_upvalueindex(i) (LUA_REGISTRYINDEX-(i)) | ||
| 43 | |||
| 44 | |||
| 38 | /* error codes for `lua_do*' and the like */ | 45 | /* error codes for `lua_do*' and the like */ |
| 39 | #define LUA_ERRRUN 1 | 46 | #define LUA_ERRRUN 1 |
| 40 | #define LUA_ERRFILE 2 | 47 | #define LUA_ERRFILE 2 |
| @@ -160,9 +167,7 @@ LUA_API void lua_rawget (lua_State *L, int index); | |||
| 160 | LUA_API void lua_rawgeti (lua_State *L, int index, int n); | 167 | LUA_API void lua_rawgeti (lua_State *L, int index, int n); |
| 161 | LUA_API void lua_getglobals (lua_State *L); | 168 | LUA_API void lua_getglobals (lua_State *L); |
| 162 | LUA_API void lua_gettagmethod (lua_State *L, int tag, const lua_char *event); | 169 | LUA_API void lua_gettagmethod (lua_State *L, int tag, const lua_char *event); |
| 163 | LUA_API int lua_getref (lua_State *L, int ref); | ||
| 164 | LUA_API void lua_newtable (lua_State *L); | 170 | LUA_API void lua_newtable (lua_State *L); |
| 165 | LUA_API void lua_getregistry (lua_State *L); | ||
| 166 | LUA_API void lua_getweakregistry (lua_State *L); | 171 | LUA_API void lua_getweakregistry (lua_State *L); |
| 167 | 172 | ||
| 168 | 173 | ||
| @@ -246,14 +251,19 @@ LUA_API int lua_getweakmode (lua_State *L, int index); | |||
| 246 | #define lua_pushliteral(L, s) lua_pushlstring(L, s, \ | 251 | #define lua_pushliteral(L, s) lua_pushlstring(L, s, \ |
| 247 | (sizeof(s)/sizeof(lua_char))-1) | 252 | (sizeof(s)/sizeof(lua_char))-1) |
| 248 | 253 | ||
| 254 | #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); | ||
| 255 | |||
| 256 | #define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) | ||
| 249 | 257 | ||
| 250 | 258 | ||
| 251 | /* | 259 | /* |
| 252 | ** compatibility macros | 260 | ** compatibility macros and functions |
| 253 | */ | 261 | */ |
| 254 | #define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE) | 262 | #define lua_newtag(L) lua_newtype(L, NULL, LUA_TNONE) |
| 255 | #define lua_typename lua_tag2name | 263 | #define lua_typename lua_tag2name |
| 256 | 264 | ||
| 265 | LUA_API void lua_pushupvalues (lua_State *L); | ||
| 266 | |||
| 257 | #endif | 267 | #endif |
| 258 | 268 | ||
| 259 | 269 | ||
