lua-users home
lua-l archive

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


It was thus said that the Great Henderson, Michael D once stated:
> In my luaopen_xxx function, I'm using
> 
>   const struct luaL_Reg myLib[] = { . . . }l
>   lua_createtable(L,0,(sizeof(myLib)/sizeof(const struct luaL_Reg))-1);
>   luaL_register(L,0,myLib);
>   return 1;
> 
> That puts my functions in the module that I'm working on. I'd like to add strings to the table and am not sure how to proceed.
> 
> To add a value to a table, I have been
> 
>   lua_newtable(L);
>   lua_pushstring(L,"version");
>   lua_pushstring(L,"0.1");
>   lua_settable(L,-3);
> 
> Does this play well with lua_createtable? Could I just do this?
> 
>   const struct luaL_Reg myLib[] = { . . . }l
>   lua_createtable(L,0,(sizeof(myLib)/sizeof(const struct luaL_Reg))-1);
>   luaL_register(L,0,myLib);
> 
>   lua_pushstring(L,"version");
>   lua_pushstring(L,"0.1");
>   lua_settable(L,-3);
> 
>   return 1;

  Yes.  Even this works (from one of my current projects [1]):

static const struct luaL_reg reg_dns[] =
{
  { "encode"    , dnslua_encode         } ,
  { "decode"    , dnslua_decode         } ,
  { "strerror"  , dnslua_strerror       } ,
  { "query"     , dnslua_query          } ,
  { NULL        , NULL                  }
};

int luaopen_org_conman_dns(lua_State *L)
{
  luaL_register(L,"org.conman.dns",reg_dns);

  lua_pushliteral(L,"Copyright 2010 by Sean Conner.  All Rights Reserved.");
  lua_setfield(L,-2,"COPYRIGHT");

  lua_pushliteral(L,"Encode/Decode and send queries via DNS");
  lua_setfield(L,-2,"DESCRIPTION");

  lua_pushliteral(L,"0.0.1");
  lua_setfield(L,-2,"VERSION");

  return 1;
}

  -spc (You can, of course, supply NULL for the table name in luaL_register
	and it'll still work)

[1]	http://www.conman.org/software/spcdns/