Skip to content

Commit ceb6023

Browse files
committed
src: clean up program/isolate/env init logic
Reorder the initialization logic so that program-wide, per-isolate and per-environment initialization is more cleanly separated. PR-URL: #9224 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d77e818 commit ceb6023

File tree

2 files changed

+81
-82
lines changed

2 files changed

+81
-82
lines changed

src/env.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ void Environment::Start(int argc,
3030
HandleScope handle_scope(isolate());
3131
Context::Scope context_scope(context());
3232

33-
isolate()->SetAutorunMicrotasks(false);
34-
3533
uv_check_init(event_loop(), immediate_check_handle());
3634
uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
3735

src/node.cc

Lines changed: 81 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,11 +3341,6 @@ void SetupProcessObject(Environment* env,
33413341
#undef READONLY_PROPERTY
33423342

33433343

3344-
static void AtProcessExit() {
3345-
uv_tty_reset_mode();
3346-
}
3347-
3348-
33493344
void SignalExit(int signo) {
33503345
uv_tty_reset_mode();
33513346
#ifdef __FreeBSD__
@@ -3375,11 +3370,6 @@ static void RawDebug(const FunctionCallbackInfo<Value>& args) {
33753370
void LoadEnvironment(Environment* env) {
33763371
HandleScope handle_scope(env->isolate());
33773372

3378-
env->isolate()->SetFatalErrorHandler(node::OnFatalError);
3379-
env->isolate()->AddMessageListener(OnMessage);
3380-
3381-
atexit(AtProcessExit);
3382-
33833373
TryCatch try_catch(env->isolate());
33843374

33853375
// Disable verbose mode to stop FatalException() handler from trying
@@ -4374,107 +4364,118 @@ void FreeEnvironment(Environment* env) {
43744364
}
43754365

43764366

4367+
inline int Start(Isolate* isolate, IsolateData* isolate_data,
4368+
int argc, const char* const* argv,
4369+
int exec_argc, const char* const* exec_argv) {
4370+
HandleScope handle_scope(isolate);
4371+
Local<Context> context = Context::New(isolate);
4372+
Context::Scope context_scope(context);
4373+
Environment env(isolate_data, context);
4374+
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
4375+
4376+
// Start debug agent when argv has --debug
4377+
if (use_debug_agent) {
4378+
const char* path = argc > 1 ? argv[1] : nullptr;
4379+
StartDebug(&env, path, debug_wait_connect);
4380+
if (use_inspector && !debugger_running)
4381+
return 12; // Signal internal error.
4382+
}
4383+
4384+
{
4385+
Environment::AsyncCallbackScope callback_scope(&env);
4386+
LoadEnvironment(&env);
4387+
}
4388+
4389+
env.set_trace_sync_io(trace_sync_io);
4390+
4391+
// Enable debugger
4392+
if (use_debug_agent)
4393+
EnableDebug(&env);
4394+
4395+
{
4396+
SealHandleScope seal(isolate);
4397+
bool more;
4398+
do {
4399+
v8_platform.PumpMessageLoop(isolate);
4400+
more = uv_run(env.event_loop(), UV_RUN_ONCE);
4401+
4402+
if (more == false) {
4403+
v8_platform.PumpMessageLoop(isolate);
4404+
EmitBeforeExit(&env);
4405+
4406+
// Emit `beforeExit` if the loop became alive either after emitting
4407+
// event, or after running some callbacks.
4408+
more = uv_loop_alive(env.event_loop());
4409+
if (uv_run(env.event_loop(), UV_RUN_NOWAIT) != 0)
4410+
more = true;
4411+
}
4412+
} while (more == true);
4413+
}
4414+
4415+
env.set_trace_sync_io(false);
4416+
4417+
const int exit_code = EmitExit(&env);
4418+
RunAtExit(&env);
4419+
4420+
WaitForInspectorDisconnect(&env);
4421+
#if defined(LEAK_SANITIZER)
4422+
__lsan_do_leak_check();
4423+
#endif
4424+
4425+
return exit_code;
4426+
}
4427+
43774428
inline int Start(uv_loop_t* event_loop,
43784429
int argc, const char* const* argv,
43794430
int exec_argc, const char* const* exec_argv) {
43804431
Isolate::CreateParams params;
4381-
ArrayBufferAllocator array_buffer_allocator;
4382-
params.array_buffer_allocator = &array_buffer_allocator;
4432+
ArrayBufferAllocator allocator;
4433+
params.array_buffer_allocator = &allocator;
43834434
#ifdef NODE_ENABLE_VTUNE_PROFILING
43844435
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
43854436
#endif
4386-
Isolate* isolate = Isolate::New(params);
4437+
4438+
Isolate* const isolate = Isolate::New(params);
4439+
if (isolate == nullptr)
4440+
return 12; // Signal internal error.
4441+
4442+
isolate->AddMessageListener(OnMessage);
4443+
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
4444+
isolate->SetAutorunMicrotasks(false);
4445+
isolate->SetFatalErrorHandler(OnFatalError);
4446+
4447+
if (track_heap_objects) {
4448+
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
4449+
}
43874450

43884451
{
43894452
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
43904453
CHECK_EQ(node_isolate, nullptr);
43914454
node_isolate = isolate;
43924455
}
43934456

4394-
if (track_heap_objects) {
4395-
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
4396-
}
4397-
43984457
int exit_code;
43994458
{
44004459
Locker locker(isolate);
44014460
Isolate::Scope isolate_scope(isolate);
44024461
HandleScope handle_scope(isolate);
4403-
IsolateData isolate_data(isolate, event_loop,
4404-
array_buffer_allocator.zero_fill_field());
4405-
Local<Context> context = Context::New(isolate);
4406-
Context::Scope context_scope(context);
4407-
Environment env(&isolate_data, context);
4408-
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
4409-
4410-
isolate->SetAbortOnUncaughtExceptionCallback(
4411-
ShouldAbortOnUncaughtException);
4412-
4413-
// Start debug agent when argv has --debug
4414-
if (use_debug_agent) {
4415-
const char* path = argc > 1 ? argv[1] : nullptr;
4416-
StartDebug(&env, path, debug_wait_connect);
4417-
if (use_inspector && !debugger_running) {
4418-
exit(12);
4419-
}
4420-
}
4421-
4422-
{
4423-
Environment::AsyncCallbackScope callback_scope(&env);
4424-
LoadEnvironment(&env);
4425-
}
4426-
4427-
env.set_trace_sync_io(trace_sync_io);
4428-
4429-
// Enable debugger
4430-
if (use_debug_agent)
4431-
EnableDebug(&env);
4432-
4433-
{
4434-
SealHandleScope seal(isolate);
4435-
bool more;
4436-
do {
4437-
v8_platform.PumpMessageLoop(isolate);
4438-
more = uv_run(env.event_loop(), UV_RUN_ONCE);
4439-
4440-
if (more == false) {
4441-
v8_platform.PumpMessageLoop(isolate);
4442-
EmitBeforeExit(&env);
4443-
4444-
// Emit `beforeExit` if the loop became alive either after emitting
4445-
// event, or after running some callbacks.
4446-
more = uv_loop_alive(env.event_loop());
4447-
if (uv_run(env.event_loop(), UV_RUN_NOWAIT) != 0)
4448-
more = true;
4449-
}
4450-
} while (more == true);
4451-
}
4452-
4453-
env.set_trace_sync_io(false);
4454-
4455-
exit_code = EmitExit(&env);
4456-
RunAtExit(&env);
4457-
4458-
WaitForInspectorDisconnect(&env);
4459-
#if defined(LEAK_SANITIZER)
4460-
__lsan_do_leak_check();
4461-
#endif
4462+
IsolateData isolate_data(isolate, event_loop, allocator.zero_fill_field());
4463+
exit_code = Start(isolate, &isolate_data, argc, argv, exec_argc, exec_argv);
44624464
}
44634465

44644466
{
44654467
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
4466-
if (node_isolate == isolate)
4467-
node_isolate = nullptr;
4468+
CHECK_EQ(node_isolate, isolate);
4469+
node_isolate = nullptr;
44684470
}
44694471

4470-
CHECK_NE(isolate, nullptr);
44714472
isolate->Dispose();
4472-
isolate = nullptr;
44734473

44744474
return exit_code;
44754475
}
44764476

44774477
int Start(int argc, char** argv) {
4478+
atexit([] () { uv_tty_reset_mode(); });
44784479
PlatformInit();
44794480

44804481
CHECK_GT(argc, 0);

0 commit comments

Comments
 (0)