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 | |
parent | 3b828d9e48caefdf4656f9920bf45c395f0f04d2 (diff) | |
download | lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.tar.gz lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.tar.bz2 lua-027e9e99ca99cb572398d18d1c61a1ea2e804b71.zip |
simpler implementation for userdata types
-rw-r--r-- | lauxlib.c | 22 | ||||
-rw-r--r-- | lauxlib.h | 5 |
2 files changed, 8 insertions, 19 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 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.80 2005/07/13 19:02:42 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.81 2005/08/15 14:12:32 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 | */ |
@@ -60,7 +60,6 @@ LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); | |||
60 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); | 60 | LUALIB_API void (luaL_checkany) (lua_State *L, int narg); |
61 | 61 | ||
62 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); | 62 | LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); |
63 | LUALIB_API void (luaL_getmetatable) (lua_State *L, const char *tname); | ||
64 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); | 63 | LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); |
65 | 64 | ||
66 | LUALIB_API void (luaL_where) (lua_State *L, int lvl); | 65 | 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, | |||
114 | 113 | ||
115 | #define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)) | 114 | #define luaL_dostring(L, s) (luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)) |
116 | 115 | ||
116 | #define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) | ||
117 | |||
117 | 118 | ||
118 | /* | 119 | /* |
119 | ** {====================================================== | 120 | ** {====================================================== |