diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-17 14:57:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-17 14:57:29 -0300 |
commit | ed2872cd3bf6d352f36bbd34529738a60b0b51eb (patch) | |
tree | a4476cca4a43deeb8eaa0d52f0f02b15acc0db09 /manual | |
parent | 2d3f09544895b422eeecf89e0d108da8b8fcdfca (diff) | |
download | lua-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.of | 38 |
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 | |||
6408 | to determine whether @id{modname} is already loaded. | 6408 | to determine whether @id{modname} is already loaded. |
6409 | If it is, then @id{require} returns the value stored | 6409 | If it is, then @id{require} returns the value stored |
6410 | at @T{package.loaded[modname]}. | 6410 | at @T{package.loaded[modname]}. |
6411 | (The absence of a second result in this case | ||
6412 | signals that this call did not have to load the module.) | ||
6411 | Otherwise, it tries to find a @emph{loader} for the module. | 6413 | Otherwise, it tries to find a @emph{loader} for the module. |
6412 | 6414 | ||
6413 | To find a loader, | 6415 | To 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}. |
6415 | By changing this sequence, | 6417 | Each item in this table is a search function, |
6418 | that searches for the module in a particular way. | ||
6419 | By changing this table, | ||
6416 | we can change how @id{require} looks for a module. | 6420 | we can change how @id{require} looks for a module. |
6417 | The following explanation is based on the default configuration | 6421 | The following explanation is based on the default configuration |
6418 | for @Lid{package.searchers}. | 6422 | for @Lid{package.searchers}. |
@@ -6429,9 +6433,14 @@ it tries an @emph{all-in-one} loader @seeF{package.searchers}. | |||
6429 | 6433 | ||
6430 | Once a loader is found, | 6434 | Once 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, | 6437 | a @emph{loader data}, |
6434 | this extra value is the file name.) | 6438 | also returned by the searcher. |
6439 | The loader data can be any value useful to the module; | ||
6440 | for the default searchers, | ||
6441 | it indicates where the loader was found. | ||
6442 | (For instance, if the loader came from a file, | ||
6443 | this extra value is the file path.) | ||
6435 | If the loader returns any non-nil value, | 6444 | If 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]}. |
6437 | If the loader does not return a non-nil value and | 6446 | If the loader does not return a non-nil value and |
@@ -6439,6 +6448,9 @@ has not assigned any value to @T{package.loaded[modname]}, | |||
6439 | then @id{require} assigns @Rw{true} to this entry. | 6448 | then @id{require} assigns @Rw{true} to this entry. |
6440 | In any case, @id{require} returns the | 6449 | In any case, @id{require} returns the |
6441 | final value of @T{package.loaded[modname]}. | 6450 | final value of @T{package.loaded[modname]}. |
6451 | Besides that value, @id{require} also returns as a second result | ||
6452 | the loader data returned by the searcher, | ||
6453 | which indicates how @id{require} found the module. | ||
6442 | 6454 | ||
6443 | If there is any error loading or running the module, | 6455 | If there is any error loading or running the module, |
6444 | or if it cannot find any loader for the module, | 6456 | or 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 | ||
6561 | A table used by @Lid{require} to control how to load modules. | 6573 | A table used by @Lid{require} to control how to find modules. |
6562 | 6574 | ||
6563 | Each entry in this table is a @def{searcher function}. | 6575 | Each entry in this table is a @def{searcher function}. |
6564 | When looking for a module, | 6576 | When 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, |
6566 | with the module name (the argument given to @Lid{require}) as its | 6578 | with the module name (the argument given to @Lid{require}) as its |
6567 | sole argument. | 6579 | sole argument. |
6568 | The function can return another function (the module @def{loader}) | 6580 | If the searcher finds the module, |
6569 | plus an extra value that will be passed to that loader, | 6581 | it returns another function, the module @def{loader}, |
6570 | or a string explaining why it did not find that module | 6582 | plus an extra value, a @emph{loader data}, |
6583 | that will be passed to that loader and | ||
6584 | returned as a second result by @Lid{require}. | ||
6585 | If it cannot find the module, | ||
6586 | it 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 | ||
6573 | Lua initializes this table with four searcher functions. | 6589 | Lua initializes this table with four searcher functions. |
@@ -6617,9 +6633,9 @@ into one single library, | |||
6617 | with each submodule keeping its original open function. | 6633 | with each submodule keeping its original open function. |
6618 | 6634 | ||
6619 | All searchers except the first one (preload) return as the extra value | 6635 | All searchers except the first one (preload) return as the extra value |
6620 | the file name where the module was found, | 6636 | the file path where the module was found, |
6621 | as returned by @Lid{package.searchpath}. | 6637 | as returned by @Lid{package.searchpath}. |
6622 | The first searcher returns no extra value. | 6638 | The first searcher always returns the string @St{:preload:}. |
6623 | 6639 | ||
6624 | } | 6640 | } |
6625 | 6641 | ||