Bläddra i källkod

simpler implementation for userdata types

Roberto Ierusalimschy 20 år sedan
förälder
incheckning
027e9e99ca
2 ändrade filer med 8 tillägg och 19 borttagningar
  1. 5 17
      lauxlib.c
  2. 3 2
      lauxlib.h

+ 5 - 17
lauxlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.c,v 1.146 2005/08/17 20:09:31 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -117,28 +117,16 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {
   lua_newtable(L);  /* create metatable */
   lua_newtable(L);  /* create metatable */
   lua_pushvalue(L, -1);
   lua_pushvalue(L, -1);
   lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
   lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */
-  lua_pushvalue(L, -1);
-  lua_pushstring(L, tname);
-  lua_rawset(L, LUA_REGISTRYINDEX);  /* registry[metatable] = name */
   return 1;
   return 1;
 }
 }
 
 
 
 
-LUALIB_API void  luaL_getmetatable (lua_State *L, const char *tname) {
-  lua_getfield(L, LUA_REGISTRYINDEX, tname);
-}
-
-
 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
 LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
   void *p = lua_touserdata(L, ud);
   void *p = lua_touserdata(L, ud);
-  const char *tn;
-  if (p == NULL ||  /* if is not a userdata? */
-      !lua_getmetatable(L, ud) ||  /* has no metatable? */
-      (lua_rawget(L, LUA_REGISTRYINDEX),  /* get registry[metatable] */
-         (tn = lua_tostring(L, -1)) == NULL) ||  /* metatable not registered? */
-      (strcmp(tn, tname) != 0))  /* or wrong? */
-    luaL_typerror(L, ud, tname);  /* then error */
-  lua_pop(L, 1);  /* remove registry[metatable] */
+  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */
+  if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
+    luaL_typerror(L, ud, tname);
+  lua_pop(L, 2);  /* remove both metatables */
   return p;
   return p;
 }
 }
 
 

+ 3 - 2
lauxlib.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.h,v 1.80 2005/07/13 19:02:42 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.81 2005/08/15 14:12:32 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -60,7 +60,6 @@ LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
 LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
 LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
 
 
 LUALIB_API int   (luaL_newmetatable) (lua_State *L, const char *tname);
 LUALIB_API int   (luaL_newmetatable) (lua_State *L, const char *tname);
-LUALIB_API void  (luaL_getmetatable) (lua_State *L, const char *tname);
 LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
 LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
 
 
 LUALIB_API void (luaL_where) (lua_State *L, int lvl);
 LUALIB_API void (luaL_where) (lua_State *L, int lvl);
@@ -114,6 +113,8 @@ LUALIB_API const char *(luaL_setfield) (lua_State *L, int idx,
 
 
 #define luaL_dostring(L, s)	(luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))
 #define luaL_dostring(L, s)	(luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0))
 
 
+#define luaL_getmetatable(L,n)	(lua_getfield(L, LUA_REGISTRYINDEX, (n)))
+
 
 
 /*
 /*
 ** {======================================================
 ** {======================================================