Skip to content

Commit f2a3b24

Browse files
bnoordhuisevanlucas
authored andcommitted
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 <[email protected]>
1 parent 9e753ba commit f2a3b24

File tree

2 files changed

+81
-82
lines changed

2 files changed

+81
-82
lines changed

src/env.cc

-2
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

+81-80
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
@@ -4369,107 +4359,118 @@ void FreeEnvironment(Environment* env) {
43694359
}
43704360

43714361

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

43834446
{
43844447
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
43854448
CHECK_EQ(node_isolate, nullptr);
43864449
node_isolate = isolate;
43874450
}
43884451

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

44594461
{
44604462
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
4461-
if (node_isolate == isolate)
4462-
node_isolate = nullptr;
4463+
CHECK_EQ(node_isolate, isolate);
4464+
node_isolate = nullptr;
44634465
}
44644466

4465-
CHECK_NE(isolate, nullptr);
44664467
isolate->Dispose();
4467-
isolate = nullptr;
44684468

44694469
return exit_code;
44704470
}
44714471

44724472
int Start(int argc, char** argv) {
4473+
atexit([] () { uv_tty_reset_mode(); });
44734474
PlatformInit();
44744475

44754476
CHECK_GT(argc, 0);

0 commit comments

Comments
 (0)