-
Notifications
You must be signed in to change notification settings - Fork 131
Interop: send events to fallback event handler if active configuration not present #1439
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| private import _TestingInternals | ||
|
|
||
| extension Event { | ||
| /// Post this event to the currently-installed fallback event handler. | ||
| /// | ||
| /// - Parameters: | ||
| /// - context: The context associated with this event. | ||
| /// | ||
| /// - Returns: Whether or not the fallback event handler was invoked. If the | ||
| /// currently-installed handler belongs to the testing library, returns | ||
| /// `false`. | ||
| borrowing func postToFallbackHandler(in context: borrowing Context) -> Bool { | ||
| #if canImport(_TestingInterop) | ||
| guard let fallbackEventHandler = _swift_testing_getFallbackEventHandler() else { | ||
| return false | ||
| } | ||
|
|
||
| // Encode the event as JSON and pass it to the handler. | ||
| let encodeAndInvoke = ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to cache this event handler somewhere as there's a bit of runtime overhead to generating it. Remember the fallback event handler can't change after its initial assignment, and will be set by the time an event is generated, so we don't need to worry about it having changed on later calls (i.e. the closure we generate here can permanently capture it.) |
||
| recordJSON in | ||
| fallbackEventHandler( | ||
| String(describing: ABI.CurrentVersion.versionNumber), | ||
| recordJSON.baseAddress!, | ||
| recordJSON.count, | ||
| nil | ||
| ) | ||
| } | ||
| encodeAndInvoke(self, context) | ||
| return true | ||
| #else | ||
| return false | ||
| #endif | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -180,6 +180,17 @@ static int swt_setfdflags(int fd, int flags) { | |||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| #if !SWT_NO_INTEROP | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anywhere you're doing this, I suggest changing it to:
Suggested change
So that when we build against the 6.2 toolchain, we don't even try to link this function. |
||||||
|
|
||||||
| typedef void (*FallbackEventHandler)(const char *recordJSONSchemaVersionNumber, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR's still a draft, but if you stick with this approach, please plan to add some documentation for these (even if it's a cursory explanation of how they shadow functions elsewhere).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The smallest possible nitpick. :) |
||||||
| const void *recordJSONBaseAddress, | ||||||
| long recordJSONByteCount, | ||||||
| const void *_Nullable reserved); | ||||||
|
|
||||||
| FallbackEventHandler _Nullable _swift_testing_getFallbackEventHandler(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The argument lists |
||||||
|
|
||||||
| #endif | ||||||
|
|
||||||
| SWT_ASSUME_NONNULL_END | ||||||
|
|
||||||
| #endif | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style-wise, we usually outdent
#ifguards to column 0