Skip to content

Commit

Permalink
module: move the CJS exports cache to internal/modules/cjs/loader
Browse files Browse the repository at this point in the history
This puts it together with the cjsParseCache and reduces the
circular dependency on the singleton loader, which is the
only place where this cache is stored.

Drive-by: remove always-false module status check because there's
no longer a local module variable after
#34605 which is now invalid
leftover code at this point and only doesn't throw because
we happen to have a top-level variable called module.
  • Loading branch information
joyeecheung committed Dec 15, 2023
1 parent 62ca050 commit 3d01dbb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,11 @@ rawMethods.resetStdioForTesting = function() {
require('fs');
require('util');
require('url'); // eslint-disable-line no-restricted-modules

internalBinding('module_wrap');
require('internal/modules/cjs/loader');
require('internal/modules/esm/utils');
require('internal/vm/module');

// Needed to refresh the time origin.
require('internal/perf/utils');
// Needed to register the async hooks.
Expand Down
21 changes: 12 additions & 9 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ const {

// Map used to store CJS parsing data.
const cjsParseCache = new SafeWeakMap();
/**
* Map of already-loaded CJS modules to use.
*/
const cjsExportsCache = new SafeWeakMap();

// Set first due to cycle with ESM loader functions.
module.exports = {
wrapSafe, Module, cjsParseCache,
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
cjsExportsCache,
cjsParseCache,
initializeCJS,
Module,
wrapSafe,
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
};

const { BuiltinModule } = require('internal/bootstrap/realm');
Expand Down Expand Up @@ -150,7 +157,6 @@ const {
isProxy,
} = require('internal/util/types');

const { kEvaluated } = internalBinding('module_wrap');
const isWindows = process.platform === 'win32';

const relativeResolveCache = { __proto__: null };
Expand Down Expand Up @@ -1206,14 +1212,11 @@ Module.prototype.load = function(filename) {
Module._extensions[extension](this, filename);
this.loaded = true;

const cascadedLoader = getCascadedLoader();
// Create module entry at load time to snapshot exports correctly
const exports = this.exports;
// Preemptively cache
if ((module?.module === undefined ||
module.module.getStatus() < kEvaluated) &&
!cascadedLoader.cjsCache.has(this)) {
cascadedLoader.cjsCache.set(this, exports);
// Preemptively cache for ESM loader.
if (!cjsExportsCache.has(this)) {
cjsExportsCache.set(this, exports);
}
};

Expand Down
6 changes: 0 additions & 6 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const {
JSONStringify,
ObjectSetPrototypeOf,
RegExpPrototypeSymbolReplace,
SafeWeakMap,
encodeURIComponent,
hardenRegExp,
} = primordials;
Expand Down Expand Up @@ -86,11 +85,6 @@ class ModuleLoader {
*/
#defaultConditions = getDefaultConditions();

/**
* Map of already-loaded CJS modules to use
*/
cjsCache = new SafeWeakMap();

/**
* The index for assigning unique URLs to anonymous module evaluation
*/
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
const {
Module: CJSModule,
cjsParseCache,
cjsExportsCache,
} = require('internal/modules/cjs/loader');
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
Expand Down Expand Up @@ -308,9 +309,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
}

let exports;
if (asyncESM.esmLoader.cjsCache.has(module)) {
exports = asyncESM.esmLoader.cjsCache.get(module);
asyncESM.esmLoader.cjsCache.delete(module);
if (cjsExportsCache.has(module)) {
exports = cjsExportsCache.get(module);
cjsExportsCache.delete(module);
} else {
({ exports } = module);
}
Expand Down

0 comments on commit 3d01dbb

Please sign in to comment.