From 257381429b91ca14b5d50fe6e2825daa088bdd71 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 30 May 2026 20:07:04 +0200 Subject: [PATCH] src: remove redundant `handle_` field in ffi This was just a copy of the immediately preceding `handle_` field in `uv_lib_t`. Signed-off-by: Anna Henningsen --- src/node_ffi.cc | 29 ++++++++++++++++------------- src/node_ffi.h | 4 ++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/node_ffi.cc b/src/node_ffi.cc index b8e6df7d29eb6c..a0d8bf04dd15e6 100644 --- a/src/node_ffi.cc +++ b/src/node_ffi.cc @@ -46,7 +46,7 @@ void FFIFunctionInfo::MemoryInfo(MemoryTracker* tracker) const { } DynamicLibrary::DynamicLibrary(Environment* env, Local object) - : BaseObject(env, object), lib_{}, handle_(nullptr), symbols_() { + : BaseObject(env, object) { MakeWeak(); } @@ -54,6 +54,10 @@ DynamicLibrary::~DynamicLibrary() { this->Close(); } +bool DynamicLibrary::is_closed() const { + return static_cast(lib_.handle) == nullptr; +} + void DynamicLibrary::MemoryInfo(MemoryTracker* tracker) const { tracker->TrackFieldWithSize("path", path_.capacity() + 1, "std::string"); @@ -85,9 +89,9 @@ void DynamicLibrary::Close() { // dangerous: it can crash the process, produce incorrect output, or corrupt // memory. - if (handle_ != nullptr) { + if (!is_closed()) { uv_dlclose(&lib_); - handle_ = nullptr; + lib_ = {}; } symbols_.clear(); @@ -97,7 +101,7 @@ void DynamicLibrary::Close() { Maybe DynamicLibrary::ResolveSymbol(Environment* env, const std::string& name) { - if (handle_ == nullptr) { + if (is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return {}; } @@ -378,13 +382,12 @@ void DynamicLibrary::New(const FunctionCallbackInfo& args) { library_path = lib->path_.c_str(); } + CHECK(lib->is_closed()); // Open the library if (uv_dlopen(library_path, &lib->lib_) != 0) { THROW_ERR_FFI_CALL_FAILED(env, "dlopen failed: %s", uv_dlerror(&lib->lib_)); return; } - - lib->handle_ = static_cast(lib->lib_.handle); } void DynamicLibrary::Close(const FunctionCallbackInfo& args) { @@ -539,7 +542,7 @@ void DynamicLibrary::InvokeCallback(ffi_cif* cif, // It is unsupported and dangerous for a callback to unregister itself or // close its owning library while executing. The current invocation must // return before teardown APIs are used. - if (cb->owner->handle_ == nullptr || cb->ptr == nullptr) { + if (cb->owner->is_closed() || cb->ptr == nullptr) { if (ret != nullptr && cb->return_type->size > 0) { std::memset(ret, 0, GetFFIReturnValueStorageSize(cb->return_type)); } @@ -669,7 +672,7 @@ void DynamicLibrary::GetFunctions(const FunctionCallbackInfo& args) { Local context = env->context(); DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } @@ -818,7 +821,7 @@ void DynamicLibrary::GetSymbols(const FunctionCallbackInfo& args) { Local context = env->context(); DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } @@ -890,7 +893,7 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo& args) { } DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } @@ -971,7 +974,7 @@ void DynamicLibrary::UnregisterCallback( Environment* env = Environment::GetCurrent(args); DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } @@ -1007,7 +1010,7 @@ void DynamicLibrary::RefCallback(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } @@ -1038,7 +1041,7 @@ void DynamicLibrary::UnrefCallback(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); DynamicLibrary* lib = Unwrap(args.This()); - if (lib->handle_ == nullptr) { + if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); return; } diff --git a/src/node_ffi.h b/src/node_ffi.h index 1626fd5fa0d756..0a8e2be1f18c65 100644 --- a/src/node_ffi.h +++ b/src/node_ffi.h @@ -139,9 +139,9 @@ class DynamicLibrary : public BaseObject { const std::shared_ptr& fn); static void CleanupFunctionInfo( const v8::WeakCallbackInfo& data); + bool is_closed() const; - uv_lib_t lib_; - void* handle_; + uv_lib_t lib_ = {}; std::string path_; std::unordered_map symbols_; std::unordered_map> functions_;