Skip to content

Commit 49ee010

Browse files
committed
lib: use getOptionValue instead of process underscore aliases
This patch reduce usage of `process._breakFirstLine` and `process._eval` in the internals and use `getOptionValue('--inspect-brk')` and `getOptionValue('--eval')` instead wherever possible. PR-URL: #27278 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent b66f01d commit 49ee010

File tree

6 files changed

+45
-21
lines changed

6 files changed

+45
-21
lines changed

lib/internal/main/eval_stdin.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const {
66
prepareMainThreadExecution
77
} = require('internal/bootstrap/pre_execution');
88

9+
const { getOptionValue } = require('internal/options');
10+
911
const {
1012
evalModule,
1113
evalScript,
@@ -16,9 +18,16 @@ prepareMainThreadExecution();
1618
markBootstrapComplete();
1719

1820
readStdin((code) => {
21+
// This is necessary for fork() and CJS module compilation.
22+
// TODO(joyeecheung): pass this with something really internal.
1923
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);
2228
else
23-
evalScript('[stdin]', process._eval, process._breakFirstLine);
29+
evalScript('[stdin]',
30+
code,
31+
getOptionValue('--inspect-brk'),
32+
print);
2433
});

lib/internal/main/eval_string.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ const { evalModule, evalScript } = require('internal/process/execution');
1010
const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers');
1111

1212
const { getOptionValue } = require('internal/options');
13-
const source = getOptionValue('--eval');
13+
1414
prepareMainThreadExecution();
1515
addBuiltinLibsToObject(global);
1616
markBootstrapComplete();
17+
18+
const source = getOptionValue('--eval');
19+
const print = getOptionValue('--print');
1720
if (getOptionValue('--input-type') === 'module')
18-
evalModule(source);
21+
evalModule(source, print);
1922
else
20-
evalScript('[eval]', source, process._breakFirstLine);
23+
evalScript('[eval]',
24+
source,
25+
getOptionValue('--inspect-brk'),
26+
print);

lib/internal/main/repl.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ const {
1313

1414
const console = require('internal/console/global');
1515

16+
const { getOptionValue } = require('internal/options');
17+
1618
prepareMainThreadExecution();
1719

20+
markBootstrapComplete();
21+
1822
// --input-type flag not supported in REPL
19-
if (require('internal/options').getOptionValue('--input-type')) {
23+
if (getOptionValue('--input-type')) {
2024
// If we can't write to stderr, we'd like to make this a noop,
2125
// so use console.error.
2226
console.error('Cannot specify --input-type for REPL');
@@ -44,8 +48,10 @@ cliRepl.createInternalRepl(process.env, (err, repl) => {
4448

4549
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
4650
// 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'));
4957
}
50-
51-
markBootstrapComplete();

lib/internal/modules/cjs/loader.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ Module.prototype.require = function(id) {
680680
// Resolved path to process.argv[1] will be lazily placed here
681681
// (needed for setting breakpoint when called with --inspect-brk)
682682
var resolvedArgv;
683-
683+
let hasPausedEntry = false;
684684

685685
// Run the file contents in the correct scope or sandbox. Expose
686686
// the correct helper variables (require, module, exports) to
@@ -736,7 +736,7 @@ Module.prototype._compile = function(content, filename) {
736736
}
737737

738738
var inspectorWrapper = null;
739-
if (process._breakFirstLine && process._eval == null) {
739+
if (getOptionValue('--inspect-brk') && process._eval == null) {
740740
if (!resolvedArgv) {
741741
// We enter the repl if we're not given a filename argument.
742742
if (process.argv[1]) {
@@ -747,8 +747,8 @@ Module.prototype._compile = function(content, filename) {
747747
}
748748

749749
// Set breakpoint on module start
750-
if (filename === resolvedArgv) {
751-
delete process._breakFirstLine;
750+
if (!hasPausedEntry && filename === resolvedArgv) {
751+
hasPausedEntry = true;
752752
inspectorWrapper = internalBinding('inspector').callAndPauseOnStart;
753753
}
754754
}

lib/internal/modules/esm/module_job.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const {
99
const { ModuleWrap } = internalBinding('module_wrap');
1010

1111
const { decorateErrorStack } = require('internal/util');
12+
const { getOptionValue } = require('internal/options');
1213
const assert = require('internal/assert');
1314
const resolvedPromise = SafePromise.resolve();
1415

1516
function noop() {}
1617

18+
let hasPausedEntry = false;
19+
1720
/* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of
1821
* its dependencies, over time. */
1922
class ModuleJob {
@@ -82,8 +85,8 @@ class ModuleJob {
8285
};
8386
await addJobsToDependencyGraph(this);
8487
try {
85-
if (this.isMain && process._breakFirstLine) {
86-
delete process._breakFirstLine;
88+
if (!hasPausedEntry && this.isMain && getOptionValue('--inspect-brk')) {
89+
hasPausedEntry = true;
8790
const initWrapper = internalBinding('inspector').callAndPauseOnStart;
8891
initWrapper(this.module.instantiate, this.module);
8992
} else {

lib/internal/process/execution.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ function tryGetCwd() {
3535
}
3636
}
3737

38-
function evalModule(source) {
38+
function evalModule(source, print) {
3939
const { log, error } = require('internal/console/global');
4040
const { decorateErrorStack } = require('internal/util');
4141
const asyncESM = require('internal/process/esm_loader');
4242
asyncESM.loaderPromise.then(async (loader) => {
4343
const { result } = await loader.eval(source);
44-
if (require('internal/options').getOptionValue('--print')) {
44+
if (print) {
4545
log(result);
4646
}
4747
})
@@ -54,7 +54,7 @@ function evalModule(source) {
5454
process._tickCallback();
5555
}
5656

57-
function evalScript(name, body, breakFirstLine) {
57+
function evalScript(name, body, breakFirstLine, print) {
5858
const CJSModule = require('internal/modules/cjs/loader');
5959
const { kVmBreakFirstLineSymbol } = require('internal/util');
6060

@@ -79,7 +79,7 @@ function evalScript(name, body, breakFirstLine) {
7979
[kVmBreakFirstLineSymbol]: ${!!breakFirstLine}
8080
});\n`;
8181
const result = module._compile(script, `${name}-wrapper`);
82-
if (require('internal/options').getOptionValue('--print')) {
82+
if (print) {
8383
const { log } = require('internal/console/global');
8484
log(result);
8585
}

0 commit comments

Comments
 (0)