Commit bc29c86cdfb for nodejs

commit bc29c86cdfb7278901770cfefc4e9cb1fb81d2b3
Author: James M Snell <jasnell@gmail.com>
Date:   Sun Nov 15 18:38:46 2026 +0000

    src: share callback lookup between callback methods

    DynamicLibrary::UnregisterCallback(), RefCallback(), and UnrefCallback()
    each duplicated the closed-library check, pointer argument validation,
    and callback map lookup. Move them into a shared FindCallback() helper.

    Assisted-by: Claude
    Signed-off-by: James M Snell <jasnell@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67496
    Reviewed-By: Vladimir Morozov <vmorozov@microsoft.com>
    Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>

diff --git a/src/node_ffi.cc b/src/node_ffi.cc
index 9b0703b84e9..d32a33f0912 100644
--- a/src/node_ffi.cc
+++ b/src/node_ffi.cc
@@ -1339,25 +1339,27 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) {
   args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, ret));
 }

-void DynamicLibrary::UnregisterCallback(
-    const FunctionCallbackInfo<Value>& args) {
+DynamicLibrary* DynamicLibrary::FindCallback(
+    const FunctionCallbackInfo<Value>& args,
+    bool require_function,
+    CallbackMap::iterator* out) {
   Environment* env = Environment::GetCurrent(args);
   DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());

   if (lib->is_closed()) {
     THROW_ERR_FFI_LIBRARY_CLOSED(env);
-    return;
+    return nullptr;
   }

   if (args.Length() < 1 || !args[0]->IsBigInt()) {
     THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint");
-    return;
+    return nullptr;
   }

   uintptr_t raw_ptr;
   if (!GetValidatedPointerAddress(env, args[0], "first argument")
            .To(&raw_ptr)) {
-    return;
+    return nullptr;
   }

   void* ptr = reinterpret_cast<void*>(raw_ptr);
@@ -1365,6 +1367,27 @@ void DynamicLibrary::UnregisterCallback(

   if (existing == lib->callbacks_.end()) {
     THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
+    return nullptr;
+  }
+
+  // The callback function may already have been collected after a previous
+  // unrefCallback() call. The persistent handle is empty in that case, and
+  // ClearWeak() or SetWeak() on an empty handle dereferences a null slot.
+  // There is also no function left to make strong again.
+  if (require_function && existing->second->fn.IsEmpty()) {
+    THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
+    return nullptr;
+  }
+
+  *out = existing;
+  return lib;
+}
+
+void DynamicLibrary::UnregisterCallback(
+    const FunctionCallbackInfo<Value>& args) {
+  CallbackMap::iterator existing;
+  DynamicLibrary* lib = FindCallback(args, false, &existing);
+  if (lib == nullptr) {
     return;
   }

@@ -1377,82 +1400,17 @@ void DynamicLibrary::UnregisterCallback(
 }

 void DynamicLibrary::RefCallback(const FunctionCallbackInfo<Value>& args) {
-  Environment* env = Environment::GetCurrent(args);
-  DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());
-
-  if (lib->is_closed()) {
-    THROW_ERR_FFI_LIBRARY_CLOSED(env);
-    return;
-  }
-
-  if (args.Length() < 1 || !args[0]->IsBigInt()) {
-    THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint");
-    return;
-  }
-
-  uintptr_t raw_ptr;
-  if (!GetValidatedPointerAddress(env, args[0], "first argument")
-           .To(&raw_ptr)) {
-    return;
-  }
-
-  void* ptr = reinterpret_cast<void*>(raw_ptr);
-  auto existing = lib->callbacks_.find(ptr);
-
-  if (existing == lib->callbacks_.end()) {
-    THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
-    return;
+  CallbackMap::iterator existing;
+  if (FindCallback(args, true, &existing) != nullptr) {
+    existing->second->fn.ClearWeak();
   }
-
-  // The callback function may already have been collected after a previous
-  // unrefCallback() call. The persistent handle is empty in that case, and
-  // ClearWeak() on an empty handle dereferences a null slot. There is also no
-  // function left to make strong again.
-  if (existing->second->fn.IsEmpty()) {
-    THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
-    return;
-  }
-
-  existing->second->fn.ClearWeak();
 }

 void DynamicLibrary::UnrefCallback(const FunctionCallbackInfo<Value>& args) {
-  Environment* env = Environment::GetCurrent(args);
-  DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());
-
-  if (lib->is_closed()) {
-    THROW_ERR_FFI_LIBRARY_CLOSED(env);
-    return;
-  }
-
-  if (args.Length() < 1 || !args[0]->IsBigInt()) {
-    THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint");
-    return;
+  CallbackMap::iterator existing;
+  if (FindCallback(args, true, &existing) != nullptr) {
+    existing->second->fn.SetWeak();
   }
-
-  uintptr_t raw_ptr;
-  if (!GetValidatedPointerAddress(env, args[0], "first argument")
-           .To(&raw_ptr)) {
-    return;
-  }
-
-  void* ptr = reinterpret_cast<void*>(raw_ptr);
-  auto existing = lib->callbacks_.find(ptr);
-
-  if (existing == lib->callbacks_.end()) {
-    THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
-    return;
-  }
-
-  // The callback function may already have been collected by a previous
-  // unrefCallback() call. The persistent handle is empty in that case, and
-  // SetWeak() on an empty handle dereferences a null slot.
-  if (existing->second->fn.IsEmpty()) {
-    THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found");
-    return;
-  }
-
-  existing->second->fn.SetWeak();
 }

 Local<FunctionTemplate> DynamicLibrary::GetConstructorTemplate(
diff --git a/src/node_ffi.h b/src/node_ffi.h
index 86737273e28..969491ff545 100644
--- a/src/node_ffi.h
+++ b/src/node_ffi.h
@@ -158,6 +158,17 @@ class DynamicLibrary : public BaseObject {
   SET_SELF_SIZE(DynamicLibrary)

  private:
+  using CallbackMap = std::unordered_map<void*, std::unique_ptr<FFICallback>>;
+
+  // Validates that args[0] is a pointer to a callback registered with the
+  // receiving library and stores its entry in `out`. When `require_function`
+  // is true, a callback whose function has already been collected is also
+  // rejected. Returns the library, or nullptr after throwing.
+  static DynamicLibrary* FindCallback(
+      const v8::FunctionCallbackInfo<v8::Value>& args,
+      bool require_function,
+      CallbackMap::iterator* out);
+
   void Close();
   v8::Maybe<void*> ResolveSymbol(Environment* env, const std::string& name);
   struct PreparedFunction {
@@ -194,7 +205,7 @@ class DynamicLibrary : public BaseObject {
   // FFIFunctionInfo.
   std::unordered_map<std::string, v8::Global<v8::Function>> function_wrappers_;
   std::shared_ptr<std::unordered_set<FFIFunction*>> pointer_functions_;
-  std::unordered_map<void*, std::unique_ptr<FFICallback>> callbacks_;
+  CallbackMap callbacks_;
 };

 void GetInt8(const v8::FunctionCallbackInfo<v8::Value>& args);