Skip to content

Commit

Permalink
Remove COMProjection.COMVirtualTable typealias. (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Apr 1, 2024
1 parent 8857eb4 commit fa69e11
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 59 deletions.
10 changes: 4 additions & 6 deletions Generator/Sources/SwiftWinRT/Writing/ABIProjectionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ fileprivate func writeInterfaceOrDelegateProjectionType(
type, visibility: .private, name: importClassName, projectionName: projectionName,
projection: projection, to: writer)

// public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
// private static var virtualTable = COMVirtualTable(...)
// public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }
writer.writeComputedProperty(
visibility: .public, static: true, name: "virtualTablePointer",
type: .identifier("COMVirtualTablePointer")) { writer in
writer.writeStatement("withUnsafePointer(to: &virtualTable) { $0 }")
type: .identifier("UnsafeRawPointer")) { writer in
writer.writeStatement(".init(withUnsafePointer(to: &virtualTable) { $0 })")
}

// private static var virtualTable = SWRT_IFooVTable(...)
try writeVirtualTableProperty(name: "virtualTable", abiType: type, swiftType: type, projection: projection, to: writer)
}
}
Expand All @@ -365,8 +365,6 @@ internal func writeReferenceTypeProjectionConformance(
target: try projection.toType(apiType.asNode).unwrapOptional())
writer.writeTypeAlias(visibility: .public, name: "COMInterface",
target: try projection.toABIType(abiType))
writer.writeTypeAlias(visibility: .public, name: "COMVirtualTable",
target: try projection.toABIVirtualTableType(abiType))

// public static var typeName: String { "..." }
try writeTypeNameProperty(type: apiType, to: writer)
Expand Down
15 changes: 6 additions & 9 deletions Support/Sources/COM/COMExportedInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ public struct COMExportedInterface {
comObject = .init()
}

