lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


... sorry, I forgot the title in last post ... here exact copy again
with title...

Hi, I am just "fighting" with designing some new libs for Lua.

I found, that liolib.c from Lua source is a very instructive lib for
showing how to install the meta stuff, as it even includes the io lib
together with a further metatype LUA_FILEHANDLE.

The function table for the file handles is strangly separated into
"new fuctions" and "standard meta functions":

/*
** methods for file handles
*/
static const luaL_Reg meth[] = {
  {"read", f_read},
  {"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},
  {"__close", f_gc},
  {"__tostring", f_tostring},
  {NULL, NULL}
};

In the function create meta, these two tables are then somehow
combined as I see it:

  luaL_setfuncs(L, metameth, 0);  /* add metamethods to new metatable */
  luaL_newlibtable(L, meth);  /* create method table */
  luaL_setfuncs(L, meth, 0);  /* add file methods to method table */

Why not use just ONE table for all meta methods, as e. g. the following table:

static const luaL_Reg meth[] = {
  {"__index", NULL},  /* place holder */
  {"__gc", f_gc},
  {"__close", f_gc},
  {"__tostring", f_tostring},
 {"read", f_read},
  {"write", f_write},
  {"lines", f_lines},
  {"flush", f_flush},
  {"seek", f_seek},
  {"close", f_close},
  {"setvbuf", f_setvbuf},
  {NULL, NULL}
};

Does this "separation to two tables" make any sense / has any advantage?