Skip to content

[vm/ffi] Add support for weak symbols @Native(weak: true) #60038

@simolus3

Description

@simolus3

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:

  1. 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();).
  2. 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 to Native.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-native-interopUsed for native interop related issues, including FFI.library-ffitype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions