-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve Swift errors through WinRT boundaries via ILanguageException…
…ErrorInfo (#261)
- Loading branch information
1 parent
8211356
commit 8b3bc8b
Showing
13 changed files
with
142 additions
and
116 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
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,25 @@ | ||
extension COMError { | ||
/// Wraps a Swift Error object into an `IErrorInfo` to preserve it across COM boundaries. | ||
internal final class SwiftErrorInfo: COMPrimaryExport<IErrorInfoProjection>, IErrorInfoProtocol { | ||
public let error: Error | ||
|
||
public init(error: Error) { | ||
self.error = error | ||
} | ||
|
||
public func toABI(setErrorInfo: Bool = true) -> HResult.Value { | ||
if setErrorInfo { try? COMError.setErrorInfo(self) } | ||
return hresult.value | ||
} | ||
|
||
public var hresult: HResult { (self.error as? ErrorWithHResult)?.hresult ?? HResult.fail } | ||
public var message: String { String(describing: error) } | ||
|
||
// IErrorInfo | ||
public var guid: GUID { get throws { throw COMError.fail } } | ||
public var source: String? { get throws { throw COMError.fail } } | ||
public var description: String? { self.message } | ||
public var helpFile: String? { get throws { throw COMError.fail } } | ||
public var helpContext: UInt32 { get throws { throw COMError.fail } } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
Support/Sources/WindowsRuntime/ProjectedTypes/Core/IActivationFactory.swift
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
52 changes: 52 additions & 0 deletions
52
Support/Sources/WindowsRuntime/ProjectedTypes/Core/ILanguageExceptionErrorInfo.swift
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,52 @@ | ||
import COM | ||
|
||
public typealias ILanguageExceptionErrorInfo = any ILanguageExceptionErrorInfoProtocol | ||
public protocol ILanguageExceptionErrorInfoProtocol: IUnknownProtocol { | ||
var languageException: IUnknown { get throws } | ||
} | ||
|
||
import WindowsRuntime_ABI | ||
|
||
public enum ILanguageExceptionErrorInfoProjection: COMTwoWayProjection { | ||
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_ILanguageExceptionErrorInfo | ||
public typealias SwiftObject = ILanguageExceptionErrorInfo | ||
|
||
public static var interfaceID: COMInterfaceID { uuidof(ABIStruct.self) } | ||
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) } | ||
|
||
public static func _wrap(_ reference: consuming ABIReference) -> SwiftObject { | ||
Import(_wrapping: reference) | ||
} | ||
|
||
public static func toCOM(_ object: SwiftObject) throws -> ABIReference { | ||
try Import.toCOM(object) | ||
} | ||
|
||
private final class Import: COMImport<ILanguageExceptionErrorInfoProjection>, ILanguageExceptionErrorInfoProtocol { | ||
var languageException: IUnknown { | ||
get throws { try NullResult.unwrap(_interop.getLanguageException()) } | ||
} | ||
} | ||
|
||
private static var virtualTable: WindowsRuntime_ABI.SWRT_ILanguageExceptionErrorInfo_VirtualTable = .init( | ||
QueryInterface: { IUnknownVirtualTable.QueryInterface($0, $1, $2) }, | ||
AddRef: { IUnknownVirtualTable.AddRef($0) }, | ||
Release: { IUnknownVirtualTable.Release($0) }, | ||
GetLanguageException: { this, languageException in _implement(this) { this in | ||
guard let languageException else { throw COMError.fail } | ||
languageException.pointee = try IUnknownProjection.toABI(this.languageException) | ||
} }) | ||
} | ||
|
||
public func uuidof(_: WindowsRuntime_ABI.SWRT_ILanguageExceptionErrorInfo.Type) -> COMInterfaceID { | ||
.init(0x04a2dbf3, 0xdf83, 0x116c, 0x0946, 0x0812abf6e07d) | ||
} | ||
|
||
extension COMInterop where ABIStruct == WindowsRuntime_ABI.SWRT_ILanguageExceptionErrorInfo { | ||
public func getLanguageException() throws -> IUnknown { | ||
var result: IUnknownPointer? = nil // IUnknownProjection.abiDefaultValue (compiler bug?) | ||
defer { IUnknownProjection.release(&result) } | ||
try COMError.fromABI(this.pointee.VirtualTable.pointee.GetLanguageException(this, &result)) | ||
return try NullResult.unwrap(IUnknownProjection.toSwift(consuming: &result)) | ||
} | ||
} |
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
4 changes: 3 additions & 1 deletion
4
Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReference.swift
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
4 changes: 3 additions & 1 deletion
4
Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReferenceSource.swift
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
36 changes: 0 additions & 36 deletions
36
Support/Sources/WindowsRuntime/SwiftRestrictedErrorInfo.swift
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
Support/Sources/WindowsRuntime/WinRTError+LanguageException.swift
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,10 @@ | ||
extension WinRTError { | ||
/// Wraps a Swift Error object so it can be associated with an `IRestrictedErrorInfo`. | ||
internal final class LanguageException: COMPrimaryExport<IUnknownProjection> { | ||
public let error: Error | ||
|
||
public init(error: Error) { | ||
self.error = error | ||
} | ||
} | ||
} |
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