Skip to content

Commit

Permalink
build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbrix committed Jan 22, 2024
1 parent 349cf26 commit 8ba4b84
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 31 deletions.
18 changes: 9 additions & 9 deletions swiftwinrt/Resources/CWinRT/robuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@

#ifndef _IBufferByteAccess_DEFINED
#define _IBufferByteAccess_DEFINED
typedef interface IBufferByteAccess IBufferByteAccess;
typedef interface C_IBufferByteAccess C_IBufferByteAccess;
#endif /* _IBufferByteAccess_DEFINED */

typedef struct IBufferByteAccessVtbl
typedef struct C_IBufferByteAccessVtbl
{
BEGIN_INTERFACE

HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in IBufferByteAccess* This,
HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in C_IBufferByteAccess* This,
REFIID riid,
_COM_Outptr_ void** ppvObject);
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in IBufferByteAccess* This);
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in IBufferByteAccess* This);
HRESULT (STDMETHODCALLTYPE* Buffer)(__RPC__in IBufferByteAccess* This,
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in C_IBufferByteAccess* This);
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in C_IBufferByteAccess* This);
HRESULT (STDMETHODCALLTYPE* Buffer)(__RPC__in C_IBufferByteAccess* This,
BYTE** value);

END_INTERFACE
} IBufferByteAccessVtbl;
} C_IBufferByteAccessVtbl;

interface IBufferByteAccess
interface C_IBufferByteAccess
{
CONST_VTBL struct IBufferByteAccessVtbl* lpVtbl;
CONST_VTBL struct C_IBufferByteAccessVtbl* lpVtbl;
};
8 changes: 4 additions & 4 deletions swiftwinrt/Resources/Support/IBufferByteAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ extension __ABI_ {

public func Buffer(_ bytes: UnsafeMutablePointer<UInt8>?) throws {
var buffer: UnsafeMutablePointer<UInt8>?
try perform(as: C_BINDINGS_MODULE.IBufferByteAccess.self) { pThis in
try perform(as: C_BINDINGS_MODULE.C_IBufferByteAccess.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Buffer(pThis, &buffer))
}
}
static fileprivate func Buffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.IBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?) -> HRESULT {
static fileprivate func Buffer(_ this: UnsafeMutablePointer<C_BINDINGS_MODULE.C_IBufferByteAccess>?, _ buffer: UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>?) -> HRESULT {
return E_NOTIMPL
}
}
Expand All @@ -37,7 +37,7 @@ public enum IBufferByteAccessBridge: AbiInterfaceBridge {
return IBufferByteAccessImpl(abi)
}

public typealias CABI = C_BINDINGS_MODULE.IBufferByteAccess
public typealias CABI = C_BINDINGS_MODULE.C_IBufferByteAccess
public typealias SwiftABI = __ABI_.IBufferByteAccess
public typealias SwiftProjection = AnyIBufferByteAccess
}
Expand All @@ -58,7 +58,7 @@ fileprivate class IBufferByteAccessImpl: AbiInterfaceImpl, IBufferByteAccess {
}
}

private var IBufferByteAccessVTable: C_BINDINGS_MODULE.IBufferByteAccessVtbl = .init(
private var IBufferByteAccessVTable: C_BINDINGS_MODULE.C_IBufferByteAccessVtbl = .init(
QueryInterface: { __ABI_.IBufferByteAccessWrapper.queryInterface($0, $1, $2) },
AddRef: { __ABI_.IBufferByteAccessWrapper.addRef($0) },
Release: { __ABI_.IBufferByteAccessWrapper.release($0) },
Expand Down
9 changes: 5 additions & 4 deletions swiftwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,10 @@ bind_bridge_fullname(type));
w.write("// MARK: WinRT\n");
}

