lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Dirk Laurie
> Sent: vrijdag 14 februari 2014 15:34
> To: Lua mailing list
> Subject: Re: protect a set in c (non-existing elements)
> 
> 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;
> }

Exactly what I was looking for!  I was indeed wondering how pass parameters to the Lua code when using dostring. Nice workaround using the global 'X'.

Btw is it possible to pass parameters to lua code loaded with luaL_loadstring?

Thijs