Skip to content

Commit

Permalink
capture the language exception (#181)
Browse files Browse the repository at this point in the history
* capture the language exception

* check in generated code
  • Loading branch information
stevenbrix authored Aug 15, 2024
1 parent b0cd253 commit a73deec
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 263 deletions.
24 changes: 21 additions & 3 deletions swiftwinrt/Resources/Support/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,26 @@ public struct Error : Swift.Error, CustomStringConvertible {
self.description = getErrorDescription(expecting: hr) ?? hrToString(hr)
self.hr = hr
}
}
}

public func failWith(err: HRESULT) -> HRESULT {
return err
public func failWith(hr: HRESULT) -> HRESULT {
return hr
}

public func failWith(error: Swift.Error) -> HRESULT {
var hresult: HRESULT = E_FAIL
let message = error.localizedDescription
if let winrtError = error as? Error {
hresult = winrtError.hr
}

do {
try message.withHStringRef {
_ = RoOriginateLanguageException(hresult, $0, nil)
}
} catch {
_ = RoOriginateLanguageException(hresult, nil, nil)
}

return hresult
}
4 changes: 2 additions & 2 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ override % func _getABI<T>() -> UnsafeMutablePointer<T>? {

static void write_not_implementable_vtable_method(writer& w, function_def const& sig)
{
w.write("%: { _, % in return failWith(err: E_NOTIMPL) }", get_abi_name(sig), bind([&](writer& w) {
w.write("%: { _, % in return failWith(hr: E_NOTIMPL) }", get_abi_name(sig), bind([&](writer& w) {
separator s{ w };
for (auto& param : sig.params)
{
Expand Down Expand Up @@ -2877,7 +2877,7 @@ override % func _getABI<T>() -> UnsafeMutablePointer<T>? {
w.write("return S_OK\n");
}
if (needs_try_catch) {
w.write("} catch { return failWith(err: E_FAIL) } \n");
w.write("} catch { return failWith(error: error) } \n");
w.m_indent -= 1;
}
w.write("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public enum __ABI_Windows_Foundation {
guard let __unwrapped__instance = IAsyncActionWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance.getResults()
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)

Expand Down Expand Up @@ -289,15 +289,15 @@ public enum __ABI_Windows_Foundation {
guard let __unwrapped__instance = IAsyncInfoWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance.cancel()
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

Close: {
do {
guard let __unwrapped__instance = IAsyncInfoWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance.close()
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)

Expand Down Expand Up @@ -346,7 +346,7 @@ public enum __ABI_Windows_Foundation {
guard let __unwrapped__instance = IClosableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance.close()
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)

Expand Down Expand Up @@ -428,7 +428,7 @@ public enum __ABI_Windows_Foundation {
let referenceWrapper = __ABI_Windows_Foundation.IMemoryBufferReferenceWrapper(reference)
referenceWrapper?.copyTo($1)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)

Expand Down Expand Up @@ -743,7 +743,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getUInt8()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetInt16: {
Expand All @@ -752,7 +752,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getInt16()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetUInt16: {
Expand All @@ -761,7 +761,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getUInt16()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetInt32: {
Expand All @@ -770,7 +770,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getInt32()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetUInt32: {
Expand All @@ -779,7 +779,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getUInt32()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetInt64: {
Expand All @@ -788,7 +788,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getInt64()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetUInt64: {
Expand All @@ -797,7 +797,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getUInt64()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetSingle: {
Expand All @@ -806,7 +806,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getSingle()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetDouble: {
Expand All @@ -815,7 +815,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getDouble()
$1?.initialize(to: value)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetChar16: {
Expand All @@ -824,7 +824,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getChar16()
$1?.initialize(to: .init(from: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetBoolean: {
Expand All @@ -833,7 +833,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getBoolean()
$1?.initialize(to: .init(from: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetString: {
Expand All @@ -842,7 +842,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getString()
$1?.initialize(to: try! HString(value).detach())
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetGuid: {
Expand All @@ -851,7 +851,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getGuid()
$1?.initialize(to: .init(from: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetDateTime: {
Expand All @@ -860,7 +860,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getDateTime()
$1?.initialize(to: .from(swift: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetTimeSpan: {
Expand All @@ -869,7 +869,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getTimeSpan()
$1?.initialize(to: .from(swift: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetPoint: {
Expand All @@ -878,7 +878,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getPoint()
$1?.initialize(to: .from(swift: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetSize: {
Expand All @@ -887,7 +887,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getSize()
$1?.initialize(to: .from(swift: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetRect: {
Expand All @@ -896,46 +896,46 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.getRect()
$1?.initialize(to: .from(swift: value))
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
},

GetUInt8Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetUInt8Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetInt16Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetInt16Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetUInt16Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetUInt16Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetInt32Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetInt32Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetUInt32Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetUInt32Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetInt64Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetInt64Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetUInt64Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetUInt64Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetSingleArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetSingleArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetDoubleArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetDoubleArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetChar16Array: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetChar16Array: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetBooleanArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetBooleanArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetStringArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetStringArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetInspectableArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetInspectableArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetGuidArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetGuidArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetDateTimeArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetDateTimeArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetTimeSpanArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetTimeSpanArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetPointArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetPointArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetSizeArray: { _, _, _ in return failWith(err: E_NOTIMPL) },
GetSizeArray: { _, _, _ in return failWith(hr: E_NOTIMPL) },

GetRectArray: { _, _, _ in return failWith(err: E_NOTIMPL) }
GetRectArray: { _, _, _ in return failWith(hr: E_NOTIMPL) }
)

public typealias IPropertyValueWrapper = InterfaceWrapperBase<__IMPL_Windows_Foundation.IPropertyValueBridge>
Expand Down Expand Up @@ -986,7 +986,7 @@ public enum __ABI_Windows_Foundation {
let value = try __unwrapped__instance.toString()
$1?.initialize(to: try! HString(value).detach())
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)

Expand Down Expand Up @@ -1353,7 +1353,7 @@ extension __ABI_Windows_Foundation {
let asyncStatus: test_component.AsyncStatus = $2
try __unwrapped__instance(asyncInfo, asyncStatus)
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)
}
Expand Down Expand Up @@ -1388,7 +1388,7 @@ extension __ABI_Windows_Foundation {
guard let __unwrapped__instance = DeferralCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance()
return S_OK
} catch { return failWith(err: E_FAIL) }
} catch { return failWith(error: error) }
}
)
}
Expand Down
Loading

0 comments on commit a73deec

Please sign in to comment.