From 37ba20112a1ba469f4e759811af26d64d6dd406a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 15 Dec 2018 20:50:42 +0100 Subject: [PATCH] src,lib: prefer internal/options over process._foo This addresses a couple `TODO` comments and allows us to remove a number of underscored properties from `process` (in a semver-major follow-up). PR-URL: https://github.com/nodejs/node/pull/25063 Reviewed-By: Joyee Cheung Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/internal/bootstrap/node.js | 25 ++++++++++--------------- src/node.cc | 12 ++++++++---- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 49a82d5137b490..2279fbeafdfae2 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -30,6 +30,7 @@ const { const { internalBinding, NativeModule } = loaderExports; const exceptionHandlerState = { captureFn: null }; +let getOptionValue; function startup() { setupTraceCategoryState(); @@ -105,7 +106,7 @@ function startup() { NativeModule.require('internal/inspector_async_hook').setup(); } - const { getOptionValue } = NativeModule.require('internal/options'); + getOptionValue = NativeModule.require('internal/options').getOptionValue; if (getOptionValue('--help')) { NativeModule.require('internal/print_help').print(process.stdout); @@ -265,8 +266,7 @@ function startExecution() { } // `node --prof-process` - // TODO(joyeecheung): use internal/options instead of process.profProcess - if (process.profProcess) { + if (getOptionValue('--prof-process')) { NativeModule.require('internal/v8_prof_processor'); return; } @@ -288,13 +288,12 @@ function prepareUserCodeExecution() { } // 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 = getOptionValue('--require'); + if (preloadModules) { const { _preloadModules } = NativeModule.require('internal/modules/cjs/loader'); - _preloadModules(process._preload_modules); + _preloadModules(preloadModules); } } @@ -303,14 +302,12 @@ function executeUserCode() { // `--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) { + if (getOptionValue('[has_eval_string]') && !getOptionValue('--interactive')) { const { addBuiltinLibsToObject } = NativeModule.require('internal/modules/cjs/helpers'); addBuiltinLibsToObject(global); - evalScript('[eval]', wrapForBreakOnFirstLine(process._eval)); + evalScript('[eval]', wrapForBreakOnFirstLine(getOptionValue('--eval'))); return; } @@ -324,9 +321,7 @@ function executeUserCode() { // 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) { + if (getOptionValue('--check')) { const fs = NativeModule.require('fs'); // Read the source. const filename = CJSModule._resolveFilename(process.argv[1]); @@ -682,7 +677,7 @@ function evalScript(name, body) { `${JSON.stringify(body)}, { filename: ` + `${JSON.stringify(name)}, displayErrors: true });\n`; const result = module._compile(script, `${name}-wrapper`); - if (process._print_eval) console.log(result); + if (getOptionValue('--print')) console.log(result); // Handle any nextTicks added in the first tick of the program. process._tickCallback(); } diff --git a/src/node.cc b/src/node.cc index 6b6ede3fdeae48..6ca74655b5d323 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1002,6 +1002,7 @@ void SetupProcessObject(Environment* env, GetParentProcessId).FromJust()); // -e, --eval + // TODO(addaleax): Remove this. if (env->options()->has_eval_string) { READONLY_PROPERTY(process, "_eval", @@ -1012,23 +1013,27 @@ void SetupProcessObject(Environment* env, } // -p, --print + // TODO(addaleax): Remove this. if (env->options()->print_eval) { READONLY_PROPERTY(process, "_print_eval", True(env->isolate())); } // -c, --check + // TODO(addaleax): Remove this. if (env->options()->syntax_check_only) { READONLY_PROPERTY(process, "_syntax_check_only", True(env->isolate())); } // -i, --interactive + // TODO(addaleax): Remove this. if (env->options()->force_repl) { READONLY_PROPERTY(process, "_forceRepl", True(env->isolate())); } // -r, --require - std::vector preload_modules = - std::move(env->options()->preload_modules); + // TODO(addaleax): Remove this. + const std::vector& preload_modules = + env->options()->preload_modules; if (!preload_modules.empty()) { Local array = Array::New(env->isolate()); for (unsigned int i = 0; i < preload_modules.size(); ++i) { @@ -1041,8 +1046,6 @@ void SetupProcessObject(Environment* env, READONLY_PROPERTY(process, "_preload_modules", array); - - preload_modules.clear(); } // --no-deprecation @@ -1071,6 +1074,7 @@ void SetupProcessObject(Environment* env, #endif // NODE_NO_BROWSER_GLOBALS // --prof-process + // TODO(addaleax): Remove this. if (env->options()->prof_process) { READONLY_PROPERTY(process, "profProcess", True(env->isolate())); }