Skip to content

Commit

Permalink
Finished ILanguageExceptionErrorInfo impl and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Sep 4, 2024
1 parent a9d59d3 commit a2cfb2d
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
2 changes: 0 additions & 2 deletions InteropTests/Tests/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class ErrorTests: WinRTTestCase {
}

func testSwiftErrorPreserved() throws {
try XCTSkipIf(true, "TODO(#248): Fix preserving Swift error objects across the WinRT boundary.")

struct SwiftError: Error {}
do {
try Errors.call { throw SwiftError() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import COM
import WindowsRuntime_ABI

public typealias IActivationFactory = any IActivationFactoryProtocol
public protocol IActivationFactoryProtocol: IInspectableProtocol {
func activateInstance() throws -> IInspectable
}

import WindowsRuntime_ABI

public enum IActivationFactoryProjection: InterfaceProjection {
public typealias SwiftObject = IActivationFactory
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_IActivationFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import COM
import WindowsRuntime_ABI

public typealias IInspectable = any IInspectableProtocol
public protocol IInspectableProtocol: IUnknownProtocol {
Expand All @@ -8,6 +7,8 @@ public protocol IInspectableProtocol: IUnknownProtocol {
func getTrustLevel() throws -> TrustLevel
}

import WindowsRuntime_ABI

public enum IInspectableProjection: InterfaceProjection {
public typealias SwiftObject = IInspectable
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_IInspectable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import WindowsRuntime_ABI
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
Expand All @@ -30,18 +32,19 @@ public enum ILanguageExceptionErrorInfoProjection: COMTwoWayProjection {
QueryInterface: { IUnknownVirtualTable.QueryInterface($0, $1, $2) },
AddRef: { IUnknownVirtualTable.AddRef($0) },
Release: { IUnknownVirtualTable.Release($0) },
GetLanguageException: { this, languageException in _implement(this) {
TODO;
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(TODO)
.init(0x04a2dbf3, 0xdf83, 0x116c, 0x0946, 0x0812abf6e07d)
}

extension COMInterop where ABIStruct == WindowsRuntime_ABI.SWRT_ILanguageExceptionErrorInfo {
public func getLanguageException() throws -> IUnkown {
var result: IUnknownProjection.abiDefaultValue
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))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WindowsRuntime_ABI
import COM

public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol
public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
Expand All @@ -10,6 +10,8 @@ public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
var reference: String? { get throws }
}

import WindowsRuntime_ABI

public enum IRestrictedErrorInfoProjection: COMTwoWayProjection {
public typealias SwiftObject = IRestrictedErrorInfo
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_IRestrictedErrorInfo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import WindowsRuntime_ABI
import COM

public typealias IWeakReference = any IWeakReferenceProtocol
public protocol IWeakReferenceProtocol: IUnknownProtocol {
func resolve() throws -> IInspectable?
}

import WindowsRuntime_ABI

public enum IWeakReferenceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReference
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_IWeakReference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import WindowsRuntime_ABI
import COM

public typealias IWeakReferenceSource = any IWeakReferenceSourceProtocol
public protocol IWeakReferenceSourceProtocol: IUnknownProtocol {
func getWeakReference() throws -> IWeakReference
}

import WindowsRuntime_ABI

public enum IWeakReferenceSourceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReferenceSource
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_IWeakReferenceSource
Expand Down
4 changes: 2 additions & 2 deletions Support/Sources/WindowsRuntime/WinRTError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible {
guard let restrictedErrorInfo else { throw WinRTError(hresult: hresult) }

if let languageExceptionErrorInfo = try? restrictedErrorInfo.queryInterface(ILanguageExceptionErrorInfoProjection.self) {
if let languageException = try? languageExceptionErrorInfo.getLanguageException() as? LanguageException {
if let languageException = try? languageExceptionErrorInfo.languageException as? LanguageException {
throw languageException.error
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ public struct WinRTError: COMErrorProtocol, CustomStringConvertible {
SWRT_RoFailFastWithErrorContext(hresult.value)
}

public static func createRestrictedErrorInfo(hresult: HResult, message: String?, languageException: IUnknown?) throws -> IRestrictedErrorInfo {
public static func createRestrictedErrorInfo(hresult: HResult, message: String?, languageException: IUnknown? = nil) throws -> IRestrictedErrorInfo {
// Ro*** APIs expect an IRestrictedErrorInfo created from Ro*** APIs, we can't implement our own.
// The only way we have to create one is RoOriginate***, which overwrites the current error info.
// So we need to artificially preserve it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include "SWRT/windows/oleauto.h"
#include "SWRT/windows/unknwn.h"

typedef struct SWRT_ILanguageExceptionErrorInfo {
struct SWRT_ILanguageExceptionErrorInfo_VirtualTable* VirtualTable;
} SWRT_ILanguageExceptionErrorInfo;

struct SWRT_ILanguageExceptionErrorInfo_VirtualTable {
SWRT_HResult (__stdcall *QueryInterface)(SWRT_ILanguageExceptionErrorInfo* _this, SWRT_Guid* riid, void** ppvObject);
uint32_t (__stdcall *AddRef)(SWRT_ILanguageExceptionErrorInfo* _this);
uint32_t (__stdcall *Release)(SWRT_ILanguageExceptionErrorInfo* _this);
SWRT_HResult (__stdcall *GetLanguageException)(SWRT_ILanguageExceptionErrorInfo* _this, SWRT_IUnknown** languageException);
};

typedef struct SWRT_IRestrictedErrorInfo {
struct SWRT_IRestrictedErrorInfo_VirtualTable* VirtualTable;
} SWRT_IRestrictedErrorInfo;
Expand Down

0 comments on commit a2cfb2d

Please sign in to comment.