lua-users home
lua-l archive

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


On Tue, Sep 27, 2011 at 11:50 PM, Tim Caswell <tim@creationix.com> wrote:
> I'm sure this has been done before, but google is not being my friend when
> trying to search for it.
> I have a folder full of lua modules (written in lua) and I want them
> embedded in my binary at compile/link time in such a way that the require
> system can find them.
> Is there a cross-platform way to embed these scripts and make
> them accessible to require?
> I'm using luajit, but I think this question is generic.
> -Tim Caswell

I believe the general principle you want is this: For each module script...
1) Generate a header file that declares a constant byte array
containing the script data.
2) At a suitable point (i.e. after the Lua state has been opened & the
standard packages loaded, and before you run any other code), use
luaL_loadbuffer(L, buffer, sizeof(buffer)); to load from the byte
array.
3) Add the loaded function to the package.preload table. The table key
should be the name that require() will use to load the module.

-Duncan