Skip to content

Commit 9c9b31c

Browse files
authored
[jnigen] Kotlin void suspend funcs now return Future<void> in Dart (dart-lang#2922)
1 parent 67ea02e commit 9c9b31c

6 files changed

Lines changed: 231 additions & 11 deletions

File tree

‎pkgs/jni/lib/src/kotlin.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ final _coroutineSuspended = _coroutineIntrinsicsClass.staticMethodId(
3939
'()Ljava/lang/Object;',
4040
)(_coroutineIntrinsicsClass, const $JObject$Type$(), []);
4141

42+
final _unitClass = JClass.forName('kotlin/Unit');
43+
final _unit = _unitClass
44+
.staticFieldId('INSTANCE', 'Lkotlin/Unit;')
45+
.get(_unitClass, const $JObject$Type$());
46+
4247
@internal
4348
class KotlinContinuation extends JObject {
4449
KotlinContinuation.fromReference(super.reference) : super.fromReference();
@@ -68,4 +73,7 @@ class KotlinContinuation extends JObject {
6873
future.then(resumeWith, onError: resumeWithException);
6974
return _coroutineSuspended;
7075
}
76+
77+
JObject resumeWithVoidFuture(Future<void> future) =>
78+
resumeWithFuture(future.then((_) => _unit));
7179
}

‎pkgs/jnigen/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
implemented using Dart functions that return a `Future`.
66
- Namespace primitive types to avoid collisions with generated API names, eg
77
`bool`.
8+
- Kotlin suspend functions with no result (a return type of `Unit`) now return
9+
`Future<void>` in Dart instead of `Future<JObject>`.
810

911
## 0.15.0
1012

‎pkgs/jnigen/lib/src/bindings/dart_generator.dart‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ extension on DeclaredType {
8383

8484
extension on Method {
8585
bool get isSuspendFun => asyncReturnType != null;
86+
bool get isAsyncVoid => asyncReturnType?.name == 'kotlin.Unit';
8687

87-
String returnTypeMaybeAsync(TypeVisitor<String> generator) => isSuspendFun
88-
? '$_core.Future<${asyncReturnType!.accept(generator)}>'
89-
: returnType.accept(generator);
88+
String returnTypeMaybeAsync(TypeVisitor<String> generator) => isAsyncVoid
89+
? '$_core.Future<void>'
90+
: isSuspendFun
91+
? '$_core.Future<${asyncReturnType!.accept(generator)}>'
92+
: returnType.accept(generator);
9093

9194
List<Param> get paramsMaybeAsync {
9295
final p = params.toList();
@@ -1485,11 +1488,8 @@ ${modifier}final _$name = $_protectedExtension
14851488
s.write(' $ifStatic$returnType $name$typeParamsDef($params$typeClassDef)');
14861489
final callExpr = methodCall(node);
14871490
if (node.isSuspendFun) {
1488-
final returningType =
1489-
node.asyncReturnType!.accept(_TypeGenerator(resolver));
1490-
final returningTypeClass =
1491-
node.asyncReturnType!.accept(_TypeClassGenerator(resolver)).name;
1492-
final isNullable = node.asyncReturnType!.isNullable;
1491+
final asyncReturnType = node.asyncReturnType!;
1492+
final isNullable = asyncReturnType.isNullable;
14931493
final continuation = node.params.last.finalName;
14941494
s.write('''async {
14951495
$typeInference
@@ -1515,10 +1515,22 @@ ${modifier}final _$name = $_protectedExtension
15151515
} else {
15161516
\$o = \$r;
15171517
}
1518+
''');
1519+
1520+
if (node.isAsyncVoid) {
1521+
s.write(' return;');
1522+
} else {
1523+
final returningType = asyncReturnType.accept(_TypeGenerator(resolver));
1524+
final returningTypeClass =
1525+
asyncReturnType.accept(_TypeClassGenerator(resolver)).name;
1526+
s.write('''
15181527
return \$o${isNullable ? '?' : ''}.as<$returningType>(
15191528
$returningTypeClass,
15201529
releaseOriginal: true,
1521-
);
1530+
);''');
1531+
}
1532+
1533+
s.write('''
15221534
}
15231535
15241536
''');
@@ -1993,12 +2005,14 @@ class _InterfaceMethodIf extends Visitor<Method, void> {
19932005
final returnValue = node.returnType.accept(returnBox);
19942006

19952007
if (node.isSuspendFun) {
2008+
final resume =
2009+
node.isAsyncVoid ? 'resumeWithVoidFuture' : 'resumeWithFuture';
19962010
final contArg = StringBuffer();
19972011
node.params.last.accept(_InterfaceParamCast(resolver, contArg,
19982012
paramIndex: node.params.length - 1));
19992013
s.write('''
20002014
final \$r = $_jni.KotlinContinuation.fromReference($contArg.reference)
2001-
.resumeWithFuture($result);
2015+
.$resume($result);
20022016
return $returnValue;
20032017
''');
20042018
} else {

‎pkgs/jnigen/test/kotlin_test/bindings/kotlin.dart‎

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,98 @@ class SuspendFun extends jni$_.JObject {
35053505
releaseOriginal: true,
35063506
);
35073507
}
3508+
3509+
static final _id_getResult = _class.instanceMethodId(
3510+
r'getResult',
3511+
r'()I',
3512+
);
3513+
3514+
static final _getResult = jni$_.ProtectedJniExtensions.lookup<
3515+
jni$_.NativeFunction<
3516+
jni$_.JniResult Function(
3517+
jni$_.Pointer<jni$_.Void>,
3518+
jni$_.JMethodIDPtr,
3519+
)>>('globalEnv_CallIntMethod')
3520+
.asFunction<
3521+
jni$_.JniResult Function(
3522+
jni$_.Pointer<jni$_.Void>,
3523+
jni$_.JMethodIDPtr,
3524+
)>();
3525+
3526+
/// from: `public final int getResult()`
3527+
int getResult() {
3528+
return _getResult(reference.pointer, _id_getResult as jni$_.JMethodIDPtr)
3529+
.integer;
3530+
}
3531+
3532+
static final _id_setResult = _class.instanceMethodId(
3533+
r'setResult',
3534+
r'(I)V',
3535+
);
3536+
3537+
static final _setResult = jni$_.ProtectedJniExtensions.lookup<
3538+
jni$_.NativeFunction<
3539+
jni$_.JThrowablePtr Function(
3540+
jni$_.Pointer<jni$_.Void>,
3541+
jni$_.JMethodIDPtr,
3542+
jni$_.VarArgs<(jni$_.Int32,)>)>>('globalEnv_CallVoidMethod')
3543+
.asFunction<
3544+
jni$_.JThrowablePtr Function(
3545+
jni$_.Pointer<jni$_.Void>, jni$_.JMethodIDPtr, int)>();
3546+
3547+
/// from: `public final void setResult(int i)`
3548+
void setResult(
3549+
int i,
3550+
) {
3551+
_setResult(reference.pointer, _id_setResult as jni$_.JMethodIDPtr, i)
3552+
.check();
3553+
}
3554+
3555+
static final _id_noReturn = _class.instanceMethodId(
3556+
r'noReturn',
3557+
r'(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;',
3558+
);
3559+
3560+
static final _noReturn = jni$_.ProtectedJniExtensions.lookup<
3561+
jni$_.NativeFunction<
3562+
jni$_.JniResult Function(
3563+
jni$_.Pointer<jni$_.Void>,
3564+
jni$_.JMethodIDPtr,
3565+
jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>)>>(
3566+
'globalEnv_CallObjectMethod')
3567+
.asFunction<
3568+
jni$_.JniResult Function(jni$_.Pointer<jni$_.Void>,
3569+
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();
3570+
3571+
/// from: `public suspend fun noReturn(): kotlin.Unit`
3572+
/// The returned object must be released after use, by calling the [release] method.
3573+
core$_.Future<void> noReturn() async {
3574+
final $p = jni$_.ReceivePort();
3575+
final _$continuation = jni$_.ProtectedJniExtensions.newPortContinuation($p);
3576+
3577+
final $r = _noReturn(reference.pointer, _id_noReturn as jni$_.JMethodIDPtr,
3578+
_$continuation.pointer)
3579+
.object<jni$_.JObject>(const jni$_.$JObject$Type$());
3580+
_$continuation.release();
3581+
jni$_.JObject $o;
3582+
if ($r.isInstanceOf(jni$_.coroutineSingletonsClass)) {
3583+
$r.release();
3584+
final $a = await $p.first;
3585+
$o = jni$_.JObject.fromReference(
3586+
jni$_.JGlobalReference(jni$_.JObjectPtr.fromAddress($a)));
3587+
if ($o.isInstanceOf(jni$_.result$Class)) {
3588+
$o = jni$_.resultValueField.get($o, const jni$_.$JObject$Type$());
3589+
} else if ($o.isInstanceOf(jni$_.result$FailureClass)) {
3590+
final $e =
3591+
jni$_.failureExceptionField.get($o, const jni$_.$JObject$Type$());
3592+
$o.release();
3593+
jni$_.Jni.throwException($e.reference.toPointer());
3594+
}
3595+
} else {
3596+
$o = $r;
3597+
}
3598+
return;
3599+
}
35083600
}
35093601

35103602
final class $SuspendFun$NullableType$ extends jni$_.JType<SuspendFun?> {
@@ -4048,6 +4140,52 @@ class SuspendInterface extends jni$_.JObject {
40484140
);
40494141
}
40504142

4143+
static final _id_noReturn = _class.instanceMethodId(
4144+
r'noReturn',
4145+
r'(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;',
4146+
);
4147+
4148+
static final _noReturn = jni$_.ProtectedJniExtensions.lookup<
4149+
jni$_.NativeFunction<
4150+
jni$_.JniResult Function(
4151+
jni$_.Pointer<jni$_.Void>,
4152+
jni$_.JMethodIDPtr,
4153+
jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>)>>(
4154+
'globalEnv_CallObjectMethod')
4155+
.asFunction<
4156+
jni$_.JniResult Function(jni$_.Pointer<jni$_.Void>,
4157+
jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>();
4158+
4159+
/// from: `public suspend fun noReturn(): kotlin.Unit`
4160+
/// The returned object must be released after use, by calling the [release] method.
4161+
core$_.Future<void> noReturn() async {
4162+
final $p = jni$_.ReceivePort();
4163+
final _$continuation = jni$_.ProtectedJniExtensions.newPortContinuation($p);
4164+
4165+
final $r = _noReturn(reference.pointer, _id_noReturn as jni$_.JMethodIDPtr,
4166+
_$continuation.pointer)
4167+
.object<jni$_.JObject>(const jni$_.$JObject$Type$());
4168+
_$continuation.release();
4169+
jni$_.JObject $o;
4170+
if ($r.isInstanceOf(jni$_.coroutineSingletonsClass)) {
4171+
$r.release();
4172+
final $a = await $p.first;
4173+
$o = jni$_.JObject.fromReference(
4174+
jni$_.JGlobalReference(jni$_.JObjectPtr.fromAddress($a)));
4175+
if ($o.isInstanceOf(jni$_.result$Class)) {
4176+
$o = jni$_.resultValueField.get($o, const jni$_.$JObject$Type$());
4177+
} else if ($o.isInstanceOf(jni$_.result$FailureClass)) {
4178+
final $e =
4179+
jni$_.failureExceptionField.get($o, const jni$_.$JObject$Type$());
4180+
$o.release();
4181+
jni$_.Jni.throwException($e.reference.toPointer());
4182+
}
4183+
} else {
4184+
$o = $r;
4185+
}
4186+
return;
4187+
}
4188+
40514189
/// Maps a specific port to the implemented interface.
40524190
static final core$_.Map<int, $SuspendInterface> _$impls = {};
40534191
static jni$_.JObjectPtr _$invoke(
@@ -4161,6 +4299,18 @@ class SuspendInterface extends jni$_.JObject {
41614299
.toPointer() ??
41624300
jni$_.nullptr;
41634301
}
4302+
if ($d ==
4303+
r'noReturn(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;') {
4304+
final $r = jni$_.KotlinContinuation.fromReference($a![0]!
4305+
.as(const jni$_.$JObject$Type$(), releaseOriginal: true)
4306+
.reference)
4307+
.resumeWithVoidFuture(_$impls[$p]!.noReturn());
4308+
return ($r as jni$_.JObject?)
4309+
?.as(const jni$_.$JObject$Type$())
4310+
.reference
4311+
.toPointer() ??
4312+
jni$_.nullptr;
4313+
}
41644314
} catch (e) {
41654315
return jni$_.ProtectedJniExtensions.newDartException(e);
41664316
}
@@ -4214,6 +4364,7 @@ abstract base mixin class $SuspendInterface {
42144364
required core$_.Future<jni$_.JInteger> Function(jni$_.JInteger integer)
42154365
sayInt$1,
42164366
required core$_.Future<jni$_.JInteger?> Function(core$_.bool z) nullableInt,
4367+
required core$_.Future<void> Function() noReturn,
42174368
}) = _$SuspendInterface;
42184369

42194370
core$_.Future<jni$_.JString> sayHello();
@@ -4222,6 +4373,7 @@ abstract base mixin class $SuspendInterface {
42224373
core$_.Future<jni$_.JInteger> sayInt();
42234374
core$_.Future<jni$_.JInteger> sayInt$1(jni$_.JInteger integer);
42244375
core$_.Future<jni$_.JInteger?> nullableInt(core$_.bool z);
4376+
core$_.Future<void> noReturn();
42254377
}
42264378

42274379
final class _$SuspendInterface with $SuspendInterface {
@@ -4235,12 +4387,14 @@ final class _$SuspendInterface with $SuspendInterface {
42354387
required core$_.Future<jni$_.JInteger> Function(jni$_.JInteger integer)
42364388
sayInt$1,
42374389
required core$_.Future<jni$_.JInteger?> Function(core$_.bool z) nullableInt,
4390+
required core$_.Future<void> Function() noReturn,
42384391
}) : _sayHello = sayHello,
42394392
_sayHello$1 = sayHello$1,
42404393
_nullableHello = nullableHello,
42414394
_sayInt = sayInt,
42424395
_sayInt$1 = sayInt$1,
4243-
_nullableInt = nullableInt;
4396+
_nullableInt = nullableInt,
4397+
_noReturn = noReturn;
42444398

42454399
final core$_.Future<jni$_.JString> Function() _sayHello;
42464400
final core$_.Future<jni$_.JString> Function(jni$_.JString string) _sayHello$1;
@@ -4249,6 +4403,7 @@ final class _$SuspendInterface with $SuspendInterface {
42494403
final core$_.Future<jni$_.JInteger> Function(jni$_.JInteger integer)
42504404
_sayInt$1;
42514405
final core$_.Future<jni$_.JInteger?> Function(core$_.bool z) _nullableInt;
4406+
final core$_.Future<void> Function() _noReturn;
42524407

42534408
core$_.Future<jni$_.JString> sayHello() {
42544409
return _sayHello();
@@ -4273,6 +4428,10 @@ final class _$SuspendInterface with $SuspendInterface {
42734428
core$_.Future<jni$_.JInteger?> nullableInt(core$_.bool z) {
42744429
return _nullableInt(z);
42754430
}
4431+
4432+
core$_.Future<void> noReturn() {
4433+
return _noReturn();
4434+
}
42764435
}
42774436

42784437
final class $SuspendInterface$NullableType$

‎pkgs/jnigen/test/kotlin_test/kotlin/src/main/kotlin/com/github/dart_lang/jnigen/SuspendFun.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public class SuspendFun {
4646
}
4747
return "Hello!"
4848
}
49+
50+
var result: Int = 0
51+
suspend fun noReturn() {
52+
delay(100L)
53+
this.result = 123
54+
}
4955
}
5056

5157
public interface SuspendInterface {
@@ -55,6 +61,7 @@ public interface SuspendInterface {
5561
suspend fun sayInt(): Integer
5662
suspend fun sayInt(value: Integer): Integer
5763
suspend fun nullableInt(returnNull: Boolean): Integer?
64+
suspend fun noReturn()
5865
}
5966

6067
suspend fun consumeOnAnotherThread(itf: SuspendInterface): String {
@@ -71,5 +78,6 @@ ${itf.nullableHello(false)}
7178
${itf.sayInt()}
7279
${itf.sayInt(Integer(789))}
7380
${itf.nullableInt(false)}
81+
${itf.noReturn()}
7482
""".trim();
7583
}

0 commit comments

Comments
 (0)