Expand description
CommonJS module loader.
Node’s require() semantics, layered on the existing engine — no bespoke VM
primitive. A .js file is wrapped in the canonical Node module wrapper
(function (exports, require, module, __dirname, __filename) { … }), compiled
through the ordinary compile → load_merged path to obtain the wrapper
FUNCTION value, then host::invoked with a fresh module = { exports: {} }.
Whatever the body assigns to module.exports (or hangs off exports) is the
module’s value; it is cached by resolved absolute path so a second require
of the same file returns the identical object and circular requires observe
the partially-filled exports.
Core modules (fs, path, http, …) short-circuit to their native
JsObj::Builtin namespace (see stdlib::resolve) and are never read from
disk. Everything else — relative paths, JSON files, and bare node_modules
packages with their package.json "exports"/"main" and index.js
fallbacks — resolves on the real filesystem and runs the genuine, unmodified
source.
Per-module require is a real JS closure that bakes in the defining module’s
directory, so a require(...) deferred inside a function called much later
still resolves against the module that defined it (a single global
“current dir” would resolve against the wrong module). The closure is minted
by a one-time compiled factory (FACTORY) invoked with the directory string;
it dispatches back into this loader through the __cjs_require /
__cjs_resolve global native builtins.
Functions§
- cache_
delete - Drop
keyfrom the module cache, so the nextrequireof that file runs it again. This is whatdelete require.cache[id]must do to mean anything. - cache_
get - The module object cached under the resolved filename
key, if any. - cache_
keys - The resolved filenames of every currently loaded module, in load order —
the keys
require.cacheexposes. - callsite_
stack - An array of
depthsynthetic V8 CallSite objects forError.captureStackTrace. Stack-introspection packages (e.g.depd) setError.prepareStackTraceto a function that receives this array; the getters return neutral placeholders (no real frame info is available), which is enough for those packages to build their deprecation sites without throwing. - entry_
dir - The ENTRY script’s base directory.
- install_
entry_ globals - Install the CJS wrapper variables the ENTRY script sees.
- require
require(spec)fromfrom_dir: the single entry point shared by the top-levelrequirebuiltin and the per-module__cjs_require. Returns the module’s exports value.- reset
- Clear all per-host loader state. Called from
host::reset_hostso a fresh eval (which rebuilds the heap) never reuses a stale heap handle. - resolve
- Resolve
specrelative tofrom_dirto an absolute file path, orNoneif no file matches (core modules are handled earlier, by the caller). - set_
entry_ dir - Set the base directory the ENTRY script’s
requireresolves against.