Skip to content

Commit b621ada

Browse files
joyeecheungRafaelGSS
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 9bfd84c commit b621ada

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

‎lib/internal/bootstrap/switches/is_main_thread.js‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,19 @@ const{
6565

6666
// Map used to store CJS parsing data.
6767
constcjsParseCache=newSafeWeakMap();
68+
/**
69+
* Map of already-loaded CJS modules to use.
70+
*/
71+
constcjsExportsCache=newSafeWeakMap();
6872

6973
// Set first due to cycle with ESM loader functions.
7074
module.exports={
71-
wrapSafe, Module, cjsParseCache,
72-
gethasLoadedAnyUserCJSModule(){returnhasLoadedAnyUserCJSModule;},
75+
cjsExportsCache,
76+
cjsParseCache,
7377
initializeCJS,
78+
Module,
79+
wrapSafe,
80+
gethasLoadedAnyUserCJSModule(){returnhasLoadedAnyUserCJSModule;},
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
constisWindows=process.platform==='win32';
155161

156162
constrelativeResolveCache={__proto__: null};
@@ -1206,14 +1212,11 @@ Module.prototype.load = function(filename){
12061212
Module._extensions[extension](this,filename);
12071213
this.loaded=true;
12081214

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

‎lib/internal/modules/esm/loader.js‎

Lines changed: 0 additions & 6 deletions
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=newSafeWeakMap();
93-
9488
/**
9589
* The index for assigning unique URLs to anonymous module evaluation
9690
*/

‎lib/internal/modules/esm/translators.js‎

Lines changed: 4 additions & 3 deletions
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
letdebug=require('internal/util/debuglog').debuglog('esm',(fn)=>{
@@ -308,9 +309,9 @@ function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule){
308309
}
309310

310311
letexports;
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)