new API for weak mode

This commit is contained in:
Roberto Ierusalimschy
2002-08-06 14:06:56 -03:00
parent a2fa48a570
commit 634344d61f
9 changed files with 88 additions and 65 deletions

27
lapi.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.206 2002/08/05 14:43:38 roberto Exp roberto $
** $Id: lapi.c,v 1.207 2002/08/06 15:32:22 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -464,6 +464,20 @@ 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;
@@ -555,6 +569,17 @@ 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;