Skip to content

src: restructure modules and bootstrappers #19177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const { openSync, closeSync, readSync } = require('fs');
const { parseExpressionAt } = require('internal/deps/acorn/dist/acorn');
const { inspect } = require('util');
const { EOL } = require('os');
const { NativeModule } = require('internal/bootstrap_loaders');
const { NativeModule } = require('internal/bootstrap/loaders');

// Escape control characters but not \n and \t to keep the line breaks and
// indentation intact.
Expand Down
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const {
// that test/parallel/test-buffer-bindingobj-no-zerofill.js is written.
let isAnyArrayBuffer;
try {
const { internalBinding } = require('internal/bootstrap_loaders');
const { internalBinding } = require('internal/bootstrap/loaders');
isAnyArrayBuffer = internalBinding('types').isAnyArrayBuffer;
} catch (e) {
isAnyArrayBuffer = require('util').types.isAnyArrayBuffer;
Expand Down
2 changes: 1 addition & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
ERR_UNHANDLED_ERROR
} = require('internal/errors').codes;
const { createHook } = require('async_hooks');
const { internalBinding } = require('internal/bootstrap_loaders');
const { internalBinding } = require('internal/bootstrap/loaders');

// overwrite process.domain with a getter/setter that will allow for more
// effective optimizations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// This file creates the internal module & binding loaders used by built-in
// modules. In contrast, user land modules are loaded using
// lib/module.js (CommonJS Modules) or lib/internal/loader/* (ES Modules).
// lib/internal/modules/cjs/loader.js (CommonJS Modules) or
// lib/internal/modules/esm/* (ES Modules).
//
// This file is compiled and run by node.cc before bootstrap_node.js
// This file is compiled and run by node.cc before bootstrap/node.js
// was called, therefore the loaders are bootstraped before we start to
// actually bootstrap Node.js. It creates the following objects:
//
Expand All @@ -29,7 +30,7 @@
// so they can be loaded faster without the cost of I/O. This class makes the
// lib/internal/*, deps/internal/* modules and internalBinding() available by
// default to core modules, and lets the core modules require itself via
// require('internal/bootstrap_loaders') even when this file is not written in
// require('internal/bootstrap/loaders') even when this file is not written in
// CommonJS style.
//
// Other objects:
Expand Down Expand Up @@ -111,7 +112,7 @@
// Think of this as module.exports in this file even though it is not
// written in CommonJS style.
const loaderExports = { internalBinding, NativeModule };
const loaderId = 'internal/bootstrap_loaders';
const loaderId = 'internal/bootstrap/loaders';
NativeModule.require = function(id) {
if (id === loaderId) {
return loaderExports;
Expand Down Expand Up @@ -224,6 +225,6 @@
};

// This will be passed to the bootstrapNodeJSCore function in
// bootstrap_node.js.
// bootstrap/node.js.
return loaderExports;
});
52 changes: 30 additions & 22 deletions lib/internal/bootstrap_node.js → lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// to the performance of the startup process, many dependencies are invoked
// lazily.
//
// Before this file is run, lib/internal/bootstrap_loaders.js gets run first
// Before this file is run, lib/internal/bootstrap/loaders.js gets run first
// to bootstrap the internal binding and module loaders, including
// process.binding(), process._linkedBinding(), internalBinding() and
// NativeModule. And then { internalBinding, NativeModule } will be passed
Expand Down Expand Up @@ -112,7 +112,7 @@
process.emitWarning(
'The ESM module loader is experimental.',
'ExperimentalWarning', undefined);
NativeModule.require('internal/process/modules').setup();
NativeModule.require('internal/process/esm_loader').setup();
}

{
Expand Down Expand Up @@ -194,22 +194,24 @@
preloadModules();
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);

const internalModule = NativeModule.require('internal/module');
internalModule.addBuiltinLibsToObject(global);
const {
addBuiltinLibsToObject
} = NativeModule.require('internal/modules/cjs/helpers');
addBuiltinLibsToObject(global);
evalScript('[eval]');
} else if (process.argv[1] && process.argv[1] !== '-') {
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_START);
// make process.argv[1] into a full path
const path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);

