From 46347d768e571ba9b36581c36d11d2de1dee2cfb Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 31 Oct 2001 17:40:14 -0200 Subject: `ref' support goes to auxlib --- lapi.c | 36 +----------------------------------- lauxlib.c | 39 +++++++++++++++++++++++++++++++++++---- lauxlib.h | 5 ++++- lua.h | 25 +++++++++++++++---------- 4 files changed, 55 insertions(+), 50 deletions(-) diff --git a/lapi.c b/lapi.c index e941139a..c0212e79 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.156 2001/10/17 21:17:45 roberto Exp $ +** $Id: lapi.c,v 1.157 2001/10/25 19:14:14 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -458,30 +458,6 @@ LUA_API void lua_setglobals (lua_State *L) { } -LUA_API int lua_ref (lua_State *L, int lock) { - int ref; - if (lock == 0) lua_error(L, l_s("unlocked references are obsolete")); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); - return LUA_REFNIL; - } - lua_rawgeti(L, LUA_REGISTRYINDEX, 0); /* get first free element */ - ref = cast(int, lua_tonumber(L, -1)); - lua_pop(L, 1); /* remove it from stack */ - if (ref != 0) { /* some free element? */ - lua_rawgeti(L, LUA_REGISTRYINDEX, ref); /* remove it from list */ - lua_rawseti(L, LUA_REGISTRYINDEX, 0); - } - else { /* no free elements */ - ref = lua_getn(L, LUA_REGISTRYINDEX) + 1; /* use next `n' */ - lua_pushliteral(L, l_s("n")); - lua_pushnumber(L, ref); - lua_settable(L, LUA_REGISTRYINDEX); /* n = n+1 */ - } - lua_rawseti(L, LUA_REGISTRYINDEX, ref); - return ref; -} - /* ** `do' functions (run Lua code) @@ -635,16 +611,6 @@ LUA_API void lua_error (lua_State *L, const l_char *s) { } -LUA_API void lua_unref (lua_State *L, int ref) { - if (ref >= 0) { - lua_rawgeti(L, LUA_REGISTRYINDEX, 0); - lua_pushnumber(L, ref); - lua_rawseti(L, LUA_REGISTRYINDEX, 0); - lua_rawseti(L, LUA_REGISTRYINDEX, ref); - } -} - - LUA_API int lua_next (lua_State *L, int index) { StkId t; int more; diff --git a/lauxlib.c b/lauxlib.c index cc1390b9..7fef8721 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.51 2001/07/12 18:11:58 roberto Exp $ +** $Id: lauxlib.c,v 1.52 2001/10/26 17:33:30 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -42,7 +42,7 @@ LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) { } -static void type_error (lua_State *L, int narg, const l_char *tname) { +LUALIB_API void luaL_typerror (lua_State *L, int narg, const l_char *tname) { l_char buff[80]; sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg)); luaL_argerror(L, narg, buff); @@ -50,7 +50,7 @@ static void type_error (lua_State *L, int narg, const l_char *tname) { static void tag_error (lua_State *L, int narg, int tag) { - type_error(L, narg, lua_typename(L, tag)); + luaL_typerror(L, narg, lua_typename(L, tag)); } @@ -75,7 +75,7 @@ LUALIB_API void luaL_check_any (lua_State *L, int narg) { LUALIB_API void *luaL_check_userdata (lua_State *L, int narg, const l_char *name) { if (strcmp(lua_type(L, narg), name) != 0) - type_error(L, narg, name); + luaL_typerror(L, narg, name); return lua_touserdata(L, narg); } @@ -223,3 +223,34 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { } /* }====================================================== */ + + +LUALIB_API int luaL_ref (lua_State *L, int t) { + int ref; + lua_rawgeti(L, t, 0); /* get first free element */ + ref = (int)lua_tonumber(L, -1); + lua_pop(L, 1); /* remove it from stack */ + if (ref != 0) { /* any free element? */ + lua_rawgeti(L, t, ref); /* remove it from list */ + lua_rawseti(L, t, 0); + } + else { /* no free elements */ + ref = lua_getn(L, t) + 1; /* use next `n' */ + lua_pushliteral(L, l_s("n")); + lua_pushnumber(L, ref); + lua_settable(L, t); /* n = n+1 */ + } + lua_rawseti(L, t, ref); + return ref; +} + + +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { + if (ref >= 0) { + lua_rawgeti(L, t, 0); + lua_pushnumber(L, ref); + lua_rawseti(L, t, 0); + lua_rawseti(L, t, ref); + } +} + diff --git a/lauxlib.h b/lauxlib.h index e3da34f3..9bc51695 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.36 2001/07/22 00:59:36 roberto Exp $ +** $Id: lauxlib.h,v 1.37 2001/10/26 17:33:30 roberto Exp $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -27,6 +27,7 @@ typedef struct luaL_reg { LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n); +LUALIB_API void luaL_typerror (lua_State *L, int narg, const lua_char *tname); LUALIB_API void luaL_argerror (lua_State *L, int numarg, const lua_char *extramsg); LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg, @@ -46,6 +47,8 @@ LUALIB_API void luaL_verror (lua_State *L, const lua_char *fmt, ...); LUALIB_API int luaL_findstring (const lua_char *name, const lua_char *const list[]); +LUALIB_API int luaL_ref (lua_State *L, int t); +LUALIB_API void luaL_unref (lua_State *L, int t, int ref); /* diff --git a/lua.h b/lua.h index 1325f5f4..1691b07f 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.104 2001/10/11 21:41:21 roberto Exp $ +** $Id: lua.h,v 1.105 2001/10/17 21:12:57 roberto Exp $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: info@lua.org @@ -26,11 +26,6 @@ #define LUA_ERRORMESSAGE "_ERRORMESSAGE" -/* pre-defined references */ -#define LUA_NOREF (-2) -#define LUA_REFNIL (-1) - - /* option for multiple returns in `lua_call' */ #define LUA_MULTRET (-1) @@ -180,7 +175,6 @@ LUA_API void lua_rawset (lua_State *L, int index); LUA_API void lua_rawseti (lua_State *L, int index, int n); LUA_API void lua_setglobals (lua_State *L); LUA_API void lua_settagmethod (lua_State *L, int tag, const lua_char *event); -LUA_API int lua_ref (lua_State *L, int lock); /* @@ -214,8 +208,6 @@ LUA_API const lua_char *lua_tag2name (lua_State *L, int tag); LUA_API void lua_error (lua_State *L, const lua_char *s); -LUA_API void lua_unref (lua_State *L, int ref); - LUA_API int lua_next (lua_State *L, int index); LUA_API int lua_getn (lua_State *L, int index); @@ -253,7 +245,6 @@ LUA_API int lua_getweakmode (lua_State *L, int index); #define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX); -#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) /* @@ -264,6 +255,20 @@ LUA_API int lua_getweakmode (lua_State *L, int index); LUA_API void lua_pushupvalues (lua_State *L); + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_error(L, l_s("unlocked references are obsolete")), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref) + #endif -- cgit v1.2.3-55-g6feb