Browse Source

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

pull/9/head
Roberto Ierusalimschy 10 years ago
parent
commit
6384475ec4
  1. 33
      lauxlib.c
  2. 10
      lbaselib.c
  3. 6
      ltablib.c

33
lauxlib.c

@ -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
** 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) {
const char *msg;
const char *typearg = luaL_typename(L, arg);
if (lua_getmetatable(L, arg)) {
if (lua_getfield(L, -1, "__name") == LUA_TSTRING)
typearg = lua_tostring(L, -1);
}
const char *typearg; /* name for the type of the actual argument */
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
typearg = lua_tostring(L, -1); /* use the given type name */
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);
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) {
if (!lua_getmetatable(L, obj)) /* no metatable? */
return 0;
lua_pushstring(L, event);
if (lua_rawget(L, -2) == LUA_TNIL) { /* is metafield nil? */
lua_pop(L, 2); /* remove metatable and metafield */
return 0;
}
return LUA_TNIL;
else {
lua_remove(L, -2); /* remove only metatable */
return 1;
int tt;
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) {
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;
lua_pushvalue(L, obj);
lua_call(L, 1, 1);

10
lbaselib.c

@ -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
** See Copyright Notice in lua.h
*/
@ -129,7 +129,7 @@ static int luaB_setmetatable (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
"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");
lua_settop(L, 2);
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,
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 */
lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
@ -279,8 +279,8 @@ static int ipairsaux (lua_State *L) {
*/
static int luaB_ipairs (lua_State *L) {
lua_CFunction iter =
(luaL_getmetafield(L, 1, "__len") ||
luaL_getmetafield(L, 1, "__index"))
(luaL_getmetafield(L, 1, "__len") != LUA_TNIL ||
luaL_getmetafield(L, 1, "__index") != LUA_TNIL)
? ipairsaux : ipairsaux_raw;
#if defined(LUA_COMPAT_IPAIRS)
return pairsmeta(L, "__ipairs", 1, iter);

6
ltablib.c

@ -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
** 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");
if (e >= f) { /* otherwise, nothing to move */
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)
: 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)
: lua_seti;
n = e - f + 1; /* number of elements to move */

Loading…
Cancel
Save