Split the `internal/process/esm_loader` file which contains the singleton cascaded loader: - The the singleton cascaded loader now directly resides in `internal/modules/esm/loader`, where the constructor also lives. This file is the root of most circular dependency of ESM code, (because components of the loader need the singleton itself), so this makes the dependency more obvious. Added comments about loading it lazily to avoid circular dependency. - The getter to the cascaded loader is also turned into a method to make the side effect explicit. - The sequence of `loadESM()` and `handleMainPromise` is now merged together into `runEntryPointWithESMLoader()` in `internal/modules/run_main` because this is intended to run entry points with the ESM loader and not just any module. - Documents how top-level await is handled. PR-URL: https://github.com/nodejs/node/pull/51999 Fixes: https://github.com/nodejs/node/issues/42868 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
// User passed `-e` or `--eval` arguments to Node without `-i` or
|
|
// `--interactive`.
|
|
|
|
const {
|
|
ObjectDefineProperty,
|
|
RegExpPrototypeExec,
|
|
globalThis,
|
|
} = primordials;
|
|
|
|
const {
|
|
prepareMainThreadExecution,
|
|
markBootstrapComplete,
|
|
} = require('internal/process/pre_execution');
|
|
const { evalModuleEntryPoint, evalScript } = require('internal/process/execution');
|
|
const { addBuiltinLibsToObject } = require('internal/modules/helpers');
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
prepareMainThreadExecution();
|
|
addBuiltinLibsToObject(globalThis, '<eval>');
|
|
markBootstrapComplete();
|
|
|
|
const source = getOptionValue('--eval');
|
|
const print = getOptionValue('--print');
|
|
const shouldLoadESM = getOptionValue('--import').length > 0 || getOptionValue('--experimental-loader').length > 0;
|
|
if (getOptionValue('--input-type') === 'module' ||
|
|
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) {
|
|
evalModuleEntryPoint(source, print);
|
|
} else {
|
|
// For backward compatibility, we want the identifier crypto to be the
|
|
// `node:crypto` module rather than WebCrypto.
|
|
const isUsingCryptoIdentifier =
|
|
getOptionValue('--experimental-global-webcrypto') &&
|
|
RegExpPrototypeExec(/\bcrypto\b/, source) !== null;
|
|
const shouldDefineCrypto = isUsingCryptoIdentifier && internalBinding('config').hasOpenSSL;
|
|
|
|
if (isUsingCryptoIdentifier && !shouldDefineCrypto) {
|
|
// This is taken from `addBuiltinLibsToObject`.
|
|
const object = globalThis;
|
|
const name = 'crypto';
|
|
const setReal = (val) => {
|
|
// Deleting the property before re-assigning it disables the
|
|
// getter/setter mechanism.
|
|
delete object[name];
|
|
object[name] = val;
|
|
};
|
|
ObjectDefineProperty(object, name, { __proto__: null, set: setReal });
|
|
}
|
|
evalScript('[eval]',
|
|
shouldDefineCrypto ? (
|
|
print ? `let crypto=require("node:crypto");{${source}}` : `(crypto=>{{${source}}})(require('node:crypto'))`
|
|
) : source,
|
|
getOptionValue('--inspect-brk'),
|
|
print,
|
|
shouldLoadESM);
|
|
}
|