Skip to content
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

process: patch more process properties during pre-execution #26945

Closed
wants to merge 2 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
18 changes: 15 additions & 3 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const { getOptionValue } = require('internal/options');
const { Buffer } = require('buffer');

function prepareMainThreadExecution() {
function prepareMainThreadExecution(expandArgv1 = false) {
// Patch the process object with legacy properties and normalizations
patchProcessObject();
patchProcessObject(expandArgv1);
setupTraceCategoryState();
setupInspectorHooks();
setupWarningHandler();
Expand Down Expand Up @@ -48,14 +48,26 @@ function prepareMainThreadExecution() {
loadPreloadModules();
}

function patchProcessObject() {
function patchProcessObject(expandArgv1) {
const {
patchProcessObject: patchProcessObjectNative
} = internalBinding('process_methods');

patchProcessObjectNative(process);

Object.defineProperty(process, 'argv0', {
enumerable: true,
configurable: false,
value: process.argv[0]
});
process.argv[0] = process.execPath;

if (expandArgv1 && process.argv[1] && !process.argv[1].startsWith('-')) {
// Expand process.argv[1] into a full path.
const path = require('path');
process.argv[1] = path.resolve(process.argv[1]);
}

// TODO(joyeecheung): most of these should be deprecated and removed,
// execpt some that we need to be able to mutate during run time.
addReadOnlyProcessAlias('_eval', '--eval');
Expand Down
21 changes: 10 additions & 11 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ const {
stripShebang, stripBOM
} = require('internal/modules/cjs/helpers');

let CJSModule;
function CJSModuleInit() {
if (!CJSModule)
CJSModule = require('internal/modules/cjs/loader');
}
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution(true);

if (process.argv[1] && process.argv[1] !== '-') {
// Expand process.argv[1] into a full path.
const path = require('path');
process.argv[1] = path.resolve(process.argv[1]);

// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
CJSModuleInit();
// This has to be done after prepareMainThreadExecution because it
// relies on process.execPath
const CJSModule = require('internal/modules/cjs/loader');

// Read the source.
const filename = CJSModule._resolveFilename(process.argv[1]);

Expand All @@ -42,9 +40,6 @@ if (process.argv[1] && process.argv[1] !== '-') {

checkSyntax(source, filename);
} else {
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
CJSModuleInit();
markBootstrapComplete();

readStdin((code) => {
Expand All @@ -56,6 +51,10 @@ function checkSyntax(source, filename) {
// Remove Shebang.
source = stripShebang(source);

// This has to be done after prepareMainThreadExecution because it
// relies on process.execPath
const CJSModule = require('internal/modules/cjs/loader');

const { getOptionValue } = require('internal/options');
const experimentalModules = getOptionValue('--experimental-modules');
if (experimentalModules) {
Expand Down
6 changes: 1 addition & 5 deletions lib/internal/main/run_main_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');

// Expand process.argv[1] into a full path.
const path = require('path');
process.argv[1] = path.resolve(process.argv[1]);

prepareMainThreadExecution();
prepareMainThreadExecution(true);

const CJSModule = require('internal/modules/cjs/loader');

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const prefix = `(${process.release.name}:${process.pid}) `;
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;

// Lazily loaded
Expand Down Expand Up @@ -55,7 +54,7 @@ function onWarning(warning) {
if (isDeprecation && process.noDeprecation) return;
const trace = process.traceProcessWarnings ||
(isDeprecation && process.traceDeprecation);
var msg = prefix;
var msg = `(${process.release.name}:${process.pid}) `;
if (warning.code)
msg += `[${warning.code}] `;
if (trace && warning.stack) {
Expand Down
4 changes: 4 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ inline std::shared_ptr<EnvironmentOptions> Environment::options() {
return options_;
}

inline const std::vector<std::string>& Environment::argv() {
return argv_;
}

inline const std::vector<std::string>& Environment::exec_argv() {
return exec_argv_;
}
Expand Down
13 changes: 2 additions & 11 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,8 @@ void Environment::ExitEnv() {
MaybeLocal<Object> Environment::ProcessCliArgs(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
if (args.size() > 1) {
std::string first_arg = args[1];
if (first_arg == "inspect") {
execution_mode_ = ExecutionMode::kInspect;
} else if (first_arg == "debug") {
execution_mode_ = ExecutionMode::kDebug;
} else if (first_arg != "-") {
execution_mode_ = ExecutionMode::kRunMainModule;
}
}
argv_ = args;
exec_argv_ = exec_args;

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
Expand All @@ -377,7 +369,6 @@ MaybeLocal<Object> Environment::ProcessCliArgs(
std::move(traced_value));
}

exec_argv_ = exec_args;
Local<Object> process_object =
node::CreateProcessObject(this, args, exec_args)
.FromMaybe(Local<Object>());
Expand Down
20 changes: 2 additions & 18 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ class Environment {
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args);
inline const std::vector<std::string>& exec_argv();
inline const std::vector<std::string>& argv();

typedef void (*HandleCleanupCb)(Environment* env,
uv_handle_t* handle,
Expand Down Expand Up @@ -1057,23 +1058,6 @@ class Environment {
inline std::shared_ptr<EnvironmentOptions> options();
inline std::shared_ptr<HostPort> inspector_host_port();

enum class ExecutionMode {
kDefault,
kInspect, // node inspect
kDebug, // node debug
kPrintHelp, // node --help
kPrintBashCompletion, // node --completion-bash
kProfProcess, // node --prof-process
kEvalString, // node --eval without --interactive
kCheckSyntax, // node --check (incompatible with --eval)
kRepl,
kEvalStdin,
kRunMainModule
};

inline ExecutionMode execution_mode() { return execution_mode_; }

inline void set_execution_mode(ExecutionMode mode) { execution_mode_ = mode; }
inline AsyncRequest* thread_stopper() { return &thread_stopper_; }

private:
Expand All @@ -1085,7 +1069,6 @@ class Environment {
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);

ExecutionMode execution_mode_ = ExecutionMode::kDefault;
std::list<binding::DLib> loaded_addons_;
v8::Isolate* const isolate_;
IsolateData* const isolate_data_;
Expand Down Expand Up @@ -1117,6 +1100,7 @@ class Environment {
// used.
std::shared_ptr<HostPort> inspector_host_port_;
std::vector<std::string> exec_argv_;
std::vector<std::string> argv_;

uint32_t module_id_counter_ = 0;
uint32_t script_id_counter_ = 0;
Expand Down
17 changes: 7 additions & 10 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,47 +403,44 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
return StartExecution(env, "internal/main/run_third_party_main");
}

if (env->execution_mode() == Environment::ExecutionMode::kInspect ||
env->execution_mode() == Environment::ExecutionMode::kDebug) {
std::string first_argv;
if (env->argv().size() > 1) {
first_argv = env->argv()[1];
}

if (first_argv == "inspect" || first_argv == "debug") {
return StartExecution(env, "internal/main/inspect");
}

if (per_process::cli_options->print_help) {
env->set_execution_mode(Environment::ExecutionMode::kPrintHelp);
return StartExecution(env, "internal/main/print_help");
}

if (per_process::cli_options->print_bash_completion) {
env->set_execution_mode(Environment::ExecutionMode::kPrintBashCompletion);
return StartExecution(env, "internal/main/print_bash_completion");
}

if (env->options()->prof_process) {
env->set_execution_mode(Environment::ExecutionMode::kProfProcess);
return StartExecution(env, "internal/main/prof_process");
}

// -e/--eval without -i/--interactive
if (env->options()->has_eval_string && !env->options()->force_repl) {
env->set_execution_mode(Environment::ExecutionMode::kEvalString);
return StartExecution(env, "internal/main/eval_string");
}

if (env->options()->syntax_check_only) {
env->set_execution_mode(Environment::ExecutionMode::kCheckSyntax);
return StartExecution(env, "internal/main/check_syntax");
}

if (env->execution_mode() == Environment::ExecutionMode::kRunMainModule) {
if (!first_argv.empty() && first_argv != "-") {
return StartExecution(env, "internal/main/run_main_module");
}

if (env->options()->force_repl || uv_guess_handle(STDIN_FILENO) == UV_TTY) {
env->set_execution_mode(Environment::ExecutionMode::kRepl);
return StartExecution(env, "internal/main/repl");
}

env->set_execution_mode(Environment::ExecutionMode::kEvalStdin);
return StartExecution(env, "internal/main/eval_stdin");
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ v8::MaybeLocal<v8::Object> CreateProcessObject(
Environment* env,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args);

void PatchProcessObject(const v8::FunctionCallbackInfo<v8::Value>& args);
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_PROCESS_H_
1 change: 1 addition & 0 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ static void InitializeProcessMethods(Local<Object> target,
env->SetMethod(target, "dlopen", binding::DLOpen);
env->SetMethod(target, "reallyExit", ReallyExit);
env->SetMethodNoSideEffect(target, "uptime", Uptime);
env->SetMethod(target, "patchProcessObject", PatchProcessObject);
}

} // namespace node
Expand Down
Loading