From 62dbad3451040d34bcac968f2f1a5238dde01eb4 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 6 Apr 2024 01:09:20 +0800 Subject: [PATCH] Add init TypeConformance implementation --- .../Core/Runtime/ProtocolDescriptor.swift | 41 +++++++++++++++++++ .../Core/Runtime/TypeConformance.swift | 30 ++++++++++++++ .../Core/Util/ProtocolDescriptor.swift | 9 ---- .../OpenSwiftUI/Core/View/ViewVisitor.swift | 4 +- 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 Sources/OpenSwiftUI/Core/Runtime/ProtocolDescriptor.swift create mode 100644 Sources/OpenSwiftUI/Core/Runtime/TypeConformance.swift delete mode 100644 Sources/OpenSwiftUI/Core/Util/ProtocolDescriptor.swift diff --git a/Sources/OpenSwiftUI/Core/Runtime/ProtocolDescriptor.swift b/Sources/OpenSwiftUI/Core/Runtime/ProtocolDescriptor.swift new file mode 100644 index 00000000..668abe38 --- /dev/null +++ b/Sources/OpenSwiftUI/Core/Runtime/ProtocolDescriptor.swift @@ -0,0 +1,41 @@ +// +// ProtocolDescriptor.swift +// OpenSwiftUI +// +// Audited for RELEASE_2021 +// Status: WIP + +internal import COpenSwiftUI + +// MARK: - ProtocolDescriptor + +protocol ProtocolDescriptor { + static var descriptor: UnsafeRawPointer { get } +} + +// MARK: - TupleDescriptor + +protocol TupleDescriptor: ProtocolDescriptor { + static var typeCache: [ObjectIdentifier: TupleTypeDescription] { get set } +} + +extension TupleDescriptor where Self == ViewDescriptor { +// static func tupleDescription(__C.AGTupleType) -> SwiftUI.TupleTypeDescription +} + +// MARK: - TupleTypeDescription + +struct TupleTypeDescription { + let contentTypes: [(Int, TypeConformance)] +} + +// MARK: - ViewDescriptor + +struct ViewDescriptor: TupleDescriptor { + static var descriptor: UnsafeRawPointer { + _viewProtocolDescriptor() + } + + // FIXME + static var typeCache: [ObjectIdentifier : TupleTypeDescription] = [:] +} diff --git a/Sources/OpenSwiftUI/Core/Runtime/TypeConformance.swift b/Sources/OpenSwiftUI/Core/Runtime/TypeConformance.swift new file mode 100644 index 00000000..b67bc8e8 --- /dev/null +++ b/Sources/OpenSwiftUI/Core/Runtime/TypeConformance.swift @@ -0,0 +1,30 @@ +// +// TypeConformance.swift +// OpenSwiftUI +// +// Audited for RELEASE_2021 +// Status: Complete + +struct TypeConformance { + var metadata: UnsafeRawPointer + var conformance: UnsafeRawPointer + + init?(_ type: Any.Type) { + guard let conformance = swiftConformsToProtocol(type, PD.descriptor) else { + return nil + } + self.metadata = unsafeBitCast(type, to: UnsafeRawPointer.self) + self.conformance = conformance + } +} + +func conformsToProtocol(_ type: Any.Type, _ protocolDescriptor: UnsafeRawPointer) -> Bool { + swiftConformsToProtocol(type, protocolDescriptor) != nil +} + +@_silgen_name("swift_conformsToProtocol") +@inline(__always) +private func swiftConformsToProtocol( + _ type: Any.Type, + _ protocolDescriptor: UnsafeRawPointer +) -> UnsafeRawPointer? diff --git a/Sources/OpenSwiftUI/Core/Util/ProtocolDescriptor.swift b/Sources/OpenSwiftUI/Core/Util/ProtocolDescriptor.swift deleted file mode 100644 index 2e511487..00000000 --- a/Sources/OpenSwiftUI/Core/Util/ProtocolDescriptor.swift +++ /dev/null @@ -1,9 +0,0 @@ -func conformsToProtocol(_ type: Any.Type, _ protocolDescriptor: UnsafeRawPointer) -> Bool { - swiftConformsToProtocol(type, protocolDescriptor) != nil -} - -// FIXME: This kind usage of @_silgen_name is discouraged. But I'm not finding a way to declare or pass Any.Type to C -@_silgen_name("swift_conformsToProtocol") -@inline(__always) -private func swiftConformsToProtocol(_ type: Any.Type, - _ protocolDescriptor: UnsafeRawPointer) -> UnsafeRawPointer? diff --git a/Sources/OpenSwiftUI/Core/View/ViewVisitor.swift b/Sources/OpenSwiftUI/Core/View/ViewVisitor.swift index b17ab9af..d87bb25a 100644 --- a/Sources/OpenSwiftUI/Core/View/ViewVisitor.swift +++ b/Sources/OpenSwiftUI/Core/View/ViewVisitor.swift @@ -6,9 +6,9 @@ // Status: Complete protocol ViewVisitor { - func visit(_ view: V) + mutating func visit(_ view: V) } protocol ViewTypeVisitor { - func visit(type: V.Type) + mutating func visit(type: V.Type) }