Skip to content

Commit

Permalink
Remove sync code from bridge on Dart side
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Oct 21, 2023
1 parent c2032ce commit 7c3d021
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 198 deletions.
52 changes: 1 addition & 51 deletions flutter_ffi_plugin/lib/src/bridge_engine/basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Type>{};
Expand Down Expand Up @@ -68,33 +67,6 @@ abstract class FlutterRustBridgeBase<T extends FlutterRustBridgeWireBase> {
_transformRust2DartMessage(raw, task.parseSuccessData));
}

/// Similar to [executeNormal], except that this will return synchronously
S executeSync<S>(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<S> executeStream<S>(FlutterRustBridgeTask<S> task) async* {
Expand Down Expand Up @@ -165,26 +137,4 @@ class FlutterRustBridgeTask<S> extends FlutterRustBridgeBaseTask {
);
}

/// A task to call FFI function, but it is synchronous.
class FlutterRustBridgeSyncTask<S> 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<dynamic> argValues,
required dynamic hint,
}) : super(
constMeta: constMeta,
argValues: argValues,
hint: hint,
);
}

class _CloseStreamException {}
1 change: 1 addition & 0 deletions flutter_ffi_plugin/lib/src/bridge_engine/exports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'helpers.dart';
export 'platform_independent.dart';
export 'typed_data.dart';
export 'load.dart';
export 'ffi/stub.dart';
85 changes: 0 additions & 85 deletions flutter_ffi_plugin/lib/src/bridge_engine/ffi.dart

This file was deleted.

22 changes: 0 additions & 22 deletions flutter_ffi_plugin/lib/src/bridge_engine/ffi/io.dart
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -31,23 +29,3 @@ class DartApiDl {
}
}
}

typedef WireSyncReturn = ffi.Pointer<Dart_CObject>;

List<dynamic> wireSyncReturnIntoDart(WireSyncReturn syncReturn) =>
syncReturn.ref.intoDart();

typedef PlatformPointer = ffi.Pointer<ffi.Void>;
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);
}
21 changes: 2 additions & 19 deletions flutter_ffi_plugin/lib/src/bridge_engine/ffi/stub.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand All @@ -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<dynamic> 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.
///
Expand Down
6 changes: 0 additions & 6 deletions flutter_ffi_plugin/lib/src/bridge_engine/ffi/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -119,10 +117,6 @@ abstract class FlutterRustBridgeWireBase {
}
}

typedef WireSyncReturn = List<dynamic>;

List<dynamic> wireSyncReturnIntoDart(WireSyncReturn syncReturn) => syncReturn;

class FlutterRustBridgeWasmWireBase<T extends WasmModule>
extends FlutterRustBridgeWireBase {
final Future<T> init;
Expand Down
2 changes: 1 addition & 1 deletion flutter_ffi_plugin/lib/src/bridge_engine/helpers.dart
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 0 additions & 14 deletions flutter_ffi_plugin/lib/src/bridge_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi.Pointer<wire_uint_8_list> Function(int)>();

void free_WireSyncReturn(
WireSyncReturn ptr,
) {
return _free_WireSyncReturn(
ptr,
);
}

late final _free_WireSyncReturnPtr =
_lookup<ffi.NativeFunction<ffi.Void Function(WireSyncReturn)>>(
'free_WireSyncReturn');
late final _free_WireSyncReturn =
_free_WireSyncReturnPtr.asFunction<void Function(WireSyncReturn)>();
}

final class _Dart_Handle extends ffi.Opaque {}
Expand Down

0 comments on commit 7c3d021

Please sign in to comment.