Skip to content

Commit

Permalink
make delegates throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix committed Mar 11, 2024
1 parent 499c448 commit 4bf55fc
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 316 deletions.
12 changes: 6 additions & 6 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ vtable);
{
invoke_implementation = w.write_temp(R"(var result:%%
for handler in getInvocationList() {
result = handler(%)
result = try handler(%)
}
return result)",
bind<write_type>(*delegate_method.return_type->type, write_type_params::swift),
Expand All @@ -1245,13 +1245,13 @@ vtable);
else
{
invoke_implementation = w.write_temp(R"(for handler in getInvocationList() {
handler(%)
try handler(%)
})", bind<write_comma_param_names>(delegate_method.params));
}

assert(delegate_method.def);
w.write(R"(% extension EventSource where Handler == % {
%func invoke(%)% {
%func invoke(%) throws% {
%
}
}
Expand Down Expand Up @@ -1477,7 +1477,7 @@ vtable);
// parameters for the delegate signature to create the bridge. The swift compiler
// complains if the typealias isn't placed in a tuple
function_def delegate_method = type.functions[0];
w.write("public typealias % = (%) -> %\n",
w.write("public typealias % = (%) throws -> %\n",
type,
bind<write_comma_param_types>(delegate_method.params),
bind<write_delegate_return_type>(delegate_method));
Expand Down Expand Up @@ -1523,7 +1523,7 @@ vtable);
interface_info delegate{ &type };
delegate.is_default = true; // so that the _default name is used
auto indent_guard{ w.push_indent({3}) };
write_class_func_body(w, invoke_method, delegate, true);
write_class_func_body(w, invoke_method, delegate, false);
}));
}

Expand Down Expand Up @@ -2851,7 +2851,7 @@ override % func _getABI<T>() -> UnsafeMutablePointer<T>? {
func_call += w.write_temp(format, get_swift_name(method), bind<write_consume_args>(function));
}

