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 /lauxlib.c | |
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)
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 33 |
1 files changed, 17 insertions, 16 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); |