summaryrefslogtreecommitdiff
path: root/src/node.cc
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-03-20 00:12:23 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2019-04-03 08:46:03 +0800
commit19442656789a7edd327d453a16f699d4f5259a5c (patch)
tree78ae15e1b70df80d14b7306aea25d9d90973b7fe /src/node.cc
parente02f511dccaf41f103403936359d43ce2ad86d1e (diff)
downloadandroid-node-v8-19442656789a7edd327d453a16f699d4f5259a5c.tar.gz
android-node-v8-19442656789a7edd327d453a16f699d4f5259a5c.tar.bz2
android-node-v8-19442656789a7edd327d453a16f699d4f5259a5c.zip
process: run RunBootstrapping in CreateEnvironment
Also creates `CreateMainEnvironment` to encapsulate the code creating the main environment from the provided Isolate data and arguments. PR-URL: https://github.com/nodejs/node/pull/26788 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com>
Diffstat (limited to 'src/node.cc')
-rw-r--r--src/node.cc147
1 files changed, 83 insertions, 64 deletions
diff --git a/src/node.cc b/src/node.cc
index deb4246d35..b980493009 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -200,11 +200,10 @@ void SignalExit(int signo) {
raise(signo);
}
-static MaybeLocal<Value> ExecuteBootstrapper(
- Environment* env,
- const char* id,
- std::vector<Local<String>>* parameters,
- std::vector<Local<Value>>* arguments) {
+MaybeLocal<Value> ExecuteBootstrapper(Environment* env,
+ const char* id,
+ std::vector<Local<String>>* parameters,
+ std::vector<Local<Value>>* arguments) {
EscapableHandleScope scope(env->isolate());
MaybeLocal<Function> maybe_fn =
per_process::native_module_loader.LookupAndCompile(
@@ -453,9 +452,7 @@ void LoadEnvironment(Environment* env) {
// StartMainThreadExecution() make sense for embedders. Pick the
// useful ones out, and allow embedders to customize the entry
// point more directly without using _third_party_main.js
- if (!RunBootstrapping(env).IsEmpty()) {
- USE(StartMainThreadExecution(env));
- }
+ USE(StartMainThreadExecution(env));
}
@@ -780,90 +777,117 @@ void RunBeforeExit(Environment* env) {
EmitBeforeExit(env);
}
-inline int StartNodeWithIsolate(Isolate* isolate,
- IsolateData* isolate_data,
- const std::vector<std::string>& args,
- const std::vector<std::string>& exec_args) {
+// TODO(joyeecheung): align this with the CreateEnvironment exposed in node.h
+// and the environment creation routine in workers somehow.
+inline std::unique_ptr<Environment> CreateMainEnvironment(
+ IsolateData* isolate_data,
+ const std::vector<std::string>& args,
+ const std::vector<std::string>& exec_args,
+ int* exit_code) {
+ Isolate* isolate = isolate_data->isolate();
HandleScope handle_scope(isolate);
+
+ // TODO(addaleax): This should load a real per-Isolate option, currently
+ // this is still effectively per-process.
+ if (isolate_data->options()->track_heap_objects) {
+ isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
+ }
+
Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context);
- int exit_code = 0;
- Environment env(
+
+ std::unique_ptr<Environment> env = std::make_unique<Environment>(
isolate_data,
context,
static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState |
Environment::kOwnsInspector));
- env.InitializeLibuv(per_process::v8_is_profiling);
- env.ProcessCliArgs(args, exec_args);
+ env->InitializeLibuv(per_process::v8_is_profiling);
+ env->ProcessCliArgs(args, exec_args);
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
- CHECK(!env.inspector_agent()->IsListening());
+ CHECK(!env->inspector_agent()->IsListening());
// Inspector agent can't fail to start, but if it was configured to listen
// right away on the websocket port and fails to bind/etc, this will return
// false.
- env.inspector_agent()->Start(args.size() > 1 ? args[1].c_str() : "",
- env.options()->debug_options(),
- env.inspector_host_port(),
- true);
- if (env.options()->debug_options().inspector_enabled &&
- !env.inspector_agent()->IsListening()) {
- exit_code = 12; // Signal internal error.
- goto exit;
+ env->inspector_agent()->Start(args.size() > 1 ? args[1].c_str() : "",
+ env->options()->debug_options(),
+ env->inspector_host_port(),
+ true);
+ if (env->options()->debug_options().inspector_enabled &&
+ !env->inspector_agent()->IsListening()) {
+ *exit_code = 12; // Signal internal error.
+ return env;
}
#else
// inspector_enabled can't be true if !HAVE_INSPECTOR or !NODE_USE_V8_PLATFORM
// - the option parser should not allow that.
- CHECK(!env.options()->debug_options().inspector_enabled);
+ CHECK(!env->options()->debug_options().inspector_enabled);
#endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
- {
- AsyncCallbackScope callback_scope(&env);
- env.async_hooks()->push_async_ids(1, 0);
- LoadEnvironment(&env);
- env.async_hooks()->pop_async_id(1);
+ if (RunBootstrapping(env.get()).IsEmpty()) {
+ *exit_code = 1;
}
- {
- SealHandleScope seal(isolate);
- bool more;
- env.performance_state()->Mark(
- node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
- do {
- uv_run(env.event_loop(), UV_RUN_DEFAULT);
+ return env;
+}
- per_process::v8_platform.DrainVMTasks(isolate);
+inline int StartNodeWithIsolate(Isolate* isolate,
+ IsolateData* isolate_data,
+ const std::vector<std::string>& args,
+ const std::vector<std::string>& exec_args) {
+ int exit_code = 0;
+ std::unique_ptr<Environment> env =
+ CreateMainEnvironment(isolate_data, args, exec_args, &exit_code);
+ CHECK_NOT_NULL(env);
+ HandleScope handle_scope(env->isolate());
+ Context::Scope context_scope(env->context());
+
+ if (exit_code == 0) {
+ {
+ AsyncCallbackScope callback_scope(env.get());
+ env->async_hooks()->push_async_ids(1, 0);
+ LoadEnvironment(env.get());
+ env->async_hooks()->pop_async_id(1);
+ }
- more = uv_loop_alive(env.event_loop());
- if (more && !env.is_stopping()) continue;
+ {
+ SealHandleScope seal(isolate);
+ bool more;
+ env->performance_state()->Mark(
+ node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
+ do {
+ uv_run(env->event_loop(), UV_RUN_DEFAULT);
- RunBeforeExit(&env);
+ per_process::v8_platform.DrainVMTasks(isolate);
- // Emit `beforeExit` if the loop became alive either after emitting
- // event, or after running some callbacks.
- more = uv_loop_alive(env.event_loop());
- } while (more == true && !env.is_stopping());
- env.performance_state()->Mark(
- node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
- }
+ more = uv_loop_alive(env->event_loop());
+ if (more && !env->is_stopping()) continue;
- env.set_trace_sync_io(false);
+ RunBeforeExit(env.get());
- exit_code = EmitExit(&env);
+ // Emit `beforeExit` if the loop became alive either after emitting
+ // event, or after running some callbacks.
+ more = uv_loop_alive(env->event_loop());
+ } while (more == true && !env->is_stopping());
+ env->performance_state()->Mark(
+ node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
+ }
- WaitForInspectorDisconnect(&env);
+ env->set_trace_sync_io(false);
+ exit_code = EmitExit(env.get());
+ WaitForInspectorDisconnect(env.get());
+ }
-#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
-exit:
-#endif
- env.set_can_call_into_js(false);
- env.stop_sub_worker_contexts();
+ env->set_can_call_into_js(false);
+ env->stop_sub_worker_contexts();
uv_tty_reset_mode();
- env.RunCleanup();
- RunAtExit(&env);
+ env->RunCleanup();
+ RunAtExit(env.get());
per_process::v8_platform.DrainVMTasks(isolate);
per_process::v8_platform.CancelVMTasks(isolate);
+
#if defined(LEAK_SANITIZER)
__lsan_do_leak_check();
#endif
@@ -891,11 +915,6 @@ inline int StartNodeWithLoopAndArgs(uv_loop_t* event_loop,
per_process::v8_platform.Platform(),
allocator.get()),
&FreeIsolateData);
- // TODO(addaleax): This should load a real per-Isolate option, currently
- // this is still effectively per-process.
- if (isolate_data->options()->track_heap_objects) {
- isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
- }
exit_code =
StartNodeWithIsolate(isolate, isolate_data.get(), args, exec_args);
}