ffi.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /// bindings for `libdart_ffi`
  2. import 'dart:ffi';
  3. import 'dart:io';
  4. // ignore: import_of_legacy_library_into_null_safe
  5. import 'package:ffi/ffi.dart' as ffi;
  6. import 'package:flutter/foundation.dart' as Foundation;
  7. // ignore_for_file: unused_import, camel_case_types, non_constant_identifier_names
  8. final DynamicLibrary _dl = _open();
  9. /// Reference to the Dynamic Library, it should be only used for low-level access
  10. final DynamicLibrary dl = _dl;
  11. DynamicLibrary _open() {
  12. if (is_tester()) {
  13. return DynamicLibrary.open(
  14. '${Directory.systemTemp.path}/app_flowy/libdart_ffi.dylib');
  15. } else {
  16. if (Platform.isAndroid) return DynamicLibrary.open('libdart_ffi.so');
  17. if (Platform.isMacOS) return DynamicLibrary.executable();
  18. if (Platform.isIOS) return DynamicLibrary.executable();
  19. throw UnsupportedError('This platform is not supported.');
  20. }
  21. }
  22. /// C function `async_command`.
  23. void async_command(
  24. int port,
  25. Pointer<Uint8> input,
  26. int len,
  27. ) {
  28. _invoke_async(port, input, len);
  29. }
  30. final _invoke_async_Dart _invoke_async =
  31. _dl.lookupFunction<_invoke_async_C, _invoke_async_Dart>('async_command');
  32. typedef _invoke_async_C = Void Function(
  33. Int64 port,
  34. Pointer<Uint8> input,
  35. Uint64 len,
  36. );
  37. typedef _invoke_async_Dart = void Function(
  38. int port,
  39. Pointer<Uint8> input,
  40. int len,
  41. );
  42. /// C function `command_sync`.
  43. Pointer<Uint8> sync_command(
  44. Pointer<Uint8> input,
  45. int len,
  46. ) {
  47. return _invoke_sync(input, len);
  48. }
  49. final _invoke_sync_Dart _invoke_sync =
  50. _dl.lookupFunction<_invoke_sync_C, _invoke_sync_Dart>('sync_command');
  51. typedef _invoke_sync_C = Pointer<Uint8> Function(
  52. Pointer<Uint8> input,
  53. Uint64 len,
  54. );
  55. typedef _invoke_sync_Dart = Pointer<Uint8> Function(
  56. Pointer<Uint8> input,
  57. int len,
  58. );
  59. /// C function `init_sdk`.
  60. int init_sdk(
  61. Pointer<ffi.Utf8> path,
  62. ) {
  63. return _init_sdk(path);
  64. }
  65. final _init_sdk_Dart _init_sdk =
  66. _dl.lookupFunction<_init_sdk_C, _init_sdk_Dart>('init_sdk');
  67. typedef _init_sdk_C = Int64 Function(
  68. Pointer<ffi.Utf8> path,
  69. );
  70. typedef _init_sdk_Dart = int Function(
  71. Pointer<ffi.Utf8> path,
  72. );
  73. /// C function `link_me_please`.
  74. void link_me_please() {
  75. _link_me_please();
  76. }
  77. final _link_me_please_Dart _link_me_please = _dl
  78. .lookupFunction<_link_me_please_C, _link_me_please_Dart>('link_me_please');
  79. typedef _link_me_please_C = Void Function();
  80. typedef _link_me_please_Dart = void Function();
  81. /// Binding to `allo-isolate` crate
  82. void store_dart_post_cobject(
  83. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  84. ) {
  85. _store_dart_post_cobject(ptr);
  86. }
  87. final _store_dart_post_cobject_Dart _store_dart_post_cobject = _dl
  88. .lookupFunction<_store_dart_post_cobject_C, _store_dart_post_cobject_Dart>(
  89. 'store_dart_post_cobject');
  90. typedef _store_dart_post_cobject_C = Void Function(
  91. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  92. );
  93. typedef _store_dart_post_cobject_Dart = void Function(
  94. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  95. );
  96. bool is_tester() {
  97. if (Foundation.kDebugMode) {
  98. // ignore: unnecessary_null_comparison
  99. // if (Platform.executable.isEmpty) {
  100. // return false;
  101. // } else {
  102. // return Platform.executable.contains("tester");
  103. // }
  104. return false;
  105. } else {
  106. return false;
  107. }
  108. }