Skip to content

Commit

Permalink
rename the function init, run it from main/ files
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Apr 18, 2023
1 parent 94797cd commit ffbdec4
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 53 deletions.
3 changes: 2 additions & 1 deletion lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function loadESMIfNeeded(cb) {
const hasModulePreImport = getOptionValue('--import').length > 0;

if (hasModulePreImport) {
const { loadESM } = require('internal/process/esm_loader');
const { loadESM, init } = require('internal/process/esm_loader');
init();
loadESM(cb);
return;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/internal/main/eval_stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ const {
prepareMainThreadExecution();
markBootstrapComplete();


readStdin((code) => {
// This is necessary for fork() and CJS module compilation.
// TODO(joyeecheung): pass this with something really internal.
process._eval = code;

const print = getOptionValue('--print');
const loadESM = getOptionValue('--import').length > 0;
if (getOptionValue('--input-type') === 'module')
if (getOptionValue('--input-type') === 'module') {
require('internal/process/esm_loader').init();
evalModule(code, print);
else
} else {
evalScript('[stdin]',
code,
getOptionValue('--inspect-brk'),
print,
loadESM);
require('internal/process/esm_loader').initIfNeeded();
}
});
7 changes: 5 additions & 2 deletions lib/internal/main/eval_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ markBootstrapComplete();
const source = getOptionValue('--eval');
const print = getOptionValue('--print');
const loadESM = getOptionValue('--import').length > 0;
if (getOptionValue('--input-type') === 'module')
const esmLoader = require('internal/process/esm_loader');
if (getOptionValue('--input-type') === 'module') {
esmLoader.init();
evalModule(source, print);
else {
} else {
// For backward compatibility, we want the identifier crypto to be the
// `node:crypto` module rather than WebCrypto.
const isUsingCryptoIdentifier =
Expand All @@ -54,4 +56,5 @@ else {
getOptionValue('--inspect-brk'),
print,
loadESM);
esmLoader.initIfNeeded();
}
1 change: 1 addition & 0 deletions lib/internal/main/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ prepareMainThreadExecution();


markBootstrapComplete();
require('internal/process/esm_loader').init();

// Start the debugger agent.
process.nextTick(() => {
Expand Down
1 change: 1 addition & 0 deletions lib/internal/main/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if (process.env.NODE_REPL_EXTERNAL_MODULE) {
}

const esmLoader = require('internal/process/esm_loader');
esmLoader.init();
esmLoader.loadESM(() => {
console.log(`Welcome to Node.js ${process.version}.\n` +
'Type ".help" for more information.');
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/main/run_main_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ prepareMainThreadExecution(true);

markBootstrapComplete();

require('internal/process/esm_loader').init();

// Necessary to reset RegExp statics before user code runs.
RegExpPrototypeExec(/^/, '');

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/main/test_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ if (isUsingInspector()) {
inspectPort = process.debugPort;
}

require('internal/process/esm_loader').init();

run({ concurrency, inspectPort, watch: getOptionValue('--watch'), setup: setupTestReporters })
.once('test:fail', () => {
process.exitCode = kGenericUserError;
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ port.on('message', (message) => {
});
ArrayPrototypeSplice(process.argv, 1, 0, name);
evalScript(name, filename);
require('internal/process/esm_loader').initIfNeeded();
break;
}

case 'module': {
const { evalModule } = require('internal/process/execution');
require('internal/process/esm_loader').init();
PromisePrototypeThen(evalModule(filename), undefined, (e) => {
workerOnGlobalUncaughtException(e, true);
});
Expand All @@ -179,6 +181,7 @@ port.on('message', (message) => {
// XXX: the monkey-patchability here should probably be deprecated.
ArrayPrototypeSplice(process.argv, 1, 0, filename);
const CJSLoader = require('internal/modules/cjs/loader');
require('internal/process/esm_loader').initIfNeeded();
CJSLoader.Module.runMain(filename);
break;
}
Expand Down
35 changes: 6 additions & 29 deletions lib/internal/modules/esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

const {
ArrayIsArray,
ArrayPrototypePush,
PromisePrototypeThen,
ReflectApply,
SafeMap,
SafeSet,
SafeWeakMap,
ObjectFreeze,
Expand All @@ -17,7 +14,7 @@ const {
} = require('internal/errors').codes;
const { getOptionValue } = require('internal/options');
const { pathToFileURL } = require('internal/url');
const { kEmptyObject, createDeferredPromise } = require('internal/util');
const { kEmptyObject } = require('internal/util');
const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback,
Expand Down Expand Up @@ -95,14 +92,6 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) {
}

function initializeESM() {
const { setESMLoader } = require('internal/process/esm_loader');
// Minimal mock for letting `--require` work before we have any actual ESM loader.
setESMLoader({ __proto__: null, cjsCache: new SafeMap(), import() {
const { promise, resolve, reject } = createDeferredPromise();
ArrayPrototypePush(this.importRequests, { arguments: arguments, resolve, reject });
return promise;
}, importRequests: [] });

initializeDefaultConditions();
// Setup per-isolate callbacks that locate data or callbacks that we keep
// track of for different ESM modules.
Expand All @@ -126,22 +115,10 @@ async function initializeHooks() {
const hooks = new Hooks();

const { DefaultModuleLoader } = require('internal/modules/esm/loader');
const { esmLoader, setESMLoader } = require('internal/process/esm_loader');
const { init } = require('internal/process/esm_loader');
class ModuleLoader extends DefaultModuleLoader {
constructor() {
super();
for (const { 0: key, 1: value } of esmLoader.cjsCache) {
// Getting back the values from the mocked loader.
this.cjsCache.set(key, value);
}
for (let i = 0; i < esmLoader.importRequests.length; i++) {
PromisePrototypeThen(
ReflectApply(this.import, this, esmLoader.importRequests[i].arguments),
esmLoader.importRequests[i].resolve,
esmLoader.importRequests[i].reject,
);
}
}
// eslint-disable-next-line no-useless-constructor
constructor() { super(); }

loaderType = 'internal';
async #getModuleJob(specifier, parentURL, importAssertions) {
Expand All @@ -166,8 +143,6 @@ async function initializeHooks() {
}
const privateModuleLoader = new ModuleLoader();

setESMLoader(privateModuleLoader);

const parentURL = pathToFileURL(cwd).href;

for (let i = 0; i < customLoaderPaths.length; i++) {
Expand All @@ -185,6 +160,8 @@ async function initializeHooks() {

const preloadScripts = hooks.initializeGlobalPreload();

init(privateModuleLoader);

return { __proto__: null, hooks, preloadScripts };
}

Expand Down
5 changes: 1 addition & 4 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ function shouldUseESMLoader(mainPath) {
}

function runMainESM(mainPath) {
const { loadESM, setESMLoader } = require('internal/process/esm_loader');
const { createModuleLoader } = require('internal/modules/esm/loader');
const { loadESM } = require('internal/process/esm_loader');
const { pathToFileURL } = require('internal/url');

setESMLoader(createModuleLoader(true));

handleMainPromise(loadESM((esmLoader) => {
const main = path.isAbsolute(mainPath) ?
pathToFileURL(mainPath).href : mainPath;
Expand Down
48 changes: 44 additions & 4 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
'use strict';

const {
ArrayPrototypePush,
PromisePrototypeThen,
ReflectApply,
SafeMap,
} = primordials;

const assert = require('internal/assert');
const { createModuleLoader } = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, createDeferredPromise } = require('internal/util');

let esmLoader;
let init = false;

module.exports = {
esmLoader: undefined,
setESMLoader(loader) {
module.exports.esmLoader = loader;
get esmLoader() {
return esmLoader ??= { __proto__: null, cjsCache: new SafeMap(), import() {
const { promise, resolve, reject } = createDeferredPromise();
ArrayPrototypePush(this.importRequests, { arguments: arguments, resolve, reject });
return promise;
}, importRequests: [] };
},
initIfNeeded() {
// TODO: we could try to avoid loading ESM loader on CJS-only codebase
return module.exports.init();
},
init(loader = undefined) {
assert(!init);
init = true;

loader ??= createModuleLoader(true);

if (esmLoader != null) {
for (const { 0: key, 1: value } of esmLoader.cjsCache) {
// Getting back the values from the mocked loader.
loader.cjsCache.set(key, value);
}
for (let i = 0; i < esmLoader.importRequests.length; i++) {
PromisePrototypeThen(
ReflectApply(loader.import, loader, esmLoader.importRequests[i].arguments),
esmLoader.importRequests[i].resolve,
esmLoader.importRequests[i].reject,
);
}
}
esmLoader = loader;
},
async loadESM(callback) {
const { esmLoader } = module.exports;
Expand Down
13 changes: 3 additions & 10 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ function evalModule(source, print) {
if (print) {
throw new ERR_EVAL_ESM_CANNOT_PRINT();
}
const { loadESM, setESMLoader } = require('internal/process/esm_loader');
const { createModuleLoader } = require('internal/modules/esm/loader');
const { loadESM } = require('internal/process/esm_loader');
const { handleMainPromise } = require('internal/modules/run_main');

setESMLoader(createModuleLoader(true));

RegExpPrototypeExec(/^/, ''); // Necessary to reset RegExp statics before user code runs.
return handleMainPromise(loadESM((loader) => loader.eval(source)));
}
Expand All @@ -67,8 +63,10 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
module.filename = path.join(cwd, name);
module.paths = CJSModule._nodeModulePaths(cwd);

const { handleMainPromise } = require('internal/modules/run_main');
const asyncESM = require('internal/process/esm_loader');
const baseUrl = pathToFileURL(module.filename).href;
const { loadESM } = asyncESM;

const runScript = () => {
// Create wrapper for cache entry
Expand Down Expand Up @@ -101,11 +99,6 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
};

if (shouldLoadESM) {
const { loadESM, setESMLoader } = asyncESM;
const { createModuleLoader } = require('internal/modules/esm/loader');
const { handleMainPromise } = require('internal/modules/run_main');

setESMLoader(createModuleLoader(true));
return handleMainPromise(loadESM(runScript));
}
return runScript();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ function prepareExecution(options) {
}

function setupUserModules() {
initializeCJSLoader();
initializeESMLoader();
initializeCJSLoader();
const CJSLoader = require('internal/modules/cjs/loader');
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
loadPreloadModules();
Expand Down

0 comments on commit ffbdec4

Please sign in to comment.