Skip to content

Commit f918311

Browse files
authored
[pigeon] add support for top level consts (flutter#12032)
# Add Top-level Constants Support to Pigeon This PR adds support for defining and generating top-level constants across all target languages supported by Pigeon (Dart, Kotlin, Java, Swift, Objective-C, C++, and GObject). Resolves: flutter#188642 ## Summary of Changes ### 1. AST & Parser * Added a `Constant` node type to the AST representing top-level constants. * Updated `pigeon_lib_internal.dart` to parse `const` declarations. * Restrained constant types to: `String`, `int`, `double`, and `bool`. * Implemented validation for explicit type annotations, type matching, and initialization values (unsupported types, binary expressions, and string interpolations will trigger compilation errors). ### 2. Code Generation Added constant generation to all target languages: * **Dart**: Generates top-level `const` variables. * **Java**: Generates `public static final` fields inside the main generated class. * **Kotlin**: Generates companion object `const val` properties (using `val` for double/boolean where JVM `const` is not supported). * **Swift**: Generates global `public let` constants. * **Objective-C**: Generates global `static const` / `static NSString *const` variables in the header file. * **C++**: Generates global `inline constexpr` variables in the header. * **GObject**: Generates global `#define` macros in the header. ### 3. Tests & Documentation * Added parsing validation tests in `test/pigeon_lib_test.dart`. * Added generator unit tests (`test/*_generator_test.dart`) for each language verifying the syntax of generated constants. * Updated `pigeons/core_tests.dart` with constant declarations, and added corresponding unit tests verifying their correctness in each target platform project. * Added an integration test in `platform_tests/shared_test_plugin_code/lib/integration_tests.dart` to assert constant values at runtime. * Updated `packages/pigeon/README.md` to document the new feature, using code excerpts linked to `example/app/pigeons/messages.dart`.
1 parent b39bbbb commit f918311

48 files changed

Lines changed: 1167 additions & 10 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 27.3.0
2+
3+
* Adds support for sharing constants across platforms.
4+
15
## 27.2.0
26

37
* Adds support for empty data classes.

‎packages/pigeon/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?code-excerpt path-base="example/app"?>
12
# Pigeon
23

34
Pigeon is a code generator tool to make communication between Flutter and the
@@ -82,6 +83,20 @@ the threading model for handling HostApi methods can be selected with the
8283
Host and Flutter APIs now support the ability to provide a unique message channel suffix string
8384
to the api to allow for multiple instances to be created and operate in parallel.
8485

86+
### Constants
87+
88+
Pigeon supports generating top-level constants in the generated files. Constants can be defined at the top level of the Pigeon file:
89+
90+
<?code-excerpt "pigeons/messages.dart (constants)"?>
91+
```dart
92+
const String aStringConstant = 'stringConstantValue';
93+
const int anIntConstant = 42;
94+
const double aDoubleConstant = 3.14;
95+
const bool aBoolConstant = true;
96+
```
97+
98+
These constants will be translated into static constants or final variables in the target languages (e.g., `public static final` in Java, `let` in Swift, `const` in Dart, etc.). Only `String`, `int`, `double`, and `bool` constant types are supported.
99+
85100
## Usage
86101

87102
1) Add pigeon as a `dev_dependency`.

‎packages/pigeon/example/app/android/app/src/main/java/io/flutter/plugins/Messages.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
/** Generated class from Pigeon. */
2828
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression", "serial"})
2929
public class Messages {
30+
31+
public static final String aStringConstant = "stringConstantValue";
32+
public static final long anIntConstant = 42L;
33+
public static final double aDoubleConstant = 3.14;
34+
public static final boolean aBoolConstant = true;
35+
3036
static boolean pigeonDoubleEquals(double a, double b) {
3137
// Normalize -0.0 to 0.0 and handle NaN equality.
3238
return (a == 0.0 ? 0.0 : a) == (b == 0.0 ? 0.0 : b) || (Double.isNaN(a) && Double.isNaN(b));

‎packages/pigeon/example/app/android/app/src/main/kotlin/dev/flutter/pigeon_example_app/Messages.g.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import io.flutter.plugin.common.StandardMessageCodec
1313
import java.io.ByteArrayOutputStream
1414
import java.nio.ByteBuffer
1515

16+
const val aStringConstant: String = "stringConstantValue"
17+
const val anIntConstant: Long = 42L
18+
const val aDoubleConstant: Double = 3.14
19+
const val aBoolConstant: Boolean = true
20+
1621
private object MessagesPigeonUtils {
1722

1823
fun createConnectionError(channelName: String): FlutterError {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import Foundation
1414
#error("Unsupported platform.")
1515
#endif
1616

17+
public let aStringConstant: String = "stringConstantValue"
18+
public let anIntConstant: Int64 = 42
19+
public let aDoubleConstant: Double = 3.14
20+
public let aBoolConstant: Bool = true
21+
1722
/// Error class for passing custom error details to Dart side.
1823
final class PigeonError: Error {
1924
let code: String

‎packages/pigeon/example/app/lib/src/messages.g.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import 'dart:typed_data' show Float64List, Int32List, Int64List;
1212
import 'package:flutter/services.dart';
1313
import 'package:meta/meta.dart' show immutable, protected, visibleForTesting;
1414

15+
const String aStringConstant = 'stringConstantValue';
16+
const int anIntConstant = 42;
17+
const double aDoubleConstant = 3.14;
18+
const bool aBoolConstant = true;
19+
1520
Object? _extractReplyValueOrThrow(
1621
List<Object?>? replyList,
1722
String channelName, {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
G_BEGIN_DECLS
1313

14+
#define PIGEON_EXAMPLE_PACKAGE_A_STRING_CONSTANT "stringConstantValue"
15+
#define PIGEON_EXAMPLE_PACKAGE_AN_INT_CONSTANT 42
16+
#define PIGEON_EXAMPLE_PACKAGE_A_DOUBLE_CONSTANT 3.14
17+
#define PIGEON_EXAMPLE_PACKAGE_A_BOOL_CONSTANT TRUE
18+
1419
/**
1520
* PigeonExamplePackageCode:
1621
* PIGEON_EXAMPLE_PACKAGE_CODE_ONE:

‎packages/pigeon/example/app/macos/Runner/messages.g.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
NS_ASSUME_NONNULL_BEGIN
1515

16+
extern NSString *const PGNAStringConstant;
17+
extern const NSInteger PGNAnIntConstant;
18+
extern const double PGNADoubleConstant;
19+
extern const BOOL PGNABoolConstant;
20+
1621
typedef NS_ENUM(NSUInteger, PGNCode) {
1722
PGNCodeOne = 0,
1823
PGNCodeTwo = 1,

‎packages/pigeon/example/app/macos/Runner/messages.g.m‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
@import Flutter;
1313
#endif
1414

15+
NSString *const PGNAStringConstant = @"stringConstantValue";
16+
const NSInteger PGNAnIntConstant = 42;
17+
const double PGNADoubleConstant = 3.14;
18+
const BOOL PGNABoolConstant = YES;
1519
static BOOL __attribute__((unused)) FLTPigeonDeepEquals(id _Nullable a, id _Nullable b) {
1620
if (a == b) {
1721
return YES;

‎packages/pigeon/example/app/pigeons/messages.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ abstract class MessageFlutterApi {
6363
}
6464

6565
// #enddocregion flutter-definitions
66+
67+
// #docregion constants
68+
const String aStringConstant = 'stringConstantValue';
69+
const int anIntConstant = 42;
70+
const double aDoubleConstant = 3.14;
71+
const bool aBoolConstant = true;
72+
// #enddocregion constants

0 commit comments

Comments
 (0)