Skip to content

Commit

Permalink
process: store argv in Environment
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Signed-off-by: Beth Griggs <[email protected]>
  • Loading branch information
joyeecheung authored and BethGriggs committed Apr 9, 2019
1 parent c537daf commit a232cd6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 41 deletions.
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 @@ -379,16 +379,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 @@ -406,7 +398,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 @@ -753,6 +753,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 @@ -402,47 +402,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
4 changes: 2 additions & 2 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Worker::Worker(Environment* env,
env->inspector_agent()->GetParentHandle(thread_id_, url);
#endif

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

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

env_->InitializeLibuv(profiler_idle_notifier_started_);
env_->ProcessCliArgs(std::vector<std::string>{},
std::move(exec_argv_));
env_->ProcessCliArgs(std::move(argv_), std::move(exec_argv_));
}
{
Mutex::ScopedLock lock(mutex_);
Expand Down
2 changes: 2 additions & 0 deletions src/node_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Worker : public AsyncWrap {

std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
std::vector<std::string> exec_argv_;
std::vector<std::string> argv_;

MultiIsolatePlatform* platform_;
v8::Isolate* isolate_ = nullptr;
bool profiler_idle_notifier_started_;
Expand Down

0 comments on commit a232cd6

Please sign in to comment.