Skip to content

Commit 698bcad

Browse files
authored
Merge pull request #27266 from Catfish-Man/the-deep-blue-c
Add C calling convention shims for creating arrays and strings from buffers
2 parents b09631f + 9003774 commit 698bcad

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

stdlib/public/core/BridgeObjectiveC.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public protocol _ObjectiveCBridgeable {
8585

8686
#if _runtime(_ObjC)
8787

88+
@available(macOS, introduced: 9999, deprecated)
89+
@available(iOS, introduced: 9999, deprecated)
90+
@available(watchOS, introduced: 9999, deprecated)
91+
@available(tvOS, introduced: 9999, deprecated)
92+
@available(*, deprecated)
93+
@_cdecl("_SwiftCreateBridgedArray")
94+
@usableFromInline
95+
internal func _SwiftCreateBridgedArray(
96+
values: UnsafePointer<AnyObject>,
97+
numValues: Int
98+
) -> Unmanaged<AnyObject> {
99+
let bufPtr = UnsafeBufferPointer(start: values, count: numValues)
100+
let bridged = Array(bufPtr)._bridgeToObjectiveCImpl()
101+
return Unmanaged<AnyObject>.passRetained(bridged)
102+
}
103+
88104
@_silgen_name("swift_stdlib_connectNSBaseClasses")
89105
internal func _connectNSBaseClasses() -> Bool
90106

stdlib/public/core/StringBridge.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,31 @@ extension String {
482482
}
483483
}
484484

485+
@available(macOS, introduced: 9999, deprecated)
486+
@available(iOS, introduced: 9999, deprecated)
487+
@available(watchOS, introduced: 9999, deprecated)
488+
@available(tvOS, introduced: 9999, deprecated)
489+
@available(*, deprecated)
490+
@_cdecl("_SwiftCreateBridgedString")
491+
@usableFromInline
492+
internal func _SwiftCreateBridgedString(
493+
bytes: UnsafePointer<UInt8>,
494+
length: Int,
495+
encoding: _swift_shims_CFStringEncoding
496+
) -> Unmanaged<AnyObject> {
497+
let bufPtr = UnsafeBufferPointer(start: bytes, count: length)
498+
let str:String
499+
switch encoding {
500+
case kCFStringEncodingUTF8:
501+
str = String(decoding: bufPtr, as: Unicode.UTF8.self)
502+
case kCFStringEncodingASCII:
503+
str = String(decoding: bufPtr, as: Unicode.ASCII.self)
504+
default:
505+
fatalError("Unsupported encoding in shim")
506+
}
507+
return Unmanaged<AnyObject>.passRetained(str._bridgeToObjectiveCImpl())
508+
}
509+
485510
// At runtime, this class is derived from `__SwiftNativeNSStringBase`,
486511
// which is derived from `NSString`.
487512
//

0 commit comments

Comments
 (0)