lua-users home
lua-l archive

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

Luís Santos wrote:
> I´m new to the list, but I have worked with Lua for about an Year
> now. I am using Lua in the building of an Application Development
> Framework for my company.  
> I am trying to extend the new 'modules' feature by allowing the
> 'require' function to load encrypted lua files. I haven´t found any
> clues on how to help the built-in module loading function to load my
> scripts by adding some sort of generic loader function or something
> of the sort, so I have decided to patch the source code to give it
> this capacity. However, I have felt a little unconfortable in doing
> so in a relatively new feature, in fear that I might be updated
> several times in the near future, giving me a little headache.       
> I would like to hear some opinions on this matter. Have I overlooked
> something in the modules documentation? 

AFAIK there is no documentation for that aspect of Lua 5.1 module system. I wrote an article for the upcoming Lua Programming Gems that will explain all that. But we can discuss that here since the book is far from being published.

You don't have to patch the Lua source code to add the functionnality you ask for. What you have to do is to add a loader in the table package.loaders. A loader is a function, which takes a string parameter, the module name, and that has three possible outputs:
- If the module is found and loaded properly, it returns a function, the module entry point (for Lua modules it's the module chunk, for C modules it's the luaopen_module function).
- If the module is not found, it returns a string containing the paths that have been examined (in the form "\n\tpath1\n\tpath2").
- If the module is found, but it can't be loaded (contains an error, don't match its signature, etc.), the loader can throw a Lua error.

You can look at the loader shipped with luatcc [1] to have an example. Feel free to ask if anything is not clear.

[1] (contrary to what the page mentions, latest version is 1.0.0).