unification of __index & __gettable (and __newindex & __settable)

This commit is contained in:
Roberto Ierusalimschy 2002-09-19 17:12:47 -03:00
parent 6fb0fd5063
commit e8f35fc4ff
4 changed files with 18 additions and 22 deletions

View File

@ -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
** See Copyright Notice in lua.h
*/
@ -440,21 +440,20 @@ static const luaL_reg flib[] = {
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 */
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 */
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 */
}
/* }====================================================== */

3
ltm.c
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -27,7 +27,6 @@ void luaT_init (lua_State *L) {
static const char *const luaT_eventname[] = { /* ORDER TM */
"__index", "__newindex",
"__gc", "__eq",
"__gettable", "__settable",
"__add", "__sub", "__mul", "__div",
"__pow", "__unm", "__lt", "__le",
"__concat", "__call"

4
ltm.h
View File

@ -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
** See Copyright Notice in lua.h
*/
@ -20,8 +20,6 @@ typedef enum {
TM_NEWINDEX,
TM_GC,
TM_EQ, /* last tag method with `fast' access */
TM_GETTABLE,
TM_SETTABLE,
TM_ADD,
TM_SUB,
TM_MUL,

6
lvm.c
View File

@ -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
** 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,
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))
luaG_typeerror(L, t, "index");
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 if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_SETTABLE)))
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
luaG_typeerror(L, t, "index");
if (ttisfunction(tm)) {
callTM(L, tm, t, key, val);