lib: use getOptionValue instead of process underscore aliases · nodejs/node@49ee010 (original) (raw)

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ const {
6 6 prepareMainThreadExecution
7 7 } = require('internal/bootstrap/pre_execution');
8 8
9 +const { getOptionValue } = require('internal/options');
10 +
9 11 const {
10 12 evalModule,
11 13 evalScript,
@@ -16,9 +18,16 @@ prepareMainThreadExecution();
16 18 markBootstrapComplete();
17 19
18 20 readStdin((code) => {
21 +// This is necessary for fork() and CJS module compilation.
22 +// TODO(joyeecheung): pass this with something really internal.
19 23 process._eval = code;
20 -if (require('internal/options').getOptionValue('--input-type') === 'module')
21 -evalModule(process._eval);
24 +
25 +const print = getOptionValue('--print');
26 +if (getOptionValue('--input-type') === 'module')
27 +evalModule(code, print);
22 28 else
23 -evalScript('[stdin]', process._eval, process._breakFirstLine);
29 +evalScript('[stdin]',
30 +code,
31 +getOptionValue('--inspect-brk'),
32 +print);
24 33 });
Original file line number Diff line number Diff line change
@@ -10,11 +10,17 @@ const { evalModule, evalScript } = require('internal/process/execution');
10 10 const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
11 11
12 12 const { getOptionValue } = require('internal/options');
13 -const source = getOptionValue('--eval');
13 +
14 14 prepareMainThreadExecution();
15 15 addBuiltinLibsToObject(global);
16 16 markBootstrapComplete();
17 +
18 +const source = getOptionValue('--eval');
19 +const print = getOptionValue('--print');
17 20 if (getOptionValue('--input-type') === 'module')
18 -evalModule(source);
21 +evalModule(source, print);
19 22 else
20 -evalScript('[eval]', source, process._breakFirstLine);
23 +evalScript('[eval]',
24 +source,
25 +getOptionValue('--inspect-brk'),
26 +print);
Original file line number Diff line number Diff line change
@@ -13,10 +13,14 @@ const {
13 13
14 14 const console = require('internal/console/global');
15 15
16 +const { getOptionValue } = require('internal/options');
17 +
16 18 prepareMainThreadExecution();
17 19
20 +markBootstrapComplete();
21 +
18 22 // --input-type flag not supported in REPL
19 -if (require('internal/options').getOptionValue('--input-type')) {
23 +if (getOptionValue('--input-type')) {
20 24 // If we can't write to stderr, we'd like to make this a noop,
21 25 // so use console.error.
22 26 console.error('Cannot specify --input-type for REPL');
@@ -44,8 +48,10 @@ cliRepl.createInternalRepl(process.env, (err, repl) => {
44 48
45 49 // If user passed '-e' or '--eval' along with `-i` or `--interactive`,
46 50 // evaluate the code in the current context.
47 -if (process._eval != null) {
48 -evalScript('[eval]', process._eval, process._breakFirstLine);
51 +const source = getOptionValue('--eval');
52 +if (source != null) {
53 +evalScript('[eval]',
54 +source,
55 +getOptionValue('--inspect-brk'),
56 +getOptionValue('--print'));
49 57 }
50 -
51 -markBootstrapComplete();
Original file line number Diff line number Diff line change
@@ -680,7 +680,7 @@ Module.prototype.require = function(id) {
680 680 // Resolved path to process.argv[1] will be lazily placed here
681 681 // (needed for setting breakpoint when called with --inspect-brk)
682 682 var resolvedArgv;
683 -
683 +let hasPausedEntry = false;
684 684
685 685 // Run the file contents in the correct scope or sandbox. Expose
686 686 // the correct helper variables (require, module, exports) to
@@ -736,7 +736,7 @@ Module.prototype._compile = function(content, filename) {
736 736 }
737 737
738 738 var inspectorWrapper = null;
739 -if (process._breakFirstLine && process._eval == null) {
739 +if (getOptionValue('--inspect-brk') && process._eval == null) {
740 740 if (!resolvedArgv) {
741 741 // We enter the repl if we're not given a filename argument.
742 742 if (process.argv[1]) {
@@ -747,8 +747,8 @@ Module.prototype._compile = function(content, filename) {
747 747 }
748 748
749 749 // Set breakpoint on module start
750 -if (filename === resolvedArgv) {
751 -delete process._breakFirstLine;
750 +if (!hasPausedEntry && filename === resolvedArgv) {
751 +hasPausedEntry = true;
752 752 inspectorWrapper = internalBinding('inspector').callAndPauseOnStart;
753 753 }
754 754 }
Original file line number Diff line number Diff line change
@@ -9,11 +9,14 @@ const {
9 9 const { ModuleWrap } = internalBinding('module_wrap');
10 10
11 11 const { decorateErrorStack } = require('internal/util');
12 +const { getOptionValue } = require('internal/options');
12 13 const assert = require('internal/assert');
13 14 const resolvedPromise = SafePromise.resolve();
14 15
15 16 function noop() {}
16 17
18 +let hasPausedEntry = false;
19 +
17 20 /* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of
18 21 * its dependencies, over time. */
19 22 class ModuleJob {
@@ -82,8 +85,8 @@ class ModuleJob {
82 85 };
83 86 await addJobsToDependencyGraph(this);
84 87 try {
85 -if (this.isMain && process._breakFirstLine) {
86 -delete process._breakFirstLine;
88 +if (!hasPausedEntry && this.isMain && getOptionValue('--inspect-brk')) {
89 +hasPausedEntry = true;
87 90 const initWrapper = internalBinding('inspector').callAndPauseOnStart;
88 91 initWrapper(this.module.instantiate, this.module);
89 92 } else {
Original file line number Diff line number Diff line change
@@ -35,13 +35,13 @@ function tryGetCwd() {
35 35 }
36 36 }
37 37
38 -function evalModule(source) {
38 +function evalModule(source, print) {
39 39 const { log, error } = require('internal/console/global');
40 40 const { decorateErrorStack } = require('internal/util');
41 41 const asyncESM = require('internal/process/esm_loader');
42 42 asyncESM.loaderPromise.then(async (loader) => {
43 43 const { result } = await loader.eval(source);
44 -if (require('internal/options').getOptionValue('--print')) {
44 +if (print) {
45 45 log(result);
46 46 }
47 47 })
@@ -54,7 +54,7 @@ function evalModule(source) {
54 54 process._tickCallback();
55 55 }
56 56
57 -function evalScript(name, body, breakFirstLine) {
57 +function evalScript(name, body, breakFirstLine, print) {
58 58 const CJSModule = require('internal/modules/cjs/loader');
59 59 const { kVmBreakFirstLineSymbol } = require('internal/util');
60 60
@@ -79,7 +79,7 @@ function evalScript(name, body, breakFirstLine) {
79 79 [kVmBreakFirstLineSymbol]: ${!!breakFirstLine}
80 80 });\n`;
81 81 const result = module._compile(script, `${name}-wrapper`);
82 -if (require('internal/options').getOptionValue('--print')) {
82 +if (print) {
83 83 const { log } = require('internal/console/global');
84 84 log(result);
85 85 }