[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Overriding the default 'require' in libLua (offer)
- From: Sean Conner <sean@...>
- Date: Wed, 4 Jan 2012 12:56:17 -0500
It was thus said that the Great Paul Bergen once stated:
> We have our own application, written in C, that allows users to embed
> snippets of Lua which our C program then executes at various times.
> Rather than writing all the Lua code directly inline, our users want
> to be able to have common modules with shared code that they can
> include with a 'require'. If they put this shared code in a file,
> like mycode.lua, and put the file in the same folder as our C program,
> it works fine. However, we have our own user interface and don't want
> to put these stray files in our program directory. Rather, we will
> compress them and bundle them in our own data files.
>
> So this means the 'require' in Lua will error out and won't find the
> file. So when we create the Lua state in our C app, we want to
> override the built-in 'require' to instead point to our own C
> function. In our C function, it should be identical to the stock Lua
> require, except that if the fopen() command fails, we want to have our
> own code which retrieves the contents of the files ourself, from our
> own data store. We won't be parsing the contents or doing anything
> with them. Just putting them in a (char *) buffer and handing it back
> to the stock Lua require function to process the same as if it had
> opened the file itself with fopen().
>
> Anybody know how to do this? And if anybody cares to write the C code
> to do this, I'll be happy to paypal/wire some money for it. I'm not
> too familiar with libLua and under time pressure.
The easiest way appears to be to add a custom loader to the
package.loaders array. The function just takes a string, the name of the
module to load, and it returns a function that will load the appropriate
module (if I'm reading the documentation right).
For anyone that might be interested in writing the code, which version of
Lua, and for what platform?
-spc