[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: protect a set in c (non-existing elements)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 14 Feb 2014 16:33:52 +0200
2014-02-14 15:46 GMT+02:00 Thijs Schreijer <thijs@thijsschreijer.nl>:
> Obviously you're right. But this is simple in Lua and a bit cumbersome in c.
> So my question was more on how others solved it; possibly including
> a lua file in the module that does it, or embedding lua code in c
> (tried to find an example, but couldn't find it)
Here is an example, from the xtable module.
/* startup Lua code
Develop this as a separate Lua file, taking care to avoid strings
delimited by double-quotes. When it works, put double-quote at the
start and whitespace,double-quote at the end of every line and paste
it in here.
*/
char *xtable_init =
"print "
"'Welcome to the xtable library! The following functions are available:' "
"local contents={} "
"for k in pairs(X._block) do contents[#contents+1]='_block.'..k end "
"for k in pairs(X._tuple) do contents[#contents+1]='_tuple.'..k end "
"table.sort(contents); print(table.concat(contents,' ')) "
"X=nil"
;
LUAMOD_API int luaopen_xtable (lua_State *L) {
lua_createtable(L,8,0);
luaL_newlib(L, block_funcs);
lua_setfield(L,-2,"_block");
luaL_newlib(L, tuple_funcs);
lua_setfield(L,-2,"_tuple");
lua_pushvalue(L,-1); lua_setglobal(L,"X");
luaL_dostring(L,xtable_init);
return 1;
}