'luaL_getmetafield' returns type of metafield (instead of a boolean)

This commit is contained in:
Roberto Ierusalimschy
2014-09-22 03:42:15 -03:00
parent 3a15c7ce43
commit 6384475ec4
3 changed files with 25 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.266 2014/07/17 12:30:53 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.267 2014/07/19 14:37:09 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
*/ */
@@ -170,13 +170,13 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
static int typeerror (lua_State *L, int arg, const char *tname) { static int typeerror (lua_State *L, int arg, const char *tname) {
const char *msg; const char *msg;
const char *typearg = luaL_typename(L, arg); const char *typearg; /* name for the type of the actual argument */
if (lua_getmetatable(L, arg)) { if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
if (lua_getfield(L, -1, "__name") == LUA_TSTRING) typearg = lua_tostring(L, -1); /* use the given type name */
typearg = lua_tostring(L, -1);
}
else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA) else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
typearg = "light userdata"; typearg = "light userdata"; /* special name for messages */
else
typearg = luaL_typename(L, arg); /* standard name */
msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg); msg = lua_pushfstring(L, "%s expected, got %s", tname, typearg);
return luaL_argerror(L, arg, msg); return luaL_argerror(L, arg, msg);
} }
@@ -701,22 +701,23 @@ LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {
LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {
if (!lua_getmetatable(L, obj)) /* no metatable? */ if (!lua_getmetatable(L, obj)) /* no metatable? */
return 0; return LUA_TNIL;
lua_pushstring(L, event);
if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */
lua_pop(L, 2); /* remove metatable and metafield */
return 0;
}
else { else {
lua_remove(L, -2); /* remove only metatable */ int tt;
return 1; lua_pushstring(L, event);
tt = lua_rawget(L, -2);
if (tt == LUA_TNIL) /* is metafield nil? */
lua_pop(L, 2); /* remove metatable and metafield */
else
lua_remove(L, -2); /* remove only metatable */
return tt; /* return metafield type */
} }
} }
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
obj = lua_absindex(L, obj); obj = lua_absindex(L, obj);
if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ if (luaL_getmetafield(L, obj, event) == LUA_TNIL) /* no metafield? */
return 0; return 0;
lua_pushvalue(L, obj); lua_pushvalue(L, obj);
lua_call(L, 1, 1); lua_call(L, 1, 1);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.295 2014/08/01 17:33:08 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.296 2014/08/21 20:07:56 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -129,7 +129,7 @@ static int luaB_setmetatable (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"nil or table expected"); "nil or table expected");
if (luaL_getmetafield(L, 1, "__metatable")) if (luaL_getmetafield(L, 1, "__metatable") != LUA_TNIL)
return luaL_error(L, "cannot change a protected metatable"); return luaL_error(L, "cannot change a protected metatable");
lua_settop(L, 2); lua_settop(L, 2);
lua_setmetatable(L, 1); lua_setmetatable(L, 1);
@@ -212,7 +212,7 @@ static int luaB_type (lua_State *L) {
static int pairsmeta (lua_State *L, const char *method, int iszero, static int pairsmeta (lua_State *L, const char *method, int iszero,
lua_CFunction iter) { lua_CFunction iter) {
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ if (luaL_getmetafield(L, 1, method) == LUA_TNIL) { /* no metamethod? */
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
lua_pushcfunction(L, iter); /* will return generator, */ lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */ lua_pushvalue(L, 1); /* state, */
@@ -279,8 +279,8 @@ static int ipairsaux (lua_State *L) {
*/ */
static int luaB_ipairs (lua_State *L) { static int luaB_ipairs (lua_State *L) {
lua_CFunction iter = lua_CFunction iter =
(luaL_getmetafield(L, 1, "__len") || (luaL_getmetafield(L, 1, "__len") != LUA_TNIL ||
luaL_getmetafield(L, 1, "__index")) luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
? ipairsaux : ipairsaux_raw; ? ipairsaux : ipairsaux_raw;
#if defined(LUA_COMPAT_IPAIRS) #if defined(LUA_COMPAT_IPAIRS)
return pairsmeta(L, "__ipairs", 1, iter); return pairsmeta(L, "__ipairs", 1, iter);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.74 2014/08/21 19:13:55 roberto Exp roberto $ ** $Id: ltablib.c,v 1.75 2014/08/21 20:07:56 roberto Exp roberto $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -126,10 +126,10 @@ static int tmove (lua_State *L) {
luaL_argcheck(L, f > 0, 2, "initial position must be positive"); luaL_argcheck(L, f > 0, 2, "initial position must be positive");
if (e >= f) { /* otherwise, nothing to move */ if (e >= f) { /* otherwise, nothing to move */
lua_Integer n, i; lua_Integer n, i;
ta.geti = (!luaL_getmetafield(L, 1, "__index")) ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL)
? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
: lua_geti; : lua_geti;
ta.seti = (!luaL_getmetafield(L, tt, "__newindex")) ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL)
? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
: lua_seti; : lua_seti;
n = e - f + 1; /* number of elements to move */ n = e - f + 1; /* number of elements to move */