Skip to content

Commit 939c876

Browse files
joyeecheungtargos
authored andcommitted
module: move callbacks and conditions into modules/esm/utils.js
This moves the following utils into modules/esm/utils.js: - Code related to default conditions - The callbackMap (which is now created in the module instead of hanging off the module_wrap binding, since the C++ land does not need it). - Per-isolate module callbacks These are self-contained code that can be included into the built-in snapshot. PR-URL: #45849 Backport-PR-URL: #46425 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent c6b2f56 commit 939c876

File tree

10 files changed

+25
-91
lines changed

10 files changed

+25
-91
lines changed

lib/internal/modules/esm/create_dynamic_module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ${ArrayPrototypeJoin(ArrayPrototypeMap(imports, createImport), '\n')}
3535
${ArrayPrototypeJoin(ArrayPrototypeMap(exports, createExport), '\n')}
3636
import.meta.done();
3737
`;
38-
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
38+
const { ModuleWrap } = internalBinding('module_wrap');
3939
const m = new ModuleWrap(`${url}`, undefined, source, 0, 0);
4040

4141
const readyfns = new SafeSet();
@@ -46,8 +46,8 @@ import.meta.done();
4646

4747
if (imports.length)
4848
reflect.imports = ObjectCreate(null);
49-
50-
callbackMap.set(m, {
49+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
50+
setCallbackForWrap(m, {
5151
initializeImportMeta: (meta, wrap) => {
5252
meta.exports = reflect.exports;
5353
if (reflect.imports)

lib/internal/modules/esm/loader.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ function newModuleMap() {
4747

4848
const {
4949
defaultResolve,
50-
DEFAULT_CONDITIONS,
5150
} = require('internal/modules/esm/resolve');
5251

52+
const {
53+
getDefaultConditions,
54+
} = require('internal/modules/esm/utils');
55+
5356
function getTranslators() {
5457
const { translators } = require('internal/modules/esm/translators');
5558
return translators;
@@ -375,9 +378,10 @@ class ESMLoader {
375378
url = pathToFileURL(`${process.cwd()}/[eval${++this.evalIndex}]`).href,
376379
) {
377380
const evalInstance = (url) => {
378-
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
381+
const { ModuleWrap } = internalBinding('module_wrap');
382+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
379383
const module = new ModuleWrap(url, undefined, source, 0, 0);
380-
callbackMap.set(module, {
384+
setCallbackForWrap(module, {
381385
importModuleDynamically: (specifier, { url }, importAssertions) => {
382386
return this.import(specifier, url, importAssertions);
383387
},
@@ -802,7 +806,7 @@ class ESMLoader {
802806
}
803807
const chain = this.#hooks.resolve;
804808
const context = {
805-
conditions: DEFAULT_CONDITIONS,
809+
conditions: getDefaultConditions(),
806810
importAssertions,
807811
parentURL,
808812
};

lib/internal/modules/esm/resolve.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
ArrayPrototypeShift,
77
JSONParse,
88
JSONStringify,
9-
ObjectFreeze,
109
ObjectGetOwnPropertyNames,
1110
ObjectPrototypeHasOwnProperty,
1211
RegExp,
@@ -46,7 +45,6 @@ const typeFlag = getOptionValue('--input-type');
4645
const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
4746
const {
4847
ERR_INPUT_TYPE_NOT_ALLOWED,
49-
ERR_INVALID_ARG_VALUE,
5048
ERR_INVALID_MODULE_SPECIFIER,
5149
ERR_INVALID_PACKAGE_CONFIG,
5250
ERR_INVALID_PACKAGE_TARGET,
@@ -61,25 +59,13 @@ const {
6159
const { Module: CJSModule } = require('internal/modules/cjs/loader');
6260
const packageJsonReader = require('internal/modules/package_json_reader');
6361
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
62+
const { getConditionsSet } = require('internal/modules/esm/utils');
6463

6564
/**
6665
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
6766
*/
6867

6968

70-
const userConditions = getOptionValue('--conditions');
71-
const noAddons = getOptionValue('--no-addons');
72-
const addonConditions = noAddons ? [] : ['node-addons'];
73-
74-
const DEFAULT_CONDITIONS = ObjectFreeze([
75-
'node',
76-
'import',
77-
...addonConditions,
78-
...userConditions,
79-
]);
80-
81-
const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);
82-
8369
const emittedPackageWarnings = new SafeSet();
8470

8571
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
@@ -150,21 +136,6 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
150136
}
151137
}
152138

153-
/**
154-
* @param {string[]} [conditions]
155-
* @returns {Set<string>}
156-
*/
157-
function getConditionsSet(conditions) {
158-
if (conditions !== undefined && conditions !== DEFAULT_CONDITIONS) {
159-
if (!ArrayIsArray(conditions)) {
160-
throw new ERR_INVALID_ARG_VALUE('conditions', conditions,
161-
'expected an array');
162-
}
163-
return new SafeSet(conditions);
164-
}
165-
return DEFAULT_CONDITIONS_SET;
166-
}
167-
168139
const realpathCache = new SafeMap();
169140

170141
/**
@@ -1167,7 +1138,6 @@ async function defaultResolve(specifier, context = {}) {
11671138
}
11681139

11691140
module.exports = {
1170-
DEFAULT_CONDITIONS,
11711141
defaultResolve,
11721142
encodedSepRegEx,
11731143
getPackageScopeConfig,

lib/internal/modules/esm/translators.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ translators.set('module', async function moduleStrategy(url, source, isMain) {
115115
maybeCacheSourceMap(url, source);
116116
debug(`Translating StandardModule ${url}`);
117117
const module = new ModuleWrap(url, undefined, source, 0, 0);
118-
moduleWrap.callbackMap.set(module, {
118+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
119+
setCallbackForWrap(module, {
119120
initializeImportMeta: (meta, wrap) => this.importMetaInitialize(meta, { url }),
120121
importModuleDynamically,
121122
});

lib/internal/process/esm_loader.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,11 @@ const {
55
ObjectCreate,
66
} = primordials;
77

8-
const {
9-
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
10-
} = require('internal/errors').codes;
118
const { ESMLoader } = require('internal/modules/esm/loader');
129
const {
1310
hasUncaughtExceptionCaptureCallback,
1411
} = require('internal/process/execution');
1512
const { pathToFileURL } = require('internal/url');
16-
const {
17-
getModuleFromWrap,
18-
} = require('internal/vm/module');
19-
20-
exports.initializeImportMetaObject = function(wrap, meta) {
21-
const { callbackMap } = internalBinding('module_wrap');
22-
if (callbackMap.has(wrap)) {
23-
const { initializeImportMeta } = callbackMap.get(wrap);
24-
if (initializeImportMeta !== undefined) {
25-
initializeImportMeta(meta, getModuleFromWrap(wrap) || wrap);
26-
}
27-
}
28-
};
29-
30-
exports.importModuleDynamicallyCallback =
31-
async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
32-
const { callbackMap } = internalBinding('module_wrap');
33-
if (callbackMap.has(wrap)) {
34-
const { importModuleDynamically } = callbackMap.get(wrap);
35-
if (importModuleDynamically !== undefined) {
36-
return importModuleDynamically(
37-
specifier, getModuleFromWrap(wrap) || wrap, assertions);
38-
}
39-
}
40-
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
41-
};
4213

4314
const esmLoader = new ESMLoader();
4415
exports.esmLoader = esmLoader;

lib/internal/process/pre_execution.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
ObjectDefineProperties,
66
ObjectDefineProperty,
77
SafeMap,
8-
SafeWeakMap,
98
StringPrototypeStartsWith,
109
Symbol,
1110
SymbolDispose,
@@ -551,20 +550,10 @@ function initializeCJSLoader() {
551550
}
552551

553552
function initializeESMLoader() {
554-
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
555-
internalBinding('module_wrap').callbackMap = new SafeWeakMap();
556-
557553
if (getEmbedderOptions().shouldNotRegisterESMLoader) return;
558554

559-
const {
560-
setImportModuleDynamicallyCallback,
561-
setInitializeImportMetaObjectCallback,
562-
} = internalBinding('module_wrap');
563-
const esm = require('internal/process/esm_loader');
564-
// Setup per-isolate callbacks that locate data or callbacks that we keep
565-
// track of for different ESM modules.
566-
setInitializeImportMetaObjectCallback(esm.initializeImportMetaObject);
567-
setImportModuleDynamicallyCallback(esm.importModuleDynamicallyCallback);
555+
const { initializeESM } = require('internal/modules/esm/utils');
556+
initializeESM();
568557

569558
// Patch the vm module when --experimental-vm-modules is on.
570559
// Please update the comments in vm.js when this block changes.

lib/internal/vm.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ function internalCompileFunction(code, params, options) {
9797
if (importModuleDynamically !== undefined) {
9898
validateFunction(importModuleDynamically,
9999
'options.importModuleDynamically');
100-
const { importModuleDynamicallyWrap } =
101-
require('internal/vm/module');
102-
const { callbackMap } = internalBinding('module_wrap');
100+
const { importModuleDynamicallyWrap } = require('internal/vm/module');
103101
const wrapped = importModuleDynamicallyWrap(importModuleDynamically);
104102
const func = result.function;
105-
callbackMap.set(result.cacheKey, {
103+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
104+
setCallbackForWrap(result.cacheKey, {
106105
importModuleDynamically: (s, _k, i) => wrapped(s, func, i),
107106
});
108107
}

lib/internal/vm/module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class Module {
125125
this[kWrap] = new ModuleWrap(identifier, context, sourceText,
126126
options.lineOffset, options.columnOffset,
127127
options.cachedData);
128-
129-
binding.callbackMap.set(this[kWrap], {
128+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
129+
setCallbackForWrap(this[kWrap], {
130130
initializeImportMeta: options.initializeImportMeta,
131131
importModuleDynamically: options.importModuleDynamically ?
132132
importModuleDynamicallyWrap(options.importModuleDynamically) :

lib/vm.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ class Script extends ContextifyScript {
105105
if (importModuleDynamically !== undefined) {
106106
validateFunction(importModuleDynamically,
107107
'options.importModuleDynamically');
108-
const { importModuleDynamicallyWrap } =
109-
require('internal/vm/module');
110-
const { callbackMap } = internalBinding('module_wrap');
111-
callbackMap.set(this, {
108+
const { importModuleDynamicallyWrap } = require('internal/vm/module');
109+
const { setCallbackForWrap } = require('internal/modules/esm/utils');
110+
setCallbackForWrap(this, {
112111
importModuleDynamically:
113112
importModuleDynamicallyWrap(importModuleDynamically),
114113
});

test/parallel/test-bootstrap-modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const expectedModules = new Set([
6363
'NativeModule internal/modules/esm/package_config',
6464
'NativeModule internal/modules/esm/resolve',
6565
'NativeModule internal/modules/esm/translators',
66+
'NativeModule internal/modules/esm/utils',
6667
'NativeModule internal/modules/package_json_reader',
6768
'NativeModule internal/modules/run_main',
6869
'NativeModule internal/net',

0 commit comments

Comments
 (0)