dart-archive/ffi

Add `DynamicLibrary.functionExists()`

vaind opened this issue · 1 comments

vaind commented

Currently, the only way to check whether a function exists in a given DL (or an already loaded library in DynamicLibrary.Process()) is to call .lookup() and catch exceptions. This doesn't work all that well when someone has "catch all exceptions" enabled, e.g. in VS Code.

Consider adding a boolean returning function to check whether a function with a given name exists. I've had a quick look at the SDK code and adding a new function based on the existing ResolveSymbol looks reasonably simple (ffi_dynamic_library.cc):

static bool SymbolExists(void* handle, const char* symbol) {
#if defined(HOST_OS_LINUX) || defined(HOST_OS_MACOS) ||                        \
    defined(HOST_OS_ANDROID) || defined(HOST_OS_FUCHSIA)
  return dlsym(handle, symbol) != nullptr;
#elif defined(HOST_OS_WINDOWS)
  return GetProcAddress(reinterpret_cast<HMODULE>(handle), symbol) != nullptr;
#else
  const Array& args = Array::Handle(Array::New(1));
  args.SetAt(0,
             String::Handle(String::New(
                 "The dart:ffi library is not available on this platform.")));
  Exceptions::ThrowByType(Exceptions::kUnsupported, args);
#endif
}

I moved this issue to the SDK repo, because it's a dart:ffi feature you're requesting, not a package:ffi feature.