mirror of
https://github.com/lua/lua
synced 2025-01-04 08:34:26 +03:00
simpler implementation for userdata types
This commit is contained in:
parent
3b828d9e48
commit
027e9e99ca
22
lauxlib.c
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
|
||||
** 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_pushvalue(L, -1);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
** 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 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_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_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
|
||||
|
||||
|
||||
/*
|
||||
** {======================================================
|
||||
|
Loading…
Reference in New Issue
Block a user