From f8c291bf439876a16c70ffc82ab15d3f75178315 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 8 Nov 2024 09:56:24 -0500 Subject: [PATCH 1/4] Move HostApi and codec declarations to the header --- packages/pigeon/lib/gobject_generator.dart | 30 +++++++++++-------- .../pigeon/test/gobject_generator_test.dart | 4 +++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/pigeon/lib/gobject_generator.dart b/packages/pigeon/lib/gobject_generator.dart index bee03804f53..04f4bd1afa8 100644 --- a/packages/pigeon/lib/gobject_generator.dart +++ b/packages/pigeon/lib/gobject_generator.dart @@ -14,6 +14,9 @@ const DocumentCommentSpecification _docCommentSpec = /// Name for codec class. const String _codecBaseName = 'MessageCodec'; +/// Name of the standard codec from the Flutter SDK. +const String _standardCodecName = 'FlStandardMessageCodec'; + /// Options that control how GObject code will be generated. class GObjectOptions { /// Creates a [GObjectOptions] object @@ -282,7 +285,12 @@ class GObjectHeaderGenerator extends StructuredGenerator { Root root, Indent indent, { required String dartPackageName, - }) {} + }) { + final String module = _getModule(generatorOptions, dartPackageName); + indent.newln(); + _writeDeclareFinalType(indent, module, _codecBaseName, + parentClassName: _standardCodecName); + } @override void writeFlutterApi( @@ -508,6 +516,9 @@ class GObjectHeaderGenerator extends StructuredGenerator { final String methodPrefix = _getMethodPrefix(module, api.name); final String vtableName = _getVTableName(module, api.name); + indent.newln(); + _writeDeclareFinalType(indent, module, api.name); + final bool hasAsyncMethod = api.methods.any((Method method) => method.isAsynchronous); if (hasAsyncMethod) { @@ -950,13 +961,9 @@ class GObjectSourceGenerator extends StructuredGenerator { final Iterable customTypes = getEnumeratedTypes(root); - indent.newln(); - _writeDeclareFinalType(indent, module, _codecBaseName, - parentClassName: 'FlStandardMessageCodec'); - indent.newln(); _writeObjectStruct(indent, module, _codecBaseName, () {}, - parentClassName: 'FlStandardMessageCodec'); + parentClassName: _standardCodecName); indent.newln(); _writeDefineType(indent, module, _codecBaseName, @@ -971,7 +978,7 @@ class GObjectSourceGenerator extends StructuredGenerator { ? '$customTypeName*' : 'FlValue*'; indent.writeScoped( - 'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName(FlStandardMessageCodec* codec, GByteArray* buffer, $valueType value, GError** error) {', + 'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName($_standardCodecName* codec, GByteArray* buffer, $valueType value, GError** error) {', '}', () { indent.writeln('uint8_t type = ${customType.enumeration};'); indent.writeln('g_byte_array_append(buffer, &type, sizeof(uint8_t));'); @@ -989,7 +996,7 @@ class GObjectSourceGenerator extends StructuredGenerator { indent.newln(); indent.writeScoped( - 'static gboolean ${codecMethodPrefix}_write_value(FlStandardMessageCodec* codec, GByteArray* buffer, FlValue* value, GError** error) {', + 'static gboolean ${codecMethodPrefix}_write_value($_standardCodecName* codec, GByteArray* buffer, FlValue* value, GError** error) {', '}', () { indent.writeScoped( 'if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM) {', '}', () { @@ -1027,7 +1034,7 @@ class GObjectSourceGenerator extends StructuredGenerator { _snakeCaseFromCamelCase(customTypeName); indent.newln(); indent.writeScoped( - 'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName(FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, GError** error) {', + 'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName($_standardCodecName* codec, GBytes* buffer, size_t* offset, GError** error) {', '}', () { if (customType.type == CustomTypes.customClass) { indent.writeln( @@ -1055,7 +1062,7 @@ class GObjectSourceGenerator extends StructuredGenerator { indent.newln(); indent.writeScoped( - 'static FlValue* ${codecMethodPrefix}_read_value_of_type(FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, int type, GError** error) {', + 'static FlValue* ${codecMethodPrefix}_read_value_of_type($_standardCodecName* codec, GBytes* buffer, size_t* offset, int type, GError** error) {', '}', () { indent.writeScoped('switch (type) {', '}', () { for (final EnumeratedType customType in customTypes) { @@ -1473,9 +1480,6 @@ class GObjectSourceGenerator extends StructuredGenerator { }); } - indent.newln(); - _writeDeclareFinalType(indent, module, api.name); - indent.newln(); _writeObjectStruct(indent, module, api.name, () { indent.writeln('const ${className}VTable* vtable;'); diff --git a/packages/pigeon/test/gobject_generator_test.dart b/packages/pigeon/test/gobject_generator_test.dart index 935e9b98c80..3b9d90ad164 100644 --- a/packages/pigeon/test/gobject_generator_test.dart +++ b/packages/pigeon/test/gobject_generator_test.dart @@ -95,6 +95,10 @@ void main() { code, contains( 'static void test_package_api_init(TestPackageApi* self) {')); + // See https://github.com/flutter/flutter/issues/153083. If a private type + // is ever needed, this should be updated to ensure that any type declared + // in the implementation file has a corresponding _IS_ call in the file. + expect(code, isNot(contains('G_DECLARE_FINAL_TYPE('))); } }); From 937324c262a2d9c931a7482a4ae872a4d50cccb5 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 8 Nov 2024 09:58:54 -0500 Subject: [PATCH 2/4] Regenerate code --- .../pigeon/example/app/linux/messages.g.cc | 9 --------- packages/pigeon/example/app/linux/messages.g.h | 9 +++++++++ .../ios/Classes/CoreTests.gen.swift | 18 ------------------ .../test_plugin/linux/pigeon/core_tests.gen.cc | 17 ----------------- .../test_plugin/linux/pigeon/core_tests.gen.h | 17 +++++++++++++++++ .../macos/Classes/CoreTests.gen.swift | 18 ------------------ 6 files changed, 26 insertions(+), 62 deletions(-) diff --git a/packages/pigeon/example/app/linux/messages.g.cc b/packages/pigeon/example/app/linux/messages.g.cc index a9ee2105989..4471586316e 100644 --- a/packages/pigeon/example/app/linux/messages.g.cc +++ b/packages/pigeon/example/app/linux/messages.g.cc @@ -118,11 +118,6 @@ pigeon_example_package_message_data_new_from_list(FlValue* values) { return pigeon_example_package_message_data_new(name, description, code, data); } -G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec, - pigeon_example_package_message_codec, - PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC, - FlStandardMessageCodec) - struct _PigeonExamplePackageMessageCodec { FlStandardMessageCodec parent_instance; }; @@ -468,10 +463,6 @@ pigeon_example_package_example_host_api_send_message_response_new_error( return self; } -G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi, - pigeon_example_package_example_host_api, - PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject) - struct _PigeonExamplePackageExampleHostApi { GObject parent_instance; diff --git a/packages/pigeon/example/app/linux/messages.g.h b/packages/pigeon/example/app/linux/messages.g.h index 0f784ab1a1f..ac8dc925fe4 100644 --- a/packages/pigeon/example/app/linux/messages.g.h +++ b/packages/pigeon/example/app/linux/messages.g.h @@ -90,6 +90,15 @@ PigeonExamplePackageCode pigeon_example_package_message_data_get_code( FlValue* pigeon_example_package_message_data_get_data( PigeonExamplePackageMessageData* object); +G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec, + pigeon_example_package_message_codec, + PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC, + FlStandardMessageCodec) + +G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi, + pigeon_example_package_example_host_api, + PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject) + G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApiResponseHandle, pigeon_example_package_example_host_api_response_handle, PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API_RESPONSE_HANDLE, diff --git a/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift b/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift index 2b2001d6aa4..a90e8fa07a5 100644 --- a/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift +++ b/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift @@ -15,24 +15,6 @@ import Foundation #error("Unsupported platform.") #endif -/// Error class for passing custom error details to Dart side. -final class PigeonError: Error { - let code: String - let message: String? - let details: Any? - - init(code: String, message: String?, details: Any?) { - self.code = code - self.message = message - self.details = details - } - - var localizedDescription: String { - return - "PigeonError(code: \(code), message: \(message ?? ""), details: \(details ?? "")" - } -} - private func wrapResult(_ result: Any?) -> [Any?] { return [result] } diff --git a/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.cc b/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.cc index 5701f91c235..8dee0b40e5e 100644 --- a/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.cc +++ b/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.cc @@ -2397,11 +2397,6 @@ core_tests_pigeon_test_test_message_new_from_list(FlValue* values) { return core_tests_pigeon_test_test_message_new(test_list); } -G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestMessageCodec, - core_tests_pigeon_test_message_codec, - CORE_TESTS_PIGEON_TEST, MESSAGE_CODEC, - FlStandardMessageCodec) - struct _CoreTestsPigeonTestMessageCodec { FlStandardMessageCodec parent_instance; }; @@ -13564,10 +13559,6 @@ core_tests_pigeon_test_host_integration_core_api_call_flutter_small_api_echo_str return self; } -G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostIntegrationCoreApi, - core_tests_pigeon_test_host_integration_core_api, - CORE_TESTS_PIGEON_TEST, HOST_INTEGRATION_CORE_API, GObject) - struct _CoreTestsPigeonTestHostIntegrationCoreApi { GObject parent_instance; @@ -32742,10 +32733,6 @@ core_tests_pigeon_test_host_trivial_api_noop_response_new_error( return self; } -G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApi, - core_tests_pigeon_test_host_trivial_api, - CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API, GObject) - struct _CoreTestsPigeonTestHostTrivialApi { GObject parent_instance; @@ -33021,10 +33008,6 @@ core_tests_pigeon_test_host_small_api_void_void_response_new_error( return self; } -G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApi, - core_tests_pigeon_test_host_small_api, - CORE_TESTS_PIGEON_TEST, HOST_SMALL_API, GObject) - struct _CoreTestsPigeonTestHostSmallApi { GObject parent_instance; diff --git a/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.h b/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.h index 6cb79ba1854..dcd6cac75f5 100644 --- a/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.h +++ b/packages/pigeon/platform_tests/test_plugin/linux/pigeon/core_tests.gen.h @@ -1428,6 +1428,15 @@ CoreTestsPigeonTestTestMessage* core_tests_pigeon_test_test_message_new( FlValue* core_tests_pigeon_test_test_message_get_test_list( CoreTestsPigeonTestTestMessage* object); +G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestMessageCodec, + core_tests_pigeon_test_message_codec, + CORE_TESTS_PIGEON_TEST, MESSAGE_CODEC, + FlStandardMessageCodec) + +G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostIntegrationCoreApi, + core_tests_pigeon_test_host_integration_core_api, + CORE_TESTS_PIGEON_TEST, HOST_INTEGRATION_CORE_API, GObject) + G_DECLARE_FINAL_TYPE( CoreTestsPigeonTestHostIntegrationCoreApiResponseHandle, core_tests_pigeon_test_host_integration_core_api_response_handle, @@ -11869,6 +11878,10 @@ core_tests_pigeon_test_flutter_integration_core_api_echo_async_string_finish( CoreTestsPigeonTestFlutterIntegrationCoreApi* api, GAsyncResult* result, GError** error); +G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApi, + core_tests_pigeon_test_host_trivial_api, + CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API, GObject) + G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApiNoopResponse, core_tests_pigeon_test_host_trivial_api_noop_response, CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API_NOOP_RESPONSE, @@ -11936,6 +11949,10 @@ void core_tests_pigeon_test_host_trivial_api_set_method_handlers( void core_tests_pigeon_test_host_trivial_api_clear_method_handlers( FlBinaryMessenger* messenger, const gchar* suffix); +G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApi, + core_tests_pigeon_test_host_small_api, + CORE_TESTS_PIGEON_TEST, HOST_SMALL_API, GObject) + G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApiResponseHandle, core_tests_pigeon_test_host_small_api_response_handle, CORE_TESTS_PIGEON_TEST, HOST_SMALL_API_RESPONSE_HANDLE, diff --git a/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift b/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift index 2b2001d6aa4..a90e8fa07a5 100644 --- a/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift +++ b/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift @@ -15,24 +15,6 @@ import Foundation #error("Unsupported platform.") #endif -/// Error class for passing custom error details to Dart side. -final class PigeonError: Error { - let code: String - let message: String? - let details: Any? - - init(code: String, message: String?, details: Any?) { - self.code = code - self.message = message - self.details = details - } - - var localizedDescription: String { - return - "PigeonError(code: \(code), message: \(message ?? ""), details: \(details ?? "")" - } -} - private func wrapResult(_ result: Any?) -> [Any?] { return [result] } From ec831e0aea5f85fc3a3b78509daa6936616b339e Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 8 Nov 2024 10:00:14 -0500 Subject: [PATCH 3/4] Version bump --- packages/pigeon/CHANGELOG.md | 5 +++++ packages/pigeon/lib/generator_tools.dart | 2 +- packages/pigeon/pubspec.yaml | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index 85842f04611..d93dd1bb1a3 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,8 @@ +## 22.6.1 + +* [gobject] Moves class declarations to the header to work around a bug in some + versions of glib. + ## 22.6.0 * [swift] Adds `includeErrorClass` to `SwiftOptions`. diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index cbec3e8da6f..8349285f102 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -14,7 +14,7 @@ import 'ast.dart'; /// The current version of pigeon. /// /// This must match the version in pubspec.yaml. -const String pigeonVersion = '22.6.0'; +const String pigeonVersion = '22.6.1'; /// Read all the content from [stdin] to a String. String readStdin() { diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index cddaae51581..31b736278ce 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. repository: https://github.com/flutter/packages/tree/main/packages/pigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22 -version: 22.6.0 # This must match the version in lib/generator_tools.dart +version: 22.6.1 # This must match the version in lib/generator_tools.dart environment: sdk: ^3.3.0 From e7e898d4f9cf59a9538254e93e281ded3568b4e0 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Mon, 11 Nov 2024 10:48:21 -0500 Subject: [PATCH 4/4] Undo unrelated Swift generation changes --- .../ios/Classes/CoreTests.gen.swift | 18 ++++++++++++++++++ .../macos/Classes/CoreTests.gen.swift | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift b/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift index a90e8fa07a5..2b2001d6aa4 100644 --- a/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift +++ b/packages/pigeon/platform_tests/test_plugin/ios/Classes/CoreTests.gen.swift @@ -15,6 +15,24 @@ import Foundation #error("Unsupported platform.") #endif +/// Error class for passing custom error details to Dart side. +final class PigeonError: Error { + let code: String + let message: String? + let details: Any? + + init(code: String, message: String?, details: Any?) { + self.code = code + self.message = message + self.details = details + } + + var localizedDescription: String { + return + "PigeonError(code: \(code), message: \(message ?? ""), details: \(details ?? "")" + } +} + private func wrapResult(_ result: Any?) -> [Any?] { return [result] } diff --git a/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift b/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift index a90e8fa07a5..2b2001d6aa4 100644 --- a/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift +++ b/packages/pigeon/platform_tests/test_plugin/macos/Classes/CoreTests.gen.swift @@ -15,6 +15,24 @@ import Foundation #error("Unsupported platform.") #endif +/// Error class for passing custom error details to Dart side. +final class PigeonError: Error { + let code: String + let message: String? + let details: Any? + + init(code: String, message: String?, details: Any?) { + self.code = code + self.message = message + self.details = details + } + + var localizedDescription: String { + return + "PigeonError(code: \(code), message: \(message ?? ""), details: \(details ?? "")" + } +} + private func wrapResult(_ result: Any?) -> [Any?] { return [result] }