Skip to content

Commit

Permalink
src: inline ProcessCliArgs in the Environment constructor
Browse files Browse the repository at this point in the history
Inline `ProcessCliArgs()` in the `Environment` constructor, and
emit the `Environment` creation trace events with the arguments
earlier. Remove the unused arguments passed to `CreateProcessObject()`
since these are now attached to process in `PatchProcessObject()`
during pre-execution instead.

PR-URL: #27539
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
joyeecheung authored and BridgeAR committed Jun 17, 2019
1 parent f8c9a58 commit 70f8e71
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
Environment* env = new Environment(
isolate_data,
context,
args,
exec_args,
static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState |
Environment::kOwnsInspector));
env->InitializeLibuv(per_process::v8_is_profiling);
env->ProcessCliArgs(args, exec_args);
if (RunBootstrapping(env).IsEmpty()) {
return nullptr;
}
Expand Down
51 changes: 22 additions & 29 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,17 @@ uint64_t Environment::AllocateThreadId() {

Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
Flags flags,
uint64_t thread_id)
: isolate_(context->GetIsolate()),
isolate_data_(isolate_data),
immediate_info_(context->GetIsolate()),
tick_info_(context->GetIsolate()),
timer_base_(uv_now(isolate_data->event_loop())),
exec_argv_(exec_args),
argv_(args),
should_abort_on_uncaught_toggle_(isolate_, 1),
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
flags_(flags),
Expand Down Expand Up @@ -306,6 +310,22 @@ Environment::Environment(IsolateData* isolate_data,
performance::NODE_PERFORMANCE_MILESTONE_V8_START,
performance::performance_v8_start);

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
this,
"args",
std::move(traced_value));
}

// By default, always abort when --abort-on-uncaught-exception was passed.
should_abort_on_uncaught_toggle_[0] = 1;

Expand All @@ -318,6 +338,8 @@ Environment::Environment(IsolateData* isolate_data,
if (options_->no_force_async_hooks_checks) {
async_hooks_.no_force_checks();
}

set_process_object(node::CreateProcessObject(this).ToLocalChecked());
}

CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)
Expand Down Expand Up @@ -434,35 +456,6 @@ void Environment::ExitEnv() {
isolate_->TerminateExecution();
}

MaybeLocal<Object> Environment::ProcessCliArgs(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
argv_ = args;
exec_argv_ = exec_args;

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
this,
"args",
std::move(traced_value));
}

Local<Object> process_object =
node::CreateProcessObject(this, args, exec_args)
.FromMaybe(Local<Object>());
set_process_object(process_object);
return process_object;
}

void Environment::RegisterHandleCleanups() {
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
void* arg) {
Expand Down
5 changes: 2 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,14 +823,13 @@ class Environment : public MemoryRetainer {

Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
Flags flags = Flags(),
uint64_t thread_id = kNoThreadId);
~Environment();

void InitializeLibuv(bool start_profiler_idle_notifier);
v8::MaybeLocal<v8::Object> ProcessCliArgs(
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();

Expand Down
3 changes: 2 additions & 1 deletion src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
std::unique_ptr<Environment> env = std::make_unique<Environment>(
isolate_data_.get(),
context,
args_,
exec_args_,
static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState |
Environment::kOwnsInspector));
env->InitializeLibuv(per_process::v8_is_profiling);
env->ProcessCliArgs(args_, exec_args_);

#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
CHECK(!env->inspector_agent()->IsListening());
Expand Down
5 changes: 1 addition & 4 deletions src/node_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
const char* warning,
const char* deprecation_code);

v8::MaybeLocal<v8::Object> CreateProcessObject(
Environment* env,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args);
v8::MaybeLocal<v8::Object> CreateProcessObject(Environment* env);
void PatchProcessObject(const v8::FunctionCallbackInfo<v8::Value>& args);

namespace task_queue {
Expand Down
5 changes: 1 addition & 4 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ static void GetParentProcessId(Local<Name> property,
info.GetReturnValue().Set(uv_os_getppid());
}

MaybeLocal<Object> CreateProcessObject(
Environment* env,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
MaybeLocal<Object> CreateProcessObject(Environment* env) {
Isolate* isolate = env->isolate();
EscapableHandleScope scope(isolate);
Local<Context> context = env->context();
Expand Down
3 changes: 2 additions & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ void Worker::Run() {
// public API.
env_.reset(new Environment(data.isolate_data_.get(),
context,
std::move(argv_),
std::move(exec_argv_),
Environment::kNoFlags,
thread_id_));
CHECK_NOT_NULL(env_);
Expand All @@ -264,7 +266,6 @@ void Worker::Run() {
env_->set_worker_context(this);

env_->InitializeLibuv(profiler_idle_notifier_started_);
env_->ProcessCliArgs(std::move(argv_), std::move(exec_argv_));
}
{
Mutex::ScopedLock lock(mutex_);
Expand Down

0 comments on commit 70f8e71

Please sign in to comment.