bool needs_try_catch = !is_get_or_put && !is_winrt_generic_collection(type) && !is_delegate(type);
bool needs_try_catch = !is_get_or_put && !is_winrt_generic_collection(type);
w.write("%: {\n", func_name);
if (needs_try_catch) {
w.m_indent += 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/WinRTInterfaceImplementations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MyImplementableDelegate: IIAmImplementable {

var id: Foundation.UUID?
func fireEvent(_ data: String) {
_implementableEvent.invoke(data)
try! _implementableEvent.invoke(data)
}

private var object: Any?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,13 @@ extension __ABI_Windows_Foundation {
AddRef: { AsyncActionCompletedHandlerWrapper.addRef($0) },
Release: { AsyncActionCompletedHandlerWrapper.release($0) },
Invoke: {
guard let __unwrapped__instance = AsyncActionCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let asyncInfo: test_component.AnyIAsyncAction? = __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: ComPtr($1))
let asyncStatus: test_component.AsyncStatus = $2
__unwrapped__instance(asyncInfo, asyncStatus)
return S_OK
do {
guard let __unwrapped__instance = AsyncActionCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let asyncInfo: test_component.AnyIAsyncAction? = __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: ComPtr($1))
let asyncStatus: test_component.AsyncStatus = $2
try __unwrapped__instance(asyncInfo, asyncStatus)
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)
}
Expand Down Expand Up @@ -1382,9 +1384,11 @@ extension __ABI_Windows_Foundation {
AddRef: { DeferralCompletedHandlerWrapper.addRef($0) },
Release: { DeferralCompletedHandlerWrapper.release($0) },
Invoke: {
guard let __unwrapped__instance = DeferralCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
__unwrapped__instance()
return S_OK
do {
guard let __unwrapped__instance = DeferralCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance()
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public enum __IMPL_Windows_Foundation {
guard let abi = abi else { return nil }
let _default = SwiftABI(abi)
let handler: Handler = { (asyncInfo, asyncStatus) in
try! _default.InvokeImpl(asyncInfo, asyncStatus)
try _default.InvokeImpl(asyncInfo, asyncStatus)
}
return handler
}
Expand All @@ -419,7 +419,7 @@ public enum __IMPL_Windows_Foundation {
guard let abi = abi else { return nil }
let _default = SwiftABI(abi)
let handler: Handler = { () in
try! _default.InvokeImpl()
try _default.InvokeImpl()
}
return handler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp
}
}

public typealias MapChangedEventHandler<K,V> = (AnyIObservableMap<K, V>?, AnyIMapChangedEventArgs<K>?) -> ()
public typealias VectorChangedEventHandler<T> = (AnyIObservableVector<T>?, AnyIVectorChangedEventArgs?) -> ()
public typealias MapChangedEventHandler<K,V> = (AnyIObservableMap<K, V>?, AnyIMapChangedEventArgs<K>?) throws -> ()
public typealias VectorChangedEventHandler<T> = (AnyIObservableVector<T>?, AnyIVectorChangedEventArgs?) throws -> ()
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iiterable-1)
public protocol IIterable<T> : WinRTInterface {
associatedtype T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,13 @@ public final class WwwFormUrlDecoder : WinRTClass, IIterable, IVectorView {
}
}

public typealias AsyncActionCompletedHandler = (AnyIAsyncAction?, AsyncStatus) -> ()
public typealias AsyncOperationCompletedHandler<TResult> = (AnyIAsyncOperation<TResult>?, AsyncStatus) -> ()
public typealias AsyncOperationProgressHandler<TResult,TProgress> = (AnyIAsyncOperationWithProgress<TResult, TProgress>?, TProgress) -> ()
public typealias AsyncOperationWithProgressCompletedHandler<TResult,TProgress> = (AnyIAsyncOperationWithProgress<TResult, TProgress>?, AsyncStatus) -> ()
public typealias DeferralCompletedHandler = () -> ()
public typealias EventHandler<T> = (Any?, T) -> ()
public typealias TypedEventHandler<TSender,TResult> = (TSender, TResult) -> ()
public typealias AsyncActionCompletedHandler = (AnyIAsyncAction?, AsyncStatus) throws -> ()
public typealias AsyncOperationCompletedHandler<TResult> = (AnyIAsyncOperation<TResult>?, AsyncStatus) throws -> ()
public typealias AsyncOperationProgressHandler<TResult,TProgress> = (AnyIAsyncOperationWithProgress<TResult, TProgress>?, TProgress) throws -> ()
public typealias AsyncOperationWithProgressCompletedHandler<TResult,TProgress> = (AnyIAsyncOperationWithProgress<TResult, TProgress>?, AsyncStatus) throws -> ()
public typealias DeferralCompletedHandler = () throws -> ()
public typealias EventHandler<T> = (Any?, T) throws -> ()
public typealias TypedEventHandler<TSender,TResult> = (TSender, TResult) throws -> ()
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.datetime)
public struct DateTime: Hashable, Codable {
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.datetime.universaltime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,10 +1899,12 @@ extension __ABI_Windows_Storage {
AddRef: { StreamedFileDataRequestedHandlerWrapper.addRef($0) },
Release: { StreamedFileDataRequestedHandlerWrapper.release($0) },
Invoke: {
guard let __unwrapped__instance = StreamedFileDataRequestedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let stream: test_component.StreamedFileDataRequest? = .from(abi: ComPtr($1))
__unwrapped__instance(stream)
return S_OK
do {
guard let __unwrapped__instance = StreamedFileDataRequestedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let stream: test_component.StreamedFileDataRequest? = .from(abi: ComPtr($1))
try __unwrapped__instance(stream)
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ public enum __IMPL_Windows_Storage {
guard let abi = abi else { return nil }
let _default = SwiftABI(abi)
let handler: Handler = { (stream) in
try! _default.InvokeImpl(stream)
try _default.InvokeImpl(stream)
}
return handler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ public final class StreamedFileDataRequest : WinRTClass, test_component.IClosabl
}
}

public typealias StreamedFileDataRequestedHandler = (StreamedFileDataRequest?) -> ()
public typealias StreamedFileDataRequestedHandler = (StreamedFileDataRequest?) throws -> ()
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.storage.istoragefile)
public protocol IStorageFile : IStorageItem, test_component.IRandomAccessStreamReference, test_component.IInputStreamReference {
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.storage.istoragefile.openasync)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2708,10 +2708,12 @@ extension __ABI_test_component {
AddRef: { ObjectHandlerWrapper.addRef($0) },
Release: { ObjectHandlerWrapper.release($0) },
Invoke: {
guard let __unwrapped__instance = ObjectHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let item: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1))
__unwrapped__instance(item)
return S_OK
do {
guard let __unwrapped__instance = ObjectHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let item: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1))
try __unwrapped__instance(item)
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)
}
Expand Down Expand Up @@ -2742,9 +2744,11 @@ extension __ABI_test_component {
AddRef: { VoidToVoidDelegateWrapper.addRef($0) },
Release: { VoidToVoidDelegateWrapper.release($0) },
Invoke: {
guard let __unwrapped__instance = VoidToVoidDelegateWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
__unwrapped__instance()
return S_OK
do {
guard let __unwrapped__instance = VoidToVoidDelegateWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
try __unwrapped__instance()
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)
}
Expand Down
Loading

0 comments on commit 4bf55fc

Please sign in to comment.