Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bootstrap: reorganize the bootstrap process #27539

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
Environment* env = new Environment(
isolate_data,
context,
args,
exec_args,
static_cast<Environment::Flags>(Environment::kIsMainThread |
Environment::kOwnsProcessState |
Environment::kOwnsInspector));
env->InitializeLibuv(per_process::v8_is_profiling);
env->ProcessCliArgs(args, exec_args);
if (RunBootstrapping(env).IsEmpty()) {
if (env->RunBootstrapping().IsEmpty()) {
return nullptr;
}

Expand Down
91 changes: 50 additions & 41 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,45 @@ uint64_t Environment::AllocateThreadId() {
return next_thread_id++;
}

void Environment::CreateProperties() {
HandleScope handle_scope(isolate_);
Local<Context> ctx = context();
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(1);
Local<Object> obj = templ->GetFunction(ctx)
.ToLocalChecked()
->NewInstance(ctx)
.ToLocalChecked();
obj->SetAlignedPointerInInternalField(0, this);
set_as_callback_data(obj);
set_as_callback_data_template(templ);

// Store primordials setup by the per-context script in the environment.
Local<Object> per_context_bindings =
GetPerContextExports(ctx).ToLocalChecked();
Local<Value> primordials =
per_context_bindings->Get(ctx, primordials_string()).ToLocalChecked();
CHECK(primordials->IsObject());
set_primordials(primordials.As<Object>());

Local<Object> process_object =
node::CreateProcessObject(this).FromMaybe(Local<Object>());
set_process_object(process_object);
}

Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
Flags flags,
uint64_t thread_id)
: isolate_(context->GetIsolate()),
isolate_data_(isolate_data),
immediate_info_(context->GetIsolate()),
tick_info_(context->GetIsolate()),
timer_base_(uv_now(isolate_data->event_loop())),
exec_argv_(exec_args),
argv_(args),
should_abort_on_uncaught_toggle_(isolate_, 1),
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
flags_(flags),
Expand All @@ -253,16 +283,6 @@ Environment::Environment(IsolateData* isolate_data,
// We'll be creating new objects so make sure we've entered the context.
HandleScope handle_scope(isolate());
Context::Scope context_scope(context);
{
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
templ->InstanceTemplate()->SetInternalFieldCount(1);
Local<Object> obj =
templ->GetFunction(context).ToLocalChecked()->NewInstance(
context).ToLocalChecked();
obj->SetAlignedPointerInInternalField(0, this);
set_as_callback_data(obj);
set_as_callback_data_template(templ);
}

set_env_vars(per_process::system_environment);

Expand Down Expand Up @@ -305,18 +325,36 @@ Environment::Environment(IsolateData* isolate_data,
performance::NODE_PERFORMANCE_MILESTONE_V8_START,
performance::performance_v8_start);

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
this,
"args",
std::move(traced_value));
}

// By default, always abort when --abort-on-uncaught-exception was passed.
should_abort_on_uncaught_toggle_[0] = 1;

std::string debug_cats;
credentials::SafeGetenv("NODE_DEBUG_NATIVE", &debug_cats, this);
set_debug_categories(debug_cats, true);

isolate()->GetHeapProfiler()->AddBuildEmbedderGraphCallback(
BuildEmbedderGraph, this);
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
if (options_->no_force_async_hooks_checks) {
async_hooks_.no_force_checks();
}

// TODO(joyeecheung): deserialize when the snapshot covers the environment
// properties.
CreateProperties();
}

CompileFnEntry::CompileFnEntry(Environment* env, uint32_t id)
Expand Down Expand Up @@ -433,35 +471,6 @@ void Environment::ExitEnv() {
isolate_->TerminateExecution();
}

MaybeLocal<Object> Environment::ProcessCliArgs(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
argv_ = args;
exec_argv_ = exec_args;

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
this,
"args",
std::move(traced_value));
}

Local<Object> process_object =
node::CreateProcessObject(this, args, exec_args)
.FromMaybe(Local<Object>());
set_process_object(process_object);
return process_object;
}

void Environment::RegisterHandleCleanups() {
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
void* arg) {
Expand Down
22 changes: 19 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace profiler {
class V8CoverageConnection;
class V8CpuProfilerConnection;
} // namespace profiler

namespace inspector {
class ParentInspectorHandle;
}
#endif // HAVE_INSPECTOR

namespace worker {
Expand Down Expand Up @@ -794,6 +798,19 @@ class Environment : public MemoryRetainer {
bool IsRootNode() const override { return true; }
void MemoryInfo(MemoryTracker* tracker) const override;

void CreateProperties();
// Should be called before InitializeInspector()
void InitializeDiagnostics();
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
// If the environment is created for a worker, pass parent_handle and
// the ownership if transferred into the Environment.
int InitializeInspector(inspector::ParentInspectorHandle* parent_handle);
#endif

v8::MaybeLocal<v8::Value> BootstrapInternalLoaders();
v8::MaybeLocal<v8::Value> BootstrapNode();
v8::MaybeLocal<v8::Value> RunBootstrapping();

inline size_t async_callback_scope_depth() const;
inline void PushAsyncCallbackScope();
inline void PopAsyncCallbackScope();
Expand Down Expand Up @@ -821,14 +838,13 @@ class Environment : public MemoryRetainer {

Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
Flags flags = Flags(),
uint64_t thread_id = kNoThreadId);
~Environment();

void InitializeLibuv(bool start_profiler_idle_notifier);
v8::MaybeLocal<v8::Object> ProcessCliArgs(
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args);
inline const std::vector<std::string>& exec_argv();
inline const std::vector<std::string>& argv();

Expand Down
1 change: 1 addition & 0 deletions src/inspector/worker_inspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ParentInspectorHandle {
bool WaitForConnect() {
return wait_;
}
const std::string& url() const { return url_; }

private:
int id_;
Expand Down
Loading