From 3eeb2acd62f163e8df98d580c90ffb71459d05b8 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 27 May 2025 08:23:10 -0700 Subject: [PATCH] src: add env->cppgc_allocation_handle() convenience method Reduce some of the boilerplate code needed to access the cppgc::AllocationHandle from the Node.js Environment. --- src/README.md | 2 +- src/env-inl.h | 4 ++++ src/env.h | 1 + src/node_contextify.cc | 8 ++------ 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/README.md b/src/README.md index 5904d01f7d8d5a..eab0bb203957c7 100644 --- a/src/README.md +++ b/src/README.md @@ -1193,7 +1193,7 @@ MyWrap* MyWrap::New(Environment* env, v8::Local object) { // pointer with this, as this is not managed by the native memory // allocator but by V8. return cppgc::MakeGarbageCollected( - env->isolate()->GetCppHeap()->GetAllocationHandle(), env, object); + env->cppgc_allocation_handle(), env, object); } // Binding method to be invoked by JavaScript. diff --git a/src/env-inl.h b/src/env-inl.h index 8e72e0500ee656..f4c42efefeca05 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -205,6 +205,10 @@ inline v8::Isolate* Environment::isolate() const { return isolate_; } +inline cppgc::AllocationHandle& Environment::cppgc_allocation_handle() const { + return isolate_->GetCppHeap()->GetAllocationHandle(); +} + inline v8::ExternalMemoryAccounter* Environment::external_memory_accounter() const { return external_memory_accounter_; diff --git a/src/env.h b/src/env.h index 657c619a329b48..10e579bee9c059 100644 --- a/src/env.h +++ b/src/env.h @@ -692,6 +692,7 @@ class Environment final : public MemoryRetainer { void StartProfilerIdleNotifier(); inline v8::Isolate* isolate() const; + inline cppgc::AllocationHandle& cppgc_allocation_handle() const; inline v8::ExternalMemoryAccounter* external_memory_accounter() const; inline uv_loop_t* event_loop() const; void TryLoadAddon(const char* filename, diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 6d13a62b7feb7d..8a3172bdcd843e 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -330,11 +330,7 @@ ContextifyContext* ContextifyContext::New(Local v8_context, } DCHECK_NOT_NULL(env->isolate()->GetCppHeap()); result = cppgc::MakeGarbageCollected( - env->isolate()->GetCppHeap()->GetAllocationHandle(), - env, - wrapper, - v8_context, - options); + env->cppgc_allocation_handle(), env, wrapper, v8_context, options); } Local wrapper_holder = @@ -975,7 +971,7 @@ ContextifyScript* ContextifyScript::New(Environment* env, Local object) { DCHECK_NOT_NULL(env->isolate()->GetCppHeap()); return cppgc::MakeGarbageCollected( - env->isolate()->GetCppHeap()->GetAllocationHandle(), env, object); + env->cppgc_allocation_handle(), env, object); } void ContextifyScript::New(const FunctionCallbackInfo& args) {