aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-01 12:25:00 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-07-01 12:25:00 -0300
commit924bed7297d5ea16a78ec07e7acc64afad951aa8 (patch)
tree2a96cf02d0d8ec43472f69faef57109e6bdcebd0 /liolib.c
parent8b7cfee26b71e66de2cef9f8db9d9e18f5439afd (diff)
downloadlua-924bed7297d5ea16a78ec07e7acc64afad951aa8.tar.gz
lua-924bed7297d5ea16a78ec07e7acc64afad951aa8.tar.bz2
lua-924bed7297d5ea16a78ec07e7acc64afad951aa8.zip
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.)
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/liolib.c b/liolib.c
index 1484676d..2c0c6a3b 100644
--- a/liolib.c
+++ b/liolib.c
@@ -742,14 +742,23 @@ static const luaL_Reg iolib[] = {
742/* 742/*
743** methods for file handles 743** methods for file handles
744*/ 744*/
745static const luaL_Reg flib[] = { 745static const luaL_Reg meth[] = {
746 {"close", f_close},
747 {"flush", f_flush},
748 {"lines", f_lines},
749 {"read", f_read}, 746 {"read", f_read},
747 {"write", f_write},
748 {"lines", f_lines},
749 {"flush", f_flush},
750 {"seek", f_seek}, 750 {"seek", f_seek},
751 {"close", f_close},
751 {"setvbuf", f_setvbuf}, 752 {"setvbuf", f_setvbuf},
752 {"write", f_write}, 753 {NULL, NULL}
754};
755
756
757/*
758** metamethods for file handles
759*/
760static const luaL_Reg metameth[] = {
761 {"__index", NULL}, /* place holder */
753 {"__gc", f_gc}, 762 {"__gc", f_gc},
754 {"__close", f_gc}, 763 {"__close", f_gc},
755 {"__tostring", f_tostring}, 764 {"__tostring", f_tostring},
@@ -758,11 +767,12 @@ static const luaL_Reg flib[] = {
758 767
759 768
760static void createmeta (lua_State *L) { 769static void createmeta (lua_State *L) {
761 luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ 770 luaL_newmetatable(L, LUA_FILEHANDLE); /* metatable for file handles */
762 lua_pushvalue(L, -1); /* push metatable */ 771 luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
763 lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ 772 luaL_newlibtable(L, meth); /* create method table */
764 luaL_setfuncs(L, flib, 0); /* add file methods to new metatable */ 773 luaL_setfuncs(L, meth, 0); /* add file methods to method table */
765 lua_pop(L, 1); /* pop new metatable */ 774 lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
775 lua_pop(L, 1); /* pop metatable */
766} 776}
767 777
768 778