ffi.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 `init_stream`.
  74. int set_stream_port(int port) {
  75. return _set_stream_port(port);
  76. }
  77. final _set_stream_port_Dart _set_stream_port =
  78. _dl.lookupFunction<_set_stream_port_C, _set_stream_port_Dart>('set_stream_port');
  79. typedef _set_stream_port_C = Int32 Function(
  80. Int64 port,
  81. );
  82. typedef _set_stream_port_Dart = int Function(
  83. int port,
  84. );
  85. /// C function `link_me_please`.
  86. void link_me_please() {
  87. _link_me_please();
  88. }
  89. final _link_me_please_Dart _link_me_please = _dl
  90. .lookupFunction<_link_me_please_C, _link_me_please_Dart>('link_me_please');
  91. typedef _link_me_please_C = Void Function();
  92. typedef _link_me_please_Dart = void Function();
  93. /// Binding to `allo-isolate` crate
  94. void store_dart_post_cobject(
  95. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  96. ) {
  97. _store_dart_post_cobject(ptr);
  98. }
  99. final _store_dart_post_cobject_Dart _store_dart_post_cobject = _dl
  100. .lookupFunction<_store_dart_post_cobject_C, _store_dart_post_cobject_Dart>(
  101. 'store_dart_post_cobject');
  102. typedef _store_dart_post_cobject_C = Void Function(
  103. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  104. );
  105. typedef _store_dart_post_cobject_Dart = void Function(
  106. Pointer<NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>> ptr,
  107. );
  108. bool is_tester() {
  109. if (Foundation.kDebugMode) {
  110. // ignore: unnecessary_null_comparison
  111. // if (Platform.executable.isEmpty) {
  112. // return false;
  113. // } else {
  114. // return Platform.executable.contains("tester");
  115. // }
  116. return false;
  117. } else {
  118. return false;
  119. }
  120. }