-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add abihelper and to/from abi * fixing things up * test other types * fix tests * remove unnecessary try * move array tests to their own class * these can't throw * dont use the swift name for abi args * fix array input in debug builds * rename to get_swift_member_name * add comment on RawRepresentable * shouldn't have done this * fix build
- Loading branch information
1 parent
1095be9
commit 9a4be08
Showing
28 changed files
with
894 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import C_BINDINGS_MODULE | ||
import Foundation | ||
|
||
@_spi(WinRTInternal) | ||
public typealias WinRTArrayAbi<T> = (count: UInt32, start: UnsafeMutablePointer<T>?) | ||
|
||
@_spi(WinRTInternal) | ||
extension Array where Element: FromAbi { | ||
public static func from(abi: WinRTArrayAbi<Element.ABI>) -> [Element] { | ||
UnsafeBufferPointer(start: abi.start, count: Int(abi.count)).map { .from(abi: $0) } | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array where Element: Numeric { | ||
public static func from(abi: WinRTArrayAbi<Element>) -> [Element] { | ||
Array(UnsafeBufferPointer(start: abi.start, count: Int(abi.count))) | ||
} | ||
} | ||
|
||
// RawRepresentable covers Enums, which are simply numberic types, but the where Element: Numeric doesn't | ||
// cover them. These particular cases are written to ensure no accidental conversion of types that can't | ||
// be simply cast to a C-style Array accidentally sneak in | ||
@_spi(WinRTInternal) | ||
extension Array where Element: RawRepresentable, Element.RawValue: Numeric { | ||
public static func from(abi: WinRTArrayAbi<Element>) -> [Element] { | ||
Array(UnsafeBufferPointer(start: abi.start, count: Int(abi.count))) | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array { | ||
public static func from<Bridge: AbiInterfaceBridge>(abiBridge: Bridge.Type, abi: WinRTArrayAbi<UnsafeMutablePointer<Bridge.CABI>?>) -> [Element] where Element == Bridge.SwiftProjection? { | ||
UnsafeBufferPointer(start: abi.start, count: Int(abi.count)).map { InterfaceWrapperBase<Bridge>.unwrapFrom(abi: ComPtr($0)) } | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array { | ||
public static func from<Bridge: AbiInterfaceBridge>(abiBridge: Bridge.Type, start: UnsafeMutablePointer<UnsafeMutablePointer<Bridge.CABI>?>?, count: UInt32) -> [Element] where Element == Bridge.SwiftProjection? { | ||
UnsafeBufferPointer(start: start, count: Int(count)).map { InterfaceWrapperBase<Bridge>.unwrapFrom(abi: ComPtr($0)) } | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array { | ||
public static func from<Bridge: AbiBridge>(abiBridge: Bridge.Type, abi: WinRTArrayAbi<UnsafeMutablePointer<Bridge.CABI>?>) -> [Element] where Element == Bridge.SwiftProjection?, Bridge.SwiftProjection: WinRTClass { | ||
UnsafeBufferPointer(start: abi.1, count: Int(abi.0)).map { Bridge.from(abi: ComPtr($0)) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import C_BINDINGS_MODULE | ||
import Foundation | ||
|
||
@_spi(WinRTInternal) | ||
extension Array where Element: ToAbi { | ||
public func toABI(_ withAbi: (_ length: UInt32, _ bytes: UnsafeMutablePointer<Element.ABI>?) throws -> Void) throws { | ||
let abiArray: [Element.ABI] = try map { try $0.toABI() } | ||
try abiArray.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in | ||
let bytesPtr = bytes.baseAddress?.assumingMemoryBound(to: Element.ABI.self) | ||
try withAbi(UInt32(count), .init(mutating: bytesPtr)) | ||
} | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array where Element: Numeric { | ||
public func toABI(_ withAbi: (_ length: UInt32, _ bytes: UnsafeMutablePointer<Element>?) throws -> Void) throws { | ||
try withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in | ||
let bytesPtr = bytes.baseAddress?.assumingMemoryBound(to: Element.self) | ||
try withAbi(UInt32(count), .init(mutating: bytesPtr)) | ||
} | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array where Element: RawRepresentable, Element.RawValue: Numeric { | ||
public func toABI(_ withAbi: (_ length: UInt32, _ bytes: UnsafeMutablePointer<Element>?) throws -> Void) throws { | ||
try withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in | ||
let bytesPtr = bytes.baseAddress?.assumingMemoryBound(to: Element.self) | ||
try withAbi(UInt32(count), .init(mutating: bytesPtr)) | ||
} | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array { | ||
public func toABI<Bridge: AbiInterfaceBridge>(abiBridge: Bridge.Type, _ withAbi: (_ length: UInt32, _ bytes: UnsafeMutablePointer<UnsafeMutablePointer<Bridge.CABI>?>?) throws -> Void) throws where Element == Bridge.SwiftProjection? { | ||
let abiWrapperArray: [InterfaceWrapperBase<Bridge>?] = map { .init($0) } | ||
let abiArray = try abiWrapperArray.map { try $0?.toABI{ $0 } } | ||
try abiArray.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in | ||
let bytesPtr = bytes.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Bridge.CABI>?.self) | ||
try withAbi(UInt32(count), .init(mutating: bytesPtr)) | ||
} | ||
} | ||
} | ||
|
||
@_spi(WinRTInternal) | ||
extension Array { | ||
public func toABI<Bridge: AbiBridge>(abiBridge: Bridge.Type, _ withAbi: (_ length: UInt32, _ bytes: UnsafeMutablePointer<UnsafeMutablePointer<Bridge.CABI>?>?) throws -> Void) throws where Element == Bridge.SwiftProjection?, Bridge.SwiftProjection: WinRTClass { | ||
let abiArray: [UnsafeMutablePointer<Bridge.CABI>?] = map { RawPointer($0) } | ||
try abiArray.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in | ||
let bytesPtr = bytes.baseAddress?.assumingMemoryBound(to: UnsafeMutablePointer<Bridge.CABI>?.self) | ||
try withAbi(UInt32(count), .init(mutating: bytesPtr)) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.