const Module = NativeModule.require('module');
const CJSModule = NativeModule.require('internal/modules/cjs/loader');

// check if user passed `-c` or `--check` arguments to Node.
if (process._syntax_check_only != null) {
const fs = NativeModule.require('fs');
// read the source
const filename = Module._resolveFilename(process.argv[1]);
const filename = CJSModule._resolveFilename(process.argv[1]);
const source = fs.readFileSync(filename, 'utf-8');
checkScriptSyntax(source, filename);
process.exit(0);
Expand All @@ -220,7 +222,7 @@
preloadModules();
perf.markMilestone(
NODE_PERFORMANCE_MILESTONE_PRELOAD_MODULE_LOAD_END);
Module.runMain();
CJSModule.runMain();
} else {
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_START);
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_MODULE_LOAD_END);
Expand Down Expand Up @@ -345,15 +347,15 @@

function setupGlobalConsole() {
const originalConsole = global.console;
const Module = NativeModule.require('module');
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
// Setup Node.js global.console
const wrappedConsole = NativeModule.require('console');
Object.defineProperty(global, 'console', {
configurable: true,
enumerable: false,
value: wrappedConsole
});
setupInspector(originalConsole, wrappedConsole, Module);
setupInspector(originalConsole, wrappedConsole, CJSModule);
}

function setupGlobalURL() {
Expand All @@ -374,19 +376,20 @@
});
}

function setupInspector(originalConsole, wrappedConsole, Module) {
function setupInspector(originalConsole, wrappedConsole, CJSModule) {
if (!process.config.variables.v8_enable_inspector) {
return;
}
const { addCommandLineAPI, consoleCall } = process.binding('inspector');
// Setup inspector command line API
const { makeRequireFunction } = NativeModule.require('internal/module');
const { makeRequireFunction } =
NativeModule.require('internal/modules/cjs/helpers');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);

const consoleAPIModule = new Module('<inspector console>');
const consoleAPIModule = new CJSModule('<inspector console>');
consoleAPIModule.paths =
Module._nodeModulePaths(cwd).concat(Module.globalPaths);
CJSModule._nodeModulePaths(cwd).concat(CJSModule.globalPaths);
addCommandLineAPI('require', makeRequireFunction(consoleAPIModule));
const config = {};
for (const key of Object.keys(wrappedConsole)) {
Expand Down Expand Up @@ -515,13 +518,13 @@
}

function evalScript(name) {
const Module = NativeModule.require('module');
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);

const module = new Module(name);
const module = new CJSModule(name);
module.filename = path.join(cwd, name);
module.paths = Module._nodeModulePaths(cwd);
module.paths = CJSModule._nodeModulePaths(cwd);
const body = wrapForBreakOnFirstLine(process._eval);
const script = `global.__filename = ${JSON.stringify(name)};\n` +
'global.exports = exports;\n' +
Expand All @@ -540,21 +543,26 @@
// Load preload modules
function preloadModules() {
if (process._preload_modules) {
NativeModule.require('module')._preloadModules(process._preload_modules);
const {
_preloadModules
} = NativeModule.require('internal/modules/cjs/loader');
_preloadModules(process._preload_modules);
}
}

function checkScriptSyntax(source, filename) {
const Module = NativeModule.require('module');
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const vm = NativeModule.require('vm');
const internalModule = NativeModule.require('internal/module');
const {
stripShebang, stripBOM
} = NativeModule.require('internal/modules/cjs/helpers');

// remove Shebang
source = internalModule.stripShebang(source);
source = stripShebang(source);
// remove BOM
source = internalModule.stripBOM(source);
source = stripBOM(source);
// wrap it
source = Module.wrap(source);
source = CJSModule.wrap(source);
// compile the script, this will throw if it fails
new vm.Script(source, { displayErrors: true, filename });
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const {

const { isArrayBufferView } = require('internal/util/types');

const { internalBinding } = require('internal/bootstrap_loaders');
const { internalBinding } = require('internal/bootstrap/loaders');
const {
isArrayBuffer
} = internalBinding('types');
Expand Down
File renamed without changes.
Loading