Skip to content

Support embedded Swift#116

Open
guoye-zhang wants to merge 10 commits into
apple:mainfrom
guoye-zhang:embedded
Open

Support embedded Swift#116
guoye-zhang wants to merge 10 commits into
apple:mainfrom
guoye-zhang:embedded

Conversation

@guoye-zhang
Copy link
Copy Markdown
Contributor

In order for swift-http-api-proposal to support embedded Swift, swift-http-types has to support embedded Swift first.

Is there a CI we can enable for embedded?

@guoye-zhang guoye-zhang added the 🆕 semver/minor Adds new public API. label Apr 8, 2026
public func withUnsafeBytesOfValue<Result>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> Result
) rethrows -> Result {
public func withUnsafeBytesOfValue<Result, Failure: Error>(
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.

Is this API breakage real?

Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov Apr 10, 2026

Choose a reason for hiding this comment

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

IIUC this is not source-breaking, but very likely ABI-breaking.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct. This is considered fine.

}
#else
final func withLock<Result, Failure: Error>(_ body: () throws(Failure) -> Result) throws(Failure) -> Result {
try body()
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.

Is it OK to not lock on embedded?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not universally so, Wasm does have multi-threaded flavors, but it also has import Synchronization available in all flavors. For other embedded platforms it should be done on case-by-case basis.

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.

Embedded Swift status says Synchronization is available but only for atomics, not mutexes: https://docs.swift.org/embedded/documentation/embedded/status

I remember reading a pitch about SpinLock a while back.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think that document reflects the current state of things, at least for Wasm. import Synchronization is fully available in Embedded Swift for Wasm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah we should really use Mutex here if possible

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.

Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov Apr 14, 2026

Choose a reason for hiding this comment

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

This condition you referenced means that if arch is wasm32, then all Synchronization sources are built, and of non-wasm32 platforms only Atomic is enabled (since other Embedded platforms don't have a predefined libc, like WASI-libc). So it's ok to rely on Mutex here.

    if("${arch}" MATCHES "wasm32")
      list(APPEND SWIFT_SYNCHRONIZATION_EMBEDDED_SOURCES ${SWIFT_SYNCHRONIZATION_SOURCES})
      list(APPEND SWIFT_SYNCHRONIZATION_EMBEDDED_SOURCES ${SWIFT_SYNCHRONIZATION_WASM_SOURCES})
    else()
      list(APPEND SWIFT_SYNCHRONIZATION_EMBEDDED_SOURCES ${SWIFT_SYNCHRONIZATION_ATOMIC_SOURCES})
    endif()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As clarified in a comment above, that patch wasn't cherry-picked for 6.3 unfortunately, so you'll have to make it conditional on 6.4+ as I suggested there.

Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

It would be great for this PR or imminent followup PRs to also add Wasm (both embedded and non-embedded) jobs on CI to ensure no regressions are introduced in the future.

Comment thread .github/workflows/pull_request.yml Outdated
name: WebAssembly Swift SDK
uses: apple/swift-nio/.github/workflows/wasm_swift_sdk.yml@main
with:
additional_command_arguments: "--target HTTPTypes"
Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov Apr 13, 2026

Choose a reason for hiding this comment

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

Would you mind using this one instead? Otherwise we'll have replicate Embedded Swift builds in swift-nio workflows.

  embedded-swift:
    name: Build with Embedded Swift
    uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@0.0.9
    with:
      enable_linux_checks: false
      enable_macos_checks: false
      enable_windows_checks: false
      enable_wasm_sdk_build: false
      enable_embedded_wasm_sdk_build: true
      swift_flags: --target HTTPTypes

Comment thread Sources/HTTPTypes/HTTPFields.swift Outdated
fatalError()
}
#else
#if !hasFeature(Embedded) || os(WASI)
Copy link
Copy Markdown
Member

@MaxDesiatov MaxDesiatov Apr 15, 2026

Choose a reason for hiding this comment

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

Seems like Mutex is only available on WASI in main snapshots, but not 6.3 or 6.2:

Suggested change
#if !hasFeature(Embedded) || os(WASI)
#if !hasFeature(Embedded) || (os(WASI) && compiler(>=6.4))

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

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants