From 3edf9e1fa87337365d3d6852bf6510ce5fcb0a3f Mon Sep 17 00:00:00 2001 From: Hossein Yousefi Date: Wed, 25 Sep 2024 15:34:32 +0200 Subject: [PATCH] Use void instead of FutureOr --- pkgs/jni/lib/_internal.dart | 1 - pkgs/jnigen/docs/interface_implementation.md | 10 ++++----- .../lib/src/bindings/dart_generator.dart | 22 +++++-------------- .../bindings/simple_package.dart | 20 ++++++++--------- 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/pkgs/jni/lib/_internal.dart b/pkgs/jni/lib/_internal.dart index 93f4a5e1b..768be191f 100644 --- a/pkgs/jni/lib/_internal.dart +++ b/pkgs/jni/lib/_internal.dart @@ -12,7 +12,6 @@ import 'dart:ffi' as ffi show Int32; import 'dart:ffi' hide Int32; // Exporting all the necessary bits for the generated bindings. -export 'dart:async' show FutureOr; export 'dart:ffi' show Double, diff --git a/pkgs/jnigen/docs/interface_implementation.md b/pkgs/jnigen/docs/interface_implementation.md index a0a082e26..83e6dddd1 100644 --- a/pkgs/jnigen/docs/interface_implementation.md +++ b/pkgs/jnigen/docs/interface_implementation.md @@ -38,20 +38,20 @@ class Runnable extends JObject { abstract interface class $Runnable { factory $Runnable({ - required FutureOr Function() run, + required void Function() run, }) = _$Runnable; - FutureOr run(); + void run(); } class _$Runnable implements $Runnable { _$Runnable({ - required FutureOr Function() run, + required void Function() run, }) : _run = run; - final FutureOr Function() _run; + final void Function() _run; - FutureOr run() { + void run() { return _run(); } } diff --git a/pkgs/jnigen/lib/src/bindings/dart_generator.dart b/pkgs/jnigen/lib/src/bindings/dart_generator.dart index d105532a4..e1e688e5a 100644 --- a/pkgs/jnigen/lib/src/bindings/dart_generator.dart +++ b/pkgs/jnigen/lib/src/bindings/dart_generator.dart @@ -747,18 +747,6 @@ class _TypeGenerator extends TypeVisitor { } } -class _ImplReturnType extends _TypeGenerator { - _ImplReturnType(super.resolver); - - @override - String visitPrimitiveType(PrimitiveType node) { - // Supporting both `Future` for listener callbacks and `void` for - // blocking ones. - if (node.name == 'void') return '$_jni.FutureOr'; - return super.visitPrimitiveType(node); - } -} - class _TypeClass { final String name; final bool canBeConst; @@ -1503,7 +1491,7 @@ class _AbstractImplMethod extends Visitor { @override void visit(Method node) { - final returnType = node.returnType.accept(_ImplReturnType(resolver)); + final returnType = node.returnType.accept(_TypeGenerator(resolver)); final name = node.finalName; final args = node.params.accept(_ParamDef(resolver)).join(', '); s.writeln(' $returnType $name($args);'); @@ -1519,7 +1507,7 @@ class _ConcreteImplClosureDef extends Visitor { @override void visit(Method node) { - final returnType = node.returnType.accept(_ImplReturnType(resolver)); + final returnType = node.returnType.accept(_TypeGenerator(resolver)); final name = node.finalName; final args = node.params.accept(_ParamDef(resolver)).join(', '); s.writeln(' final $returnType Function($args) _$name;'); @@ -1535,7 +1523,7 @@ class _ConcreteImplClosureCtorArg extends Visitor { @override String visit(Method node) { - final returnType = node.returnType.accept(_ImplReturnType(resolver)); + final returnType = node.returnType.accept(_TypeGenerator(resolver)); final name = node.finalName; final args = node.params.accept(_ParamDef(resolver)).join(', '); return 'required $returnType Function($args) $name,'; @@ -1588,7 +1576,7 @@ class _ConcreteImplMethod extends Visitor { @override void visit(Method node) { - final returnType = node.returnType.accept(_ImplReturnType(resolver)); + final returnType = node.returnType.accept(_TypeGenerator(resolver)); final name = node.finalName; final argsDef = node.params.accept(_ParamDef(resolver)).join(', '); final argsCall = node.params.map((param) => param.finalName).join(', '); @@ -1647,7 +1635,7 @@ class _InterfaceIfAsyncMethod extends Visitor { final neverType = node.accept(_InterfaceNeverFunctionType(resolver)); // If the implementation is using the callback passing style, look at the // actual passed callback instead of the wrapper function. The wrapper is - // always going to return `FutureOr`. + // always going to return `void`. // // If the callback simply throws its return type will be `Never`. As any // function `R ` is a subtype of `Never `, we should have a special diff --git a/pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart b/pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart index 1904bc5bc..3904ce0c7 100644 --- a/pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart +++ b/pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart @@ -4897,7 +4897,7 @@ class MyInterface<$T extends _$jni.JObject> extends _$jni.JObject { abstract interface class $MyInterface<$T extends _$jni.JObject> { factory $MyInterface({ required _$jni.JObjType<$T> T, - required _$jni.FutureOr Function(_$jni.JString s) voidCallback, + required void Function(_$jni.JString s) voidCallback, required _$jni.JString Function(_$jni.JString s) stringCallback, required $T Function($T t) varCallback, required int Function(int a, bool b, int c, double d) manyPrimitives, @@ -4905,7 +4905,7 @@ abstract interface class $MyInterface<$T extends _$jni.JObject> { _$jni.JObjType<$T> get T; - _$jni.FutureOr voidCallback(_$jni.JString s); + void voidCallback(_$jni.JString s); _$jni.JString stringCallback(_$jni.JString s); $T varCallback($T t); int manyPrimitives(int a, bool b, int c, double d); @@ -4914,7 +4914,7 @@ abstract interface class $MyInterface<$T extends _$jni.JObject> { class _$MyInterface<$T extends _$jni.JObject> implements $MyInterface<$T> { _$MyInterface({ required this.T, - required _$jni.FutureOr Function(_$jni.JString s) voidCallback, + required void Function(_$jni.JString s) voidCallback, required _$jni.JString Function(_$jni.JString s) stringCallback, required $T Function($T t) varCallback, required int Function(int a, bool b, int c, double d) manyPrimitives, @@ -4926,12 +4926,12 @@ class _$MyInterface<$T extends _$jni.JObject> implements $MyInterface<$T> { @_$core.override final _$jni.JObjType<$T> T; - final _$jni.FutureOr Function(_$jni.JString s) _voidCallback; + final void Function(_$jni.JString s) _voidCallback; final _$jni.JString Function(_$jni.JString s) _stringCallback; final $T Function($T t) _varCallback; final int Function(int a, bool b, int c, double d) _manyPrimitives; - _$jni.FutureOr voidCallback(_$jni.JString s) { + void voidCallback(_$jni.JString s) { return _voidCallback(s); } @@ -5308,20 +5308,20 @@ class MyRunnable extends _$jni.JObject { abstract interface class $MyRunnable { factory $MyRunnable({ - required _$jni.FutureOr Function() run, + required void Function() run, }) = _$MyRunnable; - _$jni.FutureOr run(); + void run(); } class _$MyRunnable implements $MyRunnable { _$MyRunnable({ - required _$jni.FutureOr Function() run, + required void Function() run, }) : _run = run; - final _$jni.FutureOr Function() _run; + final void Function() _run; - _$jni.FutureOr run() { + void run() { return _run(); } }