lua-users home
lua-l archive

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


Looking at the new package system for Lua 5.1 (work 4) i was thinking
about a simple feature that could be added to the core system.

The ability to register module loaders, enabling a user to load lua
scripts from specific locations or even compressed files (along with
many other uses).

For example:

local function load_from_zip(name)
 -- find zip and file to load
 -- retorn nil if not found
 -- read file, throw an error if failed to read from file
 return loadstring(...)
end

package.register_loader(load_from_zip)

Two loaders will be available by default (for loading binary modules
and lua scripts).

The functions can be saved in package.loaders (registry._LOADERS)
preserving the order used to register them.

The loaders don't have to be directly related with the type of module
to load (lua scripts or (binary) shared modules), that would be
transparent to the user. Loader functions only receive the module name
as a parameter.

Regarding configuration, that would be a seperate issue. For instance,
lua scripts use package.path for possible locations on where to find
the scripts; other loaders could use similar configurations, or no
configuration needed at all.

This would allow a developer to distribute a program and provide a
module loader to read the lua scripts from a specific location (such
as a compressed file or even proprietary file format), without
changing the lua core.

One possible location to initialize available loaders would be
LUA_INIT environment var (for programs that use the lua shell).

This change could provide some level of flexibility to the module
system and would require no change to the lua sources to adapt the
program to load scripts from unusual locations.

I don't know if this was discussed previously, but i think it makes
sense to make this modification.

Regards,
Tiago

PS: attached file contains modifications to the Lua 5.1 (work 4)
version to support module loaders, without affecting existing
behaviour of the package system.

Attachment: package_loader.diff
Description: Binary data