Skip to content

[pigeon] Use a const for custom type ids for gobject generated files (#156100) #9306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 25.4.0

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

## 25.3.2

Expand Down
13 changes: 7 additions & 6 deletions packages/pigeon/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ generators with that AST.
## Source Index

* [ast.dart](./lib/src/ast.dart) - The data structure for representing the Abstract Syntax Tree.
* [dart_generator.dart](./lib/src/dart_generator.dart) - The Dart code generator.
* [java_generator.dart](./lib/src/java_generator.dart) - The Java code generator.
* [kotlin_generator.dart](./lib/src/kotlin_generator.dart) - The Kotlin code generator.
* [objc_generator.dart](./lib/src/objc_generator.dart) - The Objective-C code
* [dart_generator.dart](./lib/src/dart/dart_generator.dart) - The Dart code generator.
* [java_generator.dart](./lib/src/java/java_generator.dart) - The Java code generator.
* [kotlin_generator.dart](./lib/src/kotlin/kotlin_generator.dart) - The Kotlin code generator.
* [objc_generator.dart](./lib/src/objc/objc_generator.dart) - The Objective-C code
generator (header and source files).
* [swift_generator.dart](./lib/src/swift_generator.dart) - The Swift code generator.
* [cpp_generator.dart](./lib/src/cpp_generator.dart) - The C++ code generator.
* [swift_generator.dart](./lib/src/swift/swift_generator.dart) - The Swift code generator.
* [cpp_generator.dart](./lib/src/cpp/cpp_generator.dart) - The C++ code generator.
* [gobject_generator.dart](./lib/src/gobject/gobject_generator.dart) - The GObject code generator.
* [generator_tools.dart](./lib/src/generator_tools.dart) - Shared code between generators.
* [pigeon_cl.dart](./lib/src/pigeon_cl.dart) - The top-level function executed by
the command line tool in [bin/][./bin].
Expand Down
25 changes: 16 additions & 9 deletions packages/pigeon/example/app/linux/messages.g.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ static FlValue* pigeon_example_package_message_data_to_list(
? fl_value_new_string(self->description)
: fl_value_new_null());
fl_value_append_take(values,
fl_value_new_custom(129, fl_value_new_int(self->code),
fl_value_new_custom(pigeon_example_package_code_type_id,
fl_value_new_int(self->code),
(GDestroyNotify)fl_value_unref));
fl_value_append_take(values, fl_value_ref(self->data));
return values;
Expand Down Expand Up @@ -126,20 +127,24 @@ G_DEFINE_TYPE(PigeonExamplePackageMessageCodec,
pigeon_example_package_message_codec,
fl_standard_message_codec_get_type())

const int pigeon_example_package_code_type_id = 129;

static gboolean
pigeon_example_package_message_codec_write_pigeon_example_package_code(
FlStandardMessageCodec* codec, GByteArray* buffer, FlValue* value,
GError** error) {
uint8_t type = 129;
uint8_t type = pigeon_example_package_code_type_id;
g_byte_array_append(buffer, &type, sizeof(uint8_t));
return fl_standard_message_codec_write_value(codec, buffer, value, error);
}

const int pigeon_example_package_message_data_type_id = 130;

static gboolean
pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
FlStandardMessageCodec* codec, GByteArray* buffer,
PigeonExamplePackageMessageData* value, GError** error) {
uint8_t type = 130;
uint8_t type = pigeon_example_package_message_data_type_id;
g_byte_array_append(buffer, &type, sizeof(uint8_t));
g_autoptr(FlValue) values =
pigeon_example_package_message_data_to_list(value);
Expand All @@ -151,13 +156,13 @@ static gboolean pigeon_example_package_message_codec_write_value(
GError** error) {
if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM) {
switch (fl_value_get_custom_type(value)) {
case 129:
case pigeon_example_package_code_type_id:
return pigeon_example_package_message_codec_write_pigeon_example_package_code(
codec, buffer,
reinterpret_cast<FlValue*>(
const_cast<gpointer>(fl_value_get_custom_value(value))),
error);
case 130:
case pigeon_example_package_message_data_type_id:
return pigeon_example_package_message_codec_write_pigeon_example_package_message_data(
codec, buffer,
PIGEON_EXAMPLE_PACKAGE_MESSAGE_DATA(
Expand All @@ -176,7 +181,8 @@ pigeon_example_package_message_codec_read_pigeon_example_package_code(
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset,
GError** error) {
return fl_value_new_custom(
129, fl_standard_message_codec_read_value(codec, buffer, offset, error),
pigeon_example_package_code_type_id,
fl_standard_message_codec_read_value(codec, buffer, offset, error),
(GDestroyNotify)fl_value_unref);
}

Expand All @@ -198,17 +204,18 @@ pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
return nullptr;
}

return fl_value_new_custom_object(130, G_OBJECT(value));
return fl_value_new_custom_object(pigeon_example_package_message_data_type_id,
G_OBJECT(value));
}

static FlValue* pigeon_example_package_message_codec_read_value_of_type(
FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, int type,
GError** error) {
switch (type) {
case 129:
case pigeon_example_package_code_type_id:
return pigeon_example_package_message_codec_read_pigeon_example_package_code(
codec, buffer, offset, error);
case 130:
case pigeon_example_package_message_data_type_id:
return pigeon_example_package_message_codec_read_pigeon_example_package_message_data(
codec, buffer, offset, error);
default:
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/example/app/linux/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec,
PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC,
FlStandardMessageCodec)

extern const int pigeon_example_package_code_type_id;

extern const int pigeon_example_package_message_data_type_id;

G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi,
pigeon_example_package_example_host_api,
PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject)
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/src/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'generator.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '25.3.2';
const String pigeonVersion = '25.4.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
62 changes: 47 additions & 15 deletions packages/pigeon/lib/src/gobject/gobject_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,15 @@ class GObjectHeaderGenerator
indent.newln();
_writeDeclareFinalType(indent, module, _codecBaseName,
parentClassName: _standardCodecName);

final Iterable<EnumeratedType> customTypes =
getEnumeratedTypes(root, excludeSealedClasses: true);

for (final EnumeratedType customType in customTypes) {
final String customTypeId = _getCustomTypeId(module, customType);
indent.newln();
indent.writeln('extern const int $customTypeId;');
}
}

@override
Expand Down Expand Up @@ -1023,14 +1032,19 @@ class GObjectSourceGenerator
final String customTypeName = _getClassName(module, customType.name);
final String snakeCustomTypeName =
_snakeCaseFromCamelCase(customTypeName);
final String customTypeId = _getCustomTypeId(module, customType);

indent.newln();
indent.writeln('const int $customTypeId = ${customType.enumeration};');

indent.newln();
final String valueType = customType.type == CustomTypes.customClass
? '$customTypeName*'
: 'FlValue*';
indent.writeScoped(
'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName($_standardCodecName* codec, GByteArray* buffer, $valueType value, GError** error) {',
'}', () {
indent.writeln('uint8_t type = ${customType.enumeration};');
indent.writeln('uint8_t type = $customTypeId;');
indent.writeln('g_byte_array_append(buffer, &type, sizeof(uint8_t));');
if (customType.type == CustomTypes.customClass) {
indent.writeln(
Expand All @@ -1053,7 +1067,8 @@ class GObjectSourceGenerator
indent.writeScoped('switch (fl_value_get_custom_type(value)) {', '}',
() {
for (final EnumeratedType customType in customTypes) {
indent.writeln('case ${customType.enumeration}:');
final String customTypeId = _getCustomTypeId(module, customType);
indent.writeln('case $customTypeId:');
indent.nest(1, () {
final String customTypeName =
_getClassName(module, customType.name);
Expand Down Expand Up @@ -1082,6 +1097,7 @@ class GObjectSourceGenerator
final String customTypeName = _getClassName(module, customType.name);
final String snakeCustomTypeName =
_snakeCaseFromCamelCase(customTypeName);
final String customTypeId = _getCustomTypeId(module, customType);
indent.newln();
indent.writeScoped(
'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName($_standardCodecName* codec, GBytes* buffer, size_t* offset, GError** error) {',
Expand All @@ -1102,10 +1118,10 @@ class GObjectSourceGenerator
});
indent.newln();
indent.writeln(
'return fl_value_new_custom_object(${customType.enumeration}, G_OBJECT(value));');
'return fl_value_new_custom_object($customTypeId, G_OBJECT(value));');
} else if (customType.type == CustomTypes.customEnum) {
indent.writeln(
'return fl_value_new_custom(${customType.enumeration}, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
'return fl_value_new_custom($customTypeId, fl_standard_message_codec_read_value(codec, buffer, offset, error), (GDestroyNotify)fl_value_unref);');
}
});
}
Expand All @@ -1117,9 +1133,10 @@ class GObjectSourceGenerator
indent.writeScoped('switch (type) {', '}', () {
for (final EnumeratedType customType in customTypes) {
final String customTypeName = _getClassName(module, customType.name);
final String customTypeId = _getCustomTypeId(module, customType);
final String snakeCustomTypeName =
_snakeCaseFromCamelCase(customTypeName);
indent.writeln('case ${customType.enumeration}:');
indent.writeln('case $customTypeId:');
indent.nest(1, () {
indent.writeln(
'return ${codecMethodPrefix}_read_$snakeCustomTypeName(codec, buffer, offset, error);');
Expand Down Expand Up @@ -1922,6 +1939,16 @@ String _getMethodPrefix(String module, String name) {
return _snakeCaseFromCamelCase(className);
}

// Returns the code for the custom type id definition for [customType].
String _getCustomTypeId(String module, EnumeratedType customType) {
final String customTypeName = _getClassName(module, customType.name);

final String snakeCustomTypeName = _snakeCaseFromCamelCase(customTypeName);

final String customTypeId = '${snakeCustomTypeName}_type_id';
return customTypeId;
}

// Returns an enumeration value in C++ form.
String _getEnumValue(String module, String enumName, String memberName) {
final String snakeEnumName = _snakeCaseFromCamelCase(enumName);
Expand Down Expand Up @@ -2062,12 +2089,14 @@ String _referenceValue(String module, TypeDeclaration type, String variableName,
}
}

int _getTypeEnumeration(Root root, TypeDeclaration type) {
return getEnumeratedTypes(root, excludeSealedClasses: true)
.firstWhere((EnumeratedType t) =>
(type.isClass && t.associatedClass == type.associatedClass) ||
(type.isEnum && t.associatedEnum == type.associatedEnum))
.enumeration;
String _getCustomTypeIdFromDeclaration(
Root root, TypeDeclaration type, String module) {
return _getCustomTypeId(
module,
getEnumeratedTypes(root, excludeSealedClasses: true).firstWhere(
(EnumeratedType t) =>
(type.isClass && t.associatedClass == type.associatedClass) ||
(type.isEnum && t.associatedEnum == type.associatedEnum)));
}

// Returns code to convert the native data type stored in [variableName] to a FlValue.
Expand All @@ -2078,12 +2107,15 @@ String _makeFlValue(
{String? lengthVariableName}) {
final String value;
if (type.isClass) {
final int enumeration = _getTypeEnumeration(root, type);
value = 'fl_value_new_custom_object($enumeration, G_OBJECT($variableName))';
final String customTypeId =
_getCustomTypeIdFromDeclaration(root, type, module);
value =
'fl_value_new_custom_object($customTypeId, G_OBJECT($variableName))';
} else if (type.isEnum) {
final int enumeration = _getTypeEnumeration(root, type);
final String customTypeId =
_getCustomTypeIdFromDeclaration(root, type, module);
value =
'fl_value_new_custom($enumeration, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
'fl_value_new_custom($customTypeId, fl_value_new_int(${type.isNullable ? '*$variableName' : variableName}), (GDestroyNotify)fl_value_unref)';
} else if (_isFlValueWrappedType(type)) {
value = 'fl_value_ref($variableName)';
} else if (type.baseName == 'void') {
Expand Down
Loading