diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-18 17:36:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-08-18 17:36:26 -0300 |
commit | 027e9e99ca99cb572398d18d1c61a1ea2e804b71 (patch) | |
tree | cd83be0ee0018fd88eb8020b61ec8c829cab2d44 /lauxlib.c | |
parent | 3b828d9e48caefdf4656f9920bf45c395f0f04d2 (diff) | |
download | lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.tar.gz lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.tar.bz2 lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.zip |
simpler implementation for userdata types
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 22 |
1 files changed, 5 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.146 2005/08/17 20:09:31 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.147 2005/08/18 16:04:05 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -117,28 +117,16 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | |||
117 | lua_newtable(L); /* create metatable */ | 117 | lua_newtable(L); /* create metatable */ |
118 | lua_pushvalue(L, -1); | 118 | lua_pushvalue(L, -1); |
119 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ | 119 | lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ |
120 | lua_pushvalue(L, -1); | ||
121 | lua_pushstring(L, tname); | ||
122 | lua_rawset(L, LUA_REGISTRYINDEX); /* registry[metatable] = name */ | ||
123 | return 1; | 120 | return 1; |
124 | } | 121 | } |
125 | 122 | ||
126 | 123 | ||
127 | LUALIB_API void luaL_getmetatable (lua_State *L, const char *tname) { | ||
128 | lua_getfield(L, LUA_REGISTRYINDEX, tname); | ||
129 | } | ||
130 | |||
131 | |||
132 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | 124 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { |
133 | void *p = lua_touserdata(L, ud); | 125 | void *p = lua_touserdata(L, ud); |
134 | const char *tn; | 126 | lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ |
135 | if (p == NULL || /* if is not a userdata? */ | 127 | if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) |
136 | !lua_getmetatable(L, ud) || /* has no metatable? */ | 128 | luaL_typerror(L, ud, tname); |
137 | (lua_rawget(L, LUA_REGISTRYINDEX), /* get registry[metatable] */ | 129 | lua_pop(L, 2); /* remove both metatables */ |
138 | (tn = lua_tostring(L, -1)) == NULL) || /* metatable not registered? */ | ||
139 | (strcmp(tn, tname) != 0)) /* or wrong? */ | ||
140 | luaL_typerror(L, ud, tname); /* then error */ | ||
141 | lua_pop(L, 1); /* remove registry[metatable] */ | ||
142 | return p; | 130 | return p; |
143 | } | 131 | } |
144 | 132 | ||