aboutsummaryrefslogtreecommitdiff
path: root/manual
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-17 14:57:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-04-17 14:57:29 -0300
commited2872cd3bf6d352f36bbd34529738a60b0b51eb (patch)
treea4476cca4a43deeb8eaa0d52f0f02b15acc0db09 /manual
parent2d3f09544895b422eeecf89e0d108da8b8fcdfca (diff)
downloadlua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.tar.gz
lua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.tar.bz2
lua-ed2872cd3bf6d352f36bbd34529738a60b0b51eb.zip
'require' returns where module was found
The function 'require' returns the *loader data* as a second result. For file searchers, this data is the path where they found the module.
Diffstat (limited to 'manual')
-rw-r--r--manual/manual.of38
1 files changed, 27 insertions, 11 deletions
diff --git a/manual/manual.of b/manual/manual.of
index fea6922e..24ac45ae 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -6408,11 +6408,15 @@ The function starts by looking into the @Lid{package.loaded} table
6408to determine whether @id{modname} is already loaded. 6408to determine whether @id{modname} is already loaded.
6409If it is, then @id{require} returns the value stored 6409If it is, then @id{require} returns the value stored
6410at @T{package.loaded[modname]}. 6410at @T{package.loaded[modname]}.
6411(The absence of a second result in this case
6412signals that this call did not have to load the module.)
6411Otherwise, it tries to find a @emph{loader} for the module. 6413Otherwise, it tries to find a @emph{loader} for the module.
6412 6414
6413To find a loader, 6415To find a loader,
6414@id{require} is guided by the @Lid{package.searchers} sequence. 6416@id{require} is guided by the table @Lid{package.searchers}.
6415By changing this sequence, 6417Each item in this table is a search function,
6418that searches for the module in a particular way.
6419By changing this table,
6416we can change how @id{require} looks for a module. 6420we can change how @id{require} looks for a module.
6417The following explanation is based on the default configuration 6421The following explanation is based on the default configuration
6418for @Lid{package.searchers}. 6422for @Lid{package.searchers}.
@@ -6429,9 +6433,14 @@ it tries an @emph{all-in-one} loader @seeF{package.searchers}.
6429 6433
6430Once a loader is found, 6434Once a loader is found,
6431@id{require} calls the loader with two arguments: 6435@id{require} calls the loader with two arguments:
6432@id{modname} and an extra value dependent on how it got the loader. 6436@id{modname} and an extra value,
6433(If the loader came from a file, 6437a @emph{loader data},
6434this extra value is the file name.) 6438also returned by the searcher.
6439The loader data can be any value useful to the module;
6440for the default searchers,
6441it indicates where the loader was found.
6442(For instance, if the loader came from a file,
6443this extra value is the file path.)
6435If the loader returns any non-nil value, 6444If the loader returns any non-nil value,
6436@id{require} assigns the returned value to @T{package.loaded[modname]}. 6445@id{require} assigns the returned value to @T{package.loaded[modname]}.
6437If the loader does not return a non-nil value and 6446If the loader does not return a non-nil value and
@@ -6439,6 +6448,9 @@ has not assigned any value to @T{package.loaded[modname]},
6439then @id{require} assigns @Rw{true} to this entry. 6448then @id{require} assigns @Rw{true} to this entry.
6440In any case, @id{require} returns the 6449In any case, @id{require} returns the
6441final value of @T{package.loaded[modname]}. 6450final value of @T{package.loaded[modname]}.
6451Besides that value, @id{require} also returns as a second result
6452the loader data returned by the searcher,
6453which indicates how @id{require} found the module.
6442 6454
6443If there is any error loading or running the module, 6455If there is any error loading or running the module,
6444or if it cannot find any loader for the module, 6456or if it cannot find any loader for the module,
@@ -6558,16 +6570,20 @@ table used by @Lid{require}.
6558 6570
6559@LibEntry{package.searchers| 6571@LibEntry{package.searchers|
6560 6572
6561A table used by @Lid{require} to control how to load modules. 6573A table used by @Lid{require} to control how to find modules.
6562 6574
6563Each entry in this table is a @def{searcher function}. 6575Each entry in this table is a @def{searcher function}.
6564When looking for a module, 6576When looking for a module,
6565@Lid{require} calls each of these searchers in ascending order, 6577@Lid{require} calls each of these searchers in ascending order,
6566with the module name (the argument given to @Lid{require}) as its 6578with the module name (the argument given to @Lid{require}) as its
6567sole argument. 6579sole argument.
6568The function can return another function (the module @def{loader}) 6580If the searcher finds the module,
6569plus an extra value that will be passed to that loader, 6581it returns another function, the module @def{loader},
6570or a string explaining why it did not find that module 6582plus an extra value, a @emph{loader data},
6583that will be passed to that loader and
6584returned as a second result by @Lid{require}.
6585If it cannot find the module,
6586it returns a string explaining why
6571(or @nil if it has nothing to say). 6587(or @nil if it has nothing to say).
6572 6588
6573Lua initializes this table with four searcher functions. 6589Lua initializes this table with four searcher functions.
@@ -6617,9 +6633,9 @@ into one single library,
6617with each submodule keeping its original open function. 6633with each submodule keeping its original open function.
6618 6634
6619All searchers except the first one (preload) return as the extra value 6635All searchers except the first one (preload) return as the extra value
6620the file name where the module was found, 6636the file path where the module was found,
6621as returned by @Lid{package.searchpath}. 6637as returned by @Lid{package.searchpath}.
6622The first searcher returns no extra value. 6638The first searcher always returns the string @St{:preload:}.
6623 6639
6624} 6640}
6625 6641