mirror of
https://github.com/lua/lua
synced 2025-01-01 07:04:24 +03:00
Methods separated from metamethods in 'io'
In the 'io' library, changed the use of the metatable also as its own "method table", so that metamethods cannot be accessed as if they were methods. (For instance, 'io.stdin.__gc' does not result in the finalizer metamethod anymore.)
This commit is contained in:
parent
8b7cfee26b
commit
924bed7297
32
liolib.c
32
liolib.c
@ -742,14 +742,23 @@ static const luaL_Reg iolib[] = {
|
|||||||
/*
|
/*
|
||||||
** methods for file handles
|
** methods for file handles
|
||||||
*/
|
*/
|
||||||
static const luaL_Reg flib[] = {
|
static const luaL_Reg meth[] = {
|
||||||
{"close", f_close},
|
|
||||||
{"flush", f_flush},
|
|
||||||
{"lines", f_lines},
|
|
||||||
{"read", f_read},
|
{"read", f_read},
|
||||||
{"seek", f_seek},
|
|
||||||
{"setvbuf", f_setvbuf},
|
|
||||||
{"write", f_write},
|
{"write", f_write},
|
||||||
|
{"lines", f_lines},
|
||||||
|
{"flush", f_flush},
|
||||||
|
{"seek", f_seek},
|
||||||
|
{"close", f_close},
|
||||||
|
{"setvbuf", f_setvbuf},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** metamethods for file handles
|
||||||
|
*/
|
||||||
|
static const luaL_Reg metameth[] = {
|
||||||
|
{"__index", NULL}, /* place holder */
|
||||||
{"__gc", f_gc},
|
{"__gc", f_gc},
|
||||||
{"__close", f_gc},
|
{"__close", f_gc},
|
||||||
{"__tostring", f_tostring},
|
{"__tostring", f_tostring},
|
||||||
@ -758,11 +767,12 @@ static const luaL_Reg flib[] = {
|
|||||||
|
|
||||||
|
|
||||||
static void createmeta (lua_State *L) {
|
static void createmeta (lua_State *L) {
|
||||||
luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
|
luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
|
||||||
lua_pushvalue(L, -1); /* push metatable */
|
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
|
||||||
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
|
luaL_newlibtable(L, meth); /* create method table */
|
||||||
luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */
|
luaL_setfuncs(L, meth, 0); /* add file methods to method table */
|
||||||
lua_pop(L, 1); /* pop new metatable */
|
lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
|
||||||
|
lua_pop(L, 1); /* pop metatable */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user