metatable always return some value

This commit is contained in:
Roberto Ierusalimschy 2002-03-27 09:49:53 -03:00
parent 81215cd59f
commit 405e3a4597
3 changed files with 23 additions and 10 deletions

14
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.179 2002/03/20 12:51:29 roberto Exp roberto $
** $Id: lapi.c,v 1.180 2002/03/26 20:46:10 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -400,9 +400,10 @@ LUA_API void lua_newtable (lua_State *L) {
}
LUA_API void lua_getmetatable (lua_State *L, int objindex) {
LUA_API int lua_getmetatable (lua_State *L, int objindex) {
StkId obj;
Table *mt;
int res;
lua_lock(L);
obj = luaA_indexAcceptable(L, objindex);
switch (ttype(obj)) {
@ -415,12 +416,17 @@ LUA_API void lua_getmetatable (lua_State *L, int objindex) {
default:
mt = hvalue(defaultmeta(L));
}
if (mt == hvalue(defaultmeta(L)))
if (mt == hvalue(defaultmeta(L))) {
setnilvalue(L->top);
else
res = 0;
}
else {
sethvalue(L->top, mt);
res = 1;
}
api_incr_top(L);
lua_unlock(L);
return res;
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.59 2002/02/14 21:42:22 roberto Exp roberto $
** $Id: lbaselib.c,v 1.60 2002/03/20 12:54:08 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@ -120,11 +120,18 @@ static int luaB_error (lua_State *L) {
static int luaB_metatable (lua_State *L) {
luaL_check_type(L, 1, LUA_TTABLE);
if (lua_isnone(L, 2))
lua_getmetatable(L, 1);
luaL_check_any(L, 1);
if (lua_isnone(L, 2)) {
if (lua_getmetatable(L, 1)) {
lua_pushliteral(L, "__metatable");
lua_rawget(L, -2);
if (lua_isnil(L, -1))
lua_pop(L, 1);
}
}
else {
int t = lua_type(L, 2);
luaL_check_type(L, 1, LUA_TTABLE);
luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil/table expected");
lua_settop(L, 2);
lua_setmetatable(L, 1);

4
lua.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lua.h,v 1.122 2002/03/07 18:15:10 roberto Exp roberto $
** $Id: lua.h,v 1.123 2002/03/18 18:18:35 roberto Exp roberto $
** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: info@lua.org
@ -152,7 +152,7 @@ LUA_API void lua_gettable (lua_State *L, int index);
LUA_API void lua_rawget (lua_State *L, int index);
LUA_API void lua_rawgeti (lua_State *L, int index, int n);
LUA_API void lua_newtable (lua_State *L);
LUA_API void lua_getmetatable (lua_State *L, int objindex);
LUA_API int lua_getmetatable (lua_State *L, int objindex);
/*