-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modules: 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 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
- Loading branch information
1 parent
f6c6673
commit 0b3512f
Showing
11 changed files
with
130 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
const { | ||
ArrayIsArray, | ||
SafeSet, | ||
SafeWeakMap, | ||
ObjectFreeze, | ||
} = primordials; | ||
|
||
const { | ||
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING, | ||
ERR_INVALID_ARG_VALUE, | ||
} = require('internal/errors').codes; | ||
|
||
const { getOptionValue } = require('internal/options'); | ||
|
||
const { | ||
setImportModuleDynamicallyCallback, | ||
setInitializeImportMetaObjectCallback | ||
} = internalBinding('module_wrap'); | ||
const { | ||
getModuleFromWrap, | ||
} = require('internal/vm/module'); | ||
const assert = require('internal/assert'); | ||
|
||
const callbackMap = new SafeWeakMap(); | ||
function setCallbackForWrap(wrap, data) { | ||
callbackMap.set(wrap, data); | ||
} | ||
|
||
let defaultConditions; | ||
function getDefaultConditions() { | ||
assert(defaultConditions !== undefined); | ||
return defaultConditions; | ||
} | ||
|
||
let defaultConditionsSet; | ||
function getDefaultConditionsSet() { | ||
assert(defaultConditionsSet !== undefined); | ||
return defaultConditionsSet; | ||
} | ||
|
||
// This function is called during pre-execution, before any user code is run. | ||
function initializeDefaultConditions() { | ||
const userConditions = getOptionValue('--conditions'); | ||
const noAddons = getOptionValue('--no-addons'); | ||
const addonConditions = noAddons ? [] : ['node-addons']; | ||
|
||
defaultConditions = ObjectFreeze([ | ||
'node', | ||
'import', | ||
...addonConditions, | ||
...userConditions, | ||
]); | ||
defaultConditionsSet = new SafeSet(defaultConditions); | ||
} | ||
|
||
/** | ||
* @param {string[]} [conditions] | ||
* @returns {Set<string>} | ||
*/ | ||
function getConditionsSet(conditions) { | ||
if (conditions !== undefined && conditions !== getDefaultConditions()) { | ||
if (!ArrayIsArray(conditions)) { | ||
throw new ERR_INVALID_ARG_VALUE('conditions', conditions, | ||
'expected an array'); | ||
} | ||
return new SafeSet(conditions); | ||
} | ||
return getDefaultConditionsSet(); | ||
} | ||
|
||
function initializeImportMetaObject(wrap, meta) { | ||
if (callbackMap.has(wrap)) { | ||
const { initializeImportMeta } = callbackMap.get(wrap); | ||
if (initializeImportMeta !== undefined) { | ||
initializeImportMeta(meta, getModuleFromWrap(wrap) || wrap); | ||
} | ||
} | ||
} | ||
|
||
async function importModuleDynamicallyCallback(wrap, specifier, assertions) { | ||
if (callbackMap.has(wrap)) { | ||
const { importModuleDynamically } = callbackMap.get(wrap); | ||
if (importModuleDynamically !== undefined) { | ||
return importModuleDynamically( | ||
specifier, getModuleFromWrap(wrap) || wrap, assertions); | ||
} | ||
} | ||
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING(); | ||
} | ||
|
||
function initializeESM() { | ||
initializeDefaultConditions(); | ||
// Setup per-isolate callbacks that locate data or callbacks that we keep | ||
// track of for different ESM modules. | ||
setInitializeImportMetaObjectCallback(initializeImportMetaObject); | ||
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback); | ||
} | ||
|
||
module.exports = { | ||
setCallbackForWrap, | ||
initializeESM, | ||
getDefaultConditions, | ||
getConditionsSet, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters