diff options
| -rw-r--r-- | liolib.c | 30 |
1 files changed, 20 insertions, 10 deletions
| @@ -742,14 +742,23 @@ static const luaL_Reg iolib[] = { | |||
| 742 | /* | 742 | /* |
| 743 | ** methods for file handles | 743 | ** methods for file handles |
| 744 | */ | 744 | */ |
| 745 | static const luaL_Reg flib[] = { | 745 | static 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 | */ | ||
| 760 | static 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 | ||
| 760 | static void createmeta (lua_State *L) { | 769 | static 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 | ||
