diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-09-22 03:42:15 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-09-22 03:42:15 -0300 |
commit | 6384475ec4112361b0ab27de553e85863464b846 (patch) | |
tree | d2242751ca7ccb13e364a097388708932bdeeb52 | |
parent | 3a15c7ce4338de8414239a898f6c121294b4dde7 (diff) | |
download | lua-6384475ec4112361b0ab27de553e85863464b846.tar.gz lua-6384475ec4112361b0ab27de553e85863464b846.tar.bz2 lua-6384475ec4112361b0ab27de553e85863464b846.zip |
'luaL_getmetafield' returns type of metafield (instead of a boolean)
-rw-r--r-- | lauxlib.c | 33 | ||||
-rw-r--r-- | lbaselib.c | 10 | ||||
-rw-r--r-- | ltablib.c | 6 |
3 files changed, 25 insertions, 24 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.266 2014/07/17 12:30:53 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.267 2014/07/19 14:37:09 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 | */ |
@@ -170,13 +170,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) { | |||
170 | 170 | ||
171 | static int typeerror (lua_State *L, int arg, const char *tname) { | 171 | static int typeerror (lua_State *L, int arg, const char *tname) { |
172 | const char *msg; | 172 | const char *msg; |
173 | const char *typearg = luaL_typename(L, arg); | 173 | const char *typearg; /* name for the type of the actual argument */ |
174 | if (lua_getmetatable(L, arg)) { | 174 | if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING) |
175 | if (lua_getfield(L, -1, "__name") == LUA_TSTRING) | 175 | typearg = lua_tostring(L, -1); /* use the given type name */ |
176 | typearg = lua_tostring(L, -1); | ||
177 | } | ||
178 | else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) | 176 | else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) |
179 | typearg = "light userdata"; | 177 | typearg = "light userdata"; /* special name for messages */ |
178 | else | ||
179 | typearg = luaL_typename(L, arg); /* standard name */ | ||
180 | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); | 180 | msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); |
181 | return luaL_argerror(L, arg, msg); | 181 | return luaL_argerror(L, arg, msg); |
182 | } | 182 | } |
@@ -701,22 +701,23 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { | |||
701 | 701 | ||
702 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { | 702 | LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { |
703 | if (!lua_getmetatable(L, obj)) /* no metatable? */ | 703 | if (!lua_getmetatable(L, obj)) /* no metatable? */ |
704 | return 0; | 704 | return LUA_TNIL; |
705 | lua_pushstring(L, event); | ||
706 | if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */ | ||
707 | lua_pop(L, 2); /* remove metatable and metafield */ | ||
708 | return 0; | ||
709 | } | ||
710 | else { | 705 | else { |
711 | lua_remove(L, -2); /* remove only metatable */ | 706 | int tt; |
712 | return 1; | 707 | lua_pushstring(L, event); |
708 | tt = lua_rawget(L, -2); | ||
709 | if (tt == LUA_TNIL) /* is metafield nil? */ | ||
710 | lua_pop(L, 2); /* remove metatable and metafield */ | ||
711 | else | ||
712 | lua_remove(L, -2); /* remove only metatable */ | ||
713 | return tt; /* return metafield type */ | ||
713 | } | 714 | } |
714 | } | 715 | } |
715 | 716 | ||
716 | 717 | ||
717 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { | 718 | LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { |
718 | obj = lua_absindex(L, obj); | 719 | obj = lua_absindex(L, obj); |
719 | if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ | 720 | if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */ |
720 | return 0; | 721 | return 0; |
721 | lua_pushvalue(L, obj); | 722 | lua_pushvalue(L, obj); |
722 | lua_call(L, 1, 1); | 723 | lua_call(L, 1, 1); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.295 2014/08/01 17:33:08 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.296 2014/08/21 20:07:56 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -129,7 +129,7 @@ static int luaB_setmetatable (lua_State *L) { | |||
129 | luaL_checktype(L, 1, LUA_TTABLE); | 129 | luaL_checktype(L, 1, LUA_TTABLE); |
130 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, | 130 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, |
131 | "nil or table expected"); | 131 | "nil or table expected"); |
132 | if (luaL_getmetafield(L, 1, "__metatable")) | 132 | if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL) |
133 | return luaL_error(L, "cannot change a protected metatable"); | 133 | return luaL_error(L, "cannot change a protected metatable"); |
134 | lua_settop(L, 2); | 134 | lua_settop(L, 2); |
135 | lua_setmetatable(L, 1); | 135 | lua_setmetatable(L, 1); |
@@ -212,7 +212,7 @@ static int luaB_type (lua_State *L) { | |||
212 | 212 | ||
213 | static int pairsmeta (lua_State *L, const char *method, int iszero, | 213 | static int pairsmeta (lua_State *L, const char *method, int iszero, |
214 | lua_CFunction iter) { | 214 | lua_CFunction iter) { |
215 | if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ | 215 | if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */ |
216 | luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ | 216 | luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ |
217 | lua_pushcfunction(L, iter); /* will return generator, */ | 217 | lua_pushcfunction(L, iter); /* will return generator, */ |
218 | lua_pushvalue(L, 1); /* state, */ | 218 | lua_pushvalue(L, 1); /* state, */ |
@@ -279,8 +279,8 @@ static int ipairsaux (lua_State *L) { | |||
279 | */ | 279 | */ |
280 | static int luaB_ipairs (lua_State *L) { | 280 | static int luaB_ipairs (lua_State *L) { |
281 | lua_CFunction iter = | 281 | lua_CFunction iter = |
282 | (luaL_getmetafield(L, 1, "__len") || | 282 | (luaL_getmetafield(L, 1, "__len") != LUA_TNIL || |
283 | luaL_getmetafield(L, 1, "__index")) | 283 | luaL_getmetafield(L, 1, "__index") != LUA_TNIL) |
284 | ? ipairsaux : ipairsaux_raw; | 284 | ? ipairsaux : ipairsaux_raw; |
285 | #if defined(LUA_COMPAT_IPAIRS) | 285 | #if defined(LUA_COMPAT_IPAIRS) |
286 | return pairsmeta(L, "__ipairs", 1, iter); | 286 | return pairsmeta(L, "__ipairs", 1, iter); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.74 2014/08/21 19:13:55 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.75 2014/08/21 20:07:56 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -126,10 +126,10 @@ static int tmove (lua_State *L) { | |||
126 | luaL_argcheck(L, f > 0, 2, "initial position must be positive"); | 126 | luaL_argcheck(L, f > 0, 2, "initial position must be positive"); |
127 | if (e >= f) { /* otherwise, nothing to move */ | 127 | if (e >= f) { /* otherwise, nothing to move */ |
128 | lua_Integer n, i; | 128 | lua_Integer n, i; |
129 | ta.geti = (!luaL_getmetafield(L, 1, "__index")) | 129 | ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) |
130 | ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) | 130 | ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) |
131 | : lua_geti; | 131 | : lua_geti; |
132 | ta.seti = (!luaL_getmetafield(L, tt, "__newindex")) | 132 | ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) |
133 | ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) | 133 | ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) |
134 | : lua_seti; | 134 | : lua_seti; |
135 | n = e - f + 1; /* number of elements to move */ | 135 | n = e - f + 1; /* number of elements to move */ |