summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-11-27 21:36:22 +0800
committerRich Trott <rtrott@gmail.com>2018-11-29 12:55:56 -0800
commit7b8058a39e3485f7c6aeab87e9953f30c9974afd (patch)
tree249461ba38fefbc78d69c48fa31e566a6b4c415a /lib
parent5dacbf594ef80f8eadea274f537cc17cc1e5ebe1 (diff)
downloadandroid-node-v8-7b8058a39e3485f7c6aeab87e9953f30c9974afd.tar.gz
android-node-v8-7b8058a39e3485f7c6aeab87e9953f30c9974afd.tar.bz2
android-node-v8-7b8058a39e3485f7c6aeab87e9953f30c9974afd.zip
process: refactor the bootstrap mode branching for readability
This patch refactors the branches for choosing the mode to run Node.js in `internal/bootstrap/node.js`. Instead of inlining the decision making all in `startup`, we create a `startExecution()` function which either detects and start the non-user-code mode, or prepares for user code execution (worker setup, preloading modules) and starts user code execution. We use early returns when we decide the mode to run Node.js in for fewer indentations and better readability. This patch also adds a few comments about the command-line switches and a few TODOs to remove underscore properties on `process` that are mainly used for bootstrap mode branching. It also includes a few other refactoring such as inlining functions/variables that are not reused and removing the default argument of `evalScript` for better clarity. PR-URL: https://github.com/nodejs/node/pull/24673 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/bootstrap/node.js263
1 files changed, 151 insertions, 112 deletions
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index a207dd648f..673a3f1b1d 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -104,18 +104,13 @@
}
const { getOptionValue } = NativeModule.require('internal/options');
- const helpOption = getOptionValue('--help');
- const completionBashOption = getOptionValue('--completion-bash');
- const experimentalModulesOption = getOptionValue('--experimental-modules');
- const experimentalVMModulesOption =
- getOptionValue('--experimental-vm-modules');
- const experimentalWorkerOption = getOptionValue('--experimental-worker');
- if (helpOption) {
+
+ if (getOptionValue('--help')) {
NativeModule.require('internal/print_help').print(process.stdout);
return;
}
- if (completionBashOption) {
+ if (getOptionValue('--completion-bash')) {
NativeModule.require('internal/bash_completion').print(process.stdout);
return;
}
@@ -135,7 +130,7 @@
setupQueueMicrotask();
}
- if (experimentalWorkerOption) {
+ if (getOptionValue('--experimental-worker')) {
setupDOMException();
}
@@ -167,8 +162,10 @@
'DeprecationWarning', 'DEP0062', startup, true);
}
- if (experimentalModulesOption || experimentalVMModulesOption) {
- if (experimentalModulesOption) {
+ const experimentalModules = getOptionValue('--experimental-modules');
+ const experimentalVMModules = getOptionValue('--experimental-vm-modules');
+ if (experimentalModules || experimentalVMModules) {
+ if (experimentalModules) {
process.emitWarning(
'The ESM module loader is experimental.',
'ExperimentalWarning', undefined);
@@ -228,22 +225,33 @@
setupAllowedFlags();
- // There are various modes that Node can run in. The most common two
- // are running from a script and running the REPL - but there are a few
- // others like the debugger or running --eval arguments. Here we decide
- // which mode we run in.
+ startExecution();
+ }
+
+ // There are various modes that Node can run in. The most common two
+ // are running from a script and running the REPL - but there are a few
+ // others like the debugger or running --eval arguments. Here we decide
+ // which mode we run in.
+ function startExecution() {
+ // This means we are in a Worker context, and any script execution
+ // will be directed by the worker module.
if (internalBinding('worker').getEnvMessagePort() !== undefined) {
- // This means we are in a Worker context, and any script execution
- // will be directed by the worker module.
NativeModule.require('internal/worker').setupChild(evalScript);
- } else if (NativeModule.exists('_third_party_main')) {
- // To allow people to extend Node in different ways, this hook allows
- // one to drop a file lib/_third_party_main.js into the build
- // directory which will be executed instead of Node's normal loading.
+ return;
+ }
+
+ // To allow people to extend Node in different ways, this hook allows
+ // one to drop a file lib/_third_party_main.js into the build
+ // directory which will be executed instead of Node's normal loading.
+ if (NativeModule.exists('_third_party_main')) {
process.nextTick(() => {
NativeModule.require('_third_party_main');
});
- } else if (process.argv[1] === 'inspect' || process.argv[1] === 'debug') {
+ return;
+ }
+
+ // `node inspect ...` or `node debug ...`
+ if (process.argv[1] === 'inspect' || process.argv[1] === 'debug') {
if (process.argv[1] === 'debug') {
process.emitWarning(
'`node debug` is deprecated. Please use `node inspect` instead.',
@@ -254,95 +262,136 @@
process.nextTick(() => {
NativeModule.require('internal/deps/node-inspect/lib/_inspect').start();
});
+ return;
+ }
- } else if (process.profProcess) {
+ // `node --prof-process`
+ // TODO(joyeecheung): use internal/options instead of process.profProcess
+ if (process.profProcess) {
NativeModule.require('internal/v8_prof_processor');
- } else {
- // There is user code to be run.
-
- // If this is a worker in cluster mode, start up the communication
- // channel. This needs to be done before any user code gets executed
- // (including preload modules).
- if (process.argv[1] && process.env.NODE_UNIQUE_ID) {
- const cluster = NativeModule.require('cluster');
- cluster._setupWorker();
- // Make sure it's not accidentally inherited by child processes.
- delete process.env.NODE_UNIQUE_ID;
+ return;
+ }
+
+ // There is user code to be run.
+ prepareUserCodeExecution();
+ executeUserCode();
+ }
+
+ function prepareUserCodeExecution() {
+ // If this is a worker in cluster mode, start up the communication
+ // channel. This needs to be done before any user code gets executed
+ // (including preload modules).
+ if (process.argv[1] && process.env.NODE_UNIQUE_ID) {
+ const cluster = NativeModule.require('cluster');
+ cluster._setupWorker();
+ // Make sure it's not accidentally inherited by child processes.
+ delete process.env.NODE_UNIQUE_ID;
+ }
+
+ // For user code, we preload modules if `-r` is passed
+ // TODO(joyeecheung): use internal/options instead of
+ // process._preload_modules
+ if (process._preload_modules) {
+ const {
+ _preloadModules
+ } = NativeModule.require('internal/modules/cjs/loader');
+ _preloadModules(process._preload_modules);
+ }
+ }
+
+ function executeUserCode() {
+ // User passed `-e` or `--eval` arguments to Node without `-i` or
+ // `--interactive`.
+ // Note that the name `forceRepl` is merely an alias of `interactive`
+ // in code.
+ // TODO(joyeecheung): use internal/options instead of
+ // process._eval/process._forceRepl
+ if (process._eval != null && !process._forceRepl) {
+ const {
+ addBuiltinLibsToObject
+ } = NativeModule.require('internal/modules/cjs/helpers');
+ addBuiltinLibsToObject(global);
+ evalScript('[eval]', wrapForBreakOnFirstLine(process._eval));
+ return;
+ }
+
+ // If the first argument is a file name, run it as a main script
+ if (process.argv[1] && process.argv[1] !== '-') {
+ // Expand process.argv[1] into a full path.
+ const path = NativeModule.require('path');
+ process.argv[1] = path.resolve(process.argv[1]);
+
+ const CJSModule = NativeModule.require('internal/modules/cjs/loader');
+
+ // If user passed `-c` or `--check` arguments to Node, check its syntax
+ // instead of actually running the file.
+ // TODO(joyeecheung): use internal/options instead of
+ // process._syntax_check_only
+ if (process._syntax_check_only != null) {
+ const fs = NativeModule.require('fs');
+ // Read the source.
+ const filename = CJSModule._resolveFilename(process.argv[1]);
+ const source = fs.readFileSync(filename, 'utf-8');
+ checkScriptSyntax(source, filename);
+ process.exit(0);
}
- if (process._eval != null && !process._forceRepl) {
- // User passed '-e' or '--eval' arguments to Node without '-i' or
- // '--interactive'.
- preloadModules();
-
- const {
- addBuiltinLibsToObject
- } = NativeModule.require('internal/modules/cjs/helpers');
- addBuiltinLibsToObject(global);
- evalScript('[eval]');
- } else if (process.argv[1] && process.argv[1] !== '-') {
- // Make process.argv[1] into a full path.
- const path = NativeModule.require('path');
- process.argv[1] = path.resolve(process.argv[1]);
-
- const CJSModule = NativeModule.require('internal/modules/cjs/loader');
-
- preloadModules();
- // 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 = CJSModule._resolveFilename(process.argv[1]);
- const source = fs.readFileSync(filename, 'utf-8');
- checkScriptSyntax(source, filename);
- process.exit(0);
+ // Note: this actually tries to run the module as a ESM first if
+ // --experimental-modules is on.
+ // TODO(joyeecheung): can we move that logic to here? Note that this
+ // is an undocumented method available via `require('module').runMain`
+ CJSModule.runMain();
+ return;
+ }
+
+ // Create the REPL if `-i` or `--interactive` is passed, or if
+ // stdin is a TTY.
+ // Note that the name `forceRepl` is merely an alias of `interactive`
+ // in code.
+ if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
+ const cliRepl = NativeModule.require('internal/repl');
+ cliRepl.createInternalRepl(process.env, (err, repl) => {
+ if (err) {
+ throw err;
}
- CJSModule.runMain();
- } else {
- preloadModules();
- // If -i or --interactive were passed, or stdin is a TTY.
- if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
- // REPL
- const cliRepl = NativeModule.require('internal/repl');
- cliRepl.createInternalRepl(process.env, (err, repl) => {
- if (err) {
- throw err;
- }
- repl.on('exit', () => {
- if (repl._flushing) {
- repl.pause();
- return repl.once('flushHistory', () => {
- process.exit();
- });
- }
+ repl.on('exit', () => {
+ if (repl._flushing) {
+ repl.pause();
+ return repl.once('flushHistory', () => {
process.exit();
});
- });
-
- if (process._eval != null) {
- // User passed '-e' or '--eval'
- evalScript('[eval]');
}
- } else {
- // Read all of stdin - execute it.
- process.stdin.setEncoding('utf8');
-
- let code = '';
- process.stdin.on('data', (d) => {
- code += d;
- });
-
- process.stdin.on('end', function() {
- if (process._syntax_check_only != null) {
- checkScriptSyntax(code, '[stdin]');
- } else {
- process._eval = code;
- evalScript('[stdin]');
- }
- });
- }
+ process.exit();
+ });
+ });
+
+ // User passed '-e' or '--eval' along with `-i` or `--interactive`
+ if (process._eval != null) {
+ evalScript('[eval]', wrapForBreakOnFirstLine(process._eval));
}
+ return;
}
+
+ // Stdin is not a TTY, we will read it and execute it.
+ readAndExecuteStdin();
+ }
+
+ function readAndExecuteStdin() {
+ process.stdin.setEncoding('utf8');
+
+ let code = '';
+ process.stdin.on('data', (d) => {
+ code += d;
+ });
+
+ process.stdin.on('end', () => {
+ if (process._syntax_check_only != null) {
+ checkScriptSyntax(code, '[stdin]');
+ } else {
+ process._eval = code;
+ evalScript('[stdin]', wrapForBreakOnFirstLine(process._eval));
+ }
+ });
}
function setupTraceCategoryState() {
@@ -651,7 +700,7 @@
return `process.binding('inspector').callAndPauseOnStart(${fn}, {})`;
}
- function evalScript(name, body = wrapForBreakOnFirstLine(process._eval)) {
+ function evalScript(name, body) {
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const path = NativeModule.require('path');
const cwd = tryGetCwd(path);
@@ -673,16 +722,6 @@
process._tickCallback();
}
- // Load preload modules.
- function preloadModules() {
- if (process._preload_modules) {
- const {
- _preloadModules
- } = NativeModule.require('internal/modules/cjs/loader');
- _preloadModules(process._preload_modules);
- }
- }
-
function checkScriptSyntax(source, filename) {
const CJSModule = NativeModule.require('internal/modules/cjs/loader');
const vm = NativeModule.require('vm');