Skip to content

Commit fc6b92d

Browse files
committed
[pigeon] Use a const for custom type ids for gobject generated files (#156100)
Adds a custom type identifier to generated gobject headers for the user. Calling fl_value_new_custom_object is now possible with that constant. This fixes flutter/flutter#156100 Also updated the CONTRIBUTING.md file to include the gobject generator.
1 parent 5a7d40f commit fc6b92d

File tree

10 files changed

+420
-160
lines changed

10 files changed

+420
-160
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 25.4.0
22

3-
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
3+
* [gobject] Adds type id constants in header files so that they can be used by the user.
4+
* Updates minimum supported SDK version to Flutter 3.24/Dart 3.5.
45

56
## 25.3.2
67

packages/pigeon/CONTRIBUTING.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ generators with that AST.
1919
## Source Index
2020

2121
* [ast.dart](./lib/src/ast.dart) - The data structure for representing the Abstract Syntax Tree.
22-
* [dart_generator.dart](./lib/src/dart_generator.dart) - The Dart code generator.
23-
* [java_generator.dart](./lib/src/java_generator.dart) - The Java code generator.
24-
* [kotlin_generator.dart](./lib/src/kotlin_generator.dart) - The Kotlin code generator.
25-
* [objc_generator.dart](./lib/src/objc_generator.dart) - The Objective-C code
22+
* [dart_generator.dart](./lib/src/dart/dart_generator.dart) - The Dart code generator.
23+
* [java_generator.dart](./lib/src/java/java_generator.dart) - The Java code generator.
24+
* [kotlin_generator.dart](./lib/src/kotlin/kotlin_generator.dart) - The Kotlin code generator.
25+
* [objc_generator.dart](./lib/src/objc/objc_generator.dart) - The Objective-C code
2626
generator (header and source files).
27-
* [swift_generator.dart](./lib/src/swift_generator.dart) - The Swift code generator.
28-
* [cpp_generator.dart](./lib/src/cpp_generator.dart) - The C++ code generator.
27+
* [swift_generator.dart](./lib/src/swift/swift_generator.dart) - The Swift code generator.
28+
* [cpp_generator.dart](./lib/src/cpp/cpp_generator.dart) - The C++ code generator.
29+
* [gobject_generator.dart](./lib/src/gobject/gobject_generator.dart) - The GObject code generator.
2930
* [generator_tools.dart](./lib/src/generator_tools.dart) - Shared code between generators.
3031
* [pigeon_cl.dart](./lib/src/pigeon_cl.dart) - The top-level function executed by
3132
the command line tool in [bin/][./bin].

packages/pigeon/example/app/linux/messages.g.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ static FlValue* pigeon_example_package_message_data_to_list(
9191
? fl_value_new_string(self->description)
9292
: fl_value_new_null());
9393
fl_value_append_take(values,
94-
fl_value_new_custom(129, fl_value_new_int(self->code),
94+
fl_value_new_custom(pigeon_example_package_code_type_id,
95+
fl_value_new_int(self->code),
9596
(GDestroyNotify)fl_value_unref));
9697
fl_value_append_take(values, fl_value_ref(self->data));
9798
return values;
@@ -126,20 +127,24 @@ G_DEFINE_TYPE(PigeonExamplePackageMessageCodec,
126127
pigeon_example_package_message_codec,
127128
fl_standard_message_codec_get_type())
128129

130+
const int pigeon_example_package_code_type_id = 129;
131+
129132
static gboolean
130133
pigeon_example_package_message_codec_write_pigeon_example_package_code(
131134
FlStandardMessageCodec* codec, GByteArray* buffer, FlValue* value,
132135
GError** error) {
133-
uint8_t type = 129;
136+
uint8_t type = pigeon_example_package_code_type_id;
134137
g_byte_array_append(buffer, &type, sizeof(uint8_t));
135138
return fl_standard_message_codec_write_value(codec, buffer, value, error);
136139
}
137140

141+
const int pigeon_example_package_message_data_type_id = 130;
142+
138143
static gboolean
139144
pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
140145
FlStandardMessageCodec* codec, GByteArray* buffer,
141146
PigeonExamplePackageMessageData* value, GError** error) {
142-
uint8_t type = 130;
147+
uint8_t type = pigeon_example_package_message_data_type_id;
143148
g_byte_array_append(buffer, &type, sizeof(uint8_t));
144149
g_autoptr(FlValue) values =
145150
pigeon_example_package_message_data_to_list(value);
@@ -151,13 +156,13 @@ static gboolean pigeon_example_package_message_codec_write_value(
151156
GError** error) {
152157
if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM) {
153158
switch (fl_value_get_custom_type(value)) {
154-
case 129:
159+
case pigeon_example_package_code_type_id:
155160
return pigeon_example_package_message_codec_write_pigeon_example_package_code(
156161
codec, buffer,
157162
reinterpret_cast<FlValue*>(
158163
const_cast<gpointer>(fl_value_get_custom_value(value))),
159164
error);
160-
case 130:
165+
case pigeon_example_package_message_data_type_id:
161166
return pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
162167
codec, buffer,
163168
PIGEON_EXAMPLE_PACKAGE_MESSAGE_DATA(
@@ -176,7 +181,8 @@ pigeon_example_package_message_codec_read_pigeon_example_package_code(
176181
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset,
177182
GError** error) {
178183
return fl_value_new_custom(
179-
129, fl_standard_message_codec_read_value(codec, buffer, offset, error),
184+
pigeon_example_package_code_type_id,
185+
fl_standard_message_codec_read_value(codec, buffer, offset, error),
180186
(GDestroyNotify)fl_value_unref);
181187
}
182188

@@ -198,17 +204,18 @@ pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
198204
return nullptr;
199205
}
200206

201-
return fl_value_new_custom_object(130, G_OBJECT(value));
207+
return fl_value_new_custom_object(pigeon_example_package_message_data_type_id,
208+
G_OBJECT(value));
202209
}
203210

204211
static FlValue* pigeon_example_package_message_codec_read_value_of_type(
205212
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, int type,
206213
GError** error) {
207214
switch (type) {
208-
case 129:
215+
case pigeon_example_package_code_type_id:
209216
return pigeon_example_package_message_codec_read_pigeon_example_package_code(
210217
codec, buffer, offset, error);
211-
case 130:
218+
case pigeon_example_package_message_data_type_id:
212219
return pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
213220
codec, buffer, offset, error);
214221
default:

packages/pigeon/example/app/linux/messages.g.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec,
9595
PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC,
9696
FlStandardMessageCodec)
9797

98+
extern const int pigeon_example_package_code_type_id;
99+
100+
extern const int pigeon_example_package_message_data_type_id;
101+
98102
G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi,
99103
pigeon_example_package_example_host_api,
100104
PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject)

packages/pigeon/lib/src/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import 'generator.dart';
1515
/// The current version of pigeon.
1616
///
1717
/// This must match the version in pubspec.yaml.
18-
const String pigeonVersion = '25.3.2';
18+
const String pigeonVersion = '25.4.0';
1919

2020
/// Read all the content from [stdin] to a String.
2121
String readStdin() {

packages/pigeon/lib/src/gobject/gobject_generator.dart

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ class GObjectHeaderGenerator
338338
indent.newln();
339339
_writeDeclareFinalType(indent, module, _codecBaseName,
340340
parentClassName: _standardCodecName);
341+
342+
final Iterable<EnumeratedType> customTypes =
343+
getEnumeratedTypes(root, excludeSealedClasses: true);
344+
345+
for (final EnumeratedType customType in customTypes) {
346+
final String customTypeId = _getCustomTypeId(module, customType);
347+
indent.newln();
348+
indent.writeln('extern const int $customTypeId;');
349+
}
341350
}
342351

343352
@override
@@ -1023,14 +1032,19 @@ class GObjectSourceGenerator
10231032
final String customTypeName = _getClassName(module, customType.name);
10241033
final String snakeCustomTypeName =
10251034
_snakeCaseFromCamelCase(customTypeName);
1035+
final String customTypeId = _getCustomTypeId(module, customType);
1036+
1037+
indent.newln();
1038+
indent.writeln('const int $customTypeId = ${customType.enumeration};');
1039+
10261040
indent.newln();
10271041
final String valueType = customType.type == CustomTypes.customClass
10281042
? '$customTypeName*'
10291043
: 'FlValue*';
10301044
indent.writeScoped(
10311045
'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName($_standardCodecName* codec, GByteArray* buffer, $valueType value, GError** error) {',
10321046
'}', () {
1033-
indent.writeln('uint8_t type = ${customType.enumeration};');
1047+
indent.writeln('uint8_t type = $customTypeId;');
10341048
indent.writeln('g_byte_array_append(buffer, &type, sizeof(uint8_t));');
10351049
if (customType.type == CustomTypes.customClass) {
10361050
indent.writeln(
@@ -1053,7 +1067,8 @@ class GObjectSourceGenerator
10531067
indent.writeScoped('switch (fl_value_get_custom_type(value)) {', '}',
10541068
() {
10551069
for (final EnumeratedType customType in customTypes) {
1056-
indent.writeln('case ${customType.enumeration}:');
1070+
final String customTypeId = _getCustomTypeId(module, customType);
1071+
indent.writeln('case $customTypeId:');
10571072
indent.nest(1, () {
10581073
final String customTypeName =
10591074
_getClassName(module, customType.name);
@@ -1082,6 +1097,7 @@ class GObjectSourceGenerator
10821097
final String customTypeName = _getClassName(module, customType.name);
10831098
final String snakeCustomTypeName =
10841099
_snakeCaseFromCamelCase(customTypeName);
1100+
final String customTypeId = _getCustomTypeId(module, customType);
10851101
indent.newln();
10861102
indent.writeScoped(
10871103
'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName($_standardCodecName* codec, GBytes* buffer, size_t* offset, GError** error) {',
@@ -1102,10 +1118,10 @@ class GObjectSourceGenerator
11021118
});
11031119
indent.newln();
11041120
indent.writeln(
1105-
'return fl_value_new_custom_object(${customType.enumeration}, G_OBJECT(value));');
1121+
'return fl_value_new_custom_object($customTypeId, G_OBJECT(value));');
11061122
} else if (customType.type == CustomTypes.customEnum) {
11071123
indent.writeln(
1108-
'return fl_value_new_custom(${customType.enumeration}, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
1124+
'return fl_value_new_custom($customTypeId, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
11091125
}
11101126
});
11111127
}
@@ -1117,9 +1133,10 @@ class GObjectSourceGenerator
11171133
indent.writeScoped('switch (type) {', '}', () {
11181134
for (final EnumeratedType customType in customTypes) {
11191135
final String customTypeName = _getClassName(module, customType.name);
1136+
final String customTypeId = _getCustomTypeId(module, customType);
11201137
final String snakeCustomTypeName =
11211138
_snakeCaseFromCamelCase(customTypeName);
1122-
indent.writeln('case ${customType.enumeration}:');
1139+
indent.writeln('case $customTypeId:');
11231140
indent.nest(1, () {
11241141
indent.writeln(
11251142
'return ${codecMethodPrefix}_read_$snakeCustomTypeName(codec, buffer, offset, error);');
@@ -1922,6 +1939,16 @@ String _getMethodPrefix(String module, String name) {
19221939
return _snakeCaseFromCamelCase(className);
19231940
}
19241941

1942+
// Returns the code for the custom type id definition for [customType].
1943+
String _getCustomTypeId(String module, EnumeratedType customType) {
1944+
final String customTypeName = _getClassName(module, customType.name);
1945+
1946+
final String snakeCustomTypeName = _snakeCaseFromCamelCase(customTypeName);
1947+
1948+
final String customTypeId = '${snakeCustomTypeName}_type_id';
1949+
return customTypeId;
1950+
}
1951+
19251952
// Returns an enumeration value in C++ form.
19261953
String _getEnumValue(String module, String enumName, String memberName) {
19271954
final String snakeEnumName = _snakeCaseFromCamelCase(enumName);
@@ -2062,12 +2089,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
20622089
}
20632090
}
20642091

2065-
int _getTypeEnumeration(Root root, TypeDeclaration type) {
2066-
return getEnumeratedTypes(root, excludeSealedClasses: true)
2067-
.firstWhere((EnumeratedType t) =>
2068-
(type.isClass && t.associatedClass == type.associatedClass) ||
2069-
(type.isEnum && t.associatedEnum == type.associatedEnum))
2070-
.enumeration;
2092+
String _getCustomTypeIdFromDeclaration(
2093+
Root root, TypeDeclaration type, String module) {
2094+
return _getCustomTypeId(
2095+
module,
2096+
getEnumeratedTypes(root, excludeSealedClasses: true).firstWhere(
2097+
(EnumeratedType t) =>
2098+
(type.isClass && t.associatedClass == type.associatedClass) ||
2099+
(type.isEnum && t.associatedEnum == type.associatedEnum)));
20712100
}
20722101

20732102
// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2107,15 @@ String _makeFlValue(
20782107
{String? lengthVariableName}) {
20792108
final String value;
20802109
if (type.isClass) {
2081-
final int enumeration = _getTypeEnumeration(root, type);
2082-
value = 'fl_value_new_custom_object($enumeration, G_OBJECT($variableName))';
2110+
final String customTypeId =
2111+
_getCustomTypeIdFromDeclaration(root, type, module);
2112+
value =
2113+
'fl_value_new_custom_object($customTypeId, G_OBJECT($variableName))';
20832114
} else if (type.isEnum) {
2084-
final int enumeration = _getTypeEnumeration(root, type);
2115+
final String customTypeId =
2116+
_getCustomTypeIdFromDeclaration(root, type, module);
20852117
value =
2086-
'fl_value_new_custom($enumeration, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
2118+
'fl_value_new_custom($customTypeId, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
20872119
} else if (_isFlValueWrappedType(type)) {
20882120
value = 'fl_value_ref($variableName)';
20892121
} else if (type.baseName == 'void') {

0 commit comments

Comments
 (0)