diff --git a/lapi.c b/lapi.c index 1f8d296f..abc85e91 100644 --- a/lapi.c +++ b/lapi.c @@ -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; } diff --git a/lbaselib.c b/lbaselib.c index c760172c..2c63b3bb 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -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); diff --git a/lua.h b/lua.h index eb4f06db..4140af63 100644 --- a/lua.h +++ b/lua.h @@ -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); /*