diff --git a/Package.swift b/Package.swift index 0f0155de..594e7c3e 100644 --- a/Package.swift +++ b/Package.swift @@ -37,7 +37,7 @@ let package = Package( path: "Support/Sources/WindowsRuntime", exclude: [ "CMakeLists.txt", - "ProjectedTypes/Core/Readme.md", + "ProjectedTypes/Readme.md", "ProjectedTypes/WindowsFoundation/Readme.md", ]), .testTarget( diff --git a/Support/Sources/COM/ABIProjection/ABIProjection.swift b/Support/Sources/COM/ABIProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/ABIProjection.swift rename to Support/Sources/COM/ABIProjection.swift diff --git a/Support/Sources/COM/COMError+createErrorInfo.swift b/Support/Sources/COM/COMError+createErrorInfo.swift new file mode 100644 index 00000000..2df45585 --- /dev/null +++ b/Support/Sources/COM/COMError+createErrorInfo.swift @@ -0,0 +1,23 @@ +import COM_ABI + +extension COMError { + /// Creates an instance of `ICreateErrorInfo`. + public static func createErrorInfo() throws -> ICreateErrorInfo { + var createErrorInfo = ICreateErrorInfoProjection.abiDefaultValue + defer { ICreateErrorInfoProjection.release(&createErrorInfo) } + try COMError.fromABI(SWRT_CreateErrorInfo(&createErrorInfo)) + return try NullResult.unwrap(ICreateErrorInfoProjection.toSwift(createErrorInfo)) + } + + /// Creates an instance of `IErrorInfo` with prepopulated fields. + public static func createErrorInfo(guid: GUID? = nil, source: String? = nil, description: String?, + helpFile: String? = nil, helpContext: UInt32? = nil) throws -> IErrorInfo { + let errorInfo = try createErrorInfo() + if let guid { try errorInfo.setGUID(guid) } + if let source { try errorInfo.setSource(source) } + if let description { try errorInfo.setDescription(description) } + if let helpFile { try errorInfo.setHelpFile(helpFile) } + if let helpContext { try errorInfo.setHelpContext(helpContext) } + return try errorInfo.queryInterface(IErrorInfoProjection.self) + } +} \ No newline at end of file diff --git a/Support/Sources/COM/COMError.swift b/Support/Sources/COM/COMError.swift index 5a001790..8c69a676 100644 --- a/Support/Sources/COM/COMError.swift +++ b/Support/Sources/COM/COMError.swift @@ -38,7 +38,7 @@ public struct COMError: COMErrorProtocol, CustomStringConvertible { } public init(hresult: HResult, description: String?) { - self.init(hresult: hresult, errorInfo: description.flatMap { try? createErrorInfo(description: $0) }) + self.init(hresult: hresult, errorInfo: description.flatMap { try? Self.createErrorInfo(description: $0) }) } public var description: String { diff --git a/Support/Sources/COM/BStrProjection.swift b/Support/Sources/COM/ProjectedTypes/BStrProjection.swift similarity index 100% rename from Support/Sources/COM/BStrProjection.swift rename to Support/Sources/COM/ProjectedTypes/BStrProjection.swift diff --git a/Support/Sources/COM/ABIProjection/BoolProjection.swift b/Support/Sources/COM/ProjectedTypes/BoolProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/BoolProjection.swift rename to Support/Sources/COM/ProjectedTypes/BoolProjection.swift diff --git a/Support/Sources/COM/COMArray.swift b/Support/Sources/COM/ProjectedTypes/COMArray.swift similarity index 100% rename from Support/Sources/COM/COMArray.swift rename to Support/Sources/COM/ProjectedTypes/COMArray.swift diff --git a/Support/Sources/COM/ABIProjection/EnumProjection.swift b/Support/Sources/COM/ProjectedTypes/EnumProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/EnumProjection.swift rename to Support/Sources/COM/ProjectedTypes/EnumProjection.swift diff --git a/Support/Sources/COM/GUID.swift b/Support/Sources/COM/ProjectedTypes/GUID.swift similarity index 100% rename from Support/Sources/COM/GUID.swift rename to Support/Sources/COM/ProjectedTypes/GUID.swift diff --git a/Support/Sources/COM/HResult+getMessage.swift b/Support/Sources/COM/ProjectedTypes/HResult+getMessage.swift similarity index 100% rename from Support/Sources/COM/HResult+getMessage.swift rename to Support/Sources/COM/ProjectedTypes/HResult+getMessage.swift diff --git a/Support/Sources/COM/HResult.swift b/Support/Sources/COM/ProjectedTypes/HResult.swift similarity index 100% rename from Support/Sources/COM/HResult.swift rename to Support/Sources/COM/ProjectedTypes/HResult.swift diff --git a/Support/Sources/COM/HResultProjection.swift b/Support/Sources/COM/ProjectedTypes/HResultProjection.swift similarity index 100% rename from Support/Sources/COM/HResultProjection.swift rename to Support/Sources/COM/ProjectedTypes/HResultProjection.swift diff --git a/Support/Sources/COM/IAgileObject.swift b/Support/Sources/COM/ProjectedTypes/IAgileObject.swift similarity index 100% rename from Support/Sources/COM/IAgileObject.swift rename to Support/Sources/COM/ProjectedTypes/IAgileObject.swift diff --git a/Support/Sources/COM/ICreateErrorInfo.swift b/Support/Sources/COM/ProjectedTypes/ICreateErrorInfo.swift similarity index 100% rename from Support/Sources/COM/ICreateErrorInfo.swift rename to Support/Sources/COM/ProjectedTypes/ICreateErrorInfo.swift diff --git a/Support/Sources/COM/IErrorInfo.swift b/Support/Sources/COM/ProjectedTypes/IErrorInfo.swift similarity index 100% rename from Support/Sources/COM/IErrorInfo.swift rename to Support/Sources/COM/ProjectedTypes/IErrorInfo.swift diff --git a/Support/Sources/COM/IUnknown.swift b/Support/Sources/COM/ProjectedTypes/IUnknown.swift similarity index 100% rename from Support/Sources/COM/IUnknown.swift rename to Support/Sources/COM/ProjectedTypes/IUnknown.swift diff --git a/Support/Sources/COM/ABIProjection/IntegerEnumProjection.swift b/Support/Sources/COM/ProjectedTypes/IntegerEnumProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/IntegerEnumProjection.swift rename to Support/Sources/COM/ProjectedTypes/IntegerEnumProjection.swift diff --git a/Support/Sources/COM/ABIProjection/NumericProjection.swift b/Support/Sources/COM/ProjectedTypes/NumericProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/NumericProjection.swift rename to Support/Sources/COM/ProjectedTypes/NumericProjection.swift diff --git a/Support/Sources/COM/ABIProjection/WideCharProjection.swift b/Support/Sources/COM/ProjectedTypes/WideCharProjection.swift similarity index 100% rename from Support/Sources/COM/ABIProjection/WideCharProjection.swift rename to Support/Sources/COM/ProjectedTypes/WideCharProjection.swift diff --git a/Support/Sources/COM/createErrorInfo.swift b/Support/Sources/COM/createErrorInfo.swift deleted file mode 100644 index 245d6f60..00000000 --- a/Support/Sources/COM/createErrorInfo.swift +++ /dev/null @@ -1,21 +0,0 @@ -import COM_ABI - -/// Creates an instance of `ICreateErrorInfo`. -public func createErrorInfo() throws -> ICreateErrorInfo { - var createErrorInfo = ICreateErrorInfoProjection.abiDefaultValue - defer { ICreateErrorInfoProjection.release(&createErrorInfo) } - try COMError.fromABI(SWRT_CreateErrorInfo(&createErrorInfo)) - return try NullResult.unwrap(ICreateErrorInfoProjection.toSwift(createErrorInfo)) -} - -/// Creates an instance of `IErrorInfo` with prepopulated fields. -public func createErrorInfo(guid: GUID? = nil, source: String? = nil, description: String?, - helpFile: String? = nil, helpContext: UInt32? = nil) throws -> IErrorInfo { - let errorInfo = try createErrorInfo() - if let guid { try errorInfo.setGUID(guid) } - if let source { try errorInfo.setSource(source) } - if let description { try errorInfo.setDescription(description) } - if let helpFile { try errorInfo.setHelpFile(helpFile) } - if let helpContext { try errorInfo.setHelpContext(helpContext) } - return try errorInfo.queryInterface(IErrorInfoProjection.self) -} \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/Infra/ActivationFactoryResolver.swift b/Support/Sources/WindowsRuntime/ActivationFactoryResolver.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/ActivationFactoryResolver.swift rename to Support/Sources/WindowsRuntime/ActivationFactoryResolver.swift diff --git a/Support/Sources/WindowsRuntime/Infra/ComposableClass.swift b/Support/Sources/WindowsRuntime/ComposableClass.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/ComposableClass.swift rename to Support/Sources/WindowsRuntime/ComposableClass.swift diff --git a/Support/Sources/WindowsRuntime/Infra/ExportedDelegate.swift b/Support/Sources/WindowsRuntime/ExportedDelegate.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/ExportedDelegate.swift rename to Support/Sources/WindowsRuntime/ExportedDelegate.swift diff --git a/Support/Sources/WindowsRuntime/Infra/IInspectableVirtualTable.swift b/Support/Sources/WindowsRuntime/IInspectableVirtualTable.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/IInspectableVirtualTable.swift rename to Support/Sources/WindowsRuntime/IInspectableVirtualTable.swift diff --git a/Support/Sources/WindowsRuntime/ArrayProjection.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/ArrayProjection.swift similarity index 97% rename from Support/Sources/WindowsRuntime/ArrayProjection.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/ArrayProjection.swift index ce659f4d..85d8bc35 100644 --- a/Support/Sources/WindowsRuntime/ArrayProjection.swift +++ b/Support/Sources/WindowsRuntime/ProjectedTypes/ArrayProjection.swift @@ -1,5 +1,6 @@ import COM +/// Projects a WinRT array to a Swift array. public enum ArrayProjection: ABIProjection { // WinRT does not have a distinct representation for null and empty public typealias SwiftValue = [ElementProjection.SwiftValue] diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/Char16.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/Char16.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/Char16.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/Char16.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo+details.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo+details.swift deleted file mode 100644 index 0be27e7f..00000000 --- a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo+details.swift +++ /dev/null @@ -1,20 +0,0 @@ -public struct RestrictedErrorInfoDetails { - public var description: String? = nil - public var error: HResult = .ok - public var restrictedDescription: String? = nil - public var capabilitySid: String? = nil - - public init() {} -} - -extension IRestrictedErrorInfoProtocol { - public var details: RestrictedErrorInfoDetails { get throws { - var details = RestrictedErrorInfoDetails() - try getErrorDetails( - description: &details.description, - error: &details.error, - restrictedDescription: &details.restrictedDescription, - capabilitySid: &details.capabilitySid) - return details - } } -} \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/Readme.md b/Support/Sources/WindowsRuntime/ProjectedTypes/Core/Readme.md deleted file mode 100644 index ff714b35..00000000 --- a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/Readme.md +++ /dev/null @@ -1 +0,0 @@ -Defines core WinRT types which only have definitions in C++ headers, not in Windows Metadata files. \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/EventRegistrationToken.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/EventRegistrationToken.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/EventRegistrationToken.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/EventRegistrationToken.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/HString.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/HString.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/HString.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/HString.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IActivationFactory.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IActivationFactory.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IActivationFactory.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IActivationFactory.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IBufferByteAccess.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IBufferByteAccess.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IBufferByteAccess.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IBufferByteAccess.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IInspectable.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IInspectable.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IInspectable.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IInspectable.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/ILanguageExceptionErrorInfo.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/ILanguageExceptionErrorInfo.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/ILanguageExceptionErrorInfo.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/ILanguageExceptionErrorInfo.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/ILanguageExceptionErrorInfo2.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/ILanguageExceptionErrorInfo2.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/ILanguageExceptionErrorInfo2.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/ILanguageExceptionErrorInfo2.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IMemoryBufferByteAccess.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IMemoryBufferByteAccess.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IMemoryBufferByteAccess.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IMemoryBufferByteAccess.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IRestrictedErrorInfo.swift similarity index 74% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IRestrictedErrorInfo.swift index 9e2d9539..daaa19f9 100644 --- a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IRestrictedErrorInfo.swift +++ b/Support/Sources/WindowsRuntime/ProjectedTypes/IRestrictedErrorInfo.swift @@ -3,13 +3,26 @@ import COM public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol { func getErrorDetails( - description: inout String?, - error: inout HResult, - restrictedDescription: inout String?, - capabilitySid: inout String?) throws + _ description: inout String?, + _ error: inout HResult, + _ restrictedDescription: inout String?, + _ capabilitySid: inout String?) throws var reference: String? { get throws } } +extension IRestrictedErrorInfoProtocol { + var errorDetails: (description: String?, error: HResult, restrictedDescription: String?, capabilitySid: String?) { + get throws { + var description: String? = nil + var error: HResult = .fail + var restrictedDescription: String? = nil + var capabilitySid: String? = nil + try getErrorDetails(&description, &error, &restrictedDescription, &capabilitySid) + return (description, error, restrictedDescription, capabilitySid) + } + } +} + import WindowsRuntime_ABI public enum IRestrictedErrorInfoProjection: COMProjection { @@ -28,10 +41,10 @@ public enum IRestrictedErrorInfoProjection: COMProjection { private final class Import: COMImport, IRestrictedErrorInfoProtocol { func getErrorDetails( - description: inout String?, - error: inout HResult, - restrictedDescription: inout String?, - capabilitySid: inout String?) throws { + _ description: inout String?, + _ error: inout HResult, + _ restrictedDescription: inout String?, + _ capabilitySid: inout String?) throws { try _interop.getErrorDetails(&description, &error, &restrictedDescription, &capabilitySid) } diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReference.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IWeakReference.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReference.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IWeakReference.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReferenceSource.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/IWeakReferenceSource.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/IWeakReferenceSource.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/IWeakReferenceSource.swift diff --git a/Support/Sources/WindowsRuntime/PrimitiveProjections.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/PrimitiveProjections.swift similarity index 100% rename from Support/Sources/WindowsRuntime/PrimitiveProjections.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/PrimitiveProjections.swift diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Readme.md b/Support/Sources/WindowsRuntime/ProjectedTypes/Readme.md new file mode 100644 index 00000000..0d1fbe70 --- /dev/null +++ b/Support/Sources/WindowsRuntime/ProjectedTypes/Readme.md @@ -0,0 +1 @@ +Defines Swift projections for core WinRT types, and COM types that support the Windows Runtime. Those types are defined in native idl or C++ headers rather than Windows Metadata files and cannot be generated. \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/ProjectedTypes/Core/TrustLevel.swift b/Support/Sources/WindowsRuntime/ProjectedTypes/TrustLevel.swift similarity index 100% rename from Support/Sources/WindowsRuntime/ProjectedTypes/Core/TrustLevel.swift rename to Support/Sources/WindowsRuntime/ProjectedTypes/TrustLevel.swift diff --git a/Support/Sources/WindowsRuntime/Infra/ProjectionProtocols+extensions.swift b/Support/Sources/WindowsRuntime/ProjectionProtocols+extensions.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/ProjectionProtocols+extensions.swift rename to Support/Sources/WindowsRuntime/ProjectionProtocols+extensions.swift diff --git a/Support/Sources/WindowsRuntime/Infra/ProjectionProtocols.swift b/Support/Sources/WindowsRuntime/ProjectionProtocols.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/ProjectionProtocols.swift rename to Support/Sources/WindowsRuntime/ProjectionProtocols.swift diff --git a/Support/Sources/WindowsRuntime/Infra/SwiftWrapperFactory.swift b/Support/Sources/WindowsRuntime/SwiftWrapperFactory.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/SwiftWrapperFactory.swift rename to Support/Sources/WindowsRuntime/SwiftWrapperFactory.swift diff --git a/Support/Sources/WindowsRuntime/WinRTError.swift b/Support/Sources/WindowsRuntime/WinRTError.swift index 150643e0..03f71702 100644 --- a/Support/Sources/WindowsRuntime/WinRTError.swift +++ b/Support/Sources/WindowsRuntime/WinRTError.swift @@ -24,9 +24,13 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible { } public var description: String { - let details = (try? restrictedErrorInfo?.details) ?? RestrictedErrorInfoDetails() - // RestrictedDescription contains the value reported in RoOriginateError - return details.restrictedDescription ?? details.description ?? hresult.description + if let restrictedErrorInfo { + let errorDetails = try? restrictedErrorInfo.errorDetails + if let restrictedDescription = errorDetails?.restrictedDescription { return restrictedDescription } + if let description = errorDetails?.description { return description } + } + + return hresult.description } public func toABI(setErrorInfo: Bool = true) -> HResult.Value { @@ -41,20 +45,12 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible { guard hresult.isFailure else { return hresult } // Check for an associated IRestrictedErrorInfo - guard captureErrorInfo, let restrictedErrorInfo = try? Self.getRestrictedErrorInfo(matching: hresult) else { + guard captureErrorInfo, + let restrictedErrorInfo = try? Self.getRestrictedErrorInfo(matching: hresult), + (try? restrictedErrorInfo.errorDetails.error) == hresult else { throw WinRTError(hresult: hresult) } - // Ensure we didn't get a stale IRestrictedErrorInfo - var description: String? = nil - var error: HResult = .ok - var restrictedDescription: String? = nil - var capabilitySid: String? = nil - try? restrictedErrorInfo.getErrorDetails( - description: &description, error: &error, - restrictedDescription: &restrictedDescription, capabilitySid: &capabilitySid) - guard error == hresult else { throw WinRTError(hresult: hresult) } - // Append to the propagation context, if available. // See https://learn.microsoft.com/en-us/windows/win32/api/restrictederrorinfo/nf-restrictederrorinfo-ilanguageexceptionerrorinfo2-capturepropagationcontext if let languageExceptionErrorInfo = try? restrictedErrorInfo.queryInterface(ILanguageExceptionErrorInfoProjection.self) { diff --git a/Support/Sources/WindowsRuntime/Infra/WinRTExport.swift b/Support/Sources/WindowsRuntime/WinRTExport.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/WinRTExport.swift rename to Support/Sources/WindowsRuntime/WinRTExport.swift diff --git a/Support/Sources/WindowsRuntime/Infra/WinRTImport.swift b/Support/Sources/WindowsRuntime/WinRTImport.swift similarity index 100% rename from Support/Sources/WindowsRuntime/Infra/WinRTImport.swift rename to Support/Sources/WindowsRuntime/WinRTImport.swift