dart-lang/native

Support dynamic linking

Closed this issue ยท 3 comments

We should support dynamic linking and accept just the lookup function of a DynamicLibrary in the constructor.

  final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
      _lookup;

  /// The symbols are looked up in [dynamicLibrary].
  BoringSsl(ffi.DynamicLibrary dynamicLibrary)
      : _lookup = dynamicLibrary.lookup;

  /// The symbols are looked up with [lookup].
  BoringSsl.fromLookup(
      ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
          lookup)
      : _lookup = lookup;

The bindings should then be changed to use _lookup<NativeFunction<...>>('...').asFunction<...>() instead of _dylib.lookupFunction<...,...>('...').

By making these changes by hand, I was able to use package:ffigen for package:webcrypto. Webcrypto uses dynamic linking.

cc @jonasfj

@mannprerak2, I could make a PR maybe later this week. But if you want to go ahead and do it, that is fine too.

Context:
package:webcrypto has a lookup function in C for all the symbols it want to expose from BoringSSL to package:webcrypto.
This avoids symbol conflicts, which is particularly likely when linking against something like BoringSSL.

Very cool. I'll try to add a PR by today

It works 100% as the drop in replacement of my handmade edits! ๐Ÿ˜„ Thanks @mannprerak2!