lua-users home
lua-l archive

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


2007/5/17, Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
2007/5/17, Norman Ramsey <nr@eecs.harvard.edu>:
> Looking at the source, it does seem that if I want to provide some
> sort of precompiled or preloaded modules, I could simply write my own
> specialized package loader and then do
>
>   table.insert(package.loaders, 1, myloader)
>
> where 'myloader' returns a function on successful load, string on
> error, and nil otherwise.  Then everything would be great, right?

Mostly right. A returned string does not mean "error", it is just an
explanation of "why" it could not load the module. If no loader
is able to load the module, then all these explanations together
are presented as the error message.

And additionnaly, if you want to mimic the stock Lua module loaders,
on error you should throw a Lua error (with error() from Lua or
lua_error() from C). As said Roberto, the absence of a module is not
an error. However, if a module is found but cannot be loaded for
whatever reason, then you can consider it is an error.

So to sum up a loader:
- return a function on success
- return a string if module is not found
- throws a Lua error otherwise