diff --git a/flutter_package/lib/src/load_os.dart b/flutter_package/lib/src/load_os.dart index c9aae384..46f08666 100644 --- a/flutter_package/lib/src/load_os.dart +++ b/flutter_package/lib/src/load_os.dart @@ -51,7 +51,21 @@ final rustLibrary = loadRustLibrary(); // This is for better readability of the code. typedef PostCObjectInner = Int8 Function(Int64, Pointer); -typedef PostCObjectFn = NativeFunction; +typedef PostCObjectPtr = Pointer>; +typedef SendDartSignalExtern = Void Function( + Int32, + Pointer, + UintPtr, + Pointer, + UintPtr, +); +typedef SendDartSignalWrap = void Function( + int, + Pointer, + int, + Pointer, + int, +); // Direct access to global function symbols loaded in the process. // These are available only if the native library is @@ -69,25 +83,6 @@ external void startRustLogicExtern(); ) external void stopRustLogicExtern(); -typedef SendDartSignalExtern = Void Function( - Int32, - Pointer, - UintPtr, - Pointer, - UintPtr, -); -@Native( - isLeaf: true, - symbol: 'send_dart_signal_extern', -) -external void sendDartSignalExtern( - int messageId, - Pointer messageBytesAddress, - int messageBytesLength, - Pointer binaryAddress, - int binaryLength, -); - @Native( isLeaf: true, symbol: 'prepare_isolate_extern', @@ -96,12 +91,24 @@ external void prepareIsolateExtern( int port, ); -@Native)>( +@Native( isLeaf: true, symbol: 'store_dart_post_cobject', ) external void storeDartPostCObjectExtern( - Pointer postCObject, + PostCObjectPtr postCObject, +); + +@Native( + isLeaf: true, + symbol: 'send_dart_signal_extern', +) +external void sendDartSignalExtern( + int messageId, + Pointer messageBytesAddress, + int messageBytesLength, + Pointer binaryAddress, + int binaryLength, ); /// Abstract class for unifying the interface @@ -109,13 +116,13 @@ external void storeDartPostCObjectExtern( abstract class RustLibrary { void startRustLogic(); void stopRustLogic(); + void prepareIsolate(int port); + void storeDartPostCObject(PostCObjectPtr postCObject); void sendDartSignal( int messageId, Uint8List messageBytes, Uint8List binary, ); - void prepareIsolate(int port); - void storeDartPostCObject(Pointer postCObject); } /// Class for global native library symbols loaded with `RTLD_GLOBAL`. @@ -133,6 +140,14 @@ class RustLibraryNew extends RustLibrary { stopRustLogicExtern(); } + void prepareIsolate(int port) { + prepareIsolateExtern(port); + } + + void storeDartPostCObject(PostCObjectPtr postCObject) { + storeDartPostCObjectExtern(postCObject); + } + void sendDartSignal( int messageId, Uint8List messageBytes, @@ -146,43 +161,57 @@ class RustLibraryNew extends RustLibrary { binary.length, ); } - - void prepareIsolate(int port) { - prepareIsolateExtern(port); - } - - void storeDartPostCObject(Pointer postCObject) { - storeDartPostCObjectExtern(postCObject); - } } /// Class for local native library symbols loaded with `RTLD_LOCAL`. /// This is relatively inefficient because `malloc.allocate` is required. class RustLibraryOld extends RustLibrary { - final DynamicLibrary lib; - RustLibraryOld(this.lib); - - void storeDartPostCObject(Pointer postCObject) { - final rustFunction = lib.lookupFunction< - Pointer Function(Pointer), - Pointer Function(Pointer)>( + late DynamicLibrary lib; + late void Function() startRustLogicExtern; + late void Function() stopRustLogicExtern; + late void Function(int) prepareIsolateExtern; + late void Function(PostCObjectPtr) storeDartPostCObjectExtern; + late void Function(int, Pointer, int, Pointer, int) + sendDartSignalExtern; + + RustLibraryOld(DynamicLibrary lib) { + this.lib = lib; + this.startRustLogicExtern = + lib.lookupFunction( + 'start_rust_logic_extern', + ); + this.stopRustLogicExtern = + lib.lookupFunction( + 'stop_rust_logic_extern', + ); + this.prepareIsolateExtern = + lib.lookupFunction( + 'prepare_isolate_extern', + ); + this.storeDartPostCObjectExtern = lib.lookupFunction< + Void Function(PostCObjectPtr), void Function(PostCObjectPtr)>( 'store_dart_post_cobject', ); - rustFunction(postCObject); + this.sendDartSignalExtern = + lib.lookupFunction( + 'send_dart_signal_extern', + ); } void startRustLogic() { - final rustFunction = lib.lookupFunction( - 'start_rust_logic_extern', - ); - rustFunction(); + startRustLogicExtern(); } void stopRustLogic() { - final rustFunction = lib.lookupFunction( - 'stop_rust_logic_extern', - ); - rustFunction(); + stopRustLogicExtern(); + } + + void prepareIsolate(int port) { + prepareIsolateExtern(port); + } + + void storeDartPostCObject(PostCObjectPtr postCObject) { + storeDartPostCObjectExtern(postCObject); } void sendDartSignal(int messageId, Uint8List messageBytes, Uint8List binary) { @@ -192,13 +221,7 @@ class RustLibraryOld extends RustLibrary { final Pointer binaryMemory = malloc.allocate(binary.length); binaryMemory.asTypedList(binary.length).setAll(0, binary); - final rustFunction = lib.lookupFunction< - Void Function(Int32, Pointer, UintPtr, Pointer, UintPtr), - void Function(int, Pointer, int, Pointer, int)>( - 'send_dart_signal_extern', - ); - - rustFunction( + sendDartSignalExtern( messageId, messageMemory, messageBytes.length, @@ -209,12 +232,4 @@ class RustLibraryOld extends RustLibrary { malloc.free(messageMemory); malloc.free(binaryMemory); } - - void prepareIsolate(int port) { - final rustFunction = - lib.lookupFunction( - 'prepare_isolate_extern', - ); - rustFunction(port); - } }