-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
In the current dart:ffi
system, it's possible to deal with different versions of libraries we're binding to by checking for the existence of symbols:
final lib = DynamicLibrary.open('libhelper.so');
if (lib.providesSymbol('my_function_v2')) {
// Bind to new implementation
} else {
// Bind to older implementation
}
It looks like no similar functionality exists with the new @Native
annotations. While this might not be a big issue when we control the version of the library through native_add_library
, that's not always guaranteed and we might be accessing a library from an operating system.
For this, I think the ability to check whether the symbol a @Native
annotation points to is accessible would be nice. This could be exposed with nullable functions:
@Native()
external final void Function()? my_function_v2;
But that doesn't really translate to native globals which are already getters. So a better approach may be to:
- Allow a guarantee that binding to a symbol that doesn't exist only throws when that function/field is first accessed (e.g.
@Native(lazy: true) external void my_function_v2();
). - Add an API similar to
Native.addressOf
that checks for the existence of a symbol, e.g.bool exists = Native.exists(my_function_v2)
. This could also work for globals since the call toNative.exists
would be lowered in the CFE.
I'm happy to contribute this feature once there's an basic agreement on the API / whether this is something we should support.