static void write_bufferbyteaccess(writer& w, interface_info const& info, system_type const& type)
static std::string modifier_for(typedef_base const& type_definition, interface_info const& iface);
static void write_bufferbyteaccess(writer& w, interface_info const& info, system_type const& type, typedef_base const& type_definition)
{
w.write(R"(var data: Data {
w.write(R"(% var data: Data {
get throws {
let bufferByteAccess: %.__ABI_.% = try %.QueryInterface()
var data = Data(count: Int(capacity))
Expand All @@ -1007,7 +1008,7 @@ bind_bridge_fullname(type));
return data
}
}
)", w.support, type.swift_type_name(), get_swift_name(info));
)", modifier_for(type_definition, info), w.support, type.swift_type_name(), get_swift_name(info));
}
static void write_interface_impl_members(writer& w, interface_info const& info, typedef_base const& type_definition)
{
Expand Down Expand Up @@ -1064,7 +1065,7 @@ bind_bridge_fullname(type));
{
if (systemType->swift_type_name() == "IBufferByteAccess" || systemType->swift_type_name() == "IMemoryBufferByteAccess")
{
write_bufferbyteaccess(w, info, *systemType);
write_bufferbyteaccess(w, info, *systemType, type_definition);
}
}
else
Expand Down
22 changes: 14 additions & 8 deletions swiftwinrt/metadata_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,25 @@ void metadata_cache::get_interfaces_impl(init_state& state, writer& w, get_inter

process_contract_dependencies(*state.target, impl);
get_interfaces_impl(state, w, result, info.defaulted, info.overridable, base, typeBase->type().InterfaceImpl());
try_insert_buffer_byte_access(typeBase->type(), result, info.defaulted);
}

insert_or_assign(result, name, std::move(info));
}
};

void metadata_cache::try_insert_buffer_byte_access(winmd::reader::TypeDef const& type, get_interfaces_t& result, bool defaulted = false)
{
if (type.TypeNamespace() == "Windows.Foundation" && type.TypeName() == "IMemoryBufferReference")
{
insert_or_assign(result, "IMemoryBufferByteAccess", { &system_type::from_name("IMemoryBufferByteAccess"), false, defaulted });
}
else if (type.TypeNamespace() == "Windows.Storage.Streams" && type.TypeName() == "IBuffer")
{
insert_or_assign(result, "IBufferByteAccess", { &system_type::from_name("IBufferByteAccess"), false, defaulted });
}
}

