blog icon indicating copy to clipboard operation
blog copied to clipboard

Dart FFI: Binding C/C++/Go/Rust Libs For Dart

Open hhstore opened this issue 4 years ago • 10 comments

📖 Abstract:

  • ✅ 基于 C FFI, 把 C/C++/Rust 库 binding 成 Dart 库, 供 Dart 调用。

💯 Related:

  • ✅ #355
  • ✅ #362
  • ✅ #384
  • ✅ #432

hhstore avatar Dec 03 '20 15:12 hhstore

Dart FFI : 绑定其他语言(c/c++/go/rust)库使用

  • ✅ dart 当前 lib 生态还不够丰富. 很多比较新的领域, 缺少 lib, 或者 lib 质量不够好.
  • ✅ 需要借用其他语言的 lib 资源.

需求场景:

  • ✅ 把成熟的 c/rust/zig/go 库 binding 给 dart 调用.

使用 Zig + FFI 配合 Flutter 方案:

  • ✅ https://github.com/better-dart/learn-dart
  • ✅ https://github.com/zigcc/forum/discussions/21
  • ✅ 完整示例

dart 调用其他语言方案:

  • ✅ 通用语言: https://dart.dev/guides/libraries/c-interop
  • ✅ 针对 js: https://dart.dev/web/js-interop

原生插件:

  • https://github.com/dart-lang/sdk/issues/45451
  • https://juejin.cn/post/7005794537398337550/
  • ✅ dart 官方已经在弃用 native extensions (method channel) 方式.
  • ✅ 目前 flutter 还没完全废弃这个支持, 不过这个看起来是在日程上的了.

FFI(Foreign function interface):

  • ✅ https://en.wikipedia.org/wiki/Foreign_function_interface

dart 官方:

官方文档:

  • ✅ https://flutter.cn/docs/development/platform-integration/c-interop
  • ✅ https://flutter.dev/docs/development/platform-integration/c-interop

官方工具:

  • ✅ https://github.com/dart-lang/ffi
  • ✅ https://pub.dev/packages/ffigen
    • https://github.com/dart-lang/ffigen
    • 辅助生成工具, 避免手写 c wrap 代码.

with wasm:

  • ✅ https://github.com/dart-lang/wasm

with Java:

  • ✅ https://github.com/dart-lang/jnigen

官方示例:

  • ✅ https://github.com/dart-lang/sdk/blob/master/samples/ffi/sqlite/README.md

持续关注:

  • ✅ https://github.com/dart-lang/sdk/issues/34452

FFI 实践:

ref:

  • [x] https://juejin.im/post/6844904070352732173
  • [x] https://juejin.cn/post/6844903823136260110
  • [x] https://juejin.cn/post/6856965267202048013
  • [x] https://juejin.cn/post/6844903846267863048
  • [x] Flutter Platform Channel 和 FFI 通道性能测试
  • [x] Flutter Native Channel 设计与实现
  • [x] 质量很高的博客: http://yulingtianxia.com/archives/
  • http://blog.cnbang.net/tech/3332/
  • http://yulingtianxia.com/blog/2020/03/28/Using-Objective-C-Block-in-Flutter/

lib:

  • https://github.com/dart-native/dart_native

hhstore avatar Dec 03 '20 15:12 hhstore

C + FFI => dart

  • [x] 官方案例: https://github.com/dart-lang/tflite_native

案例:

  • https://zhuanlan.zhihu.com/p/248118194
  • https://pub.flutter-io.cn/packages/tflite_flutter
  • https://github.com/am15h/tflite_flutter_plugin

FFI示例

  • https://github.com/Sunbreak/native_interop.tour

通用USB

  • https://github.com/woodemi/quick_usb
  • https://github.com/woodemi/libusb.dart

扫描TWAIN

  • https://github.com/woodemi/twaindsm.dart

网络

  • https://github.com/Sunbreak/cronet_flutter
  • https://github.com/Sunbreak/cronet.dart

hhstore avatar Dec 03 '20 15:12 hhstore

C++ + FFI => dart

问题:

  • https://github.com/dart-lang/sdk/issues/42210

待实践的项目:

  • https://github.com/better-dart/wallet-core

  • https://developer.trustwallet.com/wallet-core/newblockchain

  • 看起来, 已经用 c interface 包裹了 c++ 功能. 应该可以直接 binding 给 dart 使用.

image

