Skip to content

Commit

Permalink
Refactor support ABI modules (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Jul 24, 2024
1 parent 4b1c553 commit 3f63e4a
Show file tree
Hide file tree
Showing 47 changed files with 125 additions and 87 deletions.
20 changes: 14 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import PackageDescription
let package = Package(
name: "SwiftWinRT",
products: [
.library(
name: "WindowsRuntime_ABI",
targets: ["COM_ABI", "WindowsRuntime_ABI"]),
.library(
name: "WindowsRuntime",
targets: ["COM", "WindowsRuntime"]),
Expand All @@ -13,19 +16,24 @@ let package = Package(
name: "COM_ABI",
path: "Support/Sources/COM_ABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "COM",
dependencies: ["COM_ABI"],
path: "Support/Sources/COM",
exclude: ["CMakeLists.txt"]),
.target(
name: "WindowsRuntime_ABI",
dependencies: ["COM_ABI"],
path: "Support/Sources/WindowsRuntime_ABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "InternalABI",
dependencies: ["COM_ABI", "WindowsRuntime_ABI"],
path: "Support/Sources/InternalABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "COM",
dependencies: ["COM_ABI", "InternalABI"],
path: "Support/Sources/COM",
exclude: ["CMakeLists.txt"]),
.target(
name: "WindowsRuntime",
dependencies: ["WindowsRuntime_ABI", "COM"],
dependencies: ["COM", "WindowsRuntime_ABI", "InternalABI"],
path: "Support/Sources/WindowsRuntime",
exclude: ["CMakeLists.txt"]),
.testTarget(
Expand Down
4 changes: 3 additions & 1 deletion Support/Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
add_subdirectory(COM_ABI)
add_subdirectory(COM)
add_subdirectory(WindowsRuntime_ABI)
add_subdirectory(InternalABI)

add_subdirectory(COM)
add_subdirectory(WindowsRuntime)
2 changes: 1 addition & 1 deletion Support/Sources/COM/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
file(GLOB_RECURSE SOURCES *.swift)
add_library(COM STATIC ${SOURCES})
target_link_libraries(COM PRIVATE COM_ABI)
target_link_libraries(COM PRIVATE COM_ABI SWRT_InternalABI)
11 changes: 6 additions & 5 deletions Support/Sources/COM/COMEmbedding.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import COM_ABI
import SWRT_SwiftCOMObject

/// Protocol for Swift objects which embed COM interfaces.
public protocol COMEmbedderWithDelegatedImplementation: IUnknownProtocol {
Expand All @@ -18,7 +19,7 @@ public struct COMEmbedding /*: ~Copyable */ {
// Instead, it is initialized in two steps: first to an invalid value, and then to a valid value.
public static var uninitialized: COMEmbedding { .init() }

private var comObject: COM_ABI.SWRT_SwiftCOMObject
private var comObject: SWRT_SwiftCOMObject

private init() {
comObject = .init()
Expand All @@ -42,13 +43,13 @@ public struct COMEmbedding /*: ~Copyable */ {
public mutating func toCOM() -> IUnknownReference { .init(addingRef: unknownPointer) }

fileprivate static func toUnmanagedUnsafe<ABIStruct>(_ this: UnsafeMutablePointer<ABIStruct>) -> Unmanaged<AnyObject> {
this.withMemoryRebound(to: COM_ABI.SWRT_SwiftCOMObject.self, capacity: 1) {
this.withMemoryRebound(to: SWRT_SwiftCOMObject.self, capacity: 1) {
Unmanaged<AnyObject>.fromOpaque($0.pointee.swiftObject)
}
}

public static func test<ABIStruct>(_ this: UnsafeMutablePointer<ABIStruct>) -> Bool {
do { _ = try COMInterop(this).queryInterface(uuidof(COM_ABI.SWRT_SwiftCOMObject.self)) } catch { return false }
do { _ = try COMInterop(this).queryInterface(uuidof(SWRT_SwiftCOMObject.self)) } catch { return false }
return true
}

Expand Down Expand Up @@ -92,7 +93,7 @@ public struct COMEmbedding /*: ~Copyable */ {
}
}

internal func uuidof(_: COM_ABI.SWRT_SwiftCOMObject.Type) -> COMInterfaceID {
internal func uuidof(_: SWRT_SwiftCOMObject.Type) -> COMInterfaceID {
.init(0x33934271, 0x7009, 0x4EF3, 0x90F1, 0x02090D7EBD64)
}

Expand Down Expand Up @@ -130,7 +131,7 @@ public enum IUnknownVirtualTable {
return HResult.catchValue {
let id = GUIDProjection.toSwift(iid.pointee)
let this = IUnknownPointer(OpaquePointer(this))
let reference = id == uuidof(COM_ABI.SWRT_SwiftCOMObject.self)
let reference = id == uuidof(SWRT_SwiftCOMObject.self)
? IUnknownReference(addingRef: this)
: try (COMEmbedding.getEmbedderObjectOrCrash(this) as! IUnknown)._queryInterface(id)
ppvObject.pointee = UnsafeMutableRawPointer(reference.detach())
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM_ABI/combaseapi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "SWRT/combaseapi.h"
#include "SWRT/windows/combaseapi.h"

#include <Windows.h>
#include <combaseapi.h>
Expand Down
7 changes: 7 additions & 0 deletions Support/Sources/COM_ABI/include/SWRT/COM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "windows/BaseTsd.h"
#include "windows/combaseapi.h"
#include "windows/guiddef.h"
#include "windows/oaidl.h"
#include "windows/objidl.h"
#include "windows/oleauto.h"
#include "windows/unknwn.h"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SWRT/BaseTsd.h"
#include "SWRT/unknwn.h"
#include "SWRT/windows/BaseTsd.h"
#include "SWRT/windows/unknwn.h"

SWRT_HResult SWRT_CoCreateFreeThreadedMarshaler(SWRT_IUnknown* punkOuter, SWRT_IUnknown** ppunkMarshal);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "SWRT/oleauto.h"
#include "SWRT/unknwn.h"
#include "SWRT/windows/oleauto.h"
#include "SWRT/windows/unknwn.h"

typedef struct SWRT_IErrorInfo {
struct SWRT_IErrorInfo_VirtualTable* VirtualTable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "SWRT/guiddef.h"
#include "SWRT/oleauto.h"
#include "SWRT/unknwn.h"
#include "SWRT/windows/guiddef.h"
#include "SWRT/windows/oleauto.h"
#include "SWRT/windows/unknwn.h"

// IAgileObject
typedef struct SWRT_IAgileObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "SWRT/guiddef.h"
#include "SWRT/BaseTsd.h"
#include "SWRT/windows/guiddef.h"
#include "SWRT/windows/BaseTsd.h"

// IUnknown
typedef struct SWRT_IUnknown {
Expand Down
18 changes: 9 additions & 9 deletions Support/Sources/COM_ABI/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module COM_ABI {
header "SwiftCOMObject.h"
header "SWRT/BaseTsd.h"
header "SWRT/combaseapi.h"
header "SWRT/guiddef.h"
header "SWRT/oaidl.h"
header "SWRT/objidl.h"
header "SWRT/oleauto.h"
header "SWRT/unknwn.h"
header "SWRT/COM.h"
header "SWRT/windows/BaseTsd.h"
header "SWRT/windows/combaseapi.h"
header "SWRT/windows/guiddef.h"
header "SWRT/windows/oaidl.h"
header "SWRT/windows/objidl.h"
header "SWRT/windows/oleauto.h"
header "SWRT/windows/unknwn.h"
export *
}
}
2 changes: 1 addition & 1 deletion Support/Sources/COM_ABI/oleauto.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "SWRT/oleauto.h"
#include "SWRT/windows/oleauto.h"

#include <Windows.h>
#include <oleauto.h>
Expand Down
3 changes: 3 additions & 0 deletions Support/Sources/InternalABI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_library(SWRT_InternalABI INTERFACE)
target_include_directories(SWRT_InternalABI INTERFACE include)
target_link_libraries(SWRT_InternalABI INTERFACE COM_ABI WindowsRuntime_ABI)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SWRT/inspectable.h"
#include "SWRT/windows/inspectable.h"

// Windows.Foundation.PropertyType
typedef int32_t SWRT_WindowsFoundation_PropertyType;
Expand Down
9 changes: 9 additions & 0 deletions Support/Sources/InternalABI/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SWRT_SwiftCOMObject {
header "SwiftCOMObject.h"
export *
}

module SWRT_WindowsFoundation {
header "SWRT/windows/windows.foundation.h"
export *
}
2 changes: 1 addition & 1 deletion Support/Sources/WindowsRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
file(GLOB_RECURSE SOURCES *.swift)
add_library(WindowsRuntime STATIC ${SOURCES})
target_link_libraries(WindowsRuntime PRIVATE COM WindowsRuntime_ABI)
target_link_libraries(WindowsRuntime PRIVATE COM WindowsRuntime_ABI SWRT_InternalABI)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import COM
import WindowsRuntime_ABI
import SWRT_WindowsFoundation

public enum IReferenceUnboxingProjection {
public typealias Boolean = Of<PrimitiveProjection.Boolean>
Expand All @@ -18,7 +19,7 @@ public enum IReferenceUnboxingProjection {

public enum Of<Projection: BoxableProjection>: WinRTProjection, COMProjection {
public typealias SwiftObject = Projection.SwiftValue
public typealias ABIStruct = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference
public typealias ABIStruct = SWRT_WindowsFoundation_IReference

public static var typeName: Swift.String { "Windows.Foundation.IReference`1<\(Projection.typeName)>" }
public static var interfaceID: COMInterfaceID { Projection.ireferenceID }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import WindowsRuntime_ABI
import SWRT_WindowsFoundation

extension BoxableProjection {
public static func box(_ value: SwiftValue) throws -> IInspectable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension WindowsFoundation_DateTime {
}
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

extension WindowsFoundation_DateTime: WindowsRuntime.StructProjection, COM.ABIInertProjection {
public typealias SwiftValue = Self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import WindowsRuntime_ABI

/// Represents a value in a property store. You can't implement this interface, see Remarks.
public typealias WindowsFoundation_IPropertyValue = any WindowsFoundation_IPropertyValueProtocol

Expand Down Expand Up @@ -93,11 +91,13 @@ extension WindowsFoundation_IPropertyValueProtocol {
public func getRectArray(_ value: inout [WindowsFoundation_Rect]) throws { throw HResult.Error.notImpl }
}

internal func uuidof(_: WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue.Type) -> COMInterfaceID {
import SWRT_WindowsFoundation

internal func uuidof(_: SWRT_WindowsFoundation_IPropertyValue.Type) -> COMInterfaceID {
.init(0x4BD682DD, 0x7554, 0x40E9, 0x9A9B, 0x82654EDE7E62)
}

extension COMInterop where ABIStruct == WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue {
extension COMInterop where ABIStruct == SWRT_WindowsFoundation_IPropertyValue {
internal func get_Type() throws -> WindowsFoundation_PropertyType {
var abi_value: SWRT_WindowsFoundation_PropertyType = .init()
try WinRTError.throwIfFailed(this.pointee.VirtualTable.pointee.get_Type(this, &abi_value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension WindowsFoundation_IReferenceProtocol {
var value: T { try! _value() }
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

public enum WindowsFoundation_IReferenceProjection<TProjection: BoxableProjection>: InterfaceProjection {
public typealias SwiftObject = WindowsFoundation_IReference<TProjection.SwiftValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public protocol WindowsFoundation_IStringableProtocol: IInspectableProtocol {
func toString() throws -> String
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

public enum WindowsFoundation_IStringableProjection: InterfaceProjection {
public typealias SwiftObject = WindowsFoundation_IStringable
Expand Down Expand Up @@ -41,7 +41,7 @@ public enum WindowsFoundation_IStringableProjection: InterfaceProjection {
ToString: { this, value in _implement(this) { try _set(value, PrimitiveProjection.String.toABI($0.toString())) } })
}

public func uuidof(_: WindowsRuntime_ABI.SWRT_WindowsFoundation_IStringable.Type) -> COMInterfaceID {
public func uuidof(_: SWRT_WindowsFoundation_IStringable.Type) -> COMInterfaceID {
.init(0x96369F54, 0x8EB6, 0x48F0, 0xABCE, 0xC1B211E627C3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct WindowsFoundation_Point: Hashable, Codable, Sendable {
}
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

extension WindowsFoundation_Point: WindowsRuntime.StructProjection, COM.ABIInertProjection {
public typealias SwiftValue = Self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct WindowsFoundation_Rect: Hashable, Codable, Sendable {
}
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

extension WindowsFoundation_Rect: WindowsRuntime.StructProjection, COM.ABIInertProjection {
public typealias SwiftValue = Self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct WindowsFoundation_Size: Hashable, Codable, Sendable {
}
}

import WindowsRuntime_ABI
import SWRT_WindowsFoundation

extension WindowsFoundation_Size: WindowsRuntime.StructProjection, COM.ABIInertProjection {
public typealias SwiftValue = Self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension WindowsFoundation_TimeSpan {
}

// Projection
import WindowsRuntime_ABI
import SWRT_WindowsFoundation

/// Projects a Windows.Foundation.TimeSpan into a Swift.Duration value.
extension WindowsFoundation_TimeSpan: StructProjection, ABIInertProjection {
Expand Down
5 changes: 3 additions & 2 deletions Support/Sources/WindowsRuntime/PropertyValueStatics.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import COM
import WindowsRuntime_ABI
import SWRT_WindowsFoundation

internal enum PropertyValueStatics {
private static let iid = COMInterfaceID(0x629BDBC8, 0xD932, 0x4FF4, 0x96B9, 0x8D96C5C1E858)
private static var lazyReference: COM.COMReference<WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValueStatics>.Optional = .none
private static var lazyReference: COM.COMReference<SWRT_WindowsFoundation_IPropertyValueStatics>.Optional = .none

private static var this: UnsafeMutablePointer<WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValueStatics> {
private static var this: UnsafeMutablePointer<SWRT_WindowsFoundation_IPropertyValueStatics> {
get throws {
try lazyReference.lazyInitPointer {
try SystemActivationFactoryResolver.resolve(
Expand Down
12 changes: 12 additions & 0 deletions Support/Sources/WindowsRuntime_ABI/include/SWRT/WindowsRuntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "SWRT/COM.h"

#include "windows/activation.h"
#include "windows/eventtoken.h"
#include "windows/inspectable.h"
#include "windows/memorybuffer.h"
#include "windows/restrictederrorinfo.h"
#include "windows/roapi.h"
#include "windows/robuffer.h"
#include "windows/roerrorapi.h"
#include "windows/weakreference.h"
#include "windows/winstring.h"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SWRT/inspectable.h"
#include "SWRT/windows/inspectable.h"

// IActivationFactory
typedef struct SWRT_IActivationFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "SWRT/unknwn.h"
#include "SWRT/winstring.h"
#include "SWRT/windows/unknwn.h"
#include "SWRT/windows/winstring.h"

// TrustLevel
typedef int32_t SWRT_TrustLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "SWRT/unknwn.h"
#include "SWRT/windows/unknwn.h"

typedef struct SWRT_IMemoryBufferByteAccess SWRT_IMemoryBufferByteAccess;

Expand Down
Loading

0 comments on commit 3f63e4a

Please sign in to comment.