| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 57ae876 commit 03858d1
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,14 +46,18 @@ void FFIFunctionInfo::MemoryInfo(MemoryTracker* tracker) const { | |||
| 46 | 46 | } | |
| 47 | 47 | ||
| 48 | 48 | DynamicLibrary::DynamicLibrary(Environment* env, Local<Object> object) | |
| 49 | - : BaseObject(env, object), lib_{}, handle_(nullptr), symbols_() { | ||
| 49 | + : BaseObject(env, object) { | ||
| 50 | 50 | MakeWeak(); | |
| 51 | 51 | } | |
| 52 | 52 | ||
| 53 | 53 | DynamicLibrary::~DynamicLibrary() { | |
| 54 | 54 | this->Close(); | |
| 55 | 55 | } | |
| 56 | 56 | ||
| 57 | + bool DynamicLibrary::is_closed() const { | ||
| 58 | + return static_cast<void*>(lib_.handle) == nullptr; | ||
| 59 | + } | ||
| 60 | + | ||
| 57 | 61 | void DynamicLibrary::MemoryInfo(MemoryTracker* tracker) const { | |
| 58 | 62 | tracker->TrackFieldWithSize("path", path_.capacity() + 1, "std::string"); | |
| 59 | 63 | ||
@@ -85,9 +89,9 @@ void DynamicLibrary::Close() { | |||
| 85 | 89 | // dangerous: it can crash the process, produce incorrect output, or corrupt | |
| 86 | 90 | // memory. | |
| 87 | 91 | ||
| 88 | - if (handle_ != nullptr) { | ||
| 92 | + if (!is_closed()) { | ||
| 89 | 93 | uv_dlclose(&lib_); | |
| 90 | - handle_ = nullptr; | ||
| 94 | + lib_ = {}; | ||
| 91 | 95 | } | |
| 92 | 96 | ||
| 93 | 97 | symbols_.clear(); | |
@@ -97,7 +101,7 @@ void DynamicLibrary::Close() { | |||
| 97 | 101 | ||
| 98 | 102 | Maybe<void*> DynamicLibrary::ResolveSymbol(Environment* env, | |
| 99 | 103 | const std::string& name) { | |
| 100 | - if (handle_ == nullptr) { | ||
| 104 | + if (is_closed()) { | ||
| 101 | 105 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 102 | 106 | return {}; | |
| 103 | 107 | } | |
@@ -378,13 +382,12 @@ void DynamicLibrary::New(const FunctionCallbackInfo<Value>& args) { | |||
| 378 | 382 | library_path = lib->path_.c_str(); | |
| 379 | 383 | } | |
| 380 | 384 | ||
| 385 | + CHECK(lib->is_closed()); | ||
| 381 | 386 | // Open the library | |
| 382 | 387 | if (uv_dlopen(library_path, &lib->lib_) != 0) { | |
| 383 | 388 | THROW_ERR_FFI_CALL_FAILED(env, "dlopen failed: %s", uv_dlerror(&lib->lib_)); | |
| 384 | 389 | return; | |
| 385 | 390 | } | |
| 386 | - | ||
| 387 | - lib->handle_ = static_cast<void*>(lib->lib_.handle); | ||
| 388 | 391 | } | |
| 389 | 392 | ||
| 390 | 393 | void DynamicLibrary::Close(const FunctionCallbackInfo<Value>& args) { | |
@@ -539,7 +542,7 @@ void DynamicLibrary::InvokeCallback(ffi_cif* cif, | |||
| 539 | 542 | // It is unsupported and dangerous for a callback to unregister itself or | |
| 540 | 543 | // close its owning library while executing. The current invocation must | |
| 541 | 544 | // return before teardown APIs are used. | |
| 542 | - if (cb->owner->handle_ == nullptr || cb->ptr == nullptr) { | ||
| 545 | + if (cb->owner->is_closed() || cb->ptr == nullptr) { | ||
| 543 | 546 | if (ret != nullptr && cb->return_type->size > 0) { | |
| 544 | 547 | std::memset(ret, 0, GetFFIReturnValueStorageSize(cb->return_type)); | |
| 545 | 548 | } | |
@@ -669,7 +672,7 @@ void DynamicLibrary::GetFunctions(const FunctionCallbackInfo<Value>& args) { | |||
| 669 | 672 | Local<Context> context = env->context(); | |
| 670 | 673 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 671 | 674 | ||
| 672 | - if (lib->handle_ == nullptr) { | ||
| 675 | + if (lib->is_closed()) { | ||
| 673 | 676 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 674 | 677 | return; | |
| 675 | 678 | } | |
@@ -818,7 +821,7 @@ void DynamicLibrary::GetSymbols(const FunctionCallbackInfo<Value>& args) { | |||
| 818 | 821 | Local<Context> context = env->context(); | |
| 819 | 822 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 820 | 823 | ||
| 821 | - if (lib->handle_ == nullptr) { | ||
| 824 | + if (lib->is_closed()) { | ||
| 822 | 825 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 823 | 826 | return; | |
| 824 | 827 | } | |
@@ -890,7 +893,7 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo<Value>& args) { | |||
| 890 | 893 | } | |
| 891 | 894 | ||
| 892 | 895 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 893 | - if (lib->handle_ == nullptr) { | ||
| 896 | + if (lib->is_closed()) { | ||
| 894 | 897 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 895 | 898 | return; | |
| 896 | 899 | } | |
@@ -971,7 +974,7 @@ void DynamicLibrary::UnregisterCallback( | |||
| 971 | 974 | Environment* env = Environment::GetCurrent(args); | |
| 972 | 975 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 973 | 976 | ||
| 974 | - if (lib->handle_ == nullptr) { | ||
| 977 | + if (lib->is_closed()) { | ||
| 975 | 978 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 976 | 979 | return; | |
| 977 | 980 | } | |
@@ -1007,7 +1010,7 @@ void DynamicLibrary::RefCallback(const FunctionCallbackInfo<Value>& args) { | |||
| 1007 | 1010 | Environment* env = Environment::GetCurrent(args); | |
| 1008 | 1011 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 1009 | 1012 | ||
| 1010 | - if (lib->handle_ == nullptr) { | ||
| 1013 | + if (lib->is_closed()) { | ||
| 1011 | 1014 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 1012 | 1015 | return; | |
| 1013 | 1016 | } | |
@@ -1038,7 +1041,7 @@ void DynamicLibrary::UnrefCallback(const FunctionCallbackInfo<Value>& args) { | |||
| 1038 | 1041 | Environment* env = Environment::GetCurrent(args); | |
| 1039 | 1042 | DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This()); | |
| 1040 | 1043 | ||
| 1041 | - if (lib->handle_ == nullptr) { | ||
| 1044 | + if (lib->is_closed()) { | ||
| 1042 | 1045 | THROW_ERR_FFI_LIBRARY_CLOSED(env); | |
| 1043 | 1046 | return; | |
| 1044 | 1047 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,9 +139,9 @@ class DynamicLibrary : public BaseObject { | |||
| 139 | 139 | const std::shared_ptr<FFIFunction>& fn); | |
| 140 | 140 | static void CleanupFunctionInfo( | |
| 141 | 141 | const v8::WeakCallbackInfo<FFIFunctionInfo>& data); | |
| 142 | + bool is_closed() const; | ||
| 142 | 143 | ||
| 143 | - uv_lib_t lib_; | ||
| 144 | - void* handle_; | ||
| 144 | + uv_lib_t lib_ = {}; | ||
| 145 | 145 | std::string path_; | |
| 146 | 146 | std::unordered_map<std::string, void*> symbols_; | |
| 147 | 147 | std::unordered_map<std::string, std::shared_ptr<FFIFunction>> functions_; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments