Skip to content

Commit

Permalink
Use void instead of FutureOr<void>
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi committed Sep 25, 2024
1 parent a2a5dc4 commit 3edf9e1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
1 change: 0 additions & 1 deletion pkgs/jni/lib/_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pkgs/jnigen/docs/interface_implementation.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ class Runnable extends JObject {
abstract interface class $Runnable {
factory $Runnable({
required FutureOr<void> Function() run,
required void Function() run,
}) = _$Runnable;
FutureOr<void> run();
void run();
}
class _$Runnable implements $Runnable {
_$Runnable({
required FutureOr<void> Function() run,
required void Function() run,
}) : _run = run;
final FutureOr<void> Function() _run;
final void Function() _run;
FutureOr<void> run() {
void run() {
return _run();
}
}
Expand Down
22 changes: 5 additions & 17 deletions pkgs/jnigen/lib/src/bindings/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -747,18 +747,6 @@ class _TypeGenerator extends TypeVisitor<String> {
}
}

class _ImplReturnType extends _TypeGenerator {
_ImplReturnType(super.resolver);

@override
String visitPrimitiveType(PrimitiveType node) {
// Supporting both `Future<void>` for listener callbacks and `void` for
// blocking ones.
if (node.name == 'void') return '$_jni.FutureOr<void>';
return super.visitPrimitiveType(node);
}
}

class _TypeClass {
final String name;
final bool canBeConst;
Expand Down Expand Up @@ -1503,7 +1491,7 @@ class _AbstractImplMethod extends Visitor<Method, void> {

@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);');
Expand All @@ -1519,7 +1507,7 @@ class _ConcreteImplClosureDef extends Visitor<Method, void> {

@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;');
Expand All @@ -1535,7 +1523,7 @@ class _ConcreteImplClosureCtorArg extends Visitor<Method, String> {

@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,';
Expand Down Expand Up @@ -1588,7 +1576,7 @@ class _ConcreteImplMethod extends Visitor<Method, void> {

@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(', ');
Expand Down Expand Up @@ -1647,7 +1635,7 @@ class _InterfaceIfAsyncMethod extends Visitor<Method, void> {
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<void>`.
// always going to return `void`.
//
// If the callback simply throws its return type will be `Never`. As any
// function `R <F>` is a subtype of `Never <F>`, we should have a special
Expand Down
20 changes: 10 additions & 10 deletions pkgs/jnigen/test/simple_package_test/bindings/simple_package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4897,15 +4897,15 @@ 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<void> 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,
}) = _$MyInterface;

_$jni.JObjType<$T> get T;

_$jni.FutureOr<void> 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);
Expand All @@ -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<void> 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,
Expand All @@ -4926,12 +4926,12 @@ class _$MyInterface<$T extends _$jni.JObject> implements $MyInterface<$T> {
@_$core.override
final _$jni.JObjType<$T> T;

final _$jni.FutureOr<void> 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<void> voidCallback(_$jni.JString s) {
void voidCallback(_$jni.JString s) {
return _voidCallback(s);
}

Expand Down Expand Up @@ -5308,20 +5308,20 @@ class MyRunnable extends _$jni.JObject {

abstract interface class $MyRunnable {
factory $MyRunnable({
required _$jni.FutureOr<void> Function() run,
required void Function() run,
}) = _$MyRunnable;

_$jni.FutureOr<void> run();
void run();
}

class _$MyRunnable implements $MyRunnable {
_$MyRunnable({
required _$jni.FutureOr<void> Function() run,
required void Function() run,
}) : _run = run;

final _$jni.FutureOr<void> Function() _run;
final void Function() _run;

_$jni.FutureOr<void> run() {
void run() {
return _run();
}
}
Expand Down

0 comments on commit 3edf9e1

Please sign in to comment.