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>
This commit is contained in:
James M Snell committed 2026-11-27 16:26:36 -08:00
1 parent 561f101bc3
commit bc29c86cdf
2 files changed
+46 -77

No files matched your search

+34 -76
View File
@@ -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;
CallbackMap::iterator existing;
if (FindCallback(args, true, &existing) != nullptr) {
existing->second->fn.ClearWeak();
}
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;
}
// 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;
CallbackMap::iterator existing;
if (FindCallback(args, true, &existing) != nullptr) {
existing->second->fn.SetWeak();
}
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;
}
// 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(
+12 -1
View File
@@ -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);