Expose additional Dart APIs through ffi

An issue from the MongoDb Realm team:

We got a lot of requests for creating a Flutter SDK for Realm mobile database (realm/realm-object-server#55 now has over a 1000 upvotes) and initially made a prototype with FFI. Unfortunately, our progress is blocked for over a year now on #37022 and #35770.

We started to look at an alternative route and found one, but it would require adding official support for a set of the Dart VM API’s in Flutter.

Realm JS SDK is designed as a Node.js native extension with an abstraction layer that abstracts away the JavaScript VM and allows for targeting different JavaScript VMs on various platforms we support.

The similarity between the Dart VM APIs and the JavaScript VM APIs allows us to reuse a major part of Realm database code that is already tested and provide only a platform specific layer for Dart and Flutter. Moreover this approach would be superior to calling Realm through the FFI mechanism.

We made a POC with a custom built Flutter engine dynamic library which exposes a set of the Dart VM API’s and allows us to reuse a major part of Realm JavaScript engine abstraction layer with the Dart and Flutter platforms. We achieved it by exposing some of the Dart API’s from the libflutter.so library by editing the https://github.com/flutter/engine/blob/master/shell/platform/android/android_exports.lst.

In the light of the recent changes https://dart-review.googlesource.com/c/sdk/+/145592, we would like to request additional Dart APIs exposed officially so it can be consumed in Flutter through the Dart ffi. This will unblock us on delivering Flutter support and potentially will also enable other Node.js native extensions built on N-API and V8 API’s to be ported to Flutter and Dart in a straightforward manner, which would be beneficial to the Flutter and Dart community as a whole.

Here is a PR with the changes in question: #42931

cheers,
blagoev

1 possible answer(s) on “Expose additional Dart APIs through ffi

  1. Unfortunately, our progress is blocked for over a year now on #37022 and #35770.

    I think both of the issues now have their own workarounds (for asynchronous case there are send ports and for finalizers there are weak handles).

    Are you still blocked even given those work-arounds?

    we would like to request additional Dart APIs exposed officially so it can be consumed in Flutter through the Dart ffi

    Unfortunately I don’t think we want to do that. The set of Dart APIs exposes through FFI is carefully curated and we don’t want to expose anything that is not absolutely necessary – I don’t think we want to expose methods that you have added through FFI.