diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/basic.dart b/flutter_ffi_plugin/lib/src/bridge_engine/basic.dart index f84084d6..3dbea6a0 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/basic.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/basic.dart @@ -2,9 +2,8 @@ import 'dart:async'; import 'platform_independent.dart'; import 'utils.dart'; -import 'ffi.dart'; -export 'ffi.dart'; import 'isolate.dart'; +import 'ffi/stub.dart'; export 'isolate.dart'; final _instances = {}; @@ -68,33 +67,6 @@ abstract class FlutterRustBridgeBase { _transformRust2DartMessage(raw, task.parseSuccessData)); } - /// Similar to [executeNormal], except that this will return synchronously - - S executeSync(FlutterRustBridgeSyncTask task) { - final WireSyncReturn syncReturn; - try { - syncReturn = task.callFfi(); - } catch (err, st) { - throw FfiException('EXECUTE_SYNC_ABORT', '$err', st); - } - try { - final syncReturnAsDartObject = wireSyncReturnIntoDart(syncReturn); - assert(syncReturnAsDartObject.length == 2); - final rawReturn = syncReturnAsDartObject[0]; - final isSuccess = syncReturnAsDartObject[1]; - if (isSuccess) { - return task.parseSuccessData(rawReturn); - } else { - throw FfiException('EXECUTE_SYNC', rawReturn as String, null); - } - } catch (err, st) { - if (err is FfiException) rethrow; - throw FfiException('EXECUTE_SYNC_ABORT', '$err', st); - } finally { - inner.free_WireSyncReturn(syncReturn); - } - } - /// Similar to [executeNormal], except that this will return a [Stream] instead of a [Future]. Stream executeStream(FlutterRustBridgeTask task) async* { @@ -165,26 +137,4 @@ class FlutterRustBridgeTask extends FlutterRustBridgeBaseTask { ); } -/// A task to call FFI function, but it is synchronous. - -class FlutterRustBridgeSyncTask extends FlutterRustBridgeBaseTask { - /// The underlying function to call FFI function, usually the generated wire function - final WireSyncReturn Function() callFfi; - - /// Parse the returned data from the underlying function - final S Function(dynamic) parseSuccessData; - - const FlutterRustBridgeSyncTask({ - required this.callFfi, - required this.parseSuccessData, - required FlutterRustBridgeTaskConstMeta constMeta, - required List argValues, - required dynamic hint, - }) : super( - constMeta: constMeta, - argValues: argValues, - hint: hint, - ); -} - class _CloseStreamException {} diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/exports.dart b/flutter_ffi_plugin/lib/src/bridge_engine/exports.dart index 95e918cc..91b67963 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/exports.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/exports.dart @@ -3,3 +3,4 @@ export 'helpers.dart'; export 'platform_independent.dart'; export 'typed_data.dart'; export 'load.dart'; +export 'ffi/stub.dart'; diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/ffi.dart b/flutter_ffi_plugin/lib/src/bridge_engine/ffi.dart deleted file mode 100755 index 5e440916..00000000 --- a/flutter_ffi_plugin/lib/src/bridge_engine/ffi.dart +++ /dev/null @@ -1,85 +0,0 @@ -import 'ffi/io.dart' if (dart.library.html) 'ffi/web.dart'; - -export 'ffi/stub.dart' - if (dart.library.io) 'ffi/io.dart' - if (dart.library.html) 'ffi/web.dart'; - -typedef DropFnType = void Function(PlatformPointer); -typedef ShareFnType = PlatformPointer Function(PlatformPointer); - -/// Rust SyncReturn type is forced cast to u64. -const syncReturnPointerLength = 8; - -/// An opaque pointer to a native C or Rust type. -/// Recipients of this type should call [dispose] at least once during runtime. -/// If passed to a native function after being [dispose]d, an exception will be thrown. -abstract class FrbOpaque extends FrbOpaqueBase { - /// Pointer to this opaque Rust type. - PlatformPointer _ptr; - - /// A native finalizer rust opaque type. - /// Is static for each frb api class instance. - OpaqueTypeFinalizer get staticFinalizer; - - /// Displays the need to release ownership when sending to rust. - bool _move = false; - set move(bool move) => _move = move; - - /// Rust type specific drop function. - /// - /// This function should never be called manually. - DropFnType get dropFn; - - /// Rust type specific share function. - /// - /// This function should never be called manually. - ShareFnType get shareFn; - - /// This constructor should never be called manually. - - FrbOpaque.unsafe(int ptr, int size) : _ptr = FrbOpaqueBase.initPtr(ptr) { - if (ptr != 0) { - FrbOpaqueBase.finalizerAttach(this, _ptr, size, staticFinalizer); - } - } - - /// Call Rust destructors on the backing memory of this pointer. - /// - /// This function should be run at least once during the lifetime of the - /// program, and can be run many times. - /// - /// When passed into a Rust function, Rust enacts *shared ownership*, - /// if this pointer is shared with Rust when [dispose] is called, - /// ownership is fully transferred to Rust else this pointer is cleared. - void dispose() { - if (!isStale()) { - var ptr = _ptr; - _ptr = FrbOpaqueBase.nullPtr(); - - staticFinalizer.detach(this); - dropFn(ptr); - } - } - - /// Increments inner reference counter and returns pointer to the underlying - /// Rust object. - /// - /// Throws a [StateError] if called after [dispose]. - - PlatformPointer shareOrMove() { - if (!isStale()) { - var ptr = shareFn(_ptr); - if (_move) { - dispose(); - } - return ptr; - } else { - return FrbOpaqueBase.nullPtr(); - } - } - - /// Checks whether [dispose] has been called at any point during the lifetime - /// of this pointer. This does not guarantee that the backing memory has - /// actually been reclaimed. - bool isStale() => FrbOpaqueBase.isStalePtr(_ptr); -} diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/io.dart b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/io.dart index 338c8782..bff4b863 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/io.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/io.dart @@ -1,7 +1,5 @@ import 'dart:ffi' as ffi; -import 'dart:ffi'; export 'dart:ffi' show NativePort, DynamicLibrary; -import 'dart_cobject.dart'; import 'stub.dart' show FlutterRustBridgeWireBase; export 'stub.dart' @@ -31,23 +29,3 @@ class DartApiDl { } } } - -typedef WireSyncReturn = ffi.Pointer; - -List wireSyncReturnIntoDart(WireSyncReturn syncReturn) => - syncReturn.ref.intoDart(); - -typedef PlatformPointer = ffi.Pointer; -typedef OpaqueTypeFinalizer = NativeFinalizer; - -/// An opaque pointer to a native C or Rust type. -/// Recipients of this type should call [dispose] at least once during runtime. -/// If passed to a native function after being [dispose]d, an exception will be thrown. -class FrbOpaqueBase implements Finalizable { - static PlatformPointer initPtr(int ptr) => ffi.Pointer.fromAddress(ptr); - static PlatformPointer nullPtr() => ffi.Pointer.fromAddress(0); - static bool isStalePtr(PlatformPointer ptr) => ptr.address == 0; - static void finalizerAttach(FrbOpaqueBase opaque, PlatformPointer ptr, - int size, OpaqueTypeFinalizer finalizer) => - finalizer.attach(opaque, ptr, detach: opaque, externalSize: size); -} diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/stub.dart b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/stub.dart index 1a4af4a7..6e2dda6b 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/stub.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/stub.dart @@ -1,16 +1,9 @@ import 'dart:async'; import 'io.dart' if (dart.library.html) 'web.dart' - show DartPostCObject, NativePortType, WireSyncReturn; + show DartPostCObject, NativePortType; export 'io.dart' if (dart.library.html) 'web.dart' - show - ExternalLibrary, - WireSyncReturn, - FrbOpaqueBase, - DartApiDl, - NativePortType, - PlatformPointer, - OpaqueTypeFinalizer; + show ExternalLibrary, DartApiDl, NativePortType; import 'package:rinf/src/bridge_engine/isolate.dart' show SendPort; /// This class, together with its subclasses, are only for internal usage. @@ -36,12 +29,6 @@ abstract class FlutterRustBridgeWireBase { int new_dart_opaque(Object obj) { throw UnimplementedError(); } - - /// Not to be used by normal users, but has to be public for generated code - // ignore: non_constant_identifier_names - void free_WireSyncReturn(WireSyncReturn val) { - throw UnimplementedError(); - } } extension NativeType on SendPort { @@ -52,10 +39,6 @@ extension StoreDartPostCObjectExt on FlutterRustBridgeWireBase { void storeDartPostCObject() => throw UnimplementedError(); } -/// Generates the dynamic Dart object from either an FFI struct or a JS value -List wireSyncReturnIntoDart(WireSyncReturn syncReturn) => - throw UnimplementedError(); - /// Whether the web platform has been isolated by COOP and COEP headers, /// and is capable of sharing buffers between workers. /// diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/web.dart b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/web.dart index fc63f820..70afd119 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/ffi/web.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/ffi/web.dart @@ -100,8 +100,6 @@ external void dropDartObject(int ptr); abstract class FlutterRustBridgeWireBase { void storeDartPostCObject() {} - // ignore: non_constant_identifier_names - void free_WireSyncReturn(WireSyncReturn raw) {} // ignore: non_constant_identifier_names Object get_dart_object(int ptr) { @@ -119,10 +117,6 @@ abstract class FlutterRustBridgeWireBase { } } -typedef WireSyncReturn = List; - -List wireSyncReturnIntoDart(WireSyncReturn syncReturn) => syncReturn; - class FlutterRustBridgeWasmWireBase extends FlutterRustBridgeWireBase { final Future init; diff --git a/flutter_ffi_plugin/lib/src/bridge_engine/helpers.dart b/flutter_ffi_plugin/lib/src/bridge_engine/helpers.dart index 77d9389e..77d0f758 100755 --- a/flutter_ffi_plugin/lib/src/bridge_engine/helpers.dart +++ b/flutter_ffi_plugin/lib/src/bridge_engine/helpers.dart @@ -1,8 +1,8 @@ import 'dart:async'; import 'basic.dart'; +import 'ffi/stub.dart'; import 'platform_independent.dart'; -export 'ffi.dart'; /// borrowed from flutter foundation [kIsWeb](https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html), /// but allows for using it in a Dart context alike diff --git a/flutter_ffi_plugin/lib/src/bridge_generated.io.dart b/flutter_ffi_plugin/lib/src/bridge_generated.io.dart index e1298c79..9b82367b 100755 --- a/flutter_ffi_plugin/lib/src/bridge_generated.io.dart +++ b/flutter_ffi_plugin/lib/src/bridge_generated.io.dart @@ -305,20 +305,6 @@ class BridgeWire implements FlutterRustBridgeWireBase { 'new_uint_8_list_0'); late final _new_uint_8_list_0 = _new_uint_8_list_0Ptr .asFunction Function(int)>(); - - void free_WireSyncReturn( - WireSyncReturn ptr, - ) { - return _free_WireSyncReturn( - ptr, - ); - } - - late final _free_WireSyncReturnPtr = - _lookup>( - 'free_WireSyncReturn'); - late final _free_WireSyncReturn = - _free_WireSyncReturnPtr.asFunction(); } final class _Dart_Handle extends ffi.Opaque {}