ffi.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /// bindings for `libflowy_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/libflowy_ffi.dylib');
  15. } else {
  16. if (Platform.isAndroid) return DynamicLibrary.open('libflowy_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 `async_query`.
  60. void async_query(
  61. int port,
  62. Pointer<Uint8> input,
  63. int len,
  64. ) {
  65. _invoke_async_query(port, input, len);
  66. }
  67. final _invoke_async_query_Dart _invoke_async_query =
  68. _dl.lookupFunction<_invoke_async_query_C, _invoke_async_query_Dart>(
  69. 'async_query');
  70. typedef _invoke_async_query_C = Void Function(
  71. Int64 port,
  72. Pointer<Uint8> input,
  73. Uint64 len,
  74. );
  75. typedef _invoke_async_query_Dart = void Function(
  76. int port,
  77. Pointer<Uint8> input,
  78. int len,
  79. );
  80. /// C function `free_rust`.
  81. void free_rust(
  82. Pointer<Uint8> input,
  83. int len,
  84. ) {
  85. _free_rust(input, len);
  86. }
  87. final _free_rust_Dart _free_rust =
  88. _dl.lookupFunction<_free_rust_C, _free_rust_Dart>('free_rust');
  89. typedef _free_rust_C = Void Function(
  90. Pointer<Uint8> input,
  91. Uint64 len,
  92. );
  93. typedef _free_rust_Dart = void Function(
  94. Pointer<Uint8> input,
  95. int len,
  96. );
  97. /// C function `init_stream`.
  98. int init_stream(int port) {
  99. return _init_stream(port);
  100. }
  101. final _init_stream_Dart _init_stream =
  102. _dl.lookupFunction<_init_stream_C, _init_stream_Dart>('init_stream');
  103. typedef _init_stream_C = Int32 Function(
  104. Int64 port,
  105. );
  106. typedef _init_stream_Dart = int Function(
  107. int port,
  108. );
  109. /// C function `init_logger`.
  110. int init_logger() {
  111. return _init_logger();
  112. }
  113. final _init_logger_Dart _init_logger =
  114. _dl.lookupFunction<_init_logger_C, _init_logger_Dart>('init_logger');
  115. typedef _init_logger_C = Int64 Function();
  116. typedef _init_logger_Dart = int Function();
  117. /// C function `init_sdk`.
  118. int init_sdk(
  119. Pointer<ffi.Utf8> path,
  120. ) {
  121. return _init_sdk(path);
  122. }
  123. final _init_sdk_Dart _init_sdk =
  124. _dl.lookupFunction<_init_sdk_C, _init_sdk_Dart>('init_sdk');
  125. typedef _init_sdk_C = Int64 Function(
  126. Pointer<ffi.Utf8> path,
  127. );
  128. typedef _init_sdk_Dart = int Function(
  129. Pointer<ffi.Utf8> path,
  130. );
  131. /// C function `link_me_please`.
  132. void link_me_please() {
  133. _link_me_please();
  134. }
  135. final _link_me_please_Dart _link_me_please = _dl
  136. .lookupFunction<_link_me_please_C, _link_me_please_Dart>('link_me_please');
  137. typedef _link_me_please_C = Void Function();
  138. typedef _link_me_please_Dart = void Function();
  139. /// Binding to `allo-isolate` crate
  140. void store_dart_post_cobject(
  141. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  142. ) {
  143. _store_dart_post_cobject(ptr);
  144. }
  145. final _store_dart_post_cobject_Dart _store_dart_post_cobject = _dl
  146. .lookupFunction<_store_dart_post_cobject_C, _store_dart_post_cobject_Dart>(
  147. 'store_dart_post_cobject');
  148. typedef _store_dart_post_cobject_C = Void Function(
  149. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  150. );
  151. typedef _store_dart_post_cobject_Dart = void Function(
  152. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  153. );
  154. /// C function `setup_logger`.
  155. void setup_logger(
  156. Pointer ptr,
  157. ) {
  158. _setup_logger(ptr);
  159. }
  160. final _setup_logger_Dart _setup_logger =
  161. _dl.lookupFunction<_setup_logger_C, _setup_logger_Dart>('setup_logger');
  162. typedef _setup_logger_C = Void Function(
  163. Pointer ptr,
  164. );
  165. typedef _setup_logger_Dart = void Function(
  166. Pointer ptr,
  167. );
  168. bool is_tester() {
  169. if (Foundation.kDebugMode) {
  170. // ignore: unnecessary_null_comparison
  171. if (Platform.executable == null) {
  172. return false;
  173. } else {
  174. return Platform.executable.contains("tester");
  175. }
  176. } else {
  177. return false;
  178. }
  179. }