Skip to content

Commit 404302f

Browse files
committed
src: split the main context initialization from Environemnt ctor
So that it's possible to create an Environment not yet attached to any V8 context. We'll use this to deserialize the V8 context before attaching an Environment to it. PR-URL: #32984 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
1 parent d379b00 commit 404302f

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

src/api/environment.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,7 @@ Environment* CreateEnvironment(
356356
exec_args,
357357
flags,
358358
thread_id);
359-
if (flags & EnvironmentFlags::kOwnsProcessState) {
360-
env->set_abort_on_uncaught_exception(false);
361-
}
362-
359+
env->InitializeMainContext(context);
363360
#if HAVE_INSPECTOR
364361
if (inspector_parent_handle) {
365362
env->InitializeInspector(

src/env.cc

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,28 +306,27 @@ std::string GetExecPath(const std::vector<std::string>& argv) {
306306
}
307307

308308
Environment::Environment(IsolateData* isolate_data,
309-
Local<Context> context,
309+
Isolate* isolate,
310310
const std::vector<std::string>& args,
311311
const std::vector<std::string>& exec_args,
312312
EnvironmentFlags::Flags flags,
313313
ThreadId thread_id)
314-
: isolate_(context->GetIsolate()),
314+
: isolate_(isolate),
315315
isolate_data_(isolate_data),
316-
immediate_info_(context->GetIsolate()),
317-
tick_info_(context->GetIsolate()),
316+
immediate_info_(isolate),
317+
tick_info_(isolate),
318318
timer_base_(uv_now(isolate_data->event_loop())),
319319
exec_argv_(exec_args),
320320
argv_(args),
321321
exec_path_(GetExecPath(args)),
322322
should_abort_on_uncaught_toggle_(isolate_, 1),
323323
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
324324
flags_(flags),
325-
thread_id_(thread_id.id == static_cast<uint64_t>(-1) ?
326-
AllocateEnvironmentThreadId().id : thread_id.id),
327-
context_(context->GetIsolate(), context) {
325+
thread_id_(thread_id.id == static_cast<uint64_t>(-1)
326+
? AllocateEnvironmentThreadId().id
327+
: thread_id.id) {
328328
// We'll be creating new objects so make sure we've entered the context.
329-
HandleScope handle_scope(isolate());
330-
Context::Scope context_scope(context);
329+
HandleScope handle_scope(isolate);
331330

332331
// Set some flags if only kDefaultFlags was passed. This can make API version
333332
// transitions easier for embedders.
@@ -338,6 +337,8 @@ Environment::Environment(IsolateData* isolate_data,
338337
}
339338

340339
set_env_vars(per_process::system_environment);
340+
// TODO(joyeecheung): pass Isolate* and env_vars to it instead of the entire
341+
// env
341342
enabled_debug_list_.Parse(this);
342343

343344
// We create new copies of the per-Environment option sets, so that it is
@@ -348,13 +349,15 @@ Environment::Environment(IsolateData* isolate_data,
348349
inspector_host_port_.reset(
349350
new ExclusiveAccess<HostPort>(options_->debug_options().host_port));
350351

352+
if (flags & EnvironmentFlags::kOwnsProcessState) {
353+
set_abort_on_uncaught_exception(false);
354+
}
355+
351356
#if HAVE_INSPECTOR
352357
// We can only create the inspector agent after having cloned the options.
353358
inspector_agent_ = std::make_unique<inspector::Agent>(this);
354359
#endif
355360

356-
AssignToContext(context, ContextInfo(""));
357-
358361
static uv_once_t init_once = UV_ONCE_INIT;
359362
uv_once(&init_once, InitThreadLocalOnce);
360363
uv_key_set(&thread_local_env, this);
@@ -367,8 +370,7 @@ Environment::Environment(IsolateData* isolate_data,
367370

368371
destroy_async_id_list_.reserve(512);
369372

370-
performance_state_ =
371-
std::make_unique<performance::PerformanceState>(isolate());
373+
performance_state_ = std::make_unique<performance::PerformanceState>(isolate);
372374
performance_state_->Mark(
373375
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
374376
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
@@ -400,6 +402,29 @@ Environment::Environment(IsolateData* isolate_data,
400402
async_hooks_.no_force_checks();
401403
}
402404

405+
// This adjusts the return value of base_object_count() so that tests that
406+
// check the count do not have to account for internally created BaseObjects.
407+
initial_base_object_count_ = base_object_count();
408+
}
409+
410+
Environment::Environment(IsolateData* isolate_data,
411+
Local<Context> context,
412+
const std::vector<std::string>& args,
413+
const std::vector<std::string>& exec_args,
414+
EnvironmentFlags::Flags flags,
415+
ThreadId thread_id)
416+
: Environment(isolate_data,
417+
context->GetIsolate(),
418+
args,
419+
exec_args,
420+
flags,
421+
thread_id) {
422+
InitializeMainContext(context);
423+
}
424+
425+
void Environment::InitializeMainContext(Local<Context> context) {
426+
context_.Reset(context->GetIsolate(), context);
427+
AssignToContext(context, ContextInfo(""));
403428
// TODO(joyeecheung): deserialize when the snapshot covers the environment
404429
// properties.
405430
CreateProperties();

src/env.h

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

922+
// Create an Environment without initializing a main Context. Use
923+
// InitializeMainContext() to initialize a main context for it.
924+
Environment(IsolateData* isolate_data,
925+
v8::Isolate* isolate,
926+
const std::vector<std::string>& args,
927+
const std::vector<std::string>& exec_args,
928+
EnvironmentFlags::Flags flags,
929+
ThreadId thread_id);
930+
void InitializeMainContext(v8::Local<v8::Context> context);
931+
// Create an Environment and initialize the provided main context for it.
922932
Environment(IsolateData* isolate_data,
923933
v8::Local<v8::Context> context,
924934
const std::vector<std::string>& args,

0 commit comments

Comments
 (0)