Skip to content

Commit 8ebf40e

Browse files
committed
src: split the main context initialization from Environemnt ctor
1 parent 36c687f commit 8ebf40e

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/env.cc

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -330,28 +330,27 @@ std::string GetExecPath(const std::vector<std::string>& argv) {
330330
}
331331

332332
Environment::Environment(IsolateData* isolate_data,
333-
Local<Context> context,
333+
Isolate* isolate,
334334
const std::vector<std::string>& args,
335335
const std::vector<std::string>& exec_args,
336336
EnvironmentFlags::Flags flags,
337337
ThreadId thread_id)
338-
: isolate_(context->GetIsolate()),
338+
: isolate_(isolate),
339339
isolate_data_(isolate_data),
340-
immediate_info_(context->GetIsolate()),
341-
tick_info_(context->GetIsolate()),
340+
immediate_info_(isolate),
341+
tick_info_(isolate),
342342
timer_base_(uv_now(isolate_data->event_loop())),
343343
exec_argv_(exec_args),
344344
argv_(args),
345345
exec_path_(GetExecPath(args)),
346346
should_abort_on_uncaught_toggle_(isolate_, 1),
347347
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
348348
flags_(flags),
349-
thread_id_(thread_id.id == static_cast<uint64_t>(-1) ?
350-
AllocateEnvironmentThreadId().id : thread_id.id),
351-
context_(context->GetIsolate(), context) {
349+
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
350+
? AllocateEnvironmentThreadId().id
351+
: thread_id.id) {
352352
// We'll be creating new objects so make sure we've entered the context.
353-
HandleScope handle_scope(isolate());
354-
Context::Scope context_scope(context);
353+
HandleScope handle_scope(isolate);
355354

356355
// Set some flags if only kDefaultFlags was passed. This can make API version
357356
// transitions easier for embedders.
@@ -362,6 +361,8 @@ Environment::Environment(IsolateData* isolate_data,
362361
}
363362

364363
set_env_vars(per_process::system_environment);
364+
// TODO(joyeecheung): pass Isolate* and env_vars to it instead of the entire
365+
// env
365366
enabled_debug_list_.Parse(this);
366367

367368
// We create new copies of the per-Environment option sets, so that it is
@@ -377,8 +378,6 @@ Environment::Environment(IsolateData* isolate_data,
377378
inspector_agent_ = std::make_unique<inspector::Agent>(this);
378379
#endif
379380

380-
AssignToContext(context, ContextInfo(""));
381-
382381
static uv_once_t init_once = UV_ONCE_INIT;
383382
uv_once(&init_once, InitThreadLocalOnce);
384383
uv_key_set(&thread_local_env, this);
@@ -398,8 +397,7 @@ Environment::Environment(IsolateData* isolate_data,
398397
},
399398
this);
400399

401-
performance_state_ =
402-
std::make_unique<performance::PerformanceState>(isolate());
400+
performance_state_ = std::make_unique<performance::PerformanceState>(isolate);
403401
performance_state_->Mark(
404402
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
405403
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
@@ -431,6 +429,29 @@ Environment::Environment(IsolateData* isolate_data,
431429
async_hooks_.no_force_checks();
432430
}
433431

432+
// This adjusts the return value of base_object_count() so that tests that
433+
// check the count do not have to account for internally created BaseObjects.
434+
initial_base_object_count_ = base_object_count();
435+
}
436+
437+
Environment::Environment(IsolateData* isolate_data,
438+
Local<Context> context,
439+
const std::vector<std::string>& args,
440+
const std::vector<std::string>& exec_args,
441+
EnvironmentFlags::Flags flags,
442+
ThreadId thread_id)
443+
: Environment(isolate_data,
444+
context->GetIsolate(),
445+
args,
446+
exec_args,
447+
flags,
448+
thread_id) {
449+
InitializeMainContext(context);
450+
}
451+
452+
void Environment::InitializeMainContext(Local<Context> context) {
453+
context_.Reset(context->GetIsolate(), context);
454+
AssignToContext(context, ContextInfo(""));
434455
// TODO(joyeecheung): deserialize when the snapshot covers the environment
435456
// properties.
436457
CreateProperties();

src/env.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,16 @@ class Environment : public MemoryRetainer {
908908
static uv_key_t thread_local_env;
909909
static inline Environment* GetThreadLocalEnv();
910910

911+
// Initialize the Environment but without a valid Context. Use
912+
// InitializeMainContext() to continue initialize the Environment fully.
913+
Environment(IsolateData* isolate_data,
914+
v8::Isolate* isolate,
915+
const std::vector<std::string>& args,
916+
const std::vector<std::string>& exec_args,
917+
EnvironmentFlags::Flags flags,
918+
ThreadId thread_id);
919+
void InitializeMainContext(v8::Local<v8::Context> context);
920+
// Fully initialize the Environment, including the main context.
911921
Environment(IsolateData* isolate_data,
912922
v8::Local<v8::Context> context,
913923
const std::vector<std::string>& args,

0 commit comments

Comments
 (0)