Skip to content

Expose __SwiftNativeNSString for Foundation's use, as well as an initializer to create Strings from them #39229

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

Merged
merged 1 commit into from
Sep 10, 2021
Merged
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
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"StringRangeReplaceableCollection.swift",
"StringGutsRangeReplaceable.swift",
"StringStorage.swift",
"StringStorageBridge.swift",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was apparently missing all along, who knew.

"StringSwitch.swift",
"StringTesting.swift",
"StringUTF16View.swift",
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ internal func _SwiftCreateBridgedString_DoNotCall(
// This allows us to subclass an Objective-C class and use the fast Swift
// memory allocator.
@objc @_swift_native_objc_runtime_base(__SwiftNativeNSStringBase)
class __SwiftNativeNSString {
@_spi(Foundation) public class __SwiftNativeNSString {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying to minimize the ABI exposure here. I could expose __StringStorage and __SharedStringStorage, but since this is their superclass, I would need to expose it anyway, and this is sufficient for hanging @objc implementations off of that operate in terms of the new initializer.

@objc internal init() {}
deinit {}
}
Expand Down
22 changes: 22 additions & 0 deletions stdlib/public/core/StringStorageBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ import SwiftShims
internal let _cocoaASCIIEncoding:UInt = 1 /* NSASCIIStringEncoding */
internal let _cocoaUTF8Encoding:UInt = 4 /* NSUTF8StringEncoding */

extension String {
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@_spi(Foundation)
public init?(_nativeStorage: AnyObject) {
Copy link
Member

Choose a reason for hiding this comment

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

What is this used for, and is this tested anywhere? Comments? That's a lot of unsafe unchecked downcasts and it's not clear to me what is assumed about _nativeStorage.

let knownOther = _KnownCocoaString(_nativeStorage)
switch knownOther {
case .storage:
self = _unsafeUncheckedDowncast(
_nativeStorage,
to: __StringStorage.self
).asString
case .shared:
self = _unsafeUncheckedDowncast(
_nativeStorage,
to: __SharedStringStorage.self
).asString
default:
return nil
}
}
}

// ObjC interfaces.
extension _AbstractStringStorage {
@inline(__always)
Expand Down