Browse Source

simpler implementation for userdata types

v5-2
Roberto Ierusalimschy 20 years ago
parent
commit
027e9e99ca
  1. 22
      lauxlib.c
  2. 5
      lauxlib.h

22
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; lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
if (p == NULL || /* if is not a userdata? */ if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
!lua_getmetatable(L, ud) || /* has no metatable? */ luaL_typerror(L, ud, tname);
(lua_rawget(L, LUA_REGISTRYINDEX), /* get registry[metatable] */ lua_pop(L, 2); /* remove both metatables */
(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] */
return p; return p;
} }

5
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)))
/* /*
** {====================================================== ** {======================================================

Loading…
Cancel
Save