From d25f058f90b886b5fd1017759d5a9a1a9a7ea8ca Mon Sep 17 00:00:00 2001 From: ishabi Date: Mon, 23 Nov 2026 17:03:17 +0100 Subject: [PATCH] src: use V8 using declarations consistently Signed-off-by: ishabi --- src/env.cc | 75 ++++--- src/node_v8.cc | 58 +++--- src/node_worker.cc | 25 +-- src/util.cc | 477 +++++++++++++++++++++++---------------------- 4 files changed, 332 insertions(+), 303 deletions(-) diff --git a/src/env.cc b/src/env.cc index 8dabc275dfd6..e0fd170585a3 100644 --- a/src/env.cc +++ b/src/env.cc @@ -53,18 +53,30 @@ using v8::BackingStore; using v8::BackingStoreInitializationMode; using v8::Boolean; using v8::Context; +using v8::CppHeapPointerTag; +using v8::CpuProfile; +using v8::CpuProfiler; +using v8::CpuProfilingMode; +using v8::CpuProfilingOptions; +using v8::CpuProfilingResult; +using v8::CpuProfilingStatus; using v8::EmbedderGraph; using v8::EscapableHandleScope; using v8::ExternalMemoryAccounter; using v8::Function; +using v8::GCCallbackFlags; +using v8::GCType; using v8::Global; using v8::HandleScope; using v8::HeapProfiler; using v8::HeapSpaceStatistics; +using v8::HeapStatistics; using v8::Integer; using v8::Isolate; +using v8::Just; using v8::JustVoid; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::NewStringType; @@ -73,6 +85,7 @@ using v8::Number; using v8::Object; using v8::ObjectTemplate; using v8::Private; +using v8::ProfilerId; using v8::Promise; using v8::PromiseHookType; using v8::Script; @@ -103,7 +116,7 @@ void AsyncHooks::ResetPromiseHooks(Local init, } Local AsyncHooks::GetPromiseHooks(Isolate* isolate) const { - v8::LocalVector values(isolate, js_promise_hooks_.size()); + LocalVector values(isolate, js_promise_hooks_.size()); for (size_t i = 0; i < js_promise_hooks_.size(); ++i) { if (js_promise_hooks_[i].IsEmpty()) { values[i] = Undefined(isolate); @@ -522,7 +535,7 @@ void IsolateData::CreateProperties() { // One byte because our strings are ASCII and we can safely skip V8's UTF-8 // decoding step. - v8::Isolate::Scope isolate_scope(isolate_); + Isolate::Scope isolate_scope(isolate_); HandleScope handle_scope(isolate_); #define V(PropertyName, StringValue) \ @@ -682,9 +695,8 @@ IsolateData::~IsolateData() { // Deprecated API, embedders should use v8::Object::Wrap() directly instead. void SetCppgcReference(Isolate* isolate, Local object, - v8::Object::Wrappable* wrappable) { - v8::Object::Wrap( - isolate, object, wrappable); + Object::Wrappable* wrappable) { + Object::Wrap(isolate, object, wrappable); } void IsolateData::MemoryInfo(MemoryTracker* tracker) const { @@ -740,7 +752,7 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() { USE(cb->Call(env_->context(), Undefined(isolate), arraysize(args), args)); } -void Environment::AssignToContext(Local context, +void Environment::AssignToContext(Local context, Realm* realm, const ContextInfo& info) { context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment, @@ -766,7 +778,7 @@ void Environment::AssignToContext(Local context, TrackContext(context); } -void Environment::UnassignFromContext(Local context) { +void Environment::UnassignFromContext(Local context) { if (!context.IsEmpty()) { context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment, nullptr, @@ -1592,7 +1604,7 @@ MaybeLocal Environment::RunSnapshotSerializeCallback() const { if (!snapshot_serialize_callback().IsEmpty()) { Context::Scope context_scope(context()); return handle_scope.EscapeMaybe(snapshot_serialize_callback()->Call( - context(), v8::Undefined(isolate()), 0, nullptr)); + context(), Undefined(isolate()), 0, nullptr)); } return handle_scope.Escape(Undefined(isolate())); } @@ -1602,7 +1614,7 @@ MaybeLocal Environment::RunSnapshotDeserializeMain() const { if (!snapshot_deserialize_main().IsEmpty()) { Context::Scope context_scope(context()); return handle_scope.EscapeMaybe(snapshot_deserialize_main()->Call( - context(), v8::Undefined(isolate()), 0, nullptr)); + context(), Undefined(isolate()), 0, nullptr)); } return handle_scope.Escape(Undefined(isolate())); } @@ -1658,23 +1670,23 @@ Maybe Environment::CheckUnsettledTopLevelAwait() const { if (!ctx->Global() ->GetPrivate(ctx, entry_point_promise_private_symbol()) .ToLocal(&entry_point_promise)) { - return v8::Nothing(); + return Nothing(); } if (!entry_point_promise->IsPromise()) { - return v8::Just(true); + return Just(true); } if (entry_point_promise.As()->State() != Promise::PromiseState::kPending) { - return v8::Just(true); + return Just(true); } if (!ctx->Global() ->GetPrivate(ctx, entry_point_module_private_symbol()) .ToLocal(&value)) { - return v8::Nothing(); + return Nothing(); } if (!value->IsObject()) { - return v8::Just(true); + return Just(true); } Local object = value.As(); CHECK(BaseObject::IsBaseObject(isolate_data_, object)); @@ -2409,8 +2421,9 @@ void Environment::AddPointerCompressionHeapWarningCallback() { pointer_compression_heap_warning_callback_added_ = true; // Only full GCs: the heap that survives a mark-compact is the closest // approximation of the live heap, and young-generation GCs are frequent. - isolate_->AddGCEpilogueCallback( - PointerCompressionHeapWarningCallback, this, v8::kGCTypeMarkSweepCompact); + isolate_->AddGCEpilogueCallback(PointerCompressionHeapWarningCallback, + this, + GCType::kGCTypeMarkSweepCompact); } void Environment::RemovePointerCompressionHeapWarningCallback() { @@ -2420,12 +2433,14 @@ void Environment::RemovePointerCompressionHeapWarningCallback() { this); } -void Environment::PointerCompressionHeapWarningCallback( - Isolate* isolate, v8::GCType type, v8::GCCallbackFlags flags, void* data) { +void Environment::PointerCompressionHeapWarningCallback(Isolate* isolate, + GCType type, + GCCallbackFlags flags, + void* data) { Environment* env = static_cast(data); if (env->pointer_compression_heap_warning_scheduled_) return; - v8::HeapStatistics stats; + HeapStatistics stats; isolate->GetHeapStatistics(&stats); const uint64_t used = stats.used_heap_size(); if (used < env->pointer_compression_heap_likely_threshold_ && @@ -2757,25 +2772,23 @@ void Environment::RunWeakRefCleanup() { isolate()->ClearKeptObjects(); } -v8::CpuProfilingResult Environment::StartCpuProfile( +CpuProfilingResult Environment::StartCpuProfile( const CpuProfileOptions& options) { HandleScope handle_scope(isolate()); if (!cpu_profiler_) { - cpu_profiler_ = v8::CpuProfiler::New(isolate()); - } - v8::CpuProfilingOptions start_options( - v8::CpuProfilingMode::kLeafNodeLineNumbers, - options.max_samples, - options.sampling_interval_us); - v8::CpuProfilingResult result = - cpu_profiler_->Start(std::move(start_options)); - if (result.status == v8::CpuProfilingStatus::kStarted) { + cpu_profiler_ = CpuProfiler::New(isolate()); + } + CpuProfilingOptions start_options(CpuProfilingMode::kLeafNodeLineNumbers, + options.max_samples, + options.sampling_interval_us); + CpuProfilingResult result = cpu_profiler_->Start(std::move(start_options)); + if (result.status == CpuProfilingStatus::kStarted) { pending_profiles_.push_back(result.id); } return result; } -v8::CpuProfile* Environment::StopCpuProfile(v8::ProfilerId profile_id) { +CpuProfile* Environment::StopCpuProfile(ProfilerId profile_id) { if (!cpu_profiler_) { return nullptr; } @@ -2784,7 +2797,7 @@ v8::CpuProfile* Environment::StopCpuProfile(v8::ProfilerId profile_id) { if (it == pending_profiles_.end()) { return nullptr; } - v8::CpuProfile* profile = cpu_profiler_->Stop(*it); + CpuProfile* profile = cpu_profiler_->Stop(*it); pending_profiles_.erase(it); return profile; } diff --git a/src/node_v8.cc b/src/node_v8.cc index 9758826ae8b3..49e4a0ca1134 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -44,8 +44,11 @@ using v8::CpuProfilingStatus; using v8::DictionaryTemplate; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; +using v8::GCCallbackFlags; +using v8::GCType; using v8::HandleScope; using v8::HeapCodeStatistics; +using v8::HeapProfiler; using v8::HeapSpaceStatistics; using v8::HeapStatistics; using v8::Integer; @@ -56,6 +59,7 @@ using v8::MaybeLocal; using v8::Number; using v8::Object; using v8::ScriptCompiler; +using v8::SnapshotCreator; using v8::String; using v8::Uint32; using v8::V8; @@ -147,7 +151,7 @@ BindingData::BindingData(Realm* realm, } bool BindingData::PrepareForSerialization(Local context, - v8::SnapshotCreator* creator) { + SnapshotCreator* creator) { DCHECK_NULL(internal_field_info_); internal_field_info_ = InternalFieldInfoBase::New(type()); internal_field_info_->heap_statistics_buffer = @@ -191,7 +195,7 @@ void CachedDataVersionTag(const FunctionCallbackInfo& args) { void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo& args) { CHECK(args[0]->IsUint32()); Environment* env = Environment::GetCurrent(args); - uint32_t limit = args[0].As()->Value(); + uint32_t limit = args[0].As()->Value(); CHECK_GT(limit, 0); std::string dir = env->options()->diagnostic_dir; @@ -208,7 +212,7 @@ void SetHeapSnapshotNearHeapLimit(const FunctionCallbackInfo& args) { void SetHeapProfileNearHeapLimit(const FunctionCallbackInfo& args) { CHECK(args[0]->IsUint32()); Environment* env = Environment::GetCurrent(args); - uint32_t limit = args[0].As()->Value(); + uint32_t limit = args[0].As()->Value(); CHECK_GT(limit, 0); std::string dir = env->options()->diagnostic_dir; @@ -230,8 +234,8 @@ void SetPointerCompressionHeapWarningThresholds( CHECK(args[0]->IsNumber()); CHECK(args[1]->IsNumber()); Environment* env = Environment::GetCurrent(args); - double likely = args[0].As()->Value(); - double certain = args[1].As()->Value(); + double likely = args[0].As()->Value(); + double certain = args[1].As()->Value(); CHECK_GE(likely, 0); CHECK_GE(certain, 0); env->set_pointer_compression_heap_warning_thresholds( @@ -254,7 +258,7 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { HeapSpaceStatistics s; Isolate* const isolate = args.GetIsolate(); CHECK(args[0]->IsUint32()); - size_t space_index = static_cast(args[0].As()->Value()); + size_t space_index = static_cast(args[0].As()->Value()); isolate->GetHeapSpaceStatistics(&s, space_index); AliasedFloat64Array& buffer = data->heap_space_statistics_buffer; @@ -367,17 +371,17 @@ void GetHashSeed(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, hash_seed)); } -static const char* GetGCTypeName(v8::GCType gc_type) { +static const char* GetGCTypeName(GCType gc_type) { switch (gc_type) { - case v8::GCType::kGCTypeScavenge: + case GCType::kGCTypeScavenge: return "Scavenge"; - case v8::GCType::kGCTypeMinorMarkSweep: + case GCType::kGCTypeMinorMarkSweep: return "MinorMarkSweep"; - case v8::GCType::kGCTypeMarkSweepCompact: + case GCType::kGCTypeMarkSweepCompact: return "MarkSweepCompact"; - case v8::GCType::kGCTypeIncrementalMarking: + case GCType::kGCTypeIncrementalMarking: return "IncrementalMarking"; - case v8::GCType::kGCTypeProcessWeakCallbacks: + case GCType::kGCTypeProcessWeakCallbacks: return "ProcessWeakCallbacks"; default: return "Unknown"; @@ -620,8 +624,8 @@ static void GetCppHeapStatistics(const FunctionCallbackInfo& args) { } static void BeforeGCCallback(Isolate* isolate, - v8::GCType gc_type, - v8::GCCallbackFlags flags, + GCType gc_type, + GCCallbackFlags flags, void* data) { GCProfiler* profiler = static_cast(data); if (profiler->current_gc_type != 0) { @@ -638,8 +642,8 @@ static void BeforeGCCallback(Isolate* isolate, } static void AfterGCCallback(Isolate* isolate, - v8::GCType gc_type, - v8::GCCallbackFlags flags, + GCType gc_type, + GCCallbackFlags flags, void* data) { GCProfiler* profiler = static_cast(data); if (profiler->current_gc_type != gc_type) { @@ -715,7 +719,7 @@ void GCProfiler::Start(const FunctionCallbackInfo& args) { profiler->state = GCProfiler::GCProfilerState::kStarted; } -void GCProfiler::Stop(const FunctionCallbackInfo& args) { +void GCProfiler::Stop(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); GCProfiler* profiler; ASSIGN_OR_RETURN_UNWRAP(&profiler, args.This()); @@ -818,16 +822,16 @@ void Initialize(Local target, SetMethod(context, target, "stopHeapProfile", StopHeapProfile); { - constexpr uint32_t kSamplingNoFlags = static_cast( - v8::HeapProfiler::SamplingFlags::kSamplingNoFlags); - constexpr uint32_t kSamplingForceGC = static_cast( - v8::HeapProfiler::SamplingFlags::kSamplingForceGC); - constexpr uint32_t kSamplingIncludeObjectsCollectedByMajorGC = - static_cast(v8::HeapProfiler::SamplingFlags:: - kSamplingIncludeObjectsCollectedByMajorGC); - constexpr uint32_t kSamplingIncludeObjectsCollectedByMinorGC = - static_cast(v8::HeapProfiler::SamplingFlags:: - kSamplingIncludeObjectsCollectedByMinorGC); + constexpr uint32_t kSamplingNoFlags = + static_cast(HeapProfiler::SamplingFlags::kSamplingNoFlags); + constexpr uint32_t kSamplingForceGC = + static_cast(HeapProfiler::SamplingFlags::kSamplingForceGC); + constexpr uint32_t kSamplingIncludeObjectsCollectedByMajorGC = static_cast< + uint32_t>( + HeapProfiler::SamplingFlags::kSamplingIncludeObjectsCollectedByMajorGC); + constexpr uint32_t kSamplingIncludeObjectsCollectedByMinorGC = static_cast< + uint32_t>( + HeapProfiler::SamplingFlags::kSamplingIncludeObjectsCollectedByMinorGC); NODE_DEFINE_CONSTANT(target, kSamplingNoFlags); NODE_DEFINE_CONSTANT(target, kSamplingForceGC); diff --git a/src/node_worker.cc b/src/node_worker.cc index c767f9ff52db..36cf67dd7309 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -22,6 +22,7 @@ using node::kAllowedInEnvvar; using node::kDisallowedInEnvvar; +using v8::AccessorNameGetterCallback; using v8::Array; using v8::ArrayBuffer; using v8::Boolean; @@ -34,6 +35,7 @@ using v8::Float64Array; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; +using v8::HeapProfiler; using v8::HeapStatistics; using v8::Integer; using v8::Isolate; @@ -41,11 +43,13 @@ using v8::Local; using v8::Locker; using v8::Maybe; using v8::MaybeLocal; +using v8::Name; using v8::NewStringType; using v8::Null; using v8::Number; using v8::Object; using v8::ObjectTemplate; +using v8::PropertyCallbackInfo; using v8::ResourceConstraints; using v8::SealHandleScope; using v8::String; @@ -1077,7 +1081,7 @@ void Worker::StartHeapProfile(const FunctionCallbackInfo& args) { bool scheduled = w->RequestInterrupt([taker = std::move(taker), env, options]( Environment* worker_env) mutable { - v8::HeapProfiler* profiler = worker_env->isolate()->GetHeapProfiler(); + HeapProfiler* profiler = worker_env->isolate()->GetHeapProfiler(); bool success = profiler->StartSamplingHeapProfiler( options.sample_interval, options.stack_depth, options.flags); env->SetImmediateThreadsafe( @@ -1434,14 +1438,12 @@ void GetEnvMessagePort(const FunctionCallbackInfo& args) { // Per-thread values of the `worker` binding are lazy properties of the // per-isolate template, so that a bootstrapped context carries none of them // and can be deserialized by any thread. -void ThreadIdGetter(Local, - const v8::PropertyCallbackInfo& info) { +void ThreadIdGetter(Local, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); info.GetReturnValue().Set(static_cast(env->thread_id())); } -void ThreadNameGetter(Local, - const v8::PropertyCallbackInfo& info) { +void ThreadNameGetter(Local, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); Local name; if (String::NewFromUtf8(info.GetIsolate(), @@ -1453,19 +1455,18 @@ void ThreadNameGetter(Local, } } -void IsMainThreadGetter(Local, - const v8::PropertyCallbackInfo& info) { +void IsMainThreadGetter(Local, const PropertyCallbackInfo& info) { info.GetReturnValue().Set(Environment::GetCurrent(info)->is_main_thread()); } -void OwnsProcessStateGetter(Local, - const v8::PropertyCallbackInfo& info) { +void OwnsProcessStateGetter(Local, + const PropertyCallbackInfo& info) { info.GetReturnValue().Set( Environment::GetCurrent(info)->owns_process_state()); } -void ResourceLimitsGetter(Local, - const v8::PropertyCallbackInfo& info) { +void ResourceLimitsGetter(Local, + const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); if (env->worker_context() != nullptr) { info.GetReturnValue().Set( @@ -1477,7 +1478,7 @@ void CreateWorkerPerIsolateProperties(IsolateData* isolate_data, Local target) { { Isolate* isolate = isolate_data->isolate(); - auto lazy = [&](const char* name, v8::AccessorNameGetterCallback getter) { + auto lazy = [&](const char* name, AccessorNameGetterCallback getter) { target->SetLazyDataProperty(OneByteString(isolate, name), getter); }; lazy("threadId", ThreadIdGetter); diff --git a/src/util.cc b/src/util.cc index 179474928626..de01b4343bd8 100644 --- a/src/util.cc +++ b/src/util.cc @@ -88,12 +88,32 @@ namespace node { using v8::ArrayBuffer; using v8::ArrayBufferView; using v8::BackingStore; +using v8::BackingStoreInitializationMode; +using v8::BackingStoreOnFailureMode; +using v8::CFunction; +using v8::ConstructorBehavior; using v8::Context; +using v8::CppHeap; +using v8::CppHeapCreateParams; +using v8::Function; +using v8::FunctionCallback; using v8::FunctionTemplate; +using v8::Int32; using v8::Isolate; +using v8::JSON; +using v8::Just; using v8::Local; +using v8::Maybe; +using v8::MaybeLocal; +using v8::MemorySpan; +using v8::NewStringType; +using v8::Nothing; +using v8::Number; using v8::Object; +using v8::SideEffectType; +using v8::Signature; using v8::String; +using v8::Symbol; using v8::Template; using v8::Value; @@ -288,59 +308,57 @@ std::string DiagnosticFilename::MakeFilename( return oss.str(); } -Local NewFunctionTemplate( - v8::Isolate* isolate, - v8::FunctionCallback callback, - Local signature, - v8::ConstructorBehavior behavior, - v8::SideEffectType side_effect_type, - const v8::CFunction* c_function) { - return v8::FunctionTemplate::New(isolate, - callback, - Local(), - signature, - 0, - behavior, - side_effect_type, - c_function); -} - -void SetMethod(Local context, - Local that, +Local NewFunctionTemplate(Isolate* isolate, + FunctionCallback callback, + Local signature, + ConstructorBehavior behavior, + SideEffectType side_effect_type, + const CFunction* c_function) { + return FunctionTemplate::New(isolate, + callback, + Local(), + signature, + 0, + behavior, + side_effect_type, + c_function); +} + +void SetMethod(Local context, + Local that, const std::string_view name, - v8::FunctionCallback callback) { + FunctionCallback callback) { Isolate* isolate = Isolate::GetCurrent(); - Local function = - NewFunctionTemplate(isolate, - callback, - Local(), - v8::ConstructorBehavior::kThrow, - v8::SideEffectType::kHasSideEffect) - ->GetFunction(context) - .ToLocalChecked(); + Local function = NewFunctionTemplate(isolate, + callback, + Local(), + ConstructorBehavior::kThrow, + SideEffectType::kHasSideEffect) + ->GetFunction(context) + .ToLocalChecked(); // kInternalized strings are created in the old space. - const v8::NewStringType type = v8::NewStringType::kInternalized; - Local name_string = - v8::String::NewFromUtf8(isolate, name.data(), type, name.size()) + const NewStringType type = NewStringType::kInternalized; + Local name_string = + String::NewFromUtf8(isolate, name.data(), type, name.size()) .ToLocalChecked(); that->Set(context, name_string, function).Check(); function->SetName(name_string); // NODE_SET_METHOD() compatibility. } -void SetMethod(v8::Isolate* isolate, - v8::Local that, +void SetMethod(Isolate* isolate, + Local