-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
require: Add partial implementation of module search algorithm
Add a partial implementation of the Node.js module search algorithm described here: https://nodejs.org/api/modules.html#modules_all_together The functions LOAD_AS_FILE, LOAD_INDEX, LOAD_AS_DIRECTORY, LOAD_NODE_MODULES and NODE_MODULES_PATHS outlined in the pseudocode linked above are implemented, whereas the functionality outlined in LOAD_SELF_REFERENCE and LOAD_PACKAGE_EXPORTS is missing. The module resolution algorithm is implemented via a new (*RequireModule).resolve(path string) method which returns the resolved path to the file that can be loaded using the Registry's SourceLoader. The returned resolved path is always cleaned via filepathClean. The resolve method uses the Registry's SourceLoader via the new (*Registry).getSource(path string) method to search for modules and supports resolving both native ("core") and JavaScript modules. Add new resolveStart string field to RequireModule. resolveStart is used to store the start path for the module search algorithm in the resolve method. When require() is called from the global context, resolveStart should be set to the directory of the currently executing script. This information is stored in r.runtime.Program.src.name but is not currently exported, therefore for now resolveStart is set to ".". During a nested require() call, resolveStart is set to the directory of the module currently being loaded. Add new constructor NewRegistry which takes a variadic argument of Option's. Currently, valid options are WithLoader and WithGlobalFolders. Add new globalFolders string slice to Registry. globalFolders stores additional folders that are searched by require() and is used in NODE_MODULES_PATHS in the pseudocode linked above. By default, a Registry has an empty globalFolders slice, but this can be changed using WithGlobalFolders. Add support for loading modules with a .json extension by passing the contents of the file through JavaScript's JSON.parse and assigning the return value to module.exports within the module wrapper function. Add new test TestResolve to test module search algorithm. Fixes #5.
- Loading branch information
Niels Widger
committed
Jul 14, 2020
1 parent
b2775b8
commit fdf3794
Showing
3 changed files
with
364 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.