src: rename Init and Start overloads to something more distinctive

In addition, move the `--help` and `--v8-help` processing out of
`StartNodeWithLoopAndArgs()` and process them earlier - right after
`InitializeNodeWithArgs()`, similar to how they are handled in
the legacy `Init()`.

PR-URL: https://github.com/nodejs/node/pull/26499
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
Joyee Cheung 2019-03-07 14:52:31 +01:00
parent 2e2670a207
commit 963ee0bc73
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
5 changed files with 30 additions and 29 deletions

View File

@ -225,7 +225,7 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
static_cast<Environment::Flags>(Environment::kIsMainThread | static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState | Environment::kOwnsProcessState |
Environment::kOwnsInspector)); Environment::kOwnsInspector));
env->Start(per_process::v8_is_profiling); env->InitializeLibuv(per_process::v8_is_profiling);
env->ProcessCliArgs(args, exec_args); env->ProcessCliArgs(args, exec_args);
return env; return env;
} }

View File

@ -320,7 +320,7 @@ Environment::~Environment() {
} }
} }
void Environment::Start(bool start_profiler_idle_notifier) { void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {
HandleScope handle_scope(isolate()); HandleScope handle_scope(isolate());
Context::Scope context_scope(context()); Context::Scope context_scope(context());

View File

@ -675,7 +675,7 @@ class Environment {
uint64_t thread_id = kNoThreadId); uint64_t thread_id = kNoThreadId);
~Environment(); ~Environment();
void Start(bool start_profiler_idle_notifier); void InitializeLibuv(bool start_profiler_idle_notifier);
v8::MaybeLocal<v8::Object> ProcessCliArgs( v8::MaybeLocal<v8::Object> ProcessCliArgs(
const std::vector<std::string>& args, const std::vector<std::string>& args,
const std::vector<std::string>& exec_args); const std::vector<std::string>& exec_args);

View File

@ -620,10 +620,10 @@ int ProcessGlobalArgs(std::vector<std::string>* args,
static std::atomic_bool init_called{false}; static std::atomic_bool init_called{false};
int Init(std::vector<std::string>* argv, int InitializeNodeWithArgs(std::vector<std::string>* argv,
std::vector<std::string>* exec_argv, std::vector<std::string>* exec_argv,
std::vector<std::string>* errors) { std::vector<std::string>* errors) {
// Make sure Init() is called only once. // Make sure InitializeNodeWithArgs() is called only once.
CHECK(!init_called.exchange(true)); CHECK(!init_called.exchange(true));
// Register built-in modules // Register built-in modules
@ -734,7 +734,7 @@ void Init(int* argc,
// This (approximately) duplicates some logic that has been moved to // This (approximately) duplicates some logic that has been moved to
// node::Start(), with the difference that here we explicitly call `exit()`. // node::Start(), with the difference that here we explicitly call `exit()`.
int exit_code = Init(&argv_, &exec_argv_, &errors); int exit_code = InitializeNodeWithArgs(&argv_, &exec_argv_, &errors);
for (const std::string& error : errors) for (const std::string& error : errors)
fprintf(stderr, "%s: %s\n", argv_.at(0).c_str(), error.c_str()); fprintf(stderr, "%s: %s\n", argv_.at(0).c_str(), error.c_str());
@ -769,9 +769,10 @@ void RunBeforeExit(Environment* env) {
EmitBeforeExit(env); EmitBeforeExit(env);
} }
inline int Start(Isolate* isolate, IsolateData* isolate_data, inline int StartNodeWithIsolate(Isolate* isolate,
const std::vector<std::string>& args, IsolateData* isolate_data,
const std::vector<std::string>& exec_args) { const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
Local<Context> context = NewContext(isolate); Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context); Context::Scope context_scope(context);
@ -782,7 +783,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
static_cast<Environment::Flags>(Environment::kIsMainThread | static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState | Environment::kOwnsProcessState |
Environment::kOwnsInspector)); Environment::kOwnsInspector));
env.Start(per_process::v8_is_profiling); env.InitializeLibuv(per_process::v8_is_profiling);
env.ProcessCliArgs(args, exec_args); env.ProcessCliArgs(args, exec_args);
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM #if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
@ -858,25 +859,15 @@ exit:
return exit_code; return exit_code;
} }
inline int Start(uv_loop_t* event_loop, inline int StartNodeWithLoopAndArgs(uv_loop_t* event_loop,
const std::vector<std::string>& args, const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) { const std::vector<std::string>& exec_args) {
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)> std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator); allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
Isolate* const isolate = NewIsolate(allocator.get(), event_loop); Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
if (isolate == nullptr) if (isolate == nullptr)
return 12; // Signal internal error. return 12; // Signal internal error.
if (per_process::cli_options->print_version) {
printf("%s\n", NODE_VERSION);
return 0;
}
if (per_process::cli_options->print_v8_help) {
V8::SetFlagsFromString("--help", 6); // Doesn't return.
UNREACHABLE();
}
int exit_code; int exit_code;
{ {
Locker locker(isolate); Locker locker(isolate);
@ -894,7 +885,7 @@ inline int Start(uv_loop_t* event_loop,
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true); isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
} }
exit_code = exit_code =
Start(isolate, isolate_data.get(), args, exec_args); StartNodeWithIsolate(isolate, isolate_data.get(), args, exec_args);
} }
isolate->Dispose(); isolate->Dispose();
@ -926,12 +917,22 @@ int Start(int argc, char** argv) {
std::vector<std::string> errors; std::vector<std::string> errors;
// This needs to run *before* V8::Initialize(). // This needs to run *before* V8::Initialize().
{ {
const int exit_code = Init(&args, &exec_args, &errors); const int exit_code = InitializeNodeWithArgs(&args, &exec_args, &errors);
for (const std::string& error : errors) for (const std::string& error : errors)
fprintf(stderr, "%s: %s\n", args.at(0).c_str(), error.c_str()); fprintf(stderr, "%s: %s\n", args.at(0).c_str(), error.c_str());
if (exit_code != 0) return exit_code; if (exit_code != 0) return exit_code;
} }
if (per_process::cli_options->print_version) {
printf("%s\n", NODE_VERSION);
return 0;
}
if (per_process::cli_options->print_v8_help) {
V8::SetFlagsFromString("--help", 6); // Doesn't return.
UNREACHABLE();
}
#if HAVE_OPENSSL #if HAVE_OPENSSL
{ {
std::string extra_ca_certs; std::string extra_ca_certs;
@ -953,7 +954,7 @@ int Start(int argc, char** argv) {
performance::performance_v8_start = PERFORMANCE_NOW(); performance::performance_v8_start = PERFORMANCE_NOW();
per_process::v8_initialized = true; per_process::v8_initialized = true;
const int exit_code = const int exit_code =
Start(uv_default_loop(), args, exec_args); StartNodeWithLoopAndArgs(uv_default_loop(), args, exec_args);
per_process::v8_initialized = false; per_process::v8_initialized = false;
V8::Dispose(); V8::Dispose();

View File

@ -284,7 +284,7 @@ void Worker::Run() {
env_->set_abort_on_uncaught_exception(false); env_->set_abort_on_uncaught_exception(false);
env_->set_worker_context(this); env_->set_worker_context(this);
env_->Start(profiler_idle_notifier_started_); env_->InitializeLibuv(profiler_idle_notifier_started_);
env_->ProcessCliArgs(std::vector<std::string>{}, env_->ProcessCliArgs(std::vector<std::string>{},
std::move(exec_argv_)); std::move(exec_argv_));
} }