diff --git a/lapi.c b/lapi.c index bcda55c3..c1880663 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.217 2002/11/07 15:37:10 roberto Exp roberto $ +** $Id: lapi.c,v 1.218 2002/11/07 15:39:23 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -505,20 +505,6 @@ LUA_API void lua_newtable (lua_State *L) { } -LUA_API const char *lua_getmode (lua_State *L, int index) { - static const char *const modes[] = {"", "k", "v", "kv"}; - int mode = 0; - TObject *t; - lua_lock(L); - t = luaA_index(L, index); - api_check(L, ttistable(t)); - if (hvalue(t)->mode & WEAKKEY) mode += 1; - if (hvalue(t)->mode & WEAKVALUE) mode += 2; - lua_unlock(L); - return modes[mode]; -} - - LUA_API int lua_getmetatable (lua_State *L, int objindex) { StkId obj; Table *mt; @@ -597,17 +583,6 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) { } -LUA_API void lua_setmode (lua_State *L, int index, const char *mode) { - TObject *t; - lua_lock(L); - t = luaA_index(L, index); - api_check(L, ttistable(t)); - hvalue(t)->mode &= ~(WEAKKEY | WEAKVALUE); /* clear bits */ - if (strchr(mode, 'k')) hvalue(t)->mode |= WEAKKEY; - if (strchr(mode, 'v')) hvalue(t)->mode |= WEAKVALUE; - lua_unlock(L); -} - LUA_API int lua_setmetatable (lua_State *L, int objindex) { TObject *obj, *mt; int res = 1; diff --git a/lbaselib.c b/lbaselib.c index 8753273a..c55d3e98 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.104 2002/11/06 19:08:00 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.105 2002/11/07 15:39:23 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -88,21 +88,6 @@ static int luaB_error (lua_State *L) { } -static int luaB_getmode (lua_State *L) { - luaL_check_type(L, 1, LUA_TTABLE); - lua_pushstring(L, lua_getmode(L, 1)); - return 1; -} - - -static int luaB_setmode (lua_State *L) { - luaL_check_type(L, 1, LUA_TTABLE); - lua_setmode(L, 1, luaL_check_string(L, 2)); - lua_settop(L, 1); - return 1; -} - - static int luaB_getmetatable (lua_State *L) { luaL_check_any(L, 1); if (!lua_getmetatable(L, 1)) { @@ -524,8 +509,6 @@ static const luaL_reg base_funcs[] = { {"setmetatable", luaB_setmetatable}, {"getglobals", luaB_getglobals}, {"setglobals", luaB_setglobals}, - {"getmode", luaB_getmode}, - {"setmode", luaB_setmode}, {"next", luaB_next}, {"ipairs", luaB_ipairs}, {"pairs", luaB_pairs}, @@ -646,7 +629,11 @@ static void base_open (lua_State *L) { /* `newproxy' needs a weaktable as upvalue */ lua_pushliteral(L, "newproxy"); lua_newtable(L); /* new table `w' */ - lua_setmode(L, -1, "k"); + lua_pushvalue(L, -1); /* `w' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "__mode"); + lua_pushliteral(L, "k"); + lua_rawset(L, -3); /* metatable(w).__mode = "k" */ lua_pushcclosure(L, luaB_newproxy, 1); lua_rawset(L, -3); /* set global `newproxy' */ lua_rawset(L, -1); /* set global _G */ diff --git a/lgc.c b/lgc.c index 72984d2a..87ec40c9 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.156 2002/11/11 11:52:43 roberto Exp roberto $ +** $Id: lgc.c,v 1.157 2002/11/13 11:49:19 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -47,6 +47,13 @@ typedef struct GCState { #define markfinalized(u) resetbit((u)->uv.marked, 1) +#define KEYWEAKBIT 1 +#define VALUEWEAKBIT 2 +#define KEYWEAK (1<metatable); lua_assert(h->lsizenode || h->node == st->G->dummynode); - if (h->mode & (WEAKKEY | WEAKVALUE)) { /* weak table? */ - GCObject **weaklist; - weakkey = h->mode & WEAKKEY; - weakvalue = h->mode & WEAKVALUE; - weaklist = (weakkey && weakvalue) ? &st->wkv : - (weakkey) ? &st->wk : - &st->wv; - h->gclist = *weaklist; /* must be cleared after GC, ... */ - *weaklist = valtogco(h); /* ... so put in the appropriate list */ + mode = gfasttm(st->G, h->metatable, TM_MODE); + if (mode && ttisstring(mode)) { /* is there a weak mode? */ + weakkey = (strchr(svalue(mode), 'k') != NULL); + weakvalue = (strchr(svalue(mode), 'v') != NULL); + if (weakkey || weakvalue) { /* is really weak? */ + GCObject **weaklist; + h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ + h->marked |= (weakkey << KEYWEAKBIT) | (weakvalue << VALUEWEAKBIT); + weaklist = (weakkey && weakvalue) ? &st->wkv : + (weakkey) ? &st->wk : + &st->wv; + h->gclist = *weaklist; /* must be cleared after GC, ... */ + *weaklist = valtogco(h); /* ... so put in the appropriate list */ + } } if (!weakvalue) { i = sizearray(h); @@ -280,7 +293,7 @@ static void cleartablekeys (GCObject *l) { while (l) { Table *h = gcotoh(l); int i = sizenode(h); - lua_assert(h->mode & WEAKKEY); + lua_assert(h->marked & KEYWEAK); while (i--) { Node *n = node(h, i); if (!valismarked(key(n))) /* key was collected? */ @@ -298,7 +311,7 @@ static void cleartablevalues (GCObject *l) { while (l) { Table *h = gcotoh(l); int i = sizearray(h); - lua_assert(h->mode & WEAKVALUE); + lua_assert(h->marked & VALUEWEAK); while (i--) { TObject *o = &h->array[i]; if (!valismarked(o)) /* value was collected? */ diff --git a/lobject.h b/lobject.h index d0352c10..d96229ac 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.152 2002/11/07 15:37:10 roberto Exp roberto $ +** $Id: lobject.h,v 1.153 2002/11/13 11:49:19 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -292,7 +292,6 @@ typedef struct Node { typedef struct Table { CommonHeader; lu_byte flags; /* 1<

metatable = hvalue(defaultmeta(L)); t->flags = cast(lu_byte, ~0); - t->mode = 0; /* temporary values (kept only if some malloc fails) */ t->array = NULL; t->sizearray = 0; diff --git a/ltablib.c b/ltablib.c index c95b621c..0893f055 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.13 2002/10/04 14:30:31 roberto Exp roberto $ +** $Id: ltablib.c,v 1.14 2002/10/23 19:08:23 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -288,7 +288,11 @@ static const luaL_reg tab_funcs[] = { LUALIB_API int lua_tablibopen (lua_State *L) { lua_newtable(L); /* create N (table to store num. elements in tables) */ - lua_setmode(L, -1, "k"); /* make it a weak table */ + lua_pushvalue(L, -1); /* `N' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "__mode"); + lua_pushliteral(L, "k"); + lua_rawset(L, -3); /* metatable(N).__mode = "k" */ luaL_opennamedlib(L, LUA_TABLIBNAME, tab_funcs, 1); return 0; } diff --git a/ltm.c b/ltm.c index ff66afdd..60451581 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.102 2002/09/19 20:12:47 roberto Exp roberto $ +** $Id: ltm.c,v 1.103 2002/10/25 20:05:28 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -26,7 +26,7 @@ const char *const luaT_typenames[] = { void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", - "__gc", "__eq", + "__gc", "__mode", "__eq", "__add", "__sub", "__mul", "__div", "__pow", "__unm", "__lt", "__le", "__concat", "__call" diff --git a/ltm.h b/ltm.h index 09f34a25..4c56a093 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 1.39 2002/08/06 17:06:56 roberto Exp roberto $ +** $Id: ltm.h,v 1.40 2002/09/19 20:12:47 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -19,6 +19,7 @@ typedef enum { TM_INDEX, TM_NEWINDEX, TM_GC, + TM_MODE, TM_EQ, /* last tag method with `fast' access */ TM_ADD, TM_SUB, @@ -35,8 +36,10 @@ typedef enum { -#define fasttm(l,et,e) \ - (((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, G(l)->tmname[e])) +#define gfasttm(g,et,e) \ + (((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) + +#define fasttm(l,et,e) gfasttm(G(l), et, e) const TObject *luaT_gettm (Table *events, TMS event, TString *ename); diff --git a/lua.h b/lua.h index 5b0270c8..b36f975c 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.162 2002/11/06 19:08:00 roberto Exp roberto $ +** $Id: lua.h,v 1.163 2002/11/07 15:39:23 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil ** http://www.lua.org mailto:info@lua.org @@ -174,7 +174,6 @@ LUA_API void lua_rawget (lua_State *L, int idx); LUA_API void lua_rawgeti (lua_State *L, int idx, int n); LUA_API void lua_newtable (lua_State *L); LUA_API int lua_getmetatable (lua_State *L, int objindex); -LUA_API const char *lua_getmode (lua_State *L, int idx); LUA_API void lua_getglobals (lua_State *L, int idx); @@ -184,7 +183,6 @@ LUA_API void lua_getglobals (lua_State *L, int idx); LUA_API void lua_settable (lua_State *L, int idx); LUA_API void lua_rawset (lua_State *L, int idx); LUA_API void lua_rawseti (lua_State *L, int idx, int n); -LUA_API void lua_setmode (lua_State *L, int idx, const char *md); LUA_API int lua_setmetatable (lua_State *L, int objindex); LUA_API int lua_setglobals (lua_State *L, int idx);