项目使用列表:

  • https://developer.trustwallet.com/wallet-core#projects-using-wallet-core-add-yours-here-too

https://developer.trustwallet.com/wallet-connect

hhstore avatar Dec 03 '20 15:12 hhstore

Go + FFI => dart

  • 不推荐使用 Go + CGo 的 FFI 方案.
  • 尽量选择 Rust + FFI 方案.

一些讨论:

  • https://github.com/go-flutter-desktop/go-flutter/issues/58

  • cgo: https://github.com/golang/go/wiki/cgo

方案:

mobile:

  • https://medium.com/flutter-community/using-go-library-in-flutter-a04e3496aa05
  • https://pkg.go.dev/golang.org/x/mobile/cmd/gomobile
  • gomobile + jni + aar + platform channel(method channel)

desktop:

  • https://github.com/go-flutter-desktop/go-flutter

项目实践:

  • https://pub.dev/packages/dexecve
  • https://github.com/brad-jones/dexecve

go 的其他方案:

  • https://github.com/hexops/vecty

hhstore avatar Dec 03 '20 15:12 hhstore

Rust + FFI => dart

  • 目前 rust 有些 FFI 的项目, 对 dart 比较友好.
  • 可以优先了解+学习.

libs + tools:

  • ✅ https://github.com/fzyzcjy/flutter_rust_bridge
  • ✅ https://github.com/shekohex/flutterust
  • ✅ https://github.com/sunshine-protocol/dart-bindgen
  • ✅ https://github.com/brickpop/flutter-rust-ffi
  • ✅ https://github.com/TimNN/cargo-lipo
  • ✅ https://github.com/bbqsrc/cargo-ndk

实践案例:

  • ✅ https://www.zhihu.com/question/313562705/answer/1057943408
  • ✅ https://github.com/Hentioe/mikack
  • ✅ https://github.com/Hentioe/mikack-ffi
  • ✅ https://github.com/Hentioe/mikack-dart
  • ✅ https://github.com/Hentioe/mikack-mobile

ref:

  • https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-06-rust-on-ios.html
  • https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html

hhstore avatar Dec 03 '20 15:12 hhstore

Dart Native 同步/异步交互示例:https://github.com/Sunbreak/native_interop.tour

Cronet的Flutter插件工程:https://github.com/Sunbreak/cronet_flutter

微信:wk2311007 我主要在做Flutter PC相关工作,有个FFI群可以多多交流~

Sunbreak avatar Apr 16 '21 02:04 Sunbreak

Dart Native 同步/异步交互示例:https://github.com/Sunbreak/native_interop.tour

Cronet的Flutter插件工程:https://github.com/Sunbreak/cronet_flutter

微信:wk2311007 我主要在做Flutter PC相关工作,有个FFI群可以多多交流~

  • 多谢提供这个 lib. 我学习一下哈.
  • 顺便我又发现几个 rust ffi 支持 flutter 的 lib.
  • 你可以了解一下.
  • FFI 对 flutter 来说, 确实是非常重要的功能.

rust + ffi + dart:

  • https://github.com/shekohex/flutterust
  • https://github.com/sunshine-protocol/dart-bindgen
  • https://github.com/brickpop/flutter-rust-ffi

hhstore avatar Apr 16 '21 02:04 hhstore

flutter 调用原生的另一种方案: platform channel

  • ✅ 不推荐使用
  • ✅ https://github.com/flutter/flutter/tree/master/examples/platform_channel
  • ✅ https://flutter.dev/docs/development/platform-integration/platform-channels
  • ✅ https://api.flutter.dev/flutter/services/MethodChannel-class.html

hhstore avatar Oct 13 '21 01:10 hhstore

一个非常好的实现 appflowy system design,目前看起来 rust 表现最好,但可惜我不会 rust,一直没发现 go 有很好的多平台方案,cgo 本身已经不简单了...

laojianzi avatar Jan 15 '22 11:01 laojianzi

一个非常好的实现 appflowy system design,目前看起来 rust 表现最好,但可惜我不会 rust,一直没发现 go 有很好的多平台方案,cgo 本身已经不简单了...

  • 对. 这个项目, 是个不错的案例.
  • rust + flutter 的典范.
  • go + cgo 的模式, 不适合作 core lib 层, 给其他语言跨平台复用.
  • core lib 跨平台, 还是建议使用 rust/c/c++.

hhstore avatar Jun 05 '22 14:06 hhstore