浏览代码

metatable always return some value

Roberto Ierusalimschy 23 年之前
父节点
当前提交
405e3a4597
共有 3 个文件被更改,包括 23 次插入10 次删除
  1. 10 4
      lapi.c
  2. 11 4
      lbaselib.c
  3. 2 2
      lua.h

+ 10 - 4
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;
 }
 
 

+ 11 - 4
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);

+ 2 - 2
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: [email protected]
@@ -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);
 
 
 /*