Skip to content

Commit b660a0c

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
1 parent d3c5cca commit b660a0c

File tree

10 files changed

+396
-159
lines changed

10 files changed

+396
-159
lines changed

packages/pigeon/CHANGELOG.md

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

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

56
## 25.3.2

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: 12 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;
@@ -130,7 +131,7 @@ static gboolean
130131
pigeon_example_package_message_codec_write_pigeon_example_package_code(
131132
FlStandardMessageCodec* codec, GByteArray* buffer, FlValue* value,
132133
GError** error) {
133-
uint8_t type = 129;
134+
uint8_t type = pigeon_example_package_code_type_id;
134135
g_byte_array_append(buffer, &type, sizeof(uint8_t));
135136
return fl_standard_message_codec_write_value(codec, buffer, value, error);
136137
}
@@ -139,7 +140,7 @@ static gboolean
139140
pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
140141
FlStandardMessageCodec* codec, GByteArray* buffer,
141142
PigeonExamplePackageMessageData* value, GError** error) {
142-
uint8_t type = 130;
143+
uint8_t type = pigeon_example_package_message_data_type_id;
143144
g_byte_array_append(buffer, &type, sizeof(uint8_t));
144145
g_autoptr(FlValue) values =
145146
pigeon_example_package_message_data_to_list(value);
@@ -151,13 +152,13 @@ static gboolean pigeon_example_package_message_codec_write_value(
151152
GError** error) {
152153
if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM) {
153154
switch (fl_value_get_custom_type(value)) {
154-
case 129:
155+
case pigeon_example_package_code_type_id:
155156
return pigeon_example_package_message_codec_write_pigeon_example_package_code(
156157
codec, buffer,
157158
reinterpret_cast<FlValue*>(
158159
const_cast<gpointer>(fl_value_get_custom_value(value))),
159160
error);
160-
case 130:
161+
case pigeon_example_package_message_data_type_id:
161162
return pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
162163
codec, buffer,
163164
PIGEON_EXAMPLE_PACKAGE_MESSAGE_DATA(
@@ -176,7 +177,8 @@ pigeon_example_package_message_codec_read_pigeon_example_package_code(
176177
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset,
177178
GError** error) {
178179
return fl_value_new_custom(
179-
129, fl_standard_message_codec_read_value(codec, buffer, offset, error),
180+
pigeon_example_package_code_type_id,
181+
fl_standard_message_codec_read_value(codec, buffer, offset, error),
180182
(GDestroyNotify)fl_value_unref);
181183
}
182184

@@ -198,17 +200,18 @@ pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
198200
return nullptr;
199201
}
200202

201-
return fl_value_new_custom_object(130, G_OBJECT(value));
203+
return fl_value_new_custom_object(pigeon_example_package_message_data_type_id,
204+
G_OBJECT(value));
202205
}
203206

204207
static FlValue* pigeon_example_package_message_codec_read_value_of_type(
205208
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, int type,
206209
GError** error) {
207210
switch (type) {
208-
case 129:
211+
case pigeon_example_package_code_type_id:
209212
return pigeon_example_package_message_codec_read_pigeon_example_package_code(
210213
codec, buffer, offset, error);
211-
case 130:
214+
case pigeon_example_package_message_data_type_id:
212215
return pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
213216
codec, buffer, offset, error);
214217
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+
const uint8_t pigeon_example_package_code_type_id = 129;
99+
100+
const uint8_t pigeon_example_package_message_data_type_id = 130;
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: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ 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
349+
.writeln('const uint8_t $customTypeId = ${customType.enumeration};');
350+
}
341351
}
342352

343353
@override
@@ -1023,14 +1033,15 @@ class GObjectSourceGenerator
10231033
final String customTypeName = _getClassName(module, customType.name);
10241034
final String snakeCustomTypeName =
10251035
_snakeCaseFromCamelCase(customTypeName);
1036+
final String customTypeId = _getCustomTypeId(module, customType);
10261037
indent.newln();
10271038
final String valueType = customType.type == CustomTypes.customClass
10281039
? '$customTypeName*'
10291040
: 'FlValue*';
10301041
indent.writeScoped(
10311042
'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName($_standardCodecName* codec, GByteArray* buffer, $valueType value, GError** error) {',
10321043
'}', () {
1033-
indent.writeln('uint8_t type = ${customType.enumeration};');
1044+
indent.writeln('uint8_t type = $customTypeId;');
10341045
indent.writeln('g_byte_array_append(buffer, &type, sizeof(uint8_t));');
10351046
if (customType.type == CustomTypes.customClass) {
10361047
indent.writeln(
@@ -1053,7 +1064,8 @@ class GObjectSourceGenerator
10531064
indent.writeScoped('switch (fl_value_get_custom_type(value)) {', '}',
10541065
() {
10551066
for (final EnumeratedType customType in customTypes) {
1056-
indent.writeln('case ${customType.enumeration}:');
1067+
final String customTypeId = _getCustomTypeId(module, customType);
1068+
indent.writeln('case $customTypeId:');
10571069
indent.nest(1, () {
10581070
final String customTypeName =
10591071
_getClassName(module, customType.name);
@@ -1082,6 +1094,7 @@ class GObjectSourceGenerator
10821094
final String customTypeName = _getClassName(module, customType.name);
10831095
final String snakeCustomTypeName =
10841096
_snakeCaseFromCamelCase(customTypeName);
1097+
final String customTypeId = _getCustomTypeId(module, customType);
10851098
indent.newln();
10861099
indent.writeScoped(
10871100
'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName($_standardCodecName* codec, GBytes* buffer, size_t* offset, GError** error) {',
@@ -1102,10 +1115,10 @@ class GObjectSourceGenerator
11021115
});
11031116
indent.newln();
11041117
indent.writeln(
1105-
'return fl_value_new_custom_object(${customType.enumeration}, G_OBJECT(value));');
1118+
'return fl_value_new_custom_object($customTypeId, G_OBJECT(value));');
11061119
} else if (customType.type == CustomTypes.customEnum) {
11071120
indent.writeln(
1108-
'return fl_value_new_custom(${customType.enumeration}, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
1121+
'return fl_value_new_custom($customTypeId, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
11091122
}
11101123
});
11111124
}
@@ -1117,9 +1130,10 @@ class GObjectSourceGenerator
11171130
indent.writeScoped('switch (type) {', '}', () {
11181131
for (final EnumeratedType customType in customTypes) {
11191132
final String customTypeName = _getClassName(module, customType.name);
1133+
final String customTypeId = _getCustomTypeId(module, customType);
11201134
final String snakeCustomTypeName =
11211135
_snakeCaseFromCamelCase(customTypeName);
1122-
indent.writeln('case ${customType.enumeration}:');
1136+
indent.writeln('case $customTypeId:');
11231137
indent.nest(1, () {
11241138
indent.writeln(
11251139
'return ${codecMethodPrefix}_read_$snakeCustomTypeName(codec, buffer, offset, error);');
@@ -1922,6 +1936,16 @@ String _getMethodPrefix(String module, String name) {
19221936
return _snakeCaseFromCamelCase(className);
19231937
}
19241938

1939+
// Returns the code for the custom type id definition for [customType].
1940+
String _getCustomTypeId(String module, EnumeratedType customType) {
1941+
final String customTypeName = _getClassName(module, customType.name);
1942+
1943+
final String snakeCustomTypeName = _snakeCaseFromCamelCase(customTypeName);
1944+
1945+
final String customTypeId = '${snakeCustomTypeName}_type_id';
1946+
return customTypeId;
1947+
}
1948+
19251949
// Returns an enumeration value in C++ form.
19261950
String _getEnumValue(String module, String enumName, String memberName) {
19271951
final String snakeEnumName = _snakeCaseFromCamelCase(enumName);
@@ -2062,12 +2086,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
20622086
}
20632087
}
20642088

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;
2089+
String _getCustomTypeIdFromDeclaration(
2090+
Root root, TypeDeclaration type, String module) {
2091+
return _getCustomTypeId(
2092+
module,
2093+
getEnumeratedTypes(root, excludeSealedClasses: true).firstWhere(
2094+
(EnumeratedType t) =>
2095+
(type.isClass && t.associatedClass == type.associatedClass) ||
2096+
(type.isEnum && t.associatedEnum == type.associatedEnum)));
20712097
}
20722098

20732099
// Returns code to convert the native data type stored in [variableName] to a FlValue.
@@ -2078,12 +2104,15 @@ String _makeFlValue(
20782104
{String? lengthVariableName}) {
20792105
final String value;
20802106
if (type.isClass) {
2081-
final int enumeration = _getTypeEnumeration(root, type);
2082-
value = 'fl_value_new_custom_object($enumeration, G_OBJECT($variableName))';
2107+
final String customTypeId =
2108+
_getCustomTypeIdFromDeclaration(root, type, module);
2109+
value =
2110+
'fl_value_new_custom_object($customTypeId, G_OBJECT($variableName))';
20832111
} else if (type.isEnum) {
2084-
final int enumeration = _getTypeEnumeration(root, type);
2112+
final String customTypeId =
2113+
_getCustomTypeIdFromDeclaration(root, type, module);
20852114
value =
2086-
'fl_value_new_custom($enumeration, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
2115+
'fl_value_new_custom($customTypeId, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
20872116
} else if (_isFlValueWrappedType(type)) {
20882117
value = 'fl_value_ref($variableName)';
20892118
} else if (type.baseName == 'void') {

0 commit comments

Comments
 (0)