public init<SwiftObject: IUnknownProtocol, VirtualTable>(
public init<SwiftObject: IUnknownProtocol>(
swiftObject: SwiftObject,
virtualTable: UnsafePointer<VirtualTable>) {
comObject = .init(
comVirtualTable: virtualTable.withMemoryRebound(to: WindowsRuntime_ABI.SWRT_IUnknownVTable.self, capacity: 1) { $0 },
virtualTable: UnsafeRawPointer) {
comObject = .init(virtualTable: virtualTable,
swiftObject: Unmanaged<AnyObject>.passUnretained(swiftObject).toOpaque())
}

private init<VirtualTable>(virtualTable: UnsafePointer<VirtualTable>) {
comObject = .init(
comVirtualTable: virtualTable.withMemoryRebound(to: WindowsRuntime_ABI.SWRT_IUnknownVTable.self, capacity: 1) { $0 },
swiftObject: nil)
private init(virtualTable: UnsafeRawPointer) {
comObject = .init(virtualTable: virtualTable, swiftObject: nil)
}

public static func withLateSwiftObjectInit<VirtualTable>(virtualTable: UnsafePointer<VirtualTable>) -> COMExportedInterface {
public static func withLateSwiftObjectInit(virtualTable: UnsafeRawPointer) -> COMExportedInterface {
.init(virtualTable: virtualTable)
}

Expand Down
4 changes: 0 additions & 4 deletions Support/Sources/COM/COMProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ public protocol COMProjection: ABIProjection where SwiftValue == SwiftObject?, A
associatedtype SwiftObject
/// The COM interface structure.
associatedtype COMInterface /* : COMIUnknownStruct */
/// The COM interface's virtual table structure.
associatedtype COMVirtualTable
/// A pointer to the COM interface structure.
typealias COMPointer = UnsafeMutablePointer<COMInterface>
/// A pointer to the COM interface's virtual table structure.
typealias COMVirtualTablePointer = UnsafePointer<COMVirtualTable>

// Non-nullable overloads
static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM/COMTwoWayProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import WindowsRuntime_ABI

/// Protocol for strongly-typed two-way COM interface projections into and from Swift.
public protocol COMTwoWayProjection: COMProjection {
static var virtualTablePointer: COMVirtualTablePointer { get }
static var virtualTablePointer: UnsafeRawPointer { get }
}

/// Helpers for implementing virtual tables
Expand Down
1 change: 0 additions & 1 deletion Support/Sources/COM/IAgileObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import WindowsRuntime_ABI
public enum IAgileObjectProjection: COMProjection {
public typealias SwiftObject = IUnknown // Avoid introducing an interface for IAgileObject since it is a marker interface.
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IAgileObject
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IAgileObjectVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }

Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/COM/IErrorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public protocol IErrorInfoProtocol: IUnknownProtocol {
public enum IErrorInfoProjection: COMTwoWayProjection {
public typealias SwiftObject = IErrorInfo
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IErrorInfo
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IErrorInfoVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -34,7 +33,7 @@ public enum IErrorInfoProjection: COMTwoWayProjection {
public var helpContext: UInt32 { get throws { try _interop.getHelpContext() } }
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IErrorInfoVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/COM/IUnknown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ extension IUnknownProtocol {
public enum IUnknownProjection: COMTwoWayProjection {
public typealias SwiftObject = IUnknown
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IUnknown
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IUnknownVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -34,7 +33,7 @@ public enum IUnknownProjection: COMTwoWayProjection {

private final class Import: COMImport<IUnknownProjection> {}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IUnknownVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) })
Expand Down
3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IActivationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ public protocol IActivationFactoryProtocol: IInspectableProtocol {
public enum IActivationFactoryProjection: WinRTInterfaceProjection {
public typealias SwiftObject = IActivationFactory
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IActivationFactory
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IActivationFactoryVTable

public static var typeName: String { "IActivationFactory" }
public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { fatalError("Not implemented: \(#function)") }
public static var virtualTablePointer: UnsafeRawPointer { fatalError("Not implemented: \(#function)") }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/WindowsRuntime/IBufferByteAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ public protocol IBufferByteAccessProtocol: IUnknownProtocol {
public enum IBufferByteAccessProjection: COMTwoWayProjection {
public typealias SwiftObject = IBufferByteAccess
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IBufferByteAccess
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IBufferByteAccessVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -26,7 +25,7 @@ public enum IBufferByteAccessProjection: COMTwoWayProjection {
public var buffer: UnsafeMutablePointer<UInt8> { get throws { try NullResult.unwrap(_interop.buffer()) } }
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IBufferByteAccessVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/WindowsRuntime/IInspectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public protocol IInspectableProtocol: IUnknownProtocol {
public enum IInspectableProjection: WinRTInterfaceProjection {
public typealias SwiftObject = IInspectable
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IInspectable
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IInspectableVTable

public static var typeName: String { "IInspectable" }
public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -28,7 +27,7 @@ public enum IInspectableProjection: WinRTInterfaceProjection {

private final class Import: WinRTImport<IInspectableProjection> {}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IInspectableVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public enum IReferenceUnboxingProjection {
public enum Of<Projection: WinRTBoxableProjection>: WinRTProjection, COMProjection {
public typealias SwiftObject = Projection.SwiftValue
public typealias COMInterface = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReferenceVTable

public static var typeName: Swift.String { "Windows.Foundation.IReference`1<\(Projection.typeName)>" }
public static var interfaceID: COMInterfaceID { Projection.ireferenceID }
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
public enum IRestrictedErrorInfoProjection: COMTwoWayProjection {
public typealias SwiftObject = IRestrictedErrorInfo
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IRestrictedErrorInfo
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IRestrictedErrorInfoVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -39,7 +38,7 @@ public enum IRestrictedErrorInfoProjection: COMTwoWayProjection {
public var reference: String? { get throws { try _interop.getReference() } }
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IRestrictedErrorInfoVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/WindowsRuntime/IWeakReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ public protocol IWeakReferenceProtocol: IUnknownProtocol {
public enum IWeakReferenceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReference
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReference
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IWeakReferenceVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -27,7 +26,7 @@ public enum IWeakReferenceProjection: COMTwoWayProjection {
}
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IWeakReferenceVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
5 changes: 2 additions & 3 deletions Support/Sources/WindowsRuntime/IWeakReferenceSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ public protocol IWeakReferenceSourceProtocol: IUnknownProtocol {
public enum IWeakReferenceSourceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReferenceSource
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReferenceSource
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IWeakReferenceSourceVTable

public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -27,7 +26,7 @@ public enum IWeakReferenceSourceProjection: COMTwoWayProjection {
}
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IWeakReferenceSourceVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ extension WindowsFoundation_IReferenceProtocol {
public enum WindowsFoundation_IReferenceProjection<TProjection: WinRTBoxableProjection>: WinRTInterfaceProjection {
public typealias SwiftObject = WindowsFoundation_IReference<TProjection.SwiftValue>
public typealias COMInterface = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReferenceVTable

public static var typeName: String { fatalError("Windows.Foundation.IReference`1<\(TProjection.typeName)>") }
public static var interfaceID: COMInterfaceID { TProjection.ireferenceID }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(consume reference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ public protocol WindowsFoundation_IStringableProtocol: IInspectableProtocol {
public enum WindowsFoundation_IStringableProjection: WinRTInterfaceProjection {
public typealias SwiftObject = WindowsFoundation_IStringable
public typealias COMInterface = WindowsRuntime_ABI.SWRT_WindowsFoundation_IStringable
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_WindowsFoundation_IStringableVTable

public static var typeName: String { "Windows.Foundation.IStringable" }
public static var interfaceID: COMInterfaceID { COMInterface.iid }
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -32,7 +31,7 @@ public enum WindowsFoundation_IStringableProjection: WinRTInterfaceProjection {
}
}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_WindowsFoundation_IStringableVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include "SWRT/unknwn.h"

// A COM-compliant structure for bridging Swift objects into COM.
typedef struct SWRT_SwiftCOMObject {
const struct SWRT_IUnknownVTable* comVirtualTable;
const void* virtualTable;
void* swiftObject;
} SWRT_SwiftCOMObject;
5 changes: 2 additions & 3 deletions Support/Tests/IInspectable2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ internal typealias IInspectable2 = any IInspectable2Protocol
internal enum IInspectable2Projection: WinRTInterfaceProjection {
public typealias SwiftObject = IInspectable2
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IInspectable
public typealias COMVirtualTable = WindowsRuntime_ABI.SWRT_IInspectableVTable

public static var typeName: String { "IInspectable2" }
public static let interfaceID = COMInterfaceID(0xB6706A54, 0xCC67, 0x4090, 0x822D, 0xE165C8E36C11)
public static var virtualTablePointer: COMVirtualTablePointer { withUnsafePointer(to: &virtualTable) { $0 } }
public static var virtualTablePointer: UnsafeRawPointer { .init(withUnsafePointer(to: &virtualTable) { $0 }) }

public static func toSwift(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
Import.toSwift(reference)
Expand All @@ -23,7 +22,7 @@ internal enum IInspectable2Projection: WinRTInterfaceProjection {

private final class Import: WinRTImport<IInspectable2Projection>, IInspectable2Protocol {}

private static var virtualTable: COMVirtualTable = .init(
private static var virtualTable: WindowsRuntime_ABI.SWRT_IInspectableVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand Down
Loading

0 comments on commit fa69e11

Please sign in to comment.