|
4 | 4 | ArrayIsArray, |
5 | 5 | SafeSet, |
6 | 6 | SafeWeakMap, |
| 7 | + Symbol, |
7 | 8 | ObjectFreeze, |
8 | 9 | } = primordials; |
9 | 10 |
|
@@ -157,6 +158,26 @@ function registerModule(referrer, registry) { |
157 | 158 | moduleRegistries.set(idSymbol, registry); |
158 | 159 | } |
159 | 160 |
|
| 161 | +/** |
| 162 | + * Registers the ModuleRegistry for dynamic import() calls with a realm |
| 163 | + * as the referrer. Similar to {@link registerModule}, but this function |
| 164 | + * generates a new id symbol instead of using the one from the referrer |
| 165 | + * object. |
| 166 | + * @param {globalThis} globalThis The globalThis object of the realm. |
| 167 | + * @param {ModuleRegistry} registry |
| 168 | + */ |
| 169 | +function registerRealm(globalThis, registry) { |
| 170 | + let idSymbol = globalThis[host_defined_option_symbol]; |
| 171 | + // If the per-realm host-defined options is already registered, do nothing. |
| 172 | + if (idSymbol) { |
| 173 | + return; |
| 174 | + } |
| 175 | + // Otherwise, register the per-realm host-defined options. |
| 176 | + idSymbol = Symbol('Realm globalThis'); |
| 177 | + globalThis[host_defined_option_symbol] = idSymbol; |
| 178 | + moduleRegistries.set(idSymbol, registry); |
| 179 | +} |
| 180 | + |
160 | 181 | /** |
161 | 182 | * Defines the `import.meta` object for a given module. |
162 | 183 | * @param {symbol} symbol - Reference to the module. |
@@ -192,28 +213,29 @@ async function importModuleDynamicallyCallback(referrerSymbol, specifier, attrib |
192 | 213 | throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING(); |
193 | 214 | } |
194 | 215 |
|
195 | | -let _isLoaderWorker = false; |
| 216 | +let _forceDefaultLoader = false; |
196 | 217 | /** |
197 | 218 | * Initializes handling of ES modules. |
198 | 219 | * This is configured during pre-execution. Specifically it's set to true for |
199 | 220 | * the loader worker in internal/main/worker_thread.js. |
200 | | - * @param {boolean} [isLoaderWorker=false] - A boolean indicating whether the loader is a worker or not. |
| 221 | + * @param {boolean} [forceDefaultLoader=false] - A boolean indicating disabling custom loaders. |
201 | 222 | */ |
202 | | -function initializeESM(isLoaderWorker = false) { |
203 | | - _isLoaderWorker = isLoaderWorker; |
| 223 | +function initializeESM(forceDefaultLoader = false) { |
| 224 | + _forceDefaultLoader = forceDefaultLoader; |
204 | 225 | initializeDefaultConditions(); |
205 | | - // Setup per-isolate callbacks that locate data or callbacks that we keep |
| 226 | + // Setup per-realm callbacks that locate data or callbacks that we keep |
206 | 227 | // track of for different ESM modules. |
207 | 228 | setInitializeImportMetaObjectCallback(initializeImportMetaObject); |
208 | 229 | setImportModuleDynamicallyCallback(importModuleDynamicallyCallback); |
209 | 230 | } |
210 | 231 |
|
211 | 232 | /** |
212 | | - * Determine whether the current process is a loader worker. |
213 | | - * @returns {boolean} Whether the current process is a loader worker. |
| 233 | + * Determine whether custom loaders are disabled and it is forced to use the |
| 234 | + * default loader. |
| 235 | + * @returns {boolean} |
214 | 236 | */ |
215 | | -function isLoaderWorker() { |
216 | | - return _isLoaderWorker; |
| 237 | +function forceDefaultLoader() { |
| 238 | + return _forceDefaultLoader; |
217 | 239 | } |
218 | 240 |
|
219 | 241 | /** |
@@ -251,10 +273,11 @@ async function initializeHooks() { |
251 | 273 |
|
252 | 274 | module.exports = { |
253 | 275 | registerModule, |
| 276 | + registerRealm, |
254 | 277 | initializeESM, |
255 | 278 | initializeHooks, |
256 | 279 | getDefaultConditions, |
257 | 280 | getConditionsSet, |
258 | 281 | loaderWorkerId: 'internal/modules/esm/worker', |
259 | | - isLoaderWorker, |
| 282 | + forceDefaultLoader, |
260 | 283 | }; |
0 commit comments