metadata_cache::get_interfaces_t metadata_cache::get_interfaces(init_state& state, TypeDef const& type)
{
get_interfaces_t result;
Expand All @@ -285,14 +298,7 @@ metadata_cache::get_interfaces_t metadata_cache::get_interfaces(init_state& stat
get_interfaces_impl(state,w, result, false, false, true, base.InterfaceImpl());
}

if (type.TypeNamespace() == "Windows.Foundation" && type.TypeName() == "IMemoryBufferReference")
{
insert_or_assign(result, "IMemoryBufferByteAccess", { &system_type::from_name("IMemoryBufferByteAccess") });
}
else if (type.TypeNamespace() == "Windows.Storage.Streams" && type.TypeName() == "IBuffer")
{
insert_or_assign(result, "IBufferByteAccess", { &system_type::from_name("IBufferByteAccess") });
}
try_insert_buffer_byte_access(type, result);

if (!has_fastabi(type))
{
Expand Down
4 changes: 3 additions & 1 deletion swiftwinrt/metadata_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace swiftwinrt
type_cache compile_namespaces(std::vector<std::string_view> const& targetNamespaces, metadata_filter const& f);

std::set<std::string_view> get_dependent_namespaces(std::vector<std::string_view> const& targetNamespaces, metadata_filter const& f);

bool has_namespace(std::string_view typeNamespace) const
{
return m_typeTable.find(typeNamespace) != m_typeTable.end();
Expand Down Expand Up @@ -171,5 +171,7 @@ namespace swiftwinrt
std::map<std::string, attributed_type> get_attributed_types(winmd::reader::TypeDef const& type) const;

std::map<std::string_view, std::map<std::string_view, metadata_type const&>> m_typeTable;

void try_insert_buffer_byte_access(winmd::reader::TypeDef const& type, get_interfaces_t& result, bool defaulted);
};
}
4 changes: 2 additions & 2 deletions swiftwinrt/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ namespace swiftwinrt
}
else if (typeName == "IBufferByteAccess")
{
static system_type const ibufferbyteaccess_type{ ""sv, typeName, typeName, typeName, "{905a0fef-bc53-11df-8c49-001e4fc686da}"sv, param_category::object_type };
static system_type const ibufferbyteaccess_type{ ""sv, typeName, typeName, "C_IBufferByteAccess"sv, "{905a0fef-bc53-11df-8c49-001e4fc686da}"sv, param_category::object_type };
return ibufferbyteaccess_type;
}
else if (typeName == "IMemoryBufferByteAccess")
{
static system_type const imemorybufferbyte_type{ ""sv, typeName, typeName, typeName, "{5b0d3235-4dba-4d44-865e-8f1d0e4fd04d}"sv, param_category::object_type };
static system_type const imemorybufferbyte_type{ ""sv, typeName, typeName, "IMemoryBufferByteAccess"sv, "{5b0d3235-4dba-4d44-865e-8f1d0e4fd04d}"sv, param_category::object_type };
return imemorybufferbyte_type;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set(SWIFT_WINRT_PARAMETERS
"-include test_component"
"-include Windows.Foundation.Collections"
"-include Windows.Foundation.IMemoryBufferReference"
"-include Windows.Storage.Streams.IBuffer"
"-include Windows.Storage.Streams.Buffer"
"-input ${WINMD_FILE}"
"-support test_component"
"-test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ private var IID___x_ABI_CWindows_CFoundation_CIDeferralFactory: test_component.I
.init(Data1: 0x65A1ECC5, Data2: 0x3FB5, Data3: 0x4832, Data4: ( 0x8C,0xA9,0xF0,0x61,0xB2,0x81,0xD1,0x3A ))// 65A1ECC5-3FB5-4832-8CA9-F061B281D13A
}

private var IID___x_ABI_CWindows_CFoundation_CIMemoryBuffer: test_component.IID {
.init(Data1: 0xFBC4DD2A, Data2: 0x245B, Data3: 0x11E4, Data4: ( 0xAF,0x98,0x68,0x94,0x23,0x26,0x0C,0xF8 ))// FBC4DD2A-245B-11E4-AF98-689423260CF8
}

private var IID___x_ABI_CWindows_CFoundation_CIMemoryBufferFactory: test_component.IID {
.init(Data1: 0xFBC4DD2B, Data2: 0x245B, Data3: 0x11E4, Data4: ( 0xAF,0x98,0x68,0x94,0x23,0x26,0x0C,0xF8 ))// FBC4DD2B-245B-11E4-AF98-689423260CF8
}

private var IID___x_ABI_CWindows_CFoundation_CIMemoryBufferReference: test_component.IID {
.init(Data1: 0xFBC4DD29, Data2: 0x245B, Data3: 0x11E4, Data4: ( 0xAF,0x98,0x68,0x94,0x23,0x26,0x0C,0xF8 ))// FBC4DD29-245B-11E4-AF98-689423260CF8
}
Expand Down Expand Up @@ -342,6 +350,75 @@ public enum __ABI_Windows_Foundation {

}

public class IMemoryBuffer: test_component.IInspectable {
override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CIMemoryBuffer }

open func CreateReferenceImpl() throws -> test_component.AnyIMemoryBufferReference? {
let (reference) = try ComPtrs.initialize { referenceAbi in
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIMemoryBuffer.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.CreateReference(pThis, &referenceAbi))
}
}
return __ABI_Windows_Foundation.IMemoryBufferReferenceWrapper.unwrapFrom(abi: reference)
}

}

internal static var IMemoryBufferVTable: __x_ABI_CWindows_CFoundation_CIMemoryBufferVtbl = .init(
QueryInterface: { IMemoryBufferWrapper.queryInterface($0, $1, $2) },
AddRef: { IMemoryBufferWrapper.addRef($0) },
Release: { IMemoryBufferWrapper.release($0) },
GetIids: {
let size = MemoryLayout<test_component.IID>.size
let iids = CoTaskMemAlloc(UInt64(size) * 4).assumingMemoryBound(to: test_component.IID.self)
iids[0] = IUnknown.IID
iids[1] = IInspectable.IID
iids[2] = __ABI_Windows_Foundation.IMemoryBufferWrapper.IID
iids[3] = __ABI_Windows_Foundation.IClosableWrapper.IID
$1!.pointee = 4
$2!.pointee = iids
return S_OK
},

GetRuntimeClassName: {
_ = $0
let hstring = try! HString("Windows.Foundation.IMemoryBuffer").detach()
$1!.pointee = hstring
return S_OK
},

GetTrustLevel: {
_ = $0
$1!.pointee = TrustLevel(rawValue: 0)
return S_OK
},

CreateReference: {
do {
guard let __unwrapped__instance = IMemoryBufferWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }
let reference = try __unwrapped__instance.createReference()
let referenceWrapper = __ABI_Windows_Foundation.IMemoryBufferReferenceWrapper(reference)
referenceWrapper?.copyTo($1)
return S_OK
} catch { return failWith(err: E_FAIL) }
}
)

public typealias IMemoryBufferWrapper = InterfaceWrapperBase<__IMPL_Windows_Foundation.IMemoryBufferBridge>
public class IMemoryBufferFactory: test_component.IInspectable {
override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CIMemoryBufferFactory }

internal func CreateImpl(_ capacity: UInt32) throws -> IMemoryBuffer {
let (value) = try ComPtrs.initialize { valueAbi in
_ = try perform(as: __x_ABI_CWindows_CFoundation_CIMemoryBufferFactory.self) { pThis in
try CHECKED(pThis.pointee.lpVtbl.pointee.Create(pThis, capacity, &valueAbi))
}
}
return IMemoryBuffer(value!)
}

}

public class IMemoryBufferReference: test_component.IInspectable {
override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CIMemoryBufferReference }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,42 @@ public enum __IMPL_Windows_Foundation {

}

public enum IMemoryBufferBridge : AbiInterfaceBridge {
public typealias CABI = __x_ABI_CWindows_CFoundation_CIMemoryBuffer
public typealias SwiftABI = __ABI_Windows_Foundation.IMemoryBuffer
public typealias SwiftProjection = AnyIMemoryBuffer
public static func from(abi: ComPtr<CABI>?) -> SwiftProjection? {
guard let abi = abi else { return nil }
return IMemoryBufferImpl(abi)
}

public static func makeAbi() -> CABI {
let vtblPtr = withUnsafeMutablePointer(to: &__ABI_Windows_Foundation.IMemoryBufferVTable) { $0 }
return .init(lpVtbl: vtblPtr)
}
}

fileprivate class IMemoryBufferImpl: IMemoryBuffer, WinRTAbiImpl {
fileprivate typealias Bridge = IMemoryBufferBridge
fileprivate let _default: Bridge.SwiftABI
fileprivate var thisPtr: test_component.IInspectable { _default }
fileprivate init(_ fromAbi: ComPtr<Bridge.CABI>) {
_default = Bridge.SwiftABI(fromAbi)
}

/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.imemorybuffer.createreference)
fileprivate func createReference() throws -> AnyIMemoryBufferReference! {
try _default.CreateReferenceImpl()
}

private lazy var _IClosable: __ABI_Windows_Foundation.IClosable! = getInterfaceForCaching()
/// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.imemorybuffer.close)
fileprivate func close() throws {
try _IClosable.CloseImpl()
}

}

public enum IMemoryBufferReferenceBridge : AbiInterfaceBridge {
public typealias CABI = __x_ABI_CWindows_CFoundation_CIMemoryBufferReference
public typealias SwiftABI = __ABI_Windows_Foundation.IMemoryBufferReference
Expand Down Expand Up @@ -194,7 +230,7 @@ public enum __IMPL_Windows_Foundation {
}

private lazy var _IMemoryBufferByteAccess: __ABI_.IMemoryBufferByteAccess! = getInterfaceForCaching()
var data: Data {
fileprivate var data: Data {
get throws {
let bufferByteAccess: test_component.__ABI_.IMemoryBufferByteAccess = try _IMemoryBufferByteAccess.QueryInterface()
var data = Data(count: Int(capacity))
Expand Down
Loading

0 comments on commit 8ba4b84

Please sign in to comment.