diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-25 16:15:21 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-25 16:15:21 -0300 |
commit | 89b102bd1d967656daeb337bf59ad6abeddeb613 (patch) | |
tree | c172e696ddb80d4a1c2852a3d6f70f8f85794a71 | |
parent | fdfd5b44ee48a5497181d0bcaa89586b12a48eb4 (diff) | |
download | lua-89b102bd1d967656daeb337bf59ad6abeddeb613.tar.gz lua-89b102bd1d967656daeb337bf59ad6abeddeb613.tar.bz2 lua-89b102bd1d967656daeb337bf59ad6abeddeb613.zip |
new `luaL_weakregistry' function
-rw-r--r-- | lauxlib.c | 23 | ||||
-rw-r--r-- | lauxlib.h | 4 |
2 files changed, 24 insertions, 3 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.74 2002/06/13 13:44:50 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.75 2002/06/18 15:19:27 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -143,7 +143,7 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { | |||
143 | return 0; | 143 | return 0; |
144 | } | 144 | } |
145 | lua_pushvalue(L, obj); | 145 | lua_pushvalue(L, obj); |
146 | lua_upcall(L, 1, 1); | 146 | lua_call(L, 1, 1); |
147 | return 1; | 147 | return 1; |
148 | } | 148 | } |
149 | 149 | ||
@@ -299,6 +299,25 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { | |||
299 | } | 299 | } |
300 | 300 | ||
301 | 301 | ||
302 | LUALIB_API void luaL_weakregistry (lua_State *L) { | ||
303 | static const char dummy = '\0'; /* index for a weak table in registry */ | ||
304 | lua_pushudataval(L, (void *)&dummy); /* push index */ | ||
305 | lua_rawget(L, LUA_REGISTRYINDEX); /* get value */ | ||
306 | if (!lua_isnil(L, -1)) return; /* weak table already created? */ | ||
307 | /* else must create a weak table */ | ||
308 | lua_pop(L, 1); /* remove previous nil */ | ||
309 | lua_newtable(L); /* new table `w' */ | ||
310 | lua_pushvalue(L, -1); | ||
311 | lua_setmetatable(L, -2); /* setmetatable(w, w) */ | ||
312 | lua_pushliteral(L, "__mode"); | ||
313 | lua_pushliteral(L, "kv"); | ||
314 | lua_rawset(L, -3); /* metatable(w).__mode = "kv" */ | ||
315 | lua_pushudataval(L, (void *)&dummy); /* push index */ | ||
316 | lua_pushvalue(L, -2); /* push value */ | ||
317 | lua_rawset(L, LUA_REGISTRYINDEX); /* store new weak table into registry */ | ||
318 | } | ||
319 | |||
320 | |||
302 | /* | 321 | /* |
303 | ** {====================================================== | 322 | ** {====================================================== |
304 | ** Load functions | 323 | ** Load functions |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.48 2002/06/03 20:11:41 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.49 2002/06/18 15:19:27 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -49,6 +49,8 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...); | |||
49 | LUALIB_API int luaL_findstring (const char *name, | 49 | LUALIB_API int luaL_findstring (const char *name, |
50 | const char *const list[]); | 50 | const char *const list[]); |
51 | 51 | ||
52 | LUALIB_API void luaL_weakregistry (lua_State *L); | ||
53 | |||
52 | LUALIB_API int luaL_ref (lua_State *L, int t); | 54 | LUALIB_API int luaL_ref (lua_State *L, int t); |
53 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref); | 55 | LUALIB_API void luaL_unref (lua_State *L, int t, int ref); |
54 | 56 | ||