api/LuaModules
_G.module(modname, ...)
Creates a Lua Module.
If there is a table in package.loaded[name], this table is the module.
Otherwise, if there is a global table t with the given name, this table is the module.
Otherwise creates a new table t and sets it as the value of the global name and the value of package.loaded[name].
This function also initializes t._NAME with the given name, t._M with the module (t itself), and t._PACKAGE with the package name (the full module name minus last component).
Finally, module sets t as the new environment of the current function and the new value of package.loaded[name], so that require returns t.
This function can receive optional options after the module name, where each option is a function to be applied over the module
Parameters
- modname
- The package name. If name is a compound name (that is, one with components separated by dots), module creates (or reuses, if they already exist) tables for each component.
For instance, if name is a.b.c, then module stores the module table in field c of field b of global a
- ...
- usually starts with the pattern visibility package.seeall e.g module("foo", package.seeall)
_G.require(modname)
Loads the given module.
The function starts by looking into the package.loaded table to determine whether modname is already loaded.
If it is, then require returns the value stored at package.loaded[modname]. Otherwise, it tries to find a loader for the module.
To find a loader, require is guided by the package.loaders array. By changing this array, we can change how require looks for a module. The following explanation is based on the default configuration for package.loaders.
First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader.
Once a loader is found, require calls the loader with a single argument, modname. If the loader returns any value, require assigns the returned value to package.loaded[modname]. If the loader returns no value and has not assigned any value to package.loaded[modname], then require assigns true to this entry. In any case, require returns the final value of package.loaded[modname].
If there is any error loading or running the module, or if it cannot find any loader for the module, then require signals an error.
Parameters
- modname
- The package name.
Comments