From 83faa7e05d288dcb543109509e17de25110203ef Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 20:35:12 +0100 Subject: Simplify and clarify metatable creation functions. --- src/lfs.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lfs.c b/src/lfs.c index f33b5ae..e95dcab 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -534,19 +534,18 @@ static int dir_iter_factory (lua_State *L) { */ static int dir_create_meta (lua_State *L) { luaL_newmetatable (L, DIR_METATABLE); - /* set its __gc field */ - lua_pushstring (L, "__index"); + + /* Method table */ lua_newtable(L); - lua_pushstring (L, "next"); lua_pushcfunction (L, dir_iter); - lua_settable(L, -3); - lua_pushstring (L, "close"); + lua_setfield(L, -2, "next"); lua_pushcfunction (L, dir_close); - lua_settable(L, -3); - lua_settable (L, -3); - lua_pushstring (L, "__gc"); + lua_setfield(L, -2, "close"); + + /* Metamethods */ + lua_setfield(L, -2, "__index"); lua_pushcfunction (L, dir_close); - lua_settable (L, -3); + lua_setfield (L, -2, "__gc"); return 1; } @@ -555,10 +554,13 @@ static int dir_create_meta (lua_State *L) { */ static int lock_create_meta (lua_State *L) { luaL_newmetatable (L, LOCK_METATABLE); - /* set its __gc field */ + + /* Method table */ lua_newtable(L); lua_pushcfunction(L, lfs_unlock_dir); lua_setfield(L, -2, "free"); + + /* Metamethods */ lua_setfield(L, -2, "__index"); lua_pushcfunction(L, lfs_unlock_dir); lua_setfield(L, -2, "__gc"); -- cgit v1.2.3-55-g6feb