first implementation for weak tables

This commit is contained in:
Roberto Ierusalimschy
2001-04-11 11:42:41 -03:00
parent 2a50188269
commit 0e0e4a480e
7 changed files with 121 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.31 2001/03/26 14:31:49 roberto Exp roberto $
** $Id: lbaselib.c,v 1.32 2001/04/06 18:25:00 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -169,6 +169,29 @@ static int luaB_settag (lua_State *L) {
return 1; /* return table */
}
static int luaB_weakmode (lua_State *L) {
const char *mode = luaL_opt_string(L, 2, NULL);
luaL_checktype(L, 1, LUA_TTABLE);
if (mode == NULL) {
char buff[3];
char *s = buff;
int imode = lua_getweakmode(L, 1);
if (imode & LUA_WEAK_KEY) *s++ = 'k';
if (imode & LUA_WEAK_VALUE) *s++ = 'v';
*s = '\0';
lua_pushstring(L, buff);
return 1;
}
else {
int imode = 0;
lua_pushvalue(L, 1); /* push table */
if (strchr(mode, l_c('k'))) imode |= LUA_WEAK_KEY;
if (strchr(mode, l_c('v'))) imode |= LUA_WEAK_VALUE;
lua_setweakmode(L, imode);
return 1;
}
}
static int luaB_newtype (lua_State *L) {
const l_char *name = luaL_opt_string(L, 1, NULL);
lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
@@ -753,6 +776,7 @@ static const luaL_reg base_funcs[] = {
{l_s("tremove"), luaB_tremove},
{l_s("unwrap"), luaB_unwrap},
{l_s("xtype"), luaB_xtype},
{l_s("weakmode"), luaB_weakmode}
};