diff --git a/Package.resolved b/Package.resolved index 990a254..bb43c47 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", "state" : { "branch" : "main", - "revision" : "8628888442c0bc214a86e887d53315981c2888d2" + "revision" : "6ff2b49a335ef2bcbf7c24dda1f65fc71e8a3179" } }, { diff --git a/Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift b/Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift index 4fd8733..070e700 100644 --- a/Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift +++ b/Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift @@ -7,6 +7,14 @@ public import OpenGraphCxx +@_silgen_name("OGGraphMutateAttribute") +private func OGGraphMutateAttribute( + _ attribute: AnyAttribute, + type: Metadata, + invalidating: Bool, + body: (UnsafeMutableRawPointer) -> Void +) + extension AnyAttribute { public typealias Flags = OGAttributeTypeFlags @@ -46,7 +54,11 @@ extension AnyAttribute { } public func mutateBody(as type: Value.Type, invalidating: Bool, _ body: (inout Value) -> Void) { - AnyAttribute.mutateAttribute(self, type: Metadata(type), invalidating: invalidating) { value in + OGGraphMutateAttribute( + self, + type: Metadata(type), + invalidating: invalidating + ) { value in body(&value.assumingMemoryBound(to: Value.self).pointee) } } @@ -87,17 +99,6 @@ extension AnyAttribute: Swift.CustomStringConvertible { public typealias AttributeUpdateBlock = () -> (UnsafeMutableRawPointer, AnyAttribute) -> Void -// FIXME: migrate to use @_extern(c, "xx") in Swift 6 -extension AnyAttribute { - @_silgen_name("OGGraphMutateAttribute") - private static func mutateAttribute( - _ attribute: AnyAttribute, - type: Metadata, - invalidating: Bool, - body: (UnsafeMutableRawPointer) -> Void - ) -} - extension [AnyAttribute] { @_transparent public var anyInputsChanged: Bool { diff --git a/Sources/OpenGraph/Graph/Graph.swift b/Sources/OpenGraph/Graph/Graph.swift index baec580..055d89a 100644 --- a/Sources/OpenGraph/Graph/Graph.swift +++ b/Sources/OpenGraph/Graph/Graph.swift @@ -20,29 +20,62 @@ extension Graph { } } +@_silgen_name("OGGraphSetInvalidationCallback") +private func OGGraphSetInvalidationCallback( + graph: Graph, + _ callback: @escaping (AnyAttribute) -> Void +) + +@_silgen_name("OGGraphSetUpdateCallback") +private func OGGraphSetUpdateCallback( + graph: Graph, + _ callback: @escaping () -> Void +) + extension Graph { - @_silgen_name("OGGraphStartProfiling") - public static func startProfiling(_ graph: Graph? = nil) + public static func withoutUpdate(_ body: () -> V) -> V { + let update = clearUpdate() + defer { setUpdate(update) } + return body() + } + + public func withoutSubgraphInvalidation(_ body: () -> V) -> V { + preconditionFailure("TODO") + } + + public func withDeadline( + _: UInt64, + _: () -> V + ) -> V { + preconditionFailure("TODO") + } + + public func onInvalidation(_ callback: @escaping (AnyAttribute) -> Void) { + OGGraphSetInvalidationCallback(graph: self, callback) + } + + public func onUpdate(_ callback: @escaping () -> Void) { + OGGraphSetUpdateCallback(graph: self, callback) + } - @_silgen_name("OGGraphStopProfiling") - public static func stopProfiling(_ graph: Graph? = nil) + public func withMainThreadHandler(_: (() -> Void) -> Void, do: () -> Void) { + // TODO: OGGraphWithMainThreadHandler + preconditionFailure("TODO") + } } -// FIXME: migrate to use @_extern(c, "xx") in Swift 6 -// > Also similar to @_silgen_name, but a function declared with @_extern(c) is assumed to use the C ABI, while @_silgen_name assumes the Swift ABI. -//extension OGGraph { -// @_silgen_name("OGGGraphSetInvalidationCallback") // Use Swift ABI(self: x20) ❌, we need C ABI here(self: x0). -// public func setInvalidationCallback(_ callback: ((AnyAttribute) -> Void)?) -// -// @_silgen_name("OGGGraphSetUpdateCallback") -// public func setUpdateCallback(_ callback: (() -> Void)?) -//} extension Graph { - @_silgen_name("OGGraphSetInvalidationCallback") - public static func setInvalidationCallback(_ graph: Graph, callback: ((AnyAttribute) -> Void)?) + public static func startProfiling() { + __OGGraphStartProfiling(nil) + } - @_silgen_name("OGGraphSetUpdateCallback") - public static func setUpdateCallback(_ graph: Graph, callback: (() -> Void)?) + public static func stopProfiling() { + __OGGraphStopProfiling(nil) + } + + public static func resetProfile() { + __OGGraphResetProfile(nil) + } } extension Graph { @@ -51,14 +84,6 @@ extension Graph { public var mainUpdates: Int { numericCast(counter(for: ._10)) } } -extension Graph { - public static func withoutUpdate(_ body: () -> Value) -> Value { - let update = clearUpdate() - defer { setUpdate(update) } - return body() - } -} - extension Graph { // NOTE: Currently Swift does not support generic computed variable @_silgen_name("OGGraphGetOutputValue") diff --git a/Sources/OpenGraph/Graph/Subgraph.swift b/Sources/OpenGraph/Graph/Subgraph.swift index 22e5639..30cddb4 100644 --- a/Sources/OpenGraph/Graph/Subgraph.swift +++ b/Sources/OpenGraph/Graph/Subgraph.swift @@ -9,7 +9,7 @@ public import OpenGraphCxx extension Subgraph { public func addObserver(_ observer: () -> Void) -> Int { - Subgraph.addObserver(self, observer: observer) + OGSubgraphAddObserver(self, observer: observer) } public func apply(_ body: () -> Value) -> Value { @@ -28,7 +28,7 @@ extension Subgraph { } public func forEach(_ flags: OGAttributeFlags, _ callback: (AnyAttribute) -> Void) { - Subgraph.apply(self, flags: flags, callback: callback) + OGSubgraphApply(self, flags: flags, callback: callback) } } @@ -52,11 +52,15 @@ extension Subgraph { } } -// FIXME: migrate to use @_extern(c, "xx") in Swift 6 -extension Subgraph { - @_silgen_name("OGSubgraphApply") - private static func apply(_ graph: Subgraph, flags: OGAttributeFlags, callback: (AnyAttribute) -> Void) - - @_silgen_name("OGSubgraphAddObserver") - private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int -} +@_silgen_name("OGSubgraphApply") +private func OGSubgraphApply( + _ graph: Subgraph, + flags: OGAttributeFlags, + callback: (AnyAttribute) -> Void +) + +@_silgen_name("OGSubgraphAddObserver") +private func OGSubgraphAddObserver( + _ graph: Subgraph, + observer: () -> Void +) -> Int diff --git a/Sources/OpenGraphCxx/Graph/OGGraph.cpp b/Sources/OpenGraphCxx/Graph/OGGraph.cpp index ffbd3fd..6aaa1a7 100644 --- a/Sources/OpenGraphCxx/Graph/OGGraph.cpp +++ b/Sources/OpenGraphCxx/Graph/OGGraph.cpp @@ -92,6 +92,10 @@ void OGGraphStopProfiling(_Nullable OGGraphRef graph) { graph->context.get_graph().stop_profiling(); } +void OGGraphResetProfile(_Nullable OGGraphRef graph) { + // TODO +} + const void * _Nullable OGGraphGetContext(OGGraphRef graph) { if (graph->context.isInvalid()) { OG::precondition_failure("invalidated graph"); diff --git a/Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h b/Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h index 811490c..abe6a4b 100644 --- a/Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h +++ b/Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h @@ -49,6 +49,10 @@ OG_EXPORT OG_REFINED_FOR_SWIFT void OGGraphStopProfiling(_Nullable OGGraphRef graph); +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGGraphResetProfile(_Nullable OGGraphRef graph); + OG_EXPORT OG_REFINED_FOR_SWIFT const void * _Nullable OGGraphGetContext(OGGraphRef graph) OG_SWIFT_NAME(getter:OGGraphRef.context(self:)); diff --git a/Tests/OpenGraphCompatibilityTests/Graph/GraphCompatibilityTests.swift b/Tests/OpenGraphCompatibilityTests/Graph/GraphCompatibilityTests.swift index fa00ca4..fdad1a2 100644 --- a/Tests/OpenGraphCompatibilityTests/Graph/GraphCompatibilityTests.swift +++ b/Tests/OpenGraphCompatibilityTests/Graph/GraphCompatibilityTests.swift @@ -70,12 +70,10 @@ struct GraphCompatibilityTests { @Test func graphCallback() { let graph = Graph() - Graph.setUpdateCallback(graph, callback: nil) - Graph.setUpdateCallback(graph) { + graph.onUpdate { print("Update") } - Graph.setInvalidationCallback(graph, callback: nil) - Graph.setInvalidationCallback(graph) { attr in + graph.onInvalidation { attr in print("Invalidate \(attr)") } }