From 40bbc6abb86faf38b8d90d0c58b38fc1360e1528 Mon Sep 17 00:00:00 2001 From: Steve Kirbach Date: Tue, 17 Oct 2023 13:32:18 -0700 Subject: [PATCH 1/3] put params in backticks --- swiftwinrt/helpers.h | 14 ++-- swiftwinrt/type_writers.h | 2 +- .../Sources/CWinRT/include/test_component.h | 39 +++++++++ .../test_component/test_component+ABI.swift | 80 +++++++++++++++++++ .../test_component/test_component+Impl.swift | 37 +++++++++ .../test_component/test_component.swift | 16 ++++ tests/test_component/cpp/Keywords.idl | 3 + 7 files changed, 183 insertions(+), 8 deletions(-) diff --git a/swiftwinrt/helpers.h b/swiftwinrt/helpers.h index 89a25bcb..43254bb7 100644 --- a/swiftwinrt/helpers.h +++ b/swiftwinrt/helpers.h @@ -86,7 +86,7 @@ namespace swiftwinrt } } - private: + private: void construct(metadata_type const* type) { // Check if this is a typedef_base, otherwise we could fail trying to get the swift_abi_namespace @@ -437,7 +437,7 @@ namespace swiftwinrt auto class_name = get_attribute_value(attribute, 0).name; return type.get_cache().find_required(class_name); } - + inline TypeDef get_exclusive_to(typedef_base const& type) { return get_exclusive_to(type.type()); @@ -523,7 +523,7 @@ namespace swiftwinrt return name; } } - + inline std::string put_in_backticks_if_needed(std::string name) { // any lowercase swift keywords neet to be put in backticks static auto keyWords = std::set{ @@ -582,7 +582,7 @@ namespace swiftwinrt result[0] = tolower(result[0]); // One or two leading capitals: GetFoo -> getFoo / UInt32 -> uint32 - // 3+ leading capitals or mixed digits, keep the last one: + // 3+ leading capitals or mixed digits, keep the last one: // UIElement -> uiElement / HELLOWorld -> helloWorld / R8G8B8Alpha -> r8g8b8Alpha if (result.size() > 1 && isupper(result[1]) || isdigit(result[1])){ result[1] = tolower(result[1]); @@ -639,12 +639,12 @@ namespace swiftwinrt return to_camel_case(field.Name()); } - inline std::string_view get_swift_name(Param const& param) + inline std::string get_swift_name(Param const& param) { - return param.Name(); + return put_in_backticks_if_needed(std::string(param.Name())); } - inline std::string_view get_swift_name(function_param const& param) + inline std::string get_swift_name(function_param const& param) { return get_swift_name(param.def); } diff --git a/swiftwinrt/type_writers.h b/swiftwinrt/type_writers.h index acdd2101..09ea2aed 100644 --- a/swiftwinrt/type_writers.h +++ b/swiftwinrt/type_writers.h @@ -17,7 +17,7 @@ namespace swiftwinrt std::string get_full_swift_type_name(writer const&, TypeDef const& type); std::string get_full_swift_type_name(writer const&, const metadata_type* type); std::string get_swift_module(std::string_view const& ns); - std::string_view get_swift_name(function_param const&); + std::string get_swift_name(function_param const&); param_category get_category(const metadata_type*, TypeDef*); diff --git a/tests/test_component/Sources/CWinRT/include/test_component.h b/tests/test_component/Sources/CWinRT/include/test_component.h index 30ff4310..094b39b0 100644 --- a/tests/test_component/Sources/CWinRT/include/test_component.h +++ b/tests/test_component/Sources/CWinRT/include/test_component.h @@ -227,6 +227,12 @@ typedef interface __x_ABI_Ctest__component_CIVoidToVoidDelegate __x_ABI_Ctest__c #endif // ____x_ABI_Ctest__component_CInterfaceWithReturnDelegate_FWD_DEFINED__ +#ifndef ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__ +#define ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__ + typedef interface __x_ABI_Ctest__component_CWithKeyword __x_ABI_Ctest__component_CWithKeyword; + +#endif // ____x_ABI_Ctest__component_CWithKeyword_FWD_DEFINED__ + // Parameterized interface forward declarations (C) // Collection interface definitions @@ -3666,3 +3672,36 @@ struct __x_ABI_Ctest__component_CStructWithEnum EXTERN_C const IID IID___x_ABI_Ctest__component_CInterfaceWithReturnDelegate; #endif /* !defined(____x_ABI_Ctest__component_CInterfaceWithReturnDelegate_INTERFACE_DEFINED__) */ +#if !defined(____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__) + #define ____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__ + typedef struct __x_ABI_Ctest__component_CWithKeywordVtbl + { + BEGIN_INTERFACE + + HRESULT (STDMETHODCALLTYPE* QueryInterface)(__x_ABI_Ctest__component_CWithKeyword* This, + REFIID riid, + void** ppvObject); + ULONG (STDMETHODCALLTYPE* AddRef)(__x_ABI_Ctest__component_CWithKeyword* This); + ULONG (STDMETHODCALLTYPE* Release)(__x_ABI_Ctest__component_CWithKeyword* This); + HRESULT (STDMETHODCALLTYPE* GetIids)(__x_ABI_Ctest__component_CWithKeyword* This, + ULONG* iidCount, + IID** iids); + HRESULT (STDMETHODCALLTYPE* GetRuntimeClassName)(__x_ABI_Ctest__component_CWithKeyword* This, + HSTRING* className); + HRESULT (STDMETHODCALLTYPE* GetTrustLevel)(__x_ABI_Ctest__component_CWithKeyword* This, + TrustLevel* trustLevel); + HRESULT (STDMETHODCALLTYPE* WithExtension)(__x_ABI_Ctest__component_CWithKeyword* This, + HSTRING extension); + + END_INTERFACE + } __x_ABI_Ctest__component_CWithKeywordVtbl; + + interface __x_ABI_Ctest__component_CWithKeyword + { + CONST_VTBL struct __x_ABI_Ctest__component_CWithKeywordVtbl* lpVtbl; + }; + + + EXTERN_C const IID IID___x_ABI_Ctest__component_CWithKeyword; +#endif /* !defined(____x_ABI_Ctest__component_CWithKeyword_INTERFACE_DEFINED__) */ + diff --git a/tests/test_component/Sources/test_component/test_component+ABI.swift b/tests/test_component/Sources/test_component/test_component+ABI.swift index c75d5af2..91a010c2 100644 --- a/tests/test_component/Sources/test_component/test_component+ABI.swift +++ b/tests/test_component/Sources/test_component/test_component+ABI.swift @@ -138,6 +138,10 @@ private var IID___x_ABI_Ctest__component_CInterfaceWithReturnDelegate: test_comp .init(Data1: 0xB0EBC406, Data2: 0x17C0, Data3: 0x5703, Data4: ( 0xB9,0xC7,0x50,0xBE,0x67,0x5B,0xBC,0x95 ))// B0EBC406-17C0-5703-B9C7-50BE675BBC95 } +private var IID___x_ABI_Ctest__component_CWithKeyword: test_component.IID { + .init(Data1: 0xA703474B, Data2: 0x0941, Data3: 0x5409, Data4: ( 0xA8,0x3E,0xD7,0x70,0x48,0x91,0xAE,0x84 ))// A703474B-0941-5409-A83E-D7704891AE84 +} + private var IID___x_ABI_Ctest__component_CIObjectHandler: test_component.IID { .init(Data1: 0x5DD35752, Data2: 0x9800, Data3: 0x5961, Data4: ( 0x80,0xDE,0xFC,0x5E,0x20,0x9E,0x6E,0x2D ))// 5DD35752-9800-5961-80DE-FC5E209E6E2D } @@ -1929,6 +1933,82 @@ public enum __ABI_test_component { ) public typealias InterfaceWithReturnDelegateWrapper = InterfaceWrapperBase<__IMPL_test_component.InterfaceWithReturnDelegateImpl> + open class WithKeyword: test_component.IInspectable { + override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CWithKeyword } + + open func WithExtensionImpl(_ `extension`: HSTRING?) throws { + _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.WithExtension(pThis, `extension`)) + } + } + + } + + internal static var WithKeywordVTable: __x_ABI_Ctest__component_CWithKeywordVtbl = .init( + QueryInterface: { + guard let pUnk = $0, let riid = $1, let ppvObject = $2 else { return E_INVALIDARG } + ppvObject.pointee = nil + + switch riid.pointee { + case IUnknown.IID, IInspectable.IID, ISwiftImplemented.IID, IAgileObject.IID, WithKeywordWrapper.IID: + _ = pUnk.pointee.lpVtbl.pointee.AddRef(pUnk) + ppvObject.pointee = UnsafeMutableRawPointer(pUnk) + return S_OK + default: + guard let instance = WithKeywordWrapper.tryUnwrapFrom(raw: pUnk), + let iUnknownRef = instance.queryInterface(riid.pointee) else { return failWith(err: E_NOINTERFACE )} + ppvObject.pointee = UnsafeMutableRawPointer(iUnknownRef.ref) + return S_OK + + } + }, + + AddRef: { + guard let wrapper = WithKeywordWrapper.fromRaw($0) else { return 1 } + _ = wrapper.retain() + return ULONG(_getRetainCount(wrapper.takeUnretainedValue())) + }, + + Release: { + guard let wrapper = WithKeywordWrapper.fromRaw($0) else { return 1 } + return ULONG(_getRetainCount(wrapper.takeRetainedValue())) + }, + + GetIids: { + let size = MemoryLayout.size + let iids = CoTaskMemAlloc(UInt64(size) * 3).assumingMemoryBound(to: test_component.IID.self) + iids[0] = IUnknown.IID + iids[1] = IInspectable.IID + iids[2] = __ABI_test_component.WithKeywordWrapper.IID + $1!.pointee = 3 + $2!.pointee = iids + return S_OK + }, + + GetRuntimeClassName: { + _ = $0 + let hstring = try! HString("test_component.WithKeyword").detach() + $1!.pointee = hstring + return S_OK + }, + + GetTrustLevel: { + _ = $0 + $1!.pointee = TrustLevel(rawValue: 0) + return S_OK + }, + + WithExtension: { + do { + guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } + let `extension`: String = .init(from: $1) + try __unwrapped__instance.withExtension(`extension`) + return S_OK + } catch { return failWith(err: E_FAIL) } + } + ) + + public typealias WithKeywordWrapper = InterfaceWrapperBase<__IMPL_test_component.WithKeywordImpl> public class _ABI_NonBlittableBoolStruct { public var val: __x_ABI_Ctest__component_CNonBlittableBoolStruct = .init() public init() { } diff --git a/tests/test_component/Sources/test_component/test_component+Impl.swift b/tests/test_component/Sources/test_component/test_component+Impl.swift index 00e0e6eb..8babf470 100644 --- a/tests/test_component/Sources/test_component/test_component+Impl.swift +++ b/tests/test_component/Sources/test_component/test_component+Impl.swift @@ -269,6 +269,31 @@ public enum __IMPL_test_component { } + public class WithKeywordImpl : WithKeyword, WinRTAbiBridge { + public typealias CABI = __x_ABI_Ctest__component_CWithKeyword + public typealias SwiftABI = __ABI_test_component.WithKeyword + public typealias SwiftProjection = AnyWithKeyword + private (set) public var _default: SwiftABI + public var thisPtr: test_component.IInspectable { _default } + public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + guard let abi = abi else { return nil } + return WithKeywordImpl(abi) + } + public init(_ fromAbi: UnsafeMutablePointer) { + _default = SwiftABI(fromAbi) + } + + public static func makeAbi() -> CABI { + let vtblPtr = withUnsafeMutablePointer(to: &__ABI_test_component.WithKeywordVTable) { $0 } + return .init(lpVtbl: vtblPtr) + } + public func withExtension(_ `extension`: String) throws { + let _`extension` = try! HString(`extension`) + try _default.WithExtensionImpl(_`extension`.get()) + } + + } + public class ObjectHandlerImpl : WinRTDelegateBridge { public typealias Handler = ObjectHandler public typealias CABI = __x_ABI_Ctest__component_CIObjectHandler @@ -372,3 +397,15 @@ public class InterfaceWithReturnDelegate_MakeFromAbi : MakeFromAbi { } } +@_spi(__MakeFromAbi_DoNotImport) +public class WithKeyword_MakeFromAbi : MakeFromAbi { + public typealias CABI = __x_ABI_Ctest__component_CWithKeyword + public typealias SwiftABI = __ABI_test_component.WithKeyword + public typealias SwiftProjection = AnyWithKeyword + public static func from(abi: UnsafeMutableRawPointer?) -> SwiftProjection? { + guard let abi else { return nil } + let swiftAbi: SwiftABI = try! test_component.IInspectable(abi).QueryInterface() + return __IMPL_test_component.WithKeywordImpl(RawPointer(swiftAbi)!) + } +} + diff --git a/tests/test_component/Sources/test_component/test_component.swift b/tests/test_component/Sources/test_component/test_component.swift index d9a7fc10..035930c1 100644 --- a/tests/test_component/Sources/test_component/test_component.swift +++ b/tests/test_component/Sources/test_component/test_component.swift @@ -1869,6 +1869,22 @@ extension InterfaceWithReturnDelegate { } public typealias AnyInterfaceWithReturnDelegate = any InterfaceWithReturnDelegate +public protocol WithKeyword : WinRTInterface { + func withExtension(_ `extension`: String) throws +} + +extension WithKeyword { + public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + switch iid { + case __ABI_test_component.WithKeywordWrapper.IID: + let wrapper = __ABI_test_component.WithKeywordWrapper(self) + return wrapper!.queryInterface(iid) + default: return nil + } + } +} +public typealias AnyWithKeyword = any WithKeyword + extension test_component.Fruit { public static var banana : test_component.Fruit { __x_ABI_Ctest__component_CFruit_Banana diff --git a/tests/test_component/cpp/Keywords.idl b/tests/test_component/cpp/Keywords.idl index 40c97299..d929700d 100644 --- a/tests/test_component/cpp/Keywords.idl +++ b/tests/test_component/cpp/Keywords.idl @@ -50,4 +50,7 @@ namespace test_component While }; + interface WithKeyword { + void WithExtension(String extension); + } } \ No newline at end of file From 4469ee5602a6e7a7f29a0768b3b521dd7a09aa65 Mon Sep 17 00:00:00 2001 From: Steve Kirbach Date: Wed, 18 Oct 2023 12:10:21 -0700 Subject: [PATCH 2/3] wrap keyword param names in backticks --- swiftwinrt/code_writers.h | 168 +++++++++--------- swiftwinrt/helpers.h | 19 ++ .../Windows.Foundation+Impl.swift | 6 +- .../Windows.Foundation.Collections+Impl.swift | 2 +- .../Windows.Foundation.Collections.swift | 12 +- .../test_component/Windows.Foundation.swift | 2 +- .../test_component+Generics.swift | 34 ++-- .../test_component/test_component+Impl.swift | 9 +- .../test_component/test_component.swift | 54 +++--- 9 files changed, 164 insertions(+), 142 deletions(-) diff --git a/swiftwinrt/code_writers.h b/swiftwinrt/code_writers.h index 46248c8b..9c8e26d3 100644 --- a/swiftwinrt/code_writers.h +++ b/swiftwinrt/code_writers.h @@ -187,6 +187,7 @@ namespace swiftwinrt TypeDef signature_type; auto category = get_category(type, &signature_type); + auto local_name = local_swift_param_name(param_name); if (category == param_category::object_type) { if (is_out) throw std::exception("out parameters of reference types should not be converted directly to abi types"); @@ -197,14 +198,14 @@ namespace swiftwinrt } else { - w.write("_%", param_name); + w.write("%", local_name); } } else if (category == param_category::string_type) { if (!is_out) { - w.write("_%.get()", param_name); + w.write("%.get()", local_name); } else { @@ -222,11 +223,11 @@ namespace swiftwinrt { if (!is_out) { - w.write("_%.val", param_name); + w.write("%.val", local_name); } else { - w.write("_%.detach()", param_name); + w.write("%.detach()", local_name); } } } @@ -235,7 +236,7 @@ namespace swiftwinrt if (is_out) throw std::exception("out parameters of generic types should not be converted directly to abi types"); // When passing generics to the ABI we wrap them before making the // api call for easy passing to the ABI - w.write("_%", param_name); + w.write("%", local_name); } else if (is_type_blittable(category)) { @@ -253,10 +254,12 @@ namespace swiftwinrt separator s{ w }; for (auto& param: function.params) { + auto param_name = get_swift_name(param); + auto local_param_name = local_swift_param_name(param_name); s(); if (param.in()) { - write_convert_to_abi_arg(w, get_swift_name(param), param.type, false); + write_convert_to_abi_arg(w, param_name, param.type, false); } else { @@ -266,20 +269,20 @@ namespace swiftwinrt { if (is_blittable) { - w.write("&_%", get_swift_name(param)); + w.write("&%", local_param_name); } else { - w.write("&_%.val", get_swift_name(param)); + w.write("&%.val", local_param_name); } } else if (is_blittable) { - w.write("&%", get_swift_name(param)); + w.write("&%", param_name); } else { - w.write("&_%", get_swift_name(param)); + w.write("&%", local_param_name); } } } @@ -386,7 +389,7 @@ bind(function)); auto overrides_format = "internal typealias Composable = %.Composable\n\n"; w.write(overrides_format, get_full_swift_type_name(w, get_exclusive_to(type))); } - + auto abi_guard = w.push_abi_types(true); auto iid_format = "override public class var IID: %.IID { IID_% }\n\n"; @@ -449,7 +452,7 @@ bind(function)); try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) % } -} +} )"; auto generic_param = type.generic_params()[0]; w.add_depends(*generic_param); @@ -476,7 +479,7 @@ bind(function)); bind(*generic_param, projection_layer::c_abi), blittable ? "self = result" : "self.init(from: result)"); } - + } static void write_class_func_body(writer& w, function_def const& function, interface_info const& iface, bool is_noexcept); static void write_comma_param_names(writer& w, std::vector const& params); @@ -518,7 +521,7 @@ bind(function)); // Don't write generic interfaces defintions at the ABI layer, we need an actual // instantiation of the type in order to create vtables and actual implementations if (!can_write(w, type) || type.is_generic()) return; - + do_write_interface_abi(w, type, type.functions); if (!is_exclusive(type)) { @@ -544,7 +547,7 @@ typealias % = InterfaceWrapperBase<%> { write_delegate_wrapper(w, generic); } - + static void write_delegate_abi(writer& w, delegate_type const& type) { if (type.is_generic()) return; @@ -560,7 +563,7 @@ typealias % = InterfaceWrapperBase<%> w.write("}\n"); write_delegate_extension(w, type, type.functions[0]); } - + static void write_struct_abi(writer& w, struct_type const& type) { bool is_blittable = is_struct_blittable(type); @@ -670,7 +673,7 @@ typealias % = InterfaceWrapperBase<%> assert(!param.in()); assert(param.out()); w.write("var %: %%\n", - get_swift_name(param), + get_swift_name(param), bind(*param.type, write_type_params::swift), bind(*param.type, projection_layer::swift)); } @@ -687,7 +690,7 @@ typealias % = InterfaceWrapperBase<%> } auto return_type = signature.return_type.value().type; - auto return_param_name = signature.return_type.value().name; + auto return_param_name = put_in_backticks_if_needed(std::string(signature.return_type.value().name)); w.write("return %", bind(return_type, return_param_name)); } @@ -758,7 +761,7 @@ typealias % = InterfaceWrapperBase<%> // ... AddRef, Release, QI, ... // HRESULT (STDMETHODCALLTYPE * Foo)(__x_ABI_IMyInterface* pThis, int number) // } - // + // // struct __x_ABI_IMyInterface { // const __x_ABI_MyObjectVTable* lpVtbl; // } @@ -813,7 +816,7 @@ bind_impl_fullname(type)); static void write_property_value_impl(writer& w) { auto winrtInterfaceConformance = w.write_temp(R"( - public func queryInterface(_ iid: %.IID) -> IUnknownRef? { + public func queryInterface(_ iid: %.IID) -> IUnknownRef? { guard iid == __ABI_Windows_Foundation.IPropertyValueWrapper.IID else { return nil } guard let thisAsIPropValue = __ABI_Windows_Foundation.IPropertyValueWrapper(self) else { fatalError("creating non-nil wrapper shouldn't fail") } return thisAsIPropValue.queryInterface(iid) @@ -861,7 +864,7 @@ bind_impl_fullname(type)); } public var type: PropertyType { propertyType } - public var isNumericScalar: Bool { + public var isNumericScalar: Bool { switch propertyType { case .int16, .int32, .int64, .uint8, .uint16, .uint32, .uint64, .single, .double: return true default: return false @@ -883,7 +886,7 @@ bind_impl_fullname(type)); public func getBoolean() -> Bool { _value as! Bool } public func getString() -> String { _value as! String } public func getGuid() -> %.GUID { _value as! %.GUID } - public func getDateTime() -> DateTime { _value as! DateTime } + public func getDateTime() -> DateTime { _value as! DateTime } public func getTimeSpan() -> TimeSpan { _value as! TimeSpan } public func getPoint() -> Point { _value as! Point } public func getSize() -> Size { _value as! Size } @@ -912,7 +915,7 @@ bind_impl_fullname(type)); i+1 } -%func index(of: Element) -> Int? { +%func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -961,7 +964,7 @@ bind_impl_fullname(type)); if (info.overridable && is_class) { // when implementing default overrides, we want to call to the inner non-delegating IUnknown - // as this will get us to the inner object. otherwise we'll end up with a stack overflow + // as this will get us to the inner object. otherwise we'll end up with a stack overflow // because we'll be calling the same method on ourselves w.write("internal lazy var %: %.% = try! IUnknown(_inner!.borrow).QueryInterface()\n", get_swift_name(info), @@ -1155,7 +1158,7 @@ public static func makeAbi() -> CABI { handler(%) })", bind(delegate_method.params)); } - + assert(delegate_method.def); w.write(R"(% extension EventSource where Handler == % { %func invoke(%)% { @@ -1246,7 +1249,7 @@ public static func makeAbi() -> CABI { get_swift_name(event.def), event.type); // only write the eventsource extension for interfaces which could be implemented by a swift object - // not only does this result in less code generated, it also helps alleviate the issue where different + // not only does this result in less code generated, it also helps alleviate the issue where different // interfaces define an event with the same type. For that scenario, we cache the event type on the // writer if (!type.is_generic()) @@ -1276,7 +1279,7 @@ public static func makeAbi() -> CABI { // they don't need to write the queryInterface implementation themselves. We // know for a fact that we're only here in the scenario that a single WinRT // interface is implemented because if they implement multiple interfaces, they - // have to write the queryInterface implementation themselves. + // have to write the queryInterface implementation themselves. w.write("extension % {\n", typeName); w.write(" public func queryInterface(_ iid: %.IID) -> IUnknownRef? {\n", w.support); w.write(" switch iid {\n"); @@ -1389,7 +1392,7 @@ public static func makeAbi() -> CABI { auto return_type = w.write_temp("%", bind(invoke_method)); constexpr bool is_generic = std::is_same_v; auto access_level = is_generic ? "internal" : "public"; - + auto handlerType = w.write_temp("%", bind(type)); auto abi_guard = w.push_abi_types(is_generic); w.write(format, @@ -1456,7 +1459,7 @@ public static func makeAbi() -> CABI { bind_type_mangled(type)); w.write(" return.init(lpVtbl: vtblPtr)\n"); w.write("}\n\n"); - + interface_info info{ &type }; info.is_default = true; // mark as default so we use the name "_default" write_collection_protocol_conformance(w, info); @@ -1511,95 +1514,96 @@ public static func makeAbi() -> CABI { { TypeDef signature_type{}; auto category = get_category(param.type, &signature_type); + auto param_name = get_swift_name(param); + auto local_param_name = local_swift_param_name(param_name); if (param.in()) { if (category == param_category::string_type) { - w.write("let _% = try! HString(%)\n", - get_swift_name(param), - get_swift_name(param)); + w.write("let % = try! HString(%)\n", + local_param_name, + param_name); } else if (category == param_category::struct_type && !is_struct_blittable(signature_type)) { - w.write("let _% = %._ABI_%(from: %)\n", - get_swift_name(param), + w.write("let % = %._ABI_%(from: %)\n", + local_param_name, abi_namespace(signature_type), signature_type.TypeName(), - get_swift_name(param)); + param_name); } else if (is_reference_type(param.type) && !is_class(param.type)) { w.write("let %Wrapper = %(%)\n", - get_swift_name(param), + param_name, bind_wrapper_fullname(param.type), - get_swift_name(param)); - w.write("let _% = try! %Wrapper?.toABI { $0 }\n", - get_swift_name(param), - get_swift_name(param)); + param_name); + w.write("let % = try! %Wrapper?.toABI { $0 }\n", + local_param_name, + param_name); } } else { if (category == param_category::string_type) { - w.write("var _%: HSTRING?\n", - get_swift_name(param)); - guard.push("% = .init(from: _%)\n", - get_swift_name(param), - get_swift_name(param)); - guard.push("WindowsDeleteString(_%)\n", get_swift_name(param)); + w.write("var %: HSTRING?\n", + local_param_name); + guard.push("% = .init(from: %)\n", + param_name, + local_param_name); + guard.push("WindowsDeleteString(%)\n", local_param_name); } else if (category == param_category::struct_type && is_struct_blittable(signature_type) && !is_guid(category)) { - w.write("var _%: % = .init()\n", - get_swift_name(param), + w.write("var %: % = .init()\n", + local_param_name, bind_type_mangled(param.type)); - guard.push("% = .from(abi: _%)\n", - get_swift_name(param), - get_swift_name(param)); + guard.push("% = .from(abi: %)\n", + param_name, + local_param_name); } else if (category == param_category::struct_type) { - w.write("let _%: %._ABI_% = .init()\n", - get_swift_name(param), + w.write("let %: %._ABI_% = .init()\n", + local_param_name, abi_namespace(param.type), param.type->swift_type_name()); - guard.push("% = .from(abi: _%.val)\n", - get_swift_name(param), - get_swift_name(param)); + guard.push("% = .from(abi: %.val)\n", + param_name, + local_param_name); } else if (category == param_category::boolean_type || category == param_category::character_type) { - w.write("var _%: % = .init()\n", - get_swift_name(param), + w.write("var %: % = .init()\n", + local_param_name, bind_type_abi(param.type)); - guard.push("% = .init(from: _%)\n", - get_swift_name(param), - get_swift_name(param)); + guard.push("% = .init(from: %)\n", + param_name, + local_param_name); } else if (category == param_category::object_type) { - auto varName = w.write_temp("_%", get_swift_name(param)); w.write("var %: %\n", - varName, + local_param_name, bind(*param.type, write_type_params::c_abi)); guard.push("% = %\n", get_swift_name(param), - bind(param.type, varName)); + bind(param.type, local_param_name)); } else if (category == param_category::generic_type) { - w.write("var _%: %\n", - get_swift_name(param), + w.write("var %: %\n", + local_param_name, bind(*param.type, write_type_params::c_abi)); - guard.push("% = %.from(abi: _%)\n", - get_swift_name(param), + guard.push("% = %.from(abi: %)\n", + param_name, bind_impl_fullname(param.type), - get_swift_name(param)); + local_param_name); } } } @@ -1694,7 +1698,7 @@ public init(_ factory : Factory) { else { auto override_composable_init = R"(override public init() { - super.init(Self._%) + super.init(Self._%) let parentDefault: UnsafeMutablePointer<%> = super._getABI()! self._default = try! IInspectable(parentDefault).QueryInterface() _ = self._default.Release() // release to reset reference count since QI caused an AddRef on ourselves @@ -1842,7 +1846,7 @@ override public init(_ factory: Factory) { w.write("let wrapper = %(newValue)\n", bind_wrapper_fullname(prop.type)); w.write("let _newValue = try! wrapper?.toABI { $0 }\n"); } - + w.write("try! %.%Impl(%)\n", impl, get_swift_name(prop.setter.value()), @@ -1851,7 +1855,7 @@ override public init(_ factory: Factory) { set_indent_guard.end(); w.write("}\n"); } - + // TODO: https://linear.app/the-browser-company/issue/WIN-82/support-setters-not-defined-in-same-api-contract-as-getters // right now require that both getter and setter are defined in the same version if (prop.getter) @@ -1900,7 +1904,7 @@ override public init(_ factory: Factory) { return try! this.add_%Impl(abi) }, remove: { [weak this = %] in - try? this?.remove_%Impl($0) + try? this?.remove_%Impl($0) } ) }() @@ -1918,7 +1922,7 @@ override public init(_ factory: Factory) { delegate_method = genericInst->functions[0]; guard = w.push_generic_params(*genericInst); } - + assert(delegate_method.def); w.write(format, iface.attributed ? "static" : "lazy", // public % @@ -2064,7 +2068,7 @@ override public init(_ factory: Factory) { w.write("%% class var _makeFromAbi : any MakeFromAbi.Type { Composable.Default.self }\n", parent.base_class ? "override " : "", modifier); } - } + } // write the default implementation for makeAbi. this way we don't need to expose the internal implementation // details of the vtable to external modules. @@ -2073,7 +2077,7 @@ override public init(_ factory: Factory) { if (!overridable.is_composable()) { return; - } + } for (const auto& [_, info] : overridable.required_interfaces) { @@ -2194,7 +2198,7 @@ private var _default: SwiftABI! %% func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -2357,7 +2361,7 @@ private var _default: SwiftABI! bind([&](writer & w) { // Delegates are implemented as simple closures in Swift, which can't implement CustomQueryInterface if (is_delegate(type)) - { + { w.write("return failWith(err: E_NOINTERFACE)"); } else @@ -2401,7 +2405,7 @@ private var _default: SwiftABI! write_iunknown_methods(w, type, {}); } } - + template static void write_iinspectable_methods(writer& w, T const& type, std::vector const& interfaces, bool composed = false) { @@ -2415,7 +2419,7 @@ private var _default: SwiftABI! w.write("iids[0] = IUnknown.IID\n"); w.write("iids[1] = IInspectable.IID\n"); w.write("iids[2] = %.IID\n", bind_wrapper_fullname(type)); - + auto iface_n = 3; for (const auto& iface : interfaces) { @@ -2470,7 +2474,7 @@ private var _default: SwiftABI! { write_iinspectable_methods(w, type, type.required_interfaces); } - + // assigns return or out parameters in vtable methods static void do_write_abi_val_assignment(writer& w, const metadata_type* type, std::string_view const& param_name, std::string_view const& return_param_name) { @@ -2716,7 +2720,7 @@ private var _default: SwiftABI! } } } - + } w.write(R"( diff --git a/swiftwinrt/helpers.h b/swiftwinrt/helpers.h index 43254bb7..372ad351 100644 --- a/swiftwinrt/helpers.h +++ b/swiftwinrt/helpers.h @@ -644,6 +644,25 @@ namespace swiftwinrt return put_in_backticks_if_needed(std::string(param.Name())); } + inline std::string local_swift_param_name(std::string const& param_name) + { + std::string local_name = "_"; + // if the param name starts with backticks + if (param_name.starts_with('`')) + { + local_name.append(param_name.substr(1, param_name.size() - 2)); + } + else { + local_name.append(param_name); + } + return local_name; + } + + inline std::string local_swift_param_name(std::string_view const& param_name) + { + return local_swift_param_name(std::string(param_name)); + } + inline std::string get_swift_name(function_param const& param) { return get_swift_name(param.def); diff --git a/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift b/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift index 0dd3e8db..6192ee97 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift @@ -203,7 +203,7 @@ public enum __IMPL_Windows_Foundation { } public var type: PropertyType { propertyType } - public var isNumericScalar: Bool { + public var isNumericScalar: Bool { switch propertyType { case .int16, .int32, .int64, .uint8, .uint16, .uint32, .uint64, .single, .double: return true default: return false @@ -225,13 +225,13 @@ public enum __IMPL_Windows_Foundation { public func getBoolean() -> Bool { _value as! Bool } public func getString() -> String { _value as! String } public func getGuid() -> test_component.GUID { _value as! test_component.GUID } - public func getDateTime() -> DateTime { _value as! DateTime } + public func getDateTime() -> DateTime { _value as! DateTime } public func getTimeSpan() -> TimeSpan { _value as! TimeSpan } public func getPoint() -> Point { _value as! Point } public func getSize() -> Size { _value as! Size } public func getRect() -> Rect { _value as! Rect } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { guard iid == __ABI_Windows_Foundation.IPropertyValueWrapper.IID else { return nil } guard let thisAsIPropValue = __ABI_Windows_Foundation.IPropertyValueWrapper(self) else { fatalError("creating non-nil wrapper shouldn't fail") } return thisAsIPropValue.queryInterface(iid) diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift b/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift index 271e4312..c75461d6 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift @@ -35,7 +35,7 @@ public enum __IMPL_Windows_Foundation_Collections { return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift b/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift index 00c70bc2..dd39ae7a 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift @@ -15,7 +15,7 @@ public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IP public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -53,7 +53,7 @@ public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IP return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() @@ -128,7 +128,7 @@ public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -221,7 +221,7 @@ public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap { return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() @@ -239,7 +239,7 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -277,7 +277,7 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.swift b/tests/test_component/Sources/test_component/Windows.Foundation.swift index 87fd59d5..5cb45a78 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.swift @@ -14,7 +14,7 @@ public final class Deferral : WinRTClass, IClosable { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } diff --git a/tests/test_component/Sources/test_component/test_component+Generics.swift b/tests/test_component/Sources/test_component/test_component+Generics.swift index bcd5b57f..5c2e0319 100644 --- a/tests/test_component/Sources/test_component/test_component+Generics.swift +++ b/tests/test_component/Sources/test_component/test_component+Generics.swift @@ -4175,7 +4175,7 @@ internal class __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableImpl : IObservab return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() @@ -4370,7 +4370,7 @@ internal class __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGImpl : IObservableMap return try! this.add_MapChangedImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_MapChangedImpl($0) + try? this?.remove_MapChangedImpl($0) } ) }() @@ -4559,7 +4559,7 @@ internal class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseI i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -4592,7 +4592,7 @@ internal class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseI return try! this.add_VectorChangedImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + try? this?.remove_VectorChangedImpl($0) } ) }() @@ -4790,7 +4790,7 @@ internal class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasi i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -4823,7 +4823,7 @@ internal class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasi return try! this.add_VectorChangedImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + try? this?.remove_VectorChangedImpl($0) } ) }() @@ -5051,7 +5051,7 @@ internal class __x_ABI_C__FIVectorView_1_IInspectableImpl : IVectorView, AbiInte i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -5244,7 +5244,7 @@ internal class __x_ABI_C__FIVectorView_1_HSTRINGImpl : IVectorView, AbiInterface i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -5436,7 +5436,7 @@ internal class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseImpl : i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -5628,7 +5628,7 @@ internal class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicImpl i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -5918,7 +5918,7 @@ internal class __x_ABI_C__FIVector_1_IInspectableImpl : IVector, AbiInterfaceImp i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -6258,7 +6258,7 @@ internal class __x_ABI_C__FIVector_1_HSTRINGImpl : IVector, AbiInterfaceImpl { i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -6594,7 +6594,7 @@ internal class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseImpl : IVec i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -6927,7 +6927,7 @@ internal class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicImpl : IV i+1 } - func index(of: Element) -> Int? { + func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -7845,7 +7845,7 @@ internal extension GUID { try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) self = result } -} +} internal var __x_ABI_C__FIReference_1_GUIDVTable: __x_ABI_C__FIReference_1_GUIDVtbl = .init( QueryInterface: { guard let pUnk = $0, let riid = $1, let ppvObject = $2 else { return E_INVALIDARG } @@ -7929,7 +7929,7 @@ internal extension Int32 { try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) self = result } -} +} internal var __x_ABI_C__FIReference_1_intVTable: __x_ABI_C__FIReference_1_intVtbl = .init( QueryInterface: { guard let pUnk = $0, let riid = $1, let ppvObject = $2 else { return E_INVALIDARG } @@ -8013,7 +8013,7 @@ internal extension test_component.Signed { try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) self = result } -} +} internal var __x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSignedVTable: __x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSignedVtbl = .init( QueryInterface: { guard let pUnk = $0, let riid = $1, let ppvObject = $2 else { return E_INVALIDARG } diff --git a/tests/test_component/Sources/test_component/test_component+Impl.swift b/tests/test_component/Sources/test_component/test_component+Impl.swift index 8babf470..3bdd1d7a 100644 --- a/tests/test_component/Sources/test_component/test_component+Impl.swift +++ b/tests/test_component/Sources/test_component/test_component+Impl.swift @@ -174,7 +174,7 @@ public enum __IMPL_test_component { return try! this.add_ImplementableEventImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_ImplementableEventImpl($0) + try? this?.remove_ImplementableEventImpl($0) } ) }() @@ -262,7 +262,7 @@ public enum __IMPL_test_component { return try! this.add_EventWithReturnImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_EventWithReturnImpl($0) + try? this?.remove_EventWithReturnImpl($0) } ) }() @@ -288,8 +288,8 @@ public enum __IMPL_test_component { return .init(lpVtbl: vtblPtr) } public func withExtension(_ `extension`: String) throws { - let _`extension` = try! HString(`extension`) - try _default.WithExtensionImpl(_`extension`.get()) + let _extension = try! HString(`extension`) + try _default.WithExtensionImpl(_extension.get()) } } @@ -408,4 +408,3 @@ public class WithKeyword_MakeFromAbi : MakeFromAbi { return __IMPL_test_component.WithKeywordImpl(RawPointer(swiftAbi)!) } } - diff --git a/tests/test_component/Sources/test_component/test_component.swift b/tests/test_component/Sources/test_component/test_component.swift index 035930c1..a4ed6af3 100644 --- a/tests/test_component/Sources/test_component/test_component.swift +++ b/tests/test_component/Sources/test_component/test_component.swift @@ -15,7 +15,7 @@ public final class AsyncOperationInt : WinRTClass, IAsyncOperation, test_compone public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -99,7 +99,7 @@ open class Base : UnsealedWinRTClass { open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -171,7 +171,7 @@ public final class BaseCollection : WinRTClass, IVector, IIterable { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -202,7 +202,7 @@ public final class BaseCollection : WinRTClass, IVector, IIterable { i+1 } - public func index(of: Element) -> Int? { + public func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -290,7 +290,7 @@ public final class BaseMapCollection : WinRTClass, IMap, IIterable { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -369,7 +369,7 @@ open class BaseNoOverrides : UnsealedWinRTClass { open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -426,7 +426,7 @@ public final class BaseObservableCollection : WinRTClass, IObservableVector, IVe public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -457,7 +457,7 @@ public final class BaseObservableCollection : WinRTClass, IObservableVector, IVe i+1 } - public func index(of: Element) -> Int? { + public func index(of: Element) -> Int? { var index: UInt32 = 0 let result = indexOf(of, &index) guard result else { return nil } @@ -489,7 +489,7 @@ public final class BaseObservableCollection : WinRTClass, IObservableVector, IVe return try! this.add_VectorChangedImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + try? this?.remove_VectorChangedImpl($0) } ) }() @@ -557,7 +557,7 @@ public final class Class : WinRTClass, IBasic { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -878,7 +878,7 @@ public final class Class : WinRTClass, IBasic { return try! this.add_DeferrableEventImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_DeferrableEventImpl($0) + try? this?.remove_DeferrableEventImpl($0) } ) }() @@ -897,7 +897,7 @@ public final class CollectionTester : WinRTClass { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -978,7 +978,7 @@ public final class DeferrableEventArgs : WinRTClass { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1017,7 +1017,7 @@ public final class Derived : test_component.Base { override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1079,7 +1079,7 @@ public final class EventTester : WinRTClass { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1138,7 +1138,7 @@ public final class NoopClosable : WinRTClass, test_component.IClosable { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1240,7 +1240,7 @@ public final class Simple : WinRTClass { public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1279,7 +1279,7 @@ public final class Simple : WinRTClass { return try! this.add_StaticEventImpl(abi) }, remove: { [weak this = _ISimpleStatics] in - try? this?.remove_StaticEventImpl($0) + try? this?.remove_StaticEventImpl($0) } ) }() @@ -1370,7 +1370,7 @@ public final class Simple : WinRTClass { return try! this.add_InEventImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_InEventImpl($0) + try? this?.remove_InEventImpl($0) } ) }() @@ -1384,7 +1384,7 @@ public final class Simple : WinRTClass { return try! this.add_SignalEventImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_SignalEventImpl($0) + try? this?.remove_SignalEventImpl($0) } ) }() @@ -1398,7 +1398,7 @@ public final class Simple : WinRTClass { return try! this.add_SimpleEventImpl(abi) }, remove: { [weak this = _default] in - try? this?.remove_SimpleEventImpl($0) + try? this?.remove_SimpleEventImpl($0) } ) }() @@ -1442,7 +1442,7 @@ open class UnsealedDerived : test_component.Base { override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1477,7 +1477,7 @@ open class UnsealedDerived : test_component.Base { } private static var _IUnsealedDerivedFactory : __ABI_test_component.IUnsealedDerivedFactory = try! RoGetActivationFactory(HString("test_component.UnsealedDerived")) override public init() { - super.init(Self._IUnsealedDerivedFactory) + super.init(Self._IUnsealedDerivedFactory) let parentDefault: UnsafeMutablePointer = super._getABI()! self._default = try! IInspectable(parentDefault).QueryInterface() _ = self._default.Release() // release to reset reference count since QI caused an AddRef on ourselves @@ -1547,7 +1547,7 @@ open class UnsealedDerived2 : test_component.UnsealedDerived { override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1573,7 +1573,7 @@ open class UnsealedDerived2 : test_component.UnsealedDerived { return super.queryInterface(iid)} private static var _IUnsealedDerived2ProtectedFactory : __ABI_test_component.IUnsealedDerived2ProtectedFactory = try! RoGetActivationFactory(HString("test_component.UnsealedDerived2")) override public init() { - super.init(Self._IUnsealedDerived2ProtectedFactory) + super.init(Self._IUnsealedDerived2ProtectedFactory) let parentDefault: UnsafeMutablePointer = super._getABI()! self._default = try! IInspectable(parentDefault).QueryInterface() _ = self._default.Release() // release to reset reference count since QI caused an AddRef on ourselves @@ -1613,7 +1613,7 @@ open class UnsealedDerivedNoOverrides : test_component.BaseNoOverrides { override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) - } + } if T.self == C_IInspectable.self { return RawPointer(_default) } @@ -1639,7 +1639,7 @@ open class UnsealedDerivedNoOverrides : test_component.BaseNoOverrides { return super.queryInterface(iid)} private static var _IUnsealedDerivedNoOverridesProtectedFactory : __ABI_test_component.IUnsealedDerivedNoOverridesProtectedFactory = try! RoGetActivationFactory(HString("test_component.UnsealedDerivedNoOverrides")) override public init() { - super.init(Self._IUnsealedDerivedNoOverridesProtectedFactory) + super.init(Self._IUnsealedDerivedNoOverridesProtectedFactory) let parentDefault: UnsafeMutablePointer = super._getABI()! self._default = try! IInspectable(parentDefault).QueryInterface() _ = self._default.Release() // release to reset reference count since QI caused an AddRef on ourselves From e0efc762e1b89c8bdc26bb3ce27ffdc488fe3c92 Mon Sep 17 00:00:00 2001 From: Steve Kirbach Date: Wed, 18 Oct 2023 13:19:12 -0700 Subject: [PATCH 3/3] add other test cases --- .../Sources/CWinRT/include/test_component.h | 11 ++- .../test_component/test_component+ABI.swift | 67 +++++++++++++++++-- .../test_component+Generics.swift | 8 +++ .../test_component/test_component+Impl.swift | 31 ++++++++- .../test_component/test_component.swift | 4 +- tests/test_component/cpp/Keywords.idl | 4 +- 6 files changed, 115 insertions(+), 10 deletions(-) diff --git a/tests/test_component/Sources/CWinRT/include/test_component.h b/tests/test_component/Sources/CWinRT/include/test_component.h index 094b39b0..61f944f5 100644 --- a/tests/test_component/Sources/CWinRT/include/test_component.h +++ b/tests/test_component/Sources/CWinRT/include/test_component.h @@ -3690,8 +3690,17 @@ struct __x_ABI_Ctest__component_CStructWithEnum HSTRING* className); HRESULT (STDMETHODCALLTYPE* GetTrustLevel)(__x_ABI_Ctest__component_CWithKeyword* This, TrustLevel* trustLevel); - HRESULT (STDMETHODCALLTYPE* WithExtension)(__x_ABI_Ctest__component_CWithKeyword* This, + HRESULT (STDMETHODCALLTYPE* Enum)(__x_ABI_Ctest__component_CWithKeyword* This, HSTRING extension); + HRESULT (STDMETHODCALLTYPE* get_Struct)(__x_ABI_Ctest__component_CWithKeyword* This, + HSTRING* value); + HRESULT (STDMETHODCALLTYPE* put_Struct)(__x_ABI_Ctest__component_CWithKeyword* This, + HSTRING value); + HRESULT (STDMETHODCALLTYPE* add_Repeat)(__x_ABI_Ctest__component_CWithKeyword* This, + __x_ABI_C__FIEventHandler_1_IInspectable* handler, + EventRegistrationToken* token); + HRESULT (STDMETHODCALLTYPE* remove_Repeat)(__x_ABI_Ctest__component_CWithKeyword* This, + EventRegistrationToken token); END_INTERFACE } __x_ABI_Ctest__component_CWithKeywordVtbl; diff --git a/tests/test_component/Sources/test_component/test_component+ABI.swift b/tests/test_component/Sources/test_component/test_component+ABI.swift index 91a010c2..6a2be8d0 100644 --- a/tests/test_component/Sources/test_component/test_component+ABI.swift +++ b/tests/test_component/Sources/test_component/test_component+ABI.swift @@ -139,7 +139,7 @@ private var IID___x_ABI_Ctest__component_CInterfaceWithReturnDelegate: test_comp } private var IID___x_ABI_Ctest__component_CWithKeyword: test_component.IID { - .init(Data1: 0xA703474B, Data2: 0x0941, Data3: 0x5409, Data4: ( 0xA8,0x3E,0xD7,0x70,0x48,0x91,0xAE,0x84 ))// A703474B-0941-5409-A83E-D7704891AE84 + .init(Data1: 0x77E9FBAD, Data2: 0x3DCE, Data3: 0x5E50, Data4: ( 0xB4,0x39,0x91,0x91,0xF5,0x23,0x2A,0x84 ))// 77E9FBAD-3DCE-5E50-B439-9191F5232A84 } private var IID___x_ABI_Ctest__component_CIObjectHandler: test_component.IID { @@ -1936,9 +1936,37 @@ public enum __ABI_test_component { open class WithKeyword: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CWithKeyword } - open func WithExtensionImpl(_ `extension`: HSTRING?) throws { + open func EnumImpl(_ `extension`: HSTRING?) throws { _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.WithExtension(pThis, `extension`)) + try CHECKED(pThis.pointee.lpVtbl.pointee.Enum(pThis, `extension`)) + } + } + + open func get_StructImpl() throws -> HSTRING? { + var value: HSTRING? + _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Struct(pThis, &value)) + } + return value + } + + open func put_StructImpl(_ value: HSTRING?) throws { + _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.put_Struct(pThis, value)) + } + } + + open func add_RepeatImpl(_ handler: UnsafeMutablePointer<__x_ABI_C__FIEventHandler_1_IInspectable>?) throws -> EventRegistrationToken { + var token: EventRegistrationToken = .init() + _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.add_Repeat(pThis, handler, &token)) + } + return token + } + + open func remove_RepeatImpl(_ token: EventRegistrationToken) throws { + _ = try perform(as: __x_ABI_Ctest__component_CWithKeyword.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.remove_Repeat(pThis, token)) } } @@ -1998,13 +2026,42 @@ public enum __ABI_test_component { return S_OK }, - WithExtension: { + Enum: { do { guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let `extension`: String = .init(from: $1) - try __unwrapped__instance.withExtension(`extension`) + try __unwrapped__instance.`enum`(`extension`) return S_OK } catch { return failWith(err: E_FAIL) } + }, + + get_Struct: { + guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } + let value = __unwrapped__instance.`struct` + $1?.initialize(to: try! HString(value).detach()) + return S_OK + }, + + put_Struct: { + guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } + let value: String = .init(from: $1) + __unwrapped__instance.`struct` = value + return S_OK + }, + + add_Repeat: { + guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } + guard let handler = test_component.__x_ABI_C__FIEventHandler_1_IInspectableWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + let token = __unwrapped__instance.`repeat`.addHandler(handler) + $2?.initialize(to: .from(swift: token)) + return S_OK + }, + + remove_Repeat: { + guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } + let token: EventRegistrationToken = $1 + __unwrapped__instance.`repeat`.removeHandler(token) + return S_OK } ) diff --git a/tests/test_component/Sources/test_component/test_component+Generics.swift b/tests/test_component/Sources/test_component/test_component+Generics.swift index 5c2e0319..fb0b8d80 100644 --- a/tests/test_component/Sources/test_component/test_component+Generics.swift +++ b/tests/test_component/Sources/test_component/test_component+Generics.swift @@ -8228,3 +8228,11 @@ internal class __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimp return handler } } +public extension EventSource where Handler == EventHandler { + func invoke(_ sender: Any!, _ args: Any!) { + for handler in getInvocationList() { + handler(sender, args) + } + } +} + diff --git a/tests/test_component/Sources/test_component/test_component+Impl.swift b/tests/test_component/Sources/test_component/test_component+Impl.swift index 3bdd1d7a..c5be1486 100644 --- a/tests/test_component/Sources/test_component/test_component+Impl.swift +++ b/tests/test_component/Sources/test_component/test_component+Impl.swift @@ -287,11 +287,37 @@ public enum __IMPL_test_component { let vtblPtr = withUnsafeMutablePointer(to: &__ABI_test_component.WithKeywordVTable) { $0 } return .init(lpVtbl: vtblPtr) } - public func withExtension(_ `extension`: String) throws { + public func `enum`(_ `extension`: String) throws { let _extension = try! HString(`extension`) - try _default.WithExtensionImpl(_extension.get()) + try _default.EnumImpl(_extension.get()) } + public var `struct` : String { + get { + let value = try! _default.get_StructImpl() + return .init(from: value) + } + + set { + let _newValue = try! HString(newValue) + try! _default.put_StructImpl(_newValue.get()) + } + } + + public lazy var `repeat` : Event> = { + .init( + add: { [weak this = _default] in + guard let this else { return .init() } + let wrapper = test_component.__x_ABI_C__FIEventHandler_1_IInspectableWrapper($0) + let abi = try! wrapper?.toABI { $0 } + return try! this.add_RepeatImpl(abi) + }, + remove: { [weak this = _default] in + try? this?.remove_RepeatImpl($0) + } + ) + }() + } public class ObjectHandlerImpl : WinRTDelegateBridge { @@ -408,3 +434,4 @@ public class WithKeyword_MakeFromAbi : MakeFromAbi { return __IMPL_test_component.WithKeywordImpl(RawPointer(swiftAbi)!) } } + diff --git a/tests/test_component/Sources/test_component/test_component.swift b/tests/test_component/Sources/test_component/test_component.swift index a4ed6af3..f4cf9c73 100644 --- a/tests/test_component/Sources/test_component/test_component.swift +++ b/tests/test_component/Sources/test_component/test_component.swift @@ -1870,7 +1870,9 @@ extension InterfaceWithReturnDelegate { public typealias AnyInterfaceWithReturnDelegate = any InterfaceWithReturnDelegate public protocol WithKeyword : WinRTInterface { - func withExtension(_ `extension`: String) throws + func `enum`(_ `extension`: String) throws + var `struct`: String { get set } + var `repeat`: Event> { get } } extension WithKeyword { diff --git a/tests/test_component/cpp/Keywords.idl b/tests/test_component/cpp/Keywords.idl index d929700d..9959f577 100644 --- a/tests/test_component/cpp/Keywords.idl +++ b/tests/test_component/cpp/Keywords.idl @@ -51,6 +51,8 @@ namespace test_component }; interface WithKeyword { - void WithExtension(String extension); + void Enum(String extension); + String Struct; + event Windows.Foundation.EventHandler Repeat; } } \ No newline at end of file