Skip to content

Migrate FlutterCodecs and its unit tests to Swift - #190329

Open
LongCatIsLooong wants to merge 11 commits into
flutter:masterfrom
LongCatIsLooong:fluttercodecs-swift
Open

LongCatIsLooong wants to merge 11 commits into
flutter:masterfrom
LongCatIsLooong:fluttercodecs-swift

Conversation

@LongCatIsLooong

@LongCatIsLooong LongCatIsLooong commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

This is an experiment that implements Objective-C APIs using @objc @implementation extensions (https://github.com/swiftlang/swift-evolution/blob/main/proposals/0436-objc-implementation.md). The migration is mostly done by gemini, told it to translate the code verbatim. Will try to parameterize the tests and fix the JSON top level element handling later.

Swift symbols

@objc @implementation extensions introduce visible Swift symbols even though they're just implementing Objective-C APIs. These symbols are stripped by the linker in the :flutter_framework_dylib target using a deny list. I've manually verified the produced Flutter.framework still work, even when the client is using a different version of swift compiler.

Tests

added engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterUmbrellaImport.swift for iOS and engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterUmbrellaImportTests.swift for macOS to make sure Swift clients can see those symbols.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added platform-ios iOS applications specifically engine flutter/engine related. See also e: labels. a: desktop Running on desktop team-ios Owned by iOS platform team team-macos Owned by the macOS platform team labels Jul 31, 2026
@LongCatIsLooong LongCatIsLooong added the CICD Run CI/CD label Jul 31, 2026
if message is [Any] || message is [AnyHashable: Any] {
encoding = try? JSONSerialization.data(withJSONObject: message, options: [])
} else {
// NSJSONSerialization does not support top-level simple values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this is true but I'll fix this in a separate PR.

@github-actions github-actions Bot added the platform-macos Building on or for macOS specifically label Sep 1, 2026
@LongCatIsLooong
LongCatIsLooong marked this pull request as ready for review September 1, 2026 18:47
@LongCatIsLooong
LongCatIsLooong requested review from a team as code owners September 1, 2026 18:47

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates the FlutterCodecs implementation and its corresponding unit tests from Objective-C to Swift. It also introduces an unexported symbols list to prevent the newly generated Swift symbols from being exported in the Flutter framework's dynamic symbol table. Review feedback suggests using more idiomatic Swift type checks for optional data and restoring several assertions from the original Objective-C implementation to prevent silent failures during decoding.

@knopp

knopp commented Sep 2, 2026

Copy link
Copy Markdown
Member

I am wondering a bit what is the benefit of translating random bits of embedder to Swift. The downside seem pretty clear, unfortunately, reducing consistency and increasing friction when navigating between the objc / swift parts of the codebase.

@LongCatIsLooong

Copy link
Copy Markdown
Contributor Author

I am wondering a bit what is the benefit of translating random bits of embedder to Swift. The downside seem pretty clear, unfortunately, reducing consistency and increasing friction when navigating between the objc / swift parts of the codebase.

I think the fraction is only temporary - it should be gone when we migrate most of the old objc codebase to swift (since with @objc @implementation we can just swap out objc implementations with swift ones in theory we should be able to migrate most objc code and keep the public headers in objc).

As for benefits, for one Swift's type system is much more capable so it's easier to spot problems during implementation and code review. I filed #192122 when doing the migration since the compiler was able to identify a couple of nullability issues in the objc logic.

@vashworth

Copy link
Copy Markdown
Contributor

cc @hellohuanlin

_$sSo22FlutterJSONMethodCodecCMa
_$sSo23FlutterJSONMessageCodecC08InternalA11SwiftCommonEABycfC
_$sSo23FlutterJSONMessageCodecC08InternalA11SwiftCommonEABycfc
_$sSo23FlutterJSONMessageCodecCMa

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are these generated? Do we have to touch this file whenever we update swift api?

import Flutter

// This file is to ensure that Flutter.framework can be cleanly imported
// by Swift clients, and certain Symbols are visible to Swift clients.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we also need to check objc still works?

@hellohuanlin hellohuanlin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexported_symbols.sym file seems like a headache when writing swift code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-ios iOS applications specifically platform-macos Building on or for macOS specifically team-ios Owned by iOS platform team team-macos Owned by the macOS platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants