Skip to content

Commit d8da197

Browse files
joyeecheungrichardlau
authored andcommitted
module: move the CJS exports cache to internal/modules/cjs/loader
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. PR-URL: #51157 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8ac05cf commit d8da197

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

lib/internal/bootstrap/switches/is_main_thread.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ rawMethods.resetStdioForTesting = function() {
291291
require('fs');
292292
require('util');
293293
require('url'); // eslint-disable-line no-restricted-modules
294-
294+
internalBinding('module_wrap');
295295
require('internal/modules/cjs/loader');
296296
require('internal/modules/esm/utils');
297297
require('internal/vm/module');
298+
298299
// Needed to refresh the time origin.
299300
require('internal/perf/utils');
300301
// Needed to register the async hooks.

lib/internal/modules/cjs/loader.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ const {
6565

6666
// Map used to store CJS parsing data.
6767
const cjsParseCache = new SafeWeakMap();
68+
/**
69+
* Map of already-loaded CJS modules to use.
70+
*/
71+
const cjsExportsCache = new SafeWeakMap();
6872

6973
// Set first due to cycle with ESM loader functions.
7074
module.exports = {
71-
wrapSafe, Module, cjsParseCache,
72-
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
75+
cjsExportsCache,
76+
cjsParseCache,
7377
initializeCJS,
78+
Module,
79+
wrapSafe,
80+
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
7481
};
7582

7683
const { BuiltinModule } = require('internal/bootstrap/realm');
@@ -150,7 +157,6 @@ const {
150157
isProxy,
151158
} = require('internal/util/types');
152159

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

156162
const relativeResolveCache = { __proto__: null };
@@ -1207,14 +1213,11 @@ Module.prototype.load = function(filename) {
12071213
Module._extensions[extension](this, filename);
12081214
this.loaded = true;
12091215

1210-
const cascadedLoader = getCascadedLoader();
12111216
// Create module entry at load time to snapshot exports correctly
12121217
const exports = this.exports;
1213-
// Preemptively cache
1214-
if ((module?.module === undefined ||
1215-
module.module.getStatus() < kEvaluated) &&
1216-
!cascadedLoader.cjsCache.has(this)) {
1217-
cascadedLoader.cjsCache.set(this, exports);
1218+
// Preemptively cache for ESM loader.
1219+
if (!cjsExportsCache.has(this)) {
1220+
cjsExportsCache.set(this, exports);
12181221
}
12191222
};
12201223

lib/internal/modules/esm/loader.js

-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
JSONStringify,
1212
ObjectSetPrototypeOf,
1313
RegExpPrototypeSymbolReplace,
14-
SafeWeakMap,
1514
encodeURIComponent,
1615
hardenRegExp,
1716
} = primordials;
@@ -86,11 +85,6 @@ class ModuleLoader {
8685
*/
8786
#defaultConditions = getDefaultConditions();
8887

89-
/**
90-
* Map of already-loaded CJS modules to use
91-
*/
92-
cjsCache = new SafeWeakMap();
93-
9488
/**
9589
* The index for assigning unique URLs to anonymous module evaluation
9690
*/

lib/internal/modules/esm/translators.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
const {
4343
Module: CJSModule,
4444
cjsParseCache,
45+
cjsExportsCache,
4546
} = require('internal/modules/cjs/loader');
4647
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
4748
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
@@ -308,9 +309,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
308309
}
309310

310311
let exports;
311-
if (asyncESM.esmLoader.cjsCache.has(module)) {
312-
exports = asyncESM.esmLoader.cjsCache.get(module);
313-
asyncESM.esmLoader.cjsCache.delete(module);
312+
if (cjsExportsCache.has(module)) {
313+
exports = cjsExportsCache.get(module);
314+
cjsExportsCache.delete(module);
314315
} else {
315316
({ exports } = module);
316317
}

0 commit comments

Comments
 (0)