Skip to content

Commit a232cd6

Browse files
joyeecheungBethGriggs
authored andcommitted
process: store argv in Environment
This gets rid of Environment::ExecutionMode as well now that we use the original arguments to determine execution mode. PR-URL: #26945 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent c537daf commit a232cd6

File tree

6 files changed

+19
-41
lines changed

6 files changed

+19
-41
lines changed

src/env-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ inline std::shared_ptr<EnvironmentOptions> Environment::options() {
630630
return options_;
631631
}
632632

633+
inline const std::vector<std::string>& Environment::argv() {
634+
return argv_;
635+
}
636+
633637
inline const std::vector<std::string>& Environment::exec_argv() {
634638
return exec_argv_;
635639
}

src/env.cc

+2-11
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,8 @@ void Environment::ExitEnv() {
379379
MaybeLocal<Object> Environment::ProcessCliArgs(
380380
const std::vector<std::string>& args,
381381
const std::vector<std::string>& exec_args) {
382-
if (args.size() > 1) {
383-
std::string first_arg = args[1];
384-
if (first_arg == "inspect") {
385-
execution_mode_ = ExecutionMode::kInspect;
386-
} else if (first_arg == "debug") {
387-
execution_mode_ = ExecutionMode::kDebug;
388-
} else if (first_arg != "-") {
389-
execution_mode_ = ExecutionMode::kRunMainModule;
390-
}
391-
}
382+
argv_ = args;
383+
exec_argv_ = exec_args;
392384

393385
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
394386
TRACING_CATEGORY_NODE1(environment)) != 0) {
@@ -406,7 +398,6 @@ MaybeLocal<Object> Environment::ProcessCliArgs(
406398
std::move(traced_value));
407399
}
408400

409-
exec_argv_ = exec_args;
410401
Local<Object> process_object =
411402
node::CreateProcessObject(this, args, exec_args)
412403
.FromMaybe(Local<Object>());

src/env.h

+2-18
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ class Environment {
753753
const std::vector<std::string>& args,
754754
const std::vector<std::string>& exec_args);
755755
inline const std::vector<std::string>& exec_argv();
756+
inline const std::vector<std::string>& argv();
756757

757758
typedef void (*HandleCleanupCb)(Environment* env,
758759
uv_handle_t* handle,
@@ -1057,23 +1058,6 @@ class Environment {
10571058
inline std::shared_ptr<EnvironmentOptions> options();
10581059
inline std::shared_ptr<HostPort> inspector_host_port();
10591060

1060-
enum class ExecutionMode {
1061-
kDefault,
1062-
kInspect, // node inspect
1063-
kDebug, // node debug
1064-
kPrintHelp, // node --help
1065-
kPrintBashCompletion, // node --completion-bash
1066-
kProfProcess, // node --prof-process
1067-
kEvalString, // node --eval without --interactive
1068-
kCheckSyntax, // node --check (incompatible with --eval)
1069-
kRepl,
1070-
kEvalStdin,
1071-
kRunMainModule
1072-
};
1073-
1074-
inline ExecutionMode execution_mode() { return execution_mode_; }
1075-
1076-
inline void set_execution_mode(ExecutionMode mode) { execution_mode_ = mode; }
10771061
inline AsyncRequest* thread_stopper() { return &thread_stopper_; }
10781062

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

1088-
ExecutionMode execution_mode_ = ExecutionMode::kDefault;
10891072
std::list<binding::DLib> loaded_addons_;
10901073
v8::Isolate* const isolate_;
10911074
IsolateData* const isolate_data_;
@@ -1117,6 +1100,7 @@ class Environment {
11171100
// used.
11181101
std::shared_ptr<HostPort> inspector_host_port_;
11191102
std::vector<std::string> exec_argv_;
1103+
std::vector<std::string> argv_;
11201104

11211105
uint32_t module_id_counter_ = 0;
11221106
uint32_t script_id_counter_ = 0;

src/node.cc

+7-10
Original file line numberDiff line numberDiff line change
@@ -402,47 +402,44 @@ MaybeLocal<Value> StartMainThreadExecution(Environment* env) {
402402
return StartExecution(env, "internal/main/run_third_party_main");
403403
}
404404

405-
if (env->execution_mode() == Environment::ExecutionMode::kInspect ||
406-
env->execution_mode() == Environment::ExecutionMode::kDebug) {
405+
std::string first_argv;
406+
if (env->argv().size() > 1) {
407+
first_argv = env->argv()[1];
408+
}
409+
410+
if (first_argv == "inspect" || first_argv == "debug") {
407411
return StartExecution(env, "internal/main/inspect");
408412
}
409413

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

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

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

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

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

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

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

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

src/node_worker.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Worker::Worker(Environment* env,
9696
env->inspector_agent()->GetParentHandle(thread_id_, url);
9797
#endif
9898

99+
argv_ = std::vector<std::string>{env->argv()[0]};
99100
// Mark this Worker object as weak until we actually start the thread.
100101
MakeWeak();
101102

@@ -256,8 +257,7 @@ void Worker::Run() {
256257
env_->set_worker_context(this);
257258

258259
env_->InitializeLibuv(profiler_idle_notifier_started_);
259-
env_->ProcessCliArgs(std::vector<std::string>{},
260-
std::move(exec_argv_));
260+
env_->ProcessCliArgs(std::move(argv_), std::move(exec_argv_));
261261
}
262262
{
263263
Mutex::ScopedLock lock(mutex_);

src/node_worker.h

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class Worker : public AsyncWrap {
5858

5959
std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
6060
std::vector<std::string> exec_argv_;
61+
std::vector<std::string> argv_;
62+
6163
MultiIsolatePlatform* platform_;
6264
v8::Isolate* isolate_ = nullptr;
6365
bool profiler_idle_notifier_started_;

0 commit comments

Comments
 (0)