Procházet zdrojové kódy

unification of __index & __gettable (and __newindex & __settable)

Roberto Ierusalimschy před 23 roky
rodič
revize
e8f35fc4ff
4 změnil soubory, kde provedl 18 přidání a 22 odebrání
  1. 13 14
      liolib.c
  2. 1 2
      ltm.c
  3. 1 3
      ltm.h
  4. 3 3
      lvm.c

+ 13 - 14
liolib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: liolib.c,v 2.17 2002/08/21 14:57:48 roberto Exp roberto $
+** $Id: liolib.c,v 2.18 2002/09/17 20:35:54 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -440,21 +440,20 @@ static const luaL_reg flib[] = {
 
 
 
 
 static void createmeta (lua_State *L) {
 static void createmeta (lua_State *L) {
-  lua_pushliteral(L, FILEHANDLE);  /* S: FH */
-  lua_newtable(L);  /* S: mt FH */
+  lua_pushliteral(L, FILEHANDLE);
+  lua_newtable(L);  /* push new metatable for file handles */
   /* close files when collected */
   /* close files when collected */
-  lua_pushliteral(L, "__gc");  /* S: `gc' mt FH */
-  lua_pushvalue(L, -2);  /* S: mt `gc' mt FH */
-  lua_pushcclosure(L, io_gc, 1);  /* S: close `gc' mt FH */
-  lua_rawset(L, -3);  /* S: mt FH */
+  lua_pushliteral(L, "__gc");
+  lua_pushvalue(L, -2);  /* push metatable (will be upvalue for `gc' method) */
+  lua_pushcclosure(L, io_gc, 1);
+  lua_rawset(L, -3);  /* metatable.__gc = io_gc */
   /* file methods */
   /* file methods */
-  lua_pushliteral(L, "__gettable");  /* S: `gettable' mt FH */
-  lua_pushvalue(L, -2);  /* S: mt `gettable' mt FH */
-  lua_rawset(L, -3);  /* S: mt FH */
-  lua_pushvalue(L, -1);  /* S: mt mt FH */
-  luaL_openlib(L, flib, 1);  /* S: mt FH */
-  /* put new metatable into registry */
-  lua_rawset(L, LUA_REGISTRYINDEX);  /* S: empty */
+  lua_pushliteral(L, "__index");
+  lua_pushvalue(L, -2);  /* push metatable */
+  lua_rawset(L, -3);  /* metatable.__index = metatable */
+  lua_pushvalue(L, -1);  /* push metatable (will be upvalue for library) */
+  luaL_openlib(L, flib, 1);
+  lua_rawset(L, LUA_REGISTRYINDEX);  /* registry.FILEHANDLE = metatable */
 }
 }
 
 
 /* }====================================================== */
 /* }====================================================== */

+ 1 - 2
ltm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltm.c,v 1.100 2002/08/06 17:06:56 roberto Exp roberto $
+** $Id: ltm.c,v 1.101 2002/08/30 19:09:21 roberto Exp roberto $
 ** Tag methods
 ** Tag methods
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -27,7 +27,6 @@ void luaT_init (lua_State *L) {
   static const char *const luaT_eventname[] = {  /* ORDER TM */
   static const char *const luaT_eventname[] = {  /* ORDER TM */
     "__index", "__newindex",
     "__index", "__newindex",
     "__gc", "__eq",
     "__gc", "__eq",
-    "__gettable", "__settable",
     "__add", "__sub", "__mul", "__div",
     "__add", "__sub", "__mul", "__div",
     "__pow", "__unm", "__lt", "__le",
     "__pow", "__unm", "__lt", "__le",
     "__concat", "__call"
     "__concat", "__call"

+ 1 - 3
ltm.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltm.h,v 1.38 2002/07/01 17:06:58 roberto Exp roberto $
+** $Id: ltm.h,v 1.39 2002/08/06 17:06:56 roberto Exp roberto $
 ** Tag methods
 ** Tag methods
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -20,8 +20,6 @@ typedef enum {
   TM_NEWINDEX,
   TM_NEWINDEX,
   TM_GC,
   TM_GC,
   TM_EQ,  /* last tag method with `fast' access */
   TM_EQ,  /* last tag method with `fast' access */
-  TM_GETTABLE,
-  TM_SETTABLE,
   TM_ADD,
   TM_ADD,
   TM_SUB,
   TM_SUB,
   TM_MUL,
   TM_MUL,

+ 3 - 3
lvm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.c,v 1.254 2002/08/21 18:56:19 roberto Exp roberto $
+** $Id: lvm.c,v 1.255 2002/09/19 13:03:53 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -134,7 +134,7 @@ static const TObject *luaV_index (lua_State *L, const TObject *t,
 
 
 static const TObject *luaV_getnotable (lua_State *L, const TObject *t,
 static const TObject *luaV_getnotable (lua_State *L, const TObject *t,
                                        TObject *key, int loop) {
                                        TObject *key, int loop) {
-  const TObject *tm = luaT_gettmbyobj(L, t, TM_GETTABLE);
+  const TObject *tm = luaT_gettmbyobj(L, t, TM_INDEX);
   if (ttisnil(tm))
   if (ttisnil(tm))
     luaG_typeerror(L, t, "index");
     luaG_typeerror(L, t, "index");
   if (ttisfunction(tm)) {
   if (ttisfunction(tm)) {
@@ -181,7 +181,7 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) {
       }
       }
       /* else will try the tag method */
       /* else will try the tag method */
     }
     }
-    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)))
+    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
       luaG_typeerror(L, t, "index");
       luaG_typeerror(L, t, "index");
     if (ttisfunction(tm)) {
     if (ttisfunction(tm)) {
       callTM(L, tm, t, key, val);
       callTM(L, tm, t, key, val);