Skip to content

Commit 29f405c

Browse files
authored
Add TupleType and UnsafeTuple (#97)
* Update OGTupleType and add openGraphSPICompatibilityTestTarget * Fix compile issue * Update TupleTypeTests * Update TupleTypeTests * Add UnsafeMutableTupleTests * add withUnsafeTuple implementation * Fix destroy typo and update dependency * Fix CI issue
1 parent cc85388 commit 29f405c

20 files changed

+827
-172
lines changed

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"originHash" : "541a191769e45d8d8acb1b48a687231b967da208924053878dcedf8652ae1c99",
2+
"originHash" : "aa0fd2e35b551c5a0558c96a63275f3b05620dac86f000f4bc0c1db36eddbed0",
33
"pins" : [
44
{
55
"identity" : "darwinprivateframeworks",
66
"kind" : "remoteSourceControl",
77
"location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git",
88
"state" : {
99
"branch" : "main",
10-
"revision" : "97476edc16f5f635fa275150012bc2facc942794"
10+
"revision" : "cab10e11f4ad918b3556b5ccaddd0aabb4cd8652"
1111
}
1212
},
1313
{

Package.swift

+34-13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
144144
cSettings: sharedCSettings,
145145
swiftSettings: sharedSwiftSettings
146146
)
147+
let openGraphSPICompatibilityTestTarget = Target.testTarget(
148+
name: "OpenGraph_SPICompatibilityTests",
149+
dependencies: [
150+
.product(name: "Numerics", package: "swift-numerics"),
151+
],
152+
exclude: ["README.md"],
153+
cSettings: sharedCSettings,
154+
swiftSettings: sharedSwiftSettings
155+
)
147156

148157
// MARK: - Package
149158

@@ -178,6 +187,7 @@ let package = Package(
178187
openGraphTestTarget,
179188
openGraphShimsTestTarget,
180189
openGraphCompatibilityTestTarget,
190+
openGraphSPICompatibilityTestTarget,
181191
],
182192
cxxLanguageStandard: .cxx17
183193
)
@@ -192,6 +202,26 @@ let attributeGraphCondition = envEnable("OPENGRAPH_ATTRIBUTEGRAPH")
192202
#endif
193203
let useLocalDeps = envEnable("OPENGRAPH_USE_LOCAL_DEPS")
194204

205+
extension Target {
206+
func addAGSettings() {
207+
dependencies.append(
208+
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
209+
)
210+
var swiftSettings = swiftSettings ?? []
211+
swiftSettings.append(.define("OPENGRAPH_ATTRIBUTEGRAPH"))
212+
self.swiftSettings = swiftSettings
213+
}
214+
215+
func addCompatibilitySettings() {
216+
dependencies.append(
217+
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
218+
)
219+
var swiftSettings = swiftSettings ?? []
220+
swiftSettings.append(.define("OPENGRAPH_COMPATIBILITY_TEST"))
221+
self.swiftSettings = swiftSettings
222+
}
223+
}
224+
195225
if attributeGraphCondition {
196226
let privateFrameworkRepo: Package.Dependency
197227
if useLocalDeps {
@@ -200,12 +230,7 @@ if attributeGraphCondition {
200230
privateFrameworkRepo = Package.Dependency.package(url: "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", branch: "main")
201231
}
202232
package.dependencies.append(privateFrameworkRepo)
203-
var swiftSettings: [SwiftSetting] = (openGraphShimsTarget.swiftSettings ?? [])
204-
swiftSettings.append(.define("OPENGRAPH_ATTRIBUTEGRAPH"))
205-
openGraphShimsTarget.swiftSettings = swiftSettings
206-
openGraphShimsTarget.dependencies.append(
207-
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
208-
)
233+
openGraphShimsTarget.addAGSettings()
209234

210235
let agVersion = Context.environment["DARWIN_PRIVATE_FRAMEWORKS_TARGET_RELEASE"].flatMap { Int($0) } ?? 2024
211236
package.platforms = switch agVersion {
@@ -219,15 +244,11 @@ if attributeGraphCondition {
219244

220245
let compatibilityTestCondition = envEnable("OPENGRAPH_COMPATIBILITY_TEST")
221246
if compatibilityTestCondition && attributeGraphCondition {
222-
openGraphCompatibilityTestTarget.dependencies.append(
223-
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks")
224-
)
225-
226-
var swiftSettings: [SwiftSetting] = (openGraphCompatibilityTestTarget.swiftSettings ?? [])
227-
swiftSettings.append(.define("OPENGRAPH_COMPATIBILITY_TEST"))
228-
openGraphCompatibilityTestTarget.swiftSettings = swiftSettings
247+
openGraphCompatibilityTestTarget.addCompatibilitySettings()
248+
openGraphSPICompatibilityTestTarget.addCompatibilitySettings()
229249
} else {
230250
openGraphCompatibilityTestTarget.dependencies.append("OpenGraph")
251+
openGraphSPICompatibilityTestTarget.dependencies.append("OpenGraph")
231252
}
232253

233254
extension [Platform] {

Sources/OpenGraph/Runtime/Metadata.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension Metadata: Swift.Hashable, Swift.CustomStringConvertible {
2727
@inlinable
2828
@inline(__always)
2929
public init(_ type: Any.Type) {
30-
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<OGSwiftMetadata>.self))
30+
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self))
3131
}
3232

3333
@inlinable

Sources/OpenGraph/Runtime/OGTupleType.swift

-38
This file was deleted.
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//
2+
// TupleType.swift
3+
// OpenGraph
4+
//
5+
// Audited for iOS 18.0
6+
// Status: WIP
7+
8+
public import OpenGraph_SPI
9+
10+
// MARK: TupleType
11+
12+
extension TupleType {
13+
public init(_ types: [Any.Type]) {
14+
self.init(count: types.count, elements: types.map(Metadata.init))
15+
}
16+
17+
public init(_ type: Any.Type) {
18+
self.init(rawValue: unsafeBitCast(type, to: UnsafePointer<_Metadata>.self))
19+
}
20+
21+
public var isEmpty: Bool { count == 0 }
22+
public var indices: Range<Int> { 0 ..< count }
23+
24+
public var type: Any.Type {
25+
unsafeBitCast(rawValue, to: Any.Type.self)
26+
}
27+
28+
public func type(at index: Int) -> Any.Type {
29+
elementType(at: index).type
30+
}
31+
32+
public func offset<T>(at index: Int, as type: T.Type) -> Int {
33+
elementOffset(at: index, type: Metadata(type))
34+
}
35+
36+
public func setElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, from srcValue: UnsafePointer<T>, options: CopyOptions) {
37+
__OGTupleSetElement(self, tupleValue, index, srcValue, Metadata(T.self), options)
38+
}
39+
40+
public func getElement<T>(in tupleValue: UnsafeMutableRawPointer, at index: Int, to dstValue: UnsafeMutablePointer<T>, options: CopyOptions) {
41+
__OGTupleGetElement(self, tupleValue, index, dstValue, Metadata(T.self), options)
42+
}
43+
}
44+
45+
@_silgen_name("OGTupleWithBuffer")
46+
public func withUnsafeTuple(of type: TupleType, count: Int, _ body: (UnsafeMutableTuple) -> ())
47+
48+
// MARK: - UnsafeTuple
49+
50+
extension UnsafeTuple {
51+
public var count: Int { type.count }
52+
public var isEmpty: Bool { type.isEmpty }
53+
public var indices: Range<Int> { type.indices }
54+
55+
public func address<T>(as _: T.Type = T.self) -> UnsafePointer<T> {
56+
guard type.type == T.self else {
57+
preconditionFailure()
58+
}
59+
return value.assumingMemoryBound(to: T.self)
60+
}
61+
62+
public func address<T>(of index: Int, as _: T.Type = T.self) -> UnsafePointer<T> {
63+
value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self)))
64+
.assumingMemoryBound(to: T.self)
65+
}
66+
67+
public subscript<T>() -> T {
68+
unsafeAddress { address(as: T.self) }
69+
}
70+
71+
public subscript<T>(_ index: Int) -> T {
72+
unsafeAddress { address(of: index, as: T.self) }
73+
}
74+
}
75+
76+
// MARK: - UnsafeMutableTuple
77+
78+
@_silgen_name("swift_slowAlloc")
79+
private func slowAlloc(_ size: Int, _ alignMask: Int) -> UnsafeMutableRawPointer
80+
81+
@_silgen_name("swift_slowDealloc")
82+
private func slowDealloc(_ ptr: UnsafeMutableRawPointer, _ size: Int, _ alignMask: Int)
83+
84+
extension UnsafeMutableTuple {
85+
public init(with tupleType: TupleType) {
86+
self.init(type: tupleType, value: slowAlloc(tupleType.size, -1))
87+
}
88+
89+
public func initialize<T>(at index: Int, to element: T) {
90+
withUnsafePointer(to: element) { elementPointer in
91+
type.setElement(in: value, at: index, from: elementPointer, options: .initCopy)
92+
}
93+
}
94+
95+
public func deinitialize() {
96+
type.destroy(value)
97+
}
98+
99+
public func deinitialize(at index: Int) {
100+
type.destroy(value, at: index)
101+
}
102+
103+
public func deallocate(initialized: Bool) {
104+
if initialized {
105+
deinitialize()
106+
}
107+
slowDealloc(value, -1, -1)
108+
}
109+
110+
public var count: Int { type.count }
111+
public var isEmpty: Bool { type.isEmpty }
112+
public var indices: Range<Int> { type.indices }
113+
114+
public func address<T>(as _: T.Type = T.self) -> UnsafeMutablePointer<T> {
115+
guard type.type == T.self else {
116+
preconditionFailure()
117+
}
118+
return value.assumingMemoryBound(to: T.self)
119+
}
120+
121+
public func address<T>(of index: Int, as _: T.Type = T.self) -> UnsafeMutablePointer<T> {
122+
value.advanced(by: type.elementOffset(at: index, type: Metadata(T.self)))
123+
.assumingMemoryBound(to: T.self)
124+
}
125+
126+
public subscript<T>() -> T {
127+
unsafeAddress { UnsafePointer(address(as: T.self)) }
128+
nonmutating unsafeMutableAddress { address(as: T.self) }
129+
}
130+
131+
public subscript<T>(_ index: Int) -> T {
132+
unsafeAddress { UnsafePointer(address(of: index, as: T.self)) }
133+
nonmutating unsafeMutableAddress { address(of: index, as: T.self) }
134+
}
135+
}

Sources/OpenGraphShims/GraphShims.swift

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public typealias OGGraphContext = AGGraphContext
1919
public typealias OGInputOptions = AGInputOptions
2020
public typealias OGSearchOptions = AGSearchOptions
2121
public typealias OGSubgraph = AGSubgraph
22-
public typealias OGSwiftMetadata = AGSwiftMetadata
23-
public typealias OGTupleType = AGTupleType
2422
public typealias OGTypeApplyOptions = AGTypeApplyOptions
2523
public typealias OGUniqueID = AGUniqueID
2624
public typealias OGValue = AGValue

Sources/OpenGraph_SPI/OGBase.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <stdint.h>
5151

5252
#define OG_ENUM CF_ENUM
53+
#define OG_CLOSED_ENUM CF_CLOSED_ENUM
5354
#define OG_OPTIONS CF_OPTIONS
5455
#define OG_EXTERN_C_BEGIN CF_EXTERN_C_BEGIN
5556
#define OG_EXTERN_C_END CF_EXTERN_C_END

0 commit comments

Comments
 (0)