Skip to content

Commit 9caa77c

Browse files
authored
[pigeon] main actor async flutter api (flutter#12729)
adds main actor annotation to async flutter api methods in swift. fixes flutter#192199
1 parent c177dfc commit 9caa77c

27 files changed

Lines changed: 1149 additions & 126 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 29.0.1
2+
3+
* [swift] Annotates async `FlutterApi` methods with `@MainActor`.
4+
15
## 29.0.0
26

37
* [swift] [kotlin] Adds experimental support for FFI and JNI.

‎packages/pigeon/example/app/ios/Runner/Messages.g.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class ExampleHostApiSetup {
373373

374374
/// Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.
375375
protocol MessageFlutterApiProtocol {
376-
func flutterMethod(aString aStringArg: String?) async throws -> String
376+
@MainActor func flutterMethod(aString aStringArg: String?) async throws -> String
377377
}
378378
class MessageFlutterApi: MessageFlutterApiProtocol {
379379
private let binaryMessenger: FlutterBinaryMessenger
@@ -385,7 +385,7 @@ class MessageFlutterApi: MessageFlutterApiProtocol {
385385
var codec: MessagesPigeonCodec {
386386
return MessagesPigeonCodec.shared
387387
}
388-
func flutterMethod(aString aStringArg: String?) async throws -> String {
388+
@MainActor func flutterMethod(aString aStringArg: String?) async throws -> String {
389389
return try await withCheckedThrowingContinuation { continuation in
390390
let channelName: String =
391391
"dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod\(messageChannelSuffix)"

‎packages/pigeon/lib/src/generator_tools.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'generator.dart';
1616
/// The current version of pigeon.
1717
///
1818
/// This must match the version in pubspec.yaml.
19-
const String pigeonVersion = '29.0.0';
19+
const String pigeonVersion = '29.0.1';
2020

2121
/// Default plugin package name.
2222
const String defaultPluginPackageName = 'dev.flutter.pigeon';

‎packages/pigeon/lib/src/swift/swift_generator.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ if (wrapped == nil) {
12631263
errorTypeName: _getErrorClassName(generatorOptions),
12641264
isAsynchronous: true,
12651265
isAsynchronousCallback: func.isAsynchronousCallback,
1266+
isMainActor: !func.isAsynchronousCallback,
12661267
swiftFunction: func.swiftFunction,
12671268
getParameterName: _getSafeArgumentName,
12681269
),
@@ -2614,6 +2615,7 @@ enum ${_classNamePrefix}PigeonInternalNumberType: Int {
26142615
errorTypeName: _getErrorClassName(generatorOptions),
26152616
isAsynchronous: isAsynchronous,
26162617
isAsynchronousCallback: isAsynchronousCallback,
2618+
isMainActor: isAsynchronous && !isAsynchronousCallback,
26172619
swiftFunction: swiftFunction,
26182620
getParameterName: _getSafeArgumentName,
26192621
);
@@ -3963,6 +3965,7 @@ String _getMethodSignature({
39633965
bool isAsynchronous = false,
39643966
bool ffiUserApi = false,
39653967
bool isAsynchronousCallback = false,
3968+
bool isMainActor = false,
39663969
String? swiftFunction,
39673970
bool ffiBridgeApi = false,
39683971
_SwiftFunctionComponents? components,
@@ -4026,8 +4029,9 @@ String _getMethodSignature({
40264029
}
40274030

40284031
if (isAsynchronous && !isAsynchronousCallback) {
4032+
final mainActorPrefix = isMainActor ? '@MainActor ' : '';
40294033
final returnTypeSuffix = returnType.isVoid ? '' : ' -> $returnTypeString';
4030-
return 'func $methodName($parameterSignature) async throws$returnTypeSuffix';
4034+
return '${mainActorPrefix}func ${components.name}($parameterSignature) async throws$returnTypeSuffix';
40314035
}
40324036

40334037
if (isAsynchronous) {

‎packages/pigeon/pigeons/core_tests.dart‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,9 @@ abstract class HostIntegrationCoreApi {
11591159

11601160
@async
11611161
void callFlutterCallbackThrowErrorFromVoid();
1162+
1163+
@async
1164+
bool callFlutterIsAsyncFlutterApiOnRoot();
11621165
}
11631166

11641167
/// A Flutter API using callback-based asynchronous methods (@asyncCallback).
@@ -1447,9 +1450,6 @@ abstract class FlutterIntegrationCoreApi {
14471450
AnotherEnum? echoAnotherNullableEnum(AnotherEnum? anotherEnum);
14481451

14491452
// ========== Async tests ==========
1450-
// These are minimal since async FlutterApi only changes Dart generation.
1451-
// Currently they aren't integration tested, but having them here ensures
1452-
// analysis coverage.
14531453

14541454
/// A no-op function taking no arguments and returning no value, to sanity
14551455
/// test basic asynchronous calling.
@@ -1461,6 +1461,10 @@ abstract class FlutterIntegrationCoreApi {
14611461
@ObjCSelector('echoAsyncString:')
14621462
@SwiftFunction('echoAsync(_:)')
14631463
String echoAsyncString(String aString);
1464+
1465+
/// Returns true if the async FlutterApi method is run on the root isolate.
1466+
@async
1467+
bool isAsyncFlutterApiOnRoot();
14641468
}
14651469

14661470
/// A Host API using callback-based asynchronous methods (@asyncCallback).

‎packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/main/java/com/example/alternate_language_test_plugin/AlternateLanguageTestPlugin.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,31 @@ public void callFlutterCallbackThrowErrorFromVoid(@NonNull VoidResult result) {
11341134
flutterCallbackApi.throwErrorFromVoid(result);
11351135
}
11361136

1137+
@Override
1138+
public void callFlutterIsAsyncFlutterApiOnRoot(@NonNull Result<Boolean> result) {
1139+
if (Looper.myLooper() != Looper.getMainLooper()) {
1140+
result.success(false);
1141+
return;
1142+
}
1143+
assert flutterApi != null;
1144+
flutterApi.isAsyncFlutterApiOnRoot(
1145+
new Result<Boolean>() {
1146+
@Override
1147+
public void success(Boolean res) {
1148+
if (Looper.myLooper() != Looper.getMainLooper()) {
1149+
result.success(false);
1150+
return;
1151+
}
1152+
result.success(res);
1153+
}
1154+
1155+
@Override
1156+
public void error(@NonNull Throwable error) {
1157+
result.error(error);
1158+
}
1159+
});
1160+
}
1161+
11371162
public @NonNull CoreTests.UnusedClass testIfUnusedClassIsGenerated() {
11381163
return new CoreTests.UnusedClass();
11391164
}

‎packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/main/java/com/example/alternate_language_test_plugin/CoreTests.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,6 +4046,8 @@ void callFlutterEchoAnotherNullableEnum(
40464046

40474047
void callFlutterCallbackThrowErrorFromVoid(@NonNull VoidResult result);
40484048

4049+
void callFlutterIsAsyncFlutterApiOnRoot(@NonNull Result<Boolean> result);
4050+
40494051
/** The codec used by HostIntegrationCoreApi. */
40504052
static @NonNull MessageCodec<Object> getCodec() {
40514053
return PigeonCodec.INSTANCE;
@@ -8845,6 +8847,36 @@ public void error(Throwable error) {
88458847
channel.setMessageHandler(null);
88468848
}
88478849
}
8850+
{
8851+
BasicMessageChannel<Object> channel =
8852+
new BasicMessageChannel<>(
8853+
binaryMessenger,
8854+
"dev.flutter.pigeon.pigeon_integration_tests.HostIntegrationCoreApi.callFlutterIsAsyncFlutterApiOnRoot"
8855+
+ messageChannelSuffix,
8856+
getCodec());
8857+
if (api != null) {
8858+
channel.setMessageHandler(
8859+
(message, reply) -> {
8860+
ArrayList<Object> wrapped = new ArrayList<>();
8861+
Result<Boolean> resultCallback =
8862+
new Result<Boolean>() {
8863+
public void success(Boolean result) {
8864+
wrapped.add(0, result);
8865+
reply.reply(wrapped);
8866+
}
8867+
8868+
public void error(Throwable error) {
8869+
ArrayList<Object> wrappedError = wrapError(error);
8870+
reply.reply(wrappedError);
8871+
}
8872+
};
8873+
8874+
api.callFlutterIsAsyncFlutterApiOnRoot(resultCallback);
8875+
});
8876+
} else {
8877+
channel.setMessageHandler(null);
8878+
}
8879+
}
88488880
}
88498881
}
88508882

@@ -10624,6 +10656,39 @@ public void echoAsyncString(@NonNull String aStringArg, @NonNull Result<String>
1062410656
}
1062510657
});
1062610658
}
10659+
10660+
/** Returns true if the async FlutterApi method is run on the root isolate. */
10661+
public void isAsyncFlutterApiOnRoot(@NonNull Result<Boolean> result) {
10662+
final String channelName =
10663+
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.isAsyncFlutterApiOnRoot"
10664+
+ messageChannelSuffix;
10665+
BasicMessageChannel<Object> channel =
10666+
new BasicMessageChannel<>(binaryMessenger, channelName, getCodec());
10667+
channel.send(
10668+
null,
10669+
channelReply -> {
10670+
if (channelReply instanceof List) {
10671+
List<Object> listReply = (List<Object>) channelReply;
10672+
if (listReply.size() > 1) {
10673+
result.error(
10674+
new FlutterError(
10675+
(String) listReply.get(0), (String) listReply.get(1), listReply.get(2)));
10676+
} else if (listReply.get(0) == null) {
10677+
result.error(
10678+
new FlutterError(
10679+
"null-error",
10680+
"Flutter api returned null value for non-null return value.",
10681+
""));
10682+
} else {
10683+
@SuppressWarnings("ConstantConditions")
10684+
Boolean output = (Boolean) listReply.get(0);
10685+
result.success(output);
10686+
}
10687+
} else {
10688+
result.error(createConnectionError(channelName));
10689+
}
10690+
});
10691+
}
1062710692
}
1062810693

1062910694
/**

‎packages/pigeon/platform_tests/alternate_language_test_plugin/darwin/alternate_language_test_plugin/Sources/alternate_language_test_plugin/AlternateLanguageTestPlugin.m‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,21 @@ - (void)callFlutterCallbackThrowErrorFromVoidWithCompletion:
12471247
[self.flutterCallbackAPI throwErrorFromVoidWithCompletion:completion];
12481248
}
12491249

1250+
- (void)callFlutterIsAsyncFlutterApiOnRootWithCompletion:
1251+
(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion {
1252+
if (![NSThread isMainThread]) {
1253+
completion(@NO, nil);
1254+
return;
1255+
}
1256+
[self.flutterAPI isAsyncFlutterApiOnRootWithCompletion:^(NSNumber *result, FlutterError *error) {
1257+
if (![NSThread isMainThread]) {
1258+
completion(@NO, nil);
1259+
return;
1260+
}
1261+
completion(result, error);
1262+
}];
1263+
}
1264+
12501265
@end
12511266

12521267
@interface AlternateLanguageTestAPIWithSuffix ()

‎packages/pigeon/platform_tests/alternate_language_test_plugin/darwin/alternate_language_test_plugin/Sources/alternate_language_test_plugin/CoreTests.gen.m‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5579,6 +5579,31 @@ void SetUpFLTHostIntegrationCoreApiWithSuffix(id<FlutterBinaryMessenger> binaryM
55795579
[channel setMessageHandler:nil];
55805580
}
55815581
}
5582+
{
5583+
FlutterBasicMessageChannel *channel = [[FlutterBasicMessageChannel alloc]
5584+
initWithName:[NSString stringWithFormat:
5585+
@"%@%@",
5586+
@"dev.flutter.pigeon.pigeon_integration_tests."
5587+
@"HostIntegrationCoreApi.callFlutterIsAsyncFlutterApiOnRoot",
5588+
messageChannelSuffix]
5589+
binaryMessenger:binaryMessenger
5590+
codec:FLTGetCoreTestsCodec()];
5591+
if (api) {
5592+
NSCAssert(
5593+
[api respondsToSelector:@selector(callFlutterIsAsyncFlutterApiOnRootWithCompletion:)],
5594+
@"FLTHostIntegrationCoreApi api (%@) doesn't respond to "
5595+
@"@selector(callFlutterIsAsyncFlutterApiOnRootWithCompletion:)",
5596+
api);
5597+
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
5598+
[api callFlutterIsAsyncFlutterApiOnRootWithCompletion:^(NSNumber *_Nullable output,
5599+
FlutterError *_Nullable error) {
5600+
callback(wrapResult(output, error));
5601+
}];
5602+
}];
5603+
} else {
5604+
[channel setMessageHandler:nil];
5605+
}
5606+
}
55825607
}
55835608
@interface FLTFlutterCallbackCoreApi ()
55845609
@property(nonatomic, strong) NSObject<FlutterBinaryMessenger> *binaryMessenger;
@@ -7192,6 +7217,33 @@ - (void)echoAsyncString:(NSString *)arg_aString
71927217
}
71937218
}];
71947219
}
7220+
- (void)isAsyncFlutterApiOnRootWithCompletion:(void (^)(NSNumber *_Nullable,
7221+
FlutterError *_Nullable))completion {
7222+
NSString *channelName =
7223+
[NSString stringWithFormat:@"%@%@",
7224+
@"dev.flutter.pigeon.pigeon_integration_tests."
7225+
@"FlutterIntegrationCoreApi.isAsyncFlutterApiOnRoot",
7226+
_messageChannelSuffix];
7227+
FlutterBasicMessageChannel *channel =
7228+
[FlutterBasicMessageChannel messageChannelWithName:channelName
7229+
binaryMessenger:self.binaryMessenger
7230+
codec:FLTGetCoreTestsCodec()];
7231+
[channel sendMessage:nil
7232+
reply:^(NSArray<id> *reply) {
7233+
if (reply != nil) {
7234+
if (reply.count > 1) {
7235+
completion(nil, [FlutterError errorWithCode:reply[0]
7236+
message:reply[1]
7237+
details:reply[2]]);
7238+
} else {
7239+
NSNumber *output = reply[0] == [NSNull null] ? nil : reply[0];
7240+
completion(output, nil);
7241+
}
7242+
} else {
7243+
completion(nil, createConnectionError(channelName));
7244+
}
7245+
}];
7246+
}
71957247
@end
71967248

71977249
void SetUpFLTHostCallbackCoreApi(id<FlutterBinaryMessenger> binaryMessenger,

‎packages/pigeon/platform_tests/alternate_language_test_plugin/darwin/alternate_language_test_plugin/Sources/alternate_language_test_plugin/include/alternate_language_test_plugin/CoreTests.gen.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ NSObject<FlutterMessageCodec> *FLTGetCoreTestsCodec(void);
939939
FlutterError *_Nullable))completion;
940940
- (void)callFlutterCallbackThrowErrorFromVoidWithCompletion:
941941
(void (^)(FlutterError *_Nullable))completion;
942+
- (void)callFlutterIsAsyncFlutterApiOnRootWithCompletion:
943+
(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion;
942944
@end
943945

944946
extern void SetUpFLTHostIntegrationCoreApi(id<FlutterBinaryMessenger> binaryMessenger,
@@ -1166,6 +1168,9 @@ extern void SetUpFLTHostIntegrationCoreApiWithSuffix(
11661168
/// Returns the passed in generic Object asynchronously.
11671169
- (void)echoAsyncString:(NSString *)aString
11681170
completion:(void (^)(NSString *_Nullable, FlutterError *_Nullable))completion;
1171+
/// Returns true if the async FlutterApi method is run on the root isolate.
1172+
- (void)isAsyncFlutterApiOnRootWithCompletion:(void (^)(NSNumber *_Nullable,
1173+
FlutterError *_Nullable))completion;
11691174
@end
11701175

11711176
/// A Host API using callback-based asynchronous methods (@asyncCallback).

0 commit comments

Comments
 (0)