From 8b8860e3b9dd200f02db787c5ff6f6d55374e0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=90=A7=E7=8E=89?= Date: Wed, 10 Aug 2022 19:18:19 +0800 Subject: [PATCH 1/3] fix: https://github.com/dart-native/dart_native/issues/99 --- dart_native/.fvm/fvm_config.json | 2 +- dart_native/CHANGELOG.md | 4 +++ dart_native/example/pubspec.lock | 35 ++++++++----------- .../lib/src/android/runtime/functions.dart | 4 +-- .../lib/src/android/runtime/messenger.dart | 10 ++---- .../src/darwin/common/callback_register.dart | 2 +- dart_native/lib/src/darwin/runtime/block.dart | 15 +++----- .../runtime/internal/native_runtime.dart | 8 ++--- dart_native/pubspec.yaml | 2 +- 9 files changed, 35 insertions(+), 47 deletions(-) diff --git a/dart_native/.fvm/fvm_config.json b/dart_native/.fvm/fvm_config.json index b3db758e..d790b239 100644 --- a/dart_native/.fvm/fvm_config.json +++ b/dart_native/.fvm/fvm_config.json @@ -1,4 +1,4 @@ { - "flutterSdkVersion": "stable", + "flutterSdkVersion": "2.2.3", "flavors": {} } \ No newline at end of file diff --git a/dart_native/CHANGELOG.md b/dart_native/CHANGELOG.md index 363109f5..a82e840f 100644 --- a/dart_native/CHANGELOG.md +++ b/dart_native/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.4 + +* [Fix] Errors and warnings. + ## 0.7.3 * [Fix] Garbage characters on Android. diff --git a/dart_native/example/pubspec.lock b/dart_native/example/pubspec.lock index ac8d03cc..705c3f69 100644 --- a/dart_native/example/pubspec.lock +++ b/dart_native/example/pubspec.lock @@ -14,7 +14,7 @@ packages: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "1.7.2" + version: "1.7.1" args: dependency: transitive description: @@ -28,7 +28,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.2" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -98,14 +98,14 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.3.1" + version: "1.2.0" checked_yaml: dependency: transitive description: @@ -140,7 +140,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.16.0" + version: "1.15.0" convert: dependency: transitive description: @@ -168,7 +168,7 @@ packages: path: ".." relative: true source: path - version: "0.7.3" + version: "0.7.4" dart_native_gen: dependency: transitive description: @@ -189,7 +189,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.2.0" ffi: dependency: transitive description: @@ -297,21 +297,14 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.11" - material_color_utilities: - dependency: transitive - description: - name: material_color_utilities - url: "https://pub.dartlang.org" - source: hosted - version: "0.1.4" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.3.0" mime: dependency: transitive description: @@ -332,7 +325,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.0" pedantic: dependency: transitive description: @@ -393,7 +386,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.2" + version: "1.8.1" stack_trace: dependency: transitive description: @@ -435,7 +428,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.9" + version: "0.3.0" timing: dependency: transitive description: @@ -456,7 +449,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.2" + version: "2.1.0" watcher: dependency: transitive description: @@ -479,5 +472,5 @@ packages: source: hosted version: "3.1.0" sdks: - dart: ">=2.17.0-0 <3.0.0" + dart: ">=2.13.0 <3.0.0" flutter: ">=2.2.0" diff --git a/dart_native/lib/src/android/runtime/functions.dart b/dart_native/lib/src/android/runtime/functions.dart index c60b6a23..eb4eb4d6 100644 --- a/dart_native/lib/src/android/runtime/functions.dart +++ b/dart_native/lib/src/android/runtime/functions.dart @@ -57,7 +57,7 @@ final Pointer Function( Pointer>, int dartPort, int thread, - bool isInterface) nativeInvoke = + int isInterface) nativeInvoke = nativeDylib .lookup< NativeFunction< @@ -72,7 +72,7 @@ final Pointer Function( Pointer>, Int64 dartPort, Int32 thread, - Bool isInterface)>>('InvokeNativeMethod') + Int32 isInterface)>>('InvokeNativeMethod') .asFunction(); // bind dart object lifecycle with java object diff --git a/dart_native/lib/src/android/runtime/messenger.dart b/dart_native/lib/src/android/runtime/messenger.dart index 99a182b1..adcc1674 100644 --- a/dart_native/lib/src/android/runtime/messenger.dart +++ b/dart_native/lib/src/android/runtime/messenger.dart @@ -68,9 +68,7 @@ void _invokeCallback(Pointer result, Pointer method, } /// remove assigned signature - _assignedSignatureMap[method]?.forEach((ptr) { - calloc.free(ptr); - }); + _assignedSignatureMap[method]?.forEach(calloc.free); _assignedSignatureMap.remove(method); calloc.free(typePtrs); @@ -122,7 +120,7 @@ dynamic _doInvoke( callbackPtr, nativePort, thread.index, - isInterface); + isInterface ? 1 : 0); dynamic result; if (callback == null) { @@ -132,9 +130,7 @@ dynamic _doInvoke( .elementAt(args?.length ?? 0) .value .toDartString()); - assignedSignaturePtr?.forEach((ptr) { - calloc.free(ptr); - }); + assignedSignaturePtr?.forEach(calloc.free); calloc.free(nativeArguments.typePointers); } return result; diff --git a/dart_native/lib/src/darwin/common/callback_register.dart b/dart_native/lib/src/darwin/common/callback_register.dart index c67abd24..7fb02a0e 100644 --- a/dart_native/lib/src/darwin/common/callback_register.dart +++ b/dart_native/lib/src/darwin/common/callback_register.dart @@ -18,7 +18,7 @@ bool registerMethodCallback( .setCallbackForSelectorOnTarget(targetPtr, selectorPtr, function); List dartTypes = dartTypeStringForFunction(function); List nativeTypes = nativeTypeStringForDartTypes(dartTypes); - bool returnString = nativeTypes.first == 'String'; + int returnString = nativeTypes.first == 'String' ? 1 : 0; int result = nativeAddMethod( targetPtr, selectorPtr, types, returnString, _callbackPtr, nativePort); return result != 0; diff --git a/dart_native/lib/src/darwin/runtime/block.dart b/dart_native/lib/src/darwin/runtime/block.dart index b9a35a62..ae930cc9 100644 --- a/dart_native/lib/src/darwin/runtime/block.dart +++ b/dart_native/lib/src/darwin/runtime/block.dart @@ -61,16 +61,15 @@ class Block extends id { List nativeTypes = nativeTypeStringForDartTypes(dartTypes); Pointer typeStringPtr = nativeTypes.join(', ').toNativeUtf8(); Pointer blockPtr = - blockCreate(typeStringPtr, _callbackPtr, shouldReturnAsync, nativePort); + blockCreate(typeStringPtr, _callbackPtr, shouldReturnAsync ? 0 : 1, nativePort); assert(blockPtr != nullptr); if (blockPtr == nullptr) { return nilBlock; } int sequence = _blockSequence(blockPtr); - Block result = Block.fromPointer(blockPtr); + Block result = Block.fromPointer(blockPtr, function: function); calloc.free(typeStringPtr); result.types = dartTypes; - result.function = function; result.shouldReturnAsync = shouldReturnAsync; result.typeEncodingsPtrPtr = _blockTypeEncodings(blockPtr); result.sequence = sequence; @@ -84,7 +83,7 @@ class Block extends id { /// Creating a [Block] from a [Pointer]. /// /// [Block] created by this method do NOT have [function] property. - Block.fromPointer(Pointer ptr) : super(ptr); + Block.fromPointer(Pointer ptr, {this.function}) : super(ptr); /// This [isa] block in iOS, but it's meaningless for a block created /// by Dart function. @@ -134,12 +133,8 @@ class Block extends id { if (newPtr == pointer) { return this; } - Block result = Block.fromPointer(newPtr); - // Block created by function. - if (function != null) { - result.function = function; - result.types = types; - } + Block result = Block.fromPointer(newPtr, function: function); + result.types = types; return result; } diff --git a/dart_native/lib/src/darwin/runtime/internal/native_runtime.dart b/dart_native/lib/src/darwin/runtime/internal/native_runtime.dart index d38ef0a4..c984984d 100644 --- a/dart_native/lib/src/darwin/runtime/internal/native_runtime.dart +++ b/dart_native/lib/src/darwin/runtime/internal/native_runtime.dart @@ -21,14 +21,14 @@ typedef AddMethodC = Int32 Function( Pointer target, Pointer selector, Pointer types, - Bool returnString, + Int32 returnString, Pointer> callback, Int64 dartPort); typedef AddMethodD = int Function( Pointer target, Pointer selector, Pointer types, - bool returnString, + int returnString, Pointer> callback, int dartPort); final AddMethodD nativeAddMethod = @@ -107,12 +107,12 @@ typedef BlockCallbackC = Void Function( typedef BlockCreateC = Pointer Function( Pointer typeEncodings, Pointer> callback, - Bool shouldReturnAsync, + Int32 shouldReturnAsync, Int64 dartPort); typedef BlockCreateD = Pointer Function( Pointer typeEncodings, Pointer> callback, - bool shouldReturnAsync, + int shouldReturnAsync, int dartPort); final BlockCreateD blockCreate = nativeDylib .lookupFunction('native_block_create'); diff --git a/dart_native/pubspec.yaml b/dart_native/pubspec.yaml index 5c036b96..9c26d007 100644 --- a/dart_native/pubspec.yaml +++ b/dart_native/pubspec.yaml @@ -1,6 +1,6 @@ name: dart_native description: Write native code using Dart. This package liberates you from native code and low performance channel. -version: 0.7.3 +version: 0.7.4 homepage: https://github.com/dart-native/dart_native environment: From 73e0fdc5cb85384e70b2cee9936e5d59a6a3772e Mon Sep 17 00:00:00 2001 From: huizzzhou Date: Wed, 10 Aug 2022 19:53:34 +0800 Subject: [PATCH 2/3] finalizer bug fix --- dart_native/lib/src/common/finalizer.dart | 1 - dart_native/lib/src/common/native_dylib.dart | 22 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/dart_native/lib/src/common/finalizer.dart b/dart_native/lib/src/common/finalizer.dart index 2b311abb..71b0ceb1 100644 --- a/dart_native/lib/src/common/finalizer.dart +++ b/dart_native/lib/src/common/finalizer.dart @@ -1,7 +1,6 @@ import 'dart:ffi'; import 'package:dart_native/src/common/native_dylib.dart'; -import 'package:dart_native/src/darwin/common/callback_manager.dart'; import 'package:ffi/ffi.dart'; typedef DartFinalizerFunction = void Function(); diff --git a/dart_native/lib/src/common/native_dylib.dart b/dart_native/lib/src/common/native_dylib.dart index 8fd87725..316651f6 100644 --- a/dart_native/lib/src/common/native_dylib.dart +++ b/dart_native/lib/src/common/native_dylib.dart @@ -2,9 +2,12 @@ import 'dart:ffi'; import 'dart:io'; import 'package:dart_native/src/android/common/library.dart' as library_android - show nativeDylib; + show nativeDylib, nativePort; + import 'package:dart_native/src/darwin/common/library.dart' as library_darwin show nativeDylib; +import 'package:dart_native/src/darwin/common/callback_manager.dart' + as callback_darwin show nativePort; DynamicLibrary? _nativeDylib; DynamicLibrary get nativeDylib { @@ -22,3 +25,20 @@ DynamicLibrary get nativeDylib { return _nativeDylib!; } + +int _nativePort = 0; +int get nativePort { + if (_nativePort != 0) { + return _nativePort; + } + + if (Platform.isIOS || Platform.isMacOS) { + _nativePort = callback_darwin.nativePort; + } else if (Platform.isAndroid) { + _nativePort = library_android.nativePort; + } else { + throw 'Platform not supported: ${Platform.localeName}'; + } + + return _nativePort; +} From 312288b075baa815235d39bdddb47dbb53b1357c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=90=A7=E7=8E=89?= Date: Wed, 10 Aug 2022 21:49:06 +0800 Subject: [PATCH 3/3] 0.7.4 --- dart_native/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/dart_native/CHANGELOG.md b/dart_native/CHANGELOG.md index a82e840f..bc4a5970 100644 --- a/dart_native/CHANGELOG.md +++ b/dart_native/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.7.4 * [Fix] Errors and warnings. +* [Fix] Dart exceptions on Android. ## 0.7.3