Browse Source

`ref' support goes to auxlib

Roberto Ierusalimschy 24 years ago
parent
commit
46347d768e
4 changed files with 55 additions and 50 deletions
  1. 1 35
      lapi.c
  2. 35 4
      lauxlib.c
  3. 4 1
      lauxlib.h
  4. 15 10
      lua.h

+ 1 - 35
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
 ** Lua API
 ** See Copyright Notice in lua.h
 ** 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)
 ** `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) {
 LUA_API int lua_next (lua_State *L, int index) {
   StkId t;
   StkId t;
   int more;
   int more;

+ 35 - 4
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
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** 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];
   l_char buff[80];
   sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
   sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
   luaL_argerror(L, narg, buff);
   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) {
 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,
 LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
                                       const l_char *name) {
                                       const l_char *name) {
   if (strcmp(lua_type(L, narg), name) != 0)
   if (strcmp(lua_type(L, narg), name) != 0)
-    type_error(L, narg, name);
+    luaL_typerror(L, narg, name);
   return lua_touserdata(L, narg);
   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);
+  }
+}
+

+ 4 - 1
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
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** 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_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,
 LUALIB_API void luaL_argerror (lua_State *L, int numarg,
                                const lua_char *extramsg);
                                const lua_char *extramsg);
 LUALIB_API const lua_char *luaL_check_lstr (lua_State *L, int numArg,
 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, 
 LUALIB_API int luaL_findstring (const lua_char *name, 
                                 const lua_char *const list[]);
                                 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);
 
 
 
 
 /*
 /*

+ 15 - 10
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
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -26,11 +26,6 @@
 #define LUA_ERRORMESSAGE	"_ERRORMESSAGE"
 #define LUA_ERRORMESSAGE	"_ERRORMESSAGE"
 
 
 
 
-/* pre-defined references */
-#define LUA_NOREF	(-2)
-#define LUA_REFNIL	(-1)
-
-
 /* option for multiple returns in `lua_call' */
 /* option for multiple returns in `lua_call' */
 #define LUA_MULTRET	(-1)
 #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_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_setglobals (lua_State *L);
 LUA_API void  lua_setglobals (lua_State *L);
 LUA_API void  lua_settagmethod (lua_State *L, int tag, const lua_char *event);
 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_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_next (lua_State *L, int index);
 LUA_API int   lua_getn (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_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);
 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
 #endif