diff --git a/Package.resolved b/Package.resolved index 6072f7ef..14711620 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", "state" : { "branch" : "main", - "revision" : "be1c53ecb3b0fbeedcddf26ece937ab3c774c03d" + "revision" : "63c037d0902c8bc9c237bfeb384c0e35a94ce4e7" } }, { diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.cpp deleted file mode 100644 index 653ccca6..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// -// OGTypeApplyEnumData.cpp -// -// -// Created by Kyle on 2024/1/29. -// - -#include "OGTypeApplyEnumData.h" - -bool OGTypeApplyEnumData() { - // TODO - return false; -} - -bool OGTypeApplyMutableEnumData() { - // TODO - return false; -} diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.h b/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.h deleted file mode 100644 index 9c7293ac..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyEnumData.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// OGTypeApplyEnumData.h -// -// -// Created by Kyle on 2024/1/29. -// - -#ifndef OGTypeApplyEnumData_h -#define OGTypeApplyEnumData_h - -#include "OGBase.h" - -OG_EXTERN_C_BEGIN - -// TODO -OG_EXPORT -OG_REFINED_FOR_SWIFT -bool OGTypeApplyEnumData(); - -OG_EXPORT -OG_REFINED_FOR_SWIFT -bool OGTypeApplyMutableEnumData(); - -OG_EXTERN_C_END -#endif /* OGTypeApplyEnumData_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.cpp deleted file mode 100644 index 03e9b393..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.cpp +++ /dev/null @@ -1,18 +0,0 @@ -// -// OGTypeApplyField.cpp -// -// -// Created by Kyle on 2024/1/24. -// - -#include "OGTypeApplyField.h" - -void OGTypeApplyFields(const void *type, const void *block, void *context) { - // TODO - return; -} - -bool OGTypeApplyFields2(const void *type, OGTypeApplyOptions options, const void *block, void *context) { - // TODO - return false; -} diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.h b/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.h deleted file mode 100644 index 334f59bf..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyField.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// OGTypeApplyField.h -// -// -// Created by Kyle on 2024/1/24. -// - -#ifndef OGTypeApplyField_h -#define OGTypeApplyField_h - -#include "OGBase.h" -#include "OGTypeApplyOptions.h" - -OG_EXTERN_C_BEGIN - -OG_EXPORT -OG_REFINED_FOR_SWIFT -void OGTypeApplyFields(const void *type, const void *block, void *context); - -OG_EXPORT -OG_REFINED_FOR_SWIFT -bool OGTypeApplyFields2(const void *type, OGTypeApplyOptions options, const void *block, void *context); - -OG_EXTERN_C_END - -#endif /* OGTypeApplyField_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyOptions.h b/Sources/OpenGraph_SPI/Runtime/OGTypeApplyOptions.h deleted file mode 100644 index 58aaff4c..00000000 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeApplyOptions.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// OGTypeApplyOptions.h -// -// -// Created by Kyle on 2024/1/24. -// - -#ifndef OGTypeApplyOptions_h -#define OGTypeApplyOptions_h - -#include "OGBase.h" - -typedef OG_OPTIONS(uint32_t, OGTypeApplyOptions) { - OGTypeApplyOptions_0 = 0, - OGTypeApplyOptions_1 = 1 << 0, - OGTypeApplyOptions_2 = 1 << 1, - OGTypeApplyOptions_4 = 1 << 2, -}; - -#endif /* OGTypeApplyOptions_h */ diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp index 7445c6a7..8a93689c 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp @@ -7,6 +7,7 @@ #include "OGTypeID.h" #include "metadata.hpp" +#include "../Util/assert.hpp" #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include @@ -40,28 +41,72 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) { #endif } +void OGTypeApplyFields(const void *type, const void *block, void *context) { + // TODO + return; +} + +bool OGTypeApplyFields2(const void *type, OGTypeApplyOptions options, const void *block, void *context) { + // TODO + return false; +} + #if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024 -OGTypeSignature const OGTypeGetSignature(OGTypeID typeID) { +uint32_t OGTypeGetEnumTag(OGTypeID typeID, const void *value) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED auto metadata = reinterpret_cast(typeID); - // TODO - return OGTypeSignature{}; + auto vwt = metadata->getValueWitnesses(); + if (!swift::EnumValueWitnessTable::classof(vwt)) { + OG::precondition_failure("not an enum type: %s", metadata->name(false).data); + } + auto enum_vwt = static_cast(vwt); + return enum_vwt->getEnumTag(static_cast(value), metadata); #else - return OGTypeSignature{}; + return 0; #endif } -void const* OGTypeGetDescriptor(OGTypeID typeID) { + +void OGTypeProjectEnumData(OGTypeID typeID, void *value) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED auto metadata = reinterpret_cast(typeID); - return metadata->descriptor(); + auto vwt = metadata->getValueWitnesses(); + if (!swift::EnumValueWitnessTable::classof(vwt)) { + OG::precondition_failure("not an enum type: %s", metadata->name(false).data); + } + auto enum_vwt = static_cast(vwt); + enum_vwt->destructiveProjectEnumData(static_cast(value), metadata); #else - return nullptr; + return; + #endif +} + +void OGTypeInjectEnumTag(OGTypeID typeID, uint32_t tag, void *value) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + auto metadata = reinterpret_cast(typeID); + auto vwt = metadata->getValueWitnesses(); + if (!swift::EnumValueWitnessTable::classof(vwt)) { + OG::precondition_failure("not an enum type: %s", metadata->name(false).data); + } + auto enum_vwt = static_cast(vwt); + return enum_vwt->destructiveInjectEnumTag(static_cast(value), tag, metadata); + #else + return; #endif } #endif /* OPENGRAPH_RELEASE */ +bool OGTypeApplyEnumData() { + // TODO + return false; +} + +bool OGTypeApplyMutableEnumData() { + // TODO + return false; +} + CFStringRef OGTypeDescription(OGTypeID typeID) { CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0); #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED @@ -71,6 +116,28 @@ CFStringRef OGTypeDescription(OGTypeID typeID) { return ref; } +#if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024 + +OGTypeSignature const OGTypeGetSignature(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + auto metadata = reinterpret_cast(typeID); + // TODO + return OGTypeSignature{}; + #else + return OGTypeSignature{}; + #endif +} +void const* OGTypeGetDescriptor(OGTypeID typeID) { + #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED + auto metadata = reinterpret_cast(typeID); + return metadata->descriptor(); + #else + return nullptr; + #endif +} + +#endif /* OPENGRAPH_RELEASE */ + void const* OGTypeNominalDescriptor(OGTypeID typeID) { #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED auto metadata = reinterpret_cast(typeID); diff --git a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h index 72a3a2d7..a411c0d6 100644 --- a/Sources/OpenGraph_SPI/Runtime/OGTypeID.h +++ b/Sources/OpenGraph_SPI/Runtime/OGTypeID.h @@ -30,6 +30,13 @@ typedef OG_CLOSED_ENUM(uint32_t, OGTypeKind) { OGTypeKindMetatype, } OG_SWIFT_NAME(Metadata.Kind); +typedef OG_OPTIONS(uint32_t, OGTypeApplyOptions) { + OGTypeApplyOptions_0 = 0, + OGTypeApplyOptions_1 = 1 << 0, + OGTypeApplyOptions_2 = 1 << 1, + OGTypeApplyOptions_4 = 1 << 2, +}; + #if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024 typedef struct OG_SWIFT_NAME(Signature) OGTypeSignature { @@ -44,22 +51,59 @@ OG_EXPORT OG_REFINED_FOR_SWIFT OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(self:)); +// TODO +// OGOverrideComparisonForTypeDescriptor(); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void OGTypeApplyFields(const void *type, const void *block, void *context); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +bool OGTypeApplyFields2(const void *type, OGTypeApplyOptions options, const void *block, void *context); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +uint32_t OGTypeGetEnumTag(OGTypeID typeID, const void *value) OG_SWIFT_NAME(Metadata.enumTag(self:_:)); + #if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024 OG_EXPORT OG_REFINED_FOR_SWIFT -OGTypeSignature const OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:)); +void OGTypeProjectEnumData(OGTypeID typeID, void *value) OG_SWIFT_NAME(Metadata.projectEnumData(self:_:)); OG_EXPORT OG_REFINED_FOR_SWIFT -void const* _Nullable OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:)); +void OGTypeInjectEnumTag(OGTypeID typeID, uint32_t tag, void *value) OG_SWIFT_NAME(Metadata.injectEnumTag(self:tag:_:)); #endif /* OPENGRAPH_RELEASE */ +// TODO +OG_EXPORT +OG_REFINED_FOR_SWIFT +bool OGTypeApplyEnumData(); + +// TODO +OG_EXPORT +OG_REFINED_FOR_SWIFT +bool OGTypeApplyMutableEnumData(); + OG_EXPORT OG_REFINED_FOR_SWIFT CFStringRef OGTypeDescription(OGTypeID typeID); +#if OPENGRAPH_RELEASE >= OPENGRAPH_RELEASE_2024 + +OG_EXPORT +OG_REFINED_FOR_SWIFT +OGTypeSignature const OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:)); + +OG_EXPORT +OG_REFINED_FOR_SWIFT +void const* _Nullable OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:)); + +#endif /* OPENGRAPH_RELEASE */ + OG_EXPORT OG_REFINED_FOR_SWIFT void const* _Nullable OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:)); diff --git a/Sources/OpenGraph_SPI/include/OGTypeApplyEnumData.h b/Sources/OpenGraph_SPI/include/OGTypeApplyEnumData.h deleted file mode 120000 index 05dc4ee2..00000000 --- a/Sources/OpenGraph_SPI/include/OGTypeApplyEnumData.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGTypeApplyEnumData.h \ No newline at end of file diff --git a/Sources/OpenGraph_SPI/include/OGTypeApplyField.h b/Sources/OpenGraph_SPI/include/OGTypeApplyField.h deleted file mode 120000 index 76a8b001..00000000 --- a/Sources/OpenGraph_SPI/include/OGTypeApplyField.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGTypeApplyField.h \ No newline at end of file diff --git a/Sources/OpenGraph_SPI/include/OGTypeApplyOptions.h b/Sources/OpenGraph_SPI/include/OGTypeApplyOptions.h deleted file mode 120000 index 1d91c84b..00000000 --- a/Sources/OpenGraph_SPI/include/OGTypeApplyOptions.h +++ /dev/null @@ -1 +0,0 @@ -../Runtime/OGTypeApplyOptions.h \ No newline at end of file diff --git a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h index 4adf653c..6dba3eda 100644 --- a/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h +++ b/Sources/OpenGraph_SPI/include/OpenGraph-umbrella.h @@ -16,9 +16,6 @@ #include "OGSearchOptions.h" #include "OGSubgraph.h" #include "OGTupleType.h" -#include "OGTypeApplyEnumData.h" -#include "OGTypeApplyField.h" -#include "OGTypeApplyOptions.h" #include "OGTypeID.h" #include "OGUniqueID.h" #include "OGValue.h" diff --git a/Tests/OpenGraph_SPICompatibilityTests/Runtime/MetadataTests.swift b/Tests/OpenGraph_SPICompatibilityTests/Runtime/MetadataTests.swift index 86d2408d..fcfc0d8d 100644 --- a/Tests/OpenGraph_SPICompatibilityTests/Runtime/MetadataTests.swift +++ b/Tests/OpenGraph_SPICompatibilityTests/Runtime/MetadataTests.swift @@ -3,7 +3,9 @@ // OpenGraph_SPICompatibilityTests import Testing +import Numerics +@Suite(.enabled(if: swiftToolchainSupported)) struct MetadataTests { class T1 { var a = 0 @@ -18,14 +20,21 @@ struct MetadataTests { enum T3 { case a, b } - + + enum T4: Equatable { + case none + case empty + case int(Int) + case double(Double) + } + protocol P { var a: Int { get } var b: Double { get } } - - @Test(.enabled(if: swiftToolchainSupported)) - func kind() throws { + + @Test + func kind() { #expect(Metadata(T1.self).kind == .class) #expect(Metadata(T2.self).kind == .struct) #expect(Metadata(T3.self).kind == .enum) @@ -47,9 +56,54 @@ struct MetadataTests { #expect(Metadata(P.Protocol.self).kind == .metatype) #expect(Metadata(type(of: Int.self)).kind == .metatype) } - + + @Test + func enumType() { + var none = T4.none + var empty = T4.empty + var int = T4.int(1) + var double = T4.double(2.0) + let metadata = Metadata(T4.self) + + withUnsafePointer(to: none) { #expect(metadata.enumTag($0) == 2) } + withUnsafePointer(to: empty) { #expect(metadata.enumTag($0) == 3) } + withUnsafePointer(to: int) { #expect(metadata.enumTag($0) == 0) } + withUnsafePointer(to: double) { #expect(metadata.enumTag($0) == 1) } + + withUnsafeMutablePointer(to: &int) { + metadata.projectEnumData($0) + #expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Int.self).pointee == 1) + } + withUnsafeMutablePointer(to: &double) { + metadata.projectEnumData($0) + #expect(UnsafeMutableRawPointer($0).assumingMemoryBound(to: Double.self).pointee.isApproximatelyEqual(to: 2.0)) + } + withUnsafeMutablePointer(to: &none) { + #expect($0.pointee == .none) + metadata.injectEnumTag(tag: 3, $0) + #expect($0.pointee == .empty) + } + withUnsafeMutablePointer(to: &empty) { + #expect($0.pointee == .empty) + metadata.injectEnumTag(tag: 2, $0) + #expect($0.pointee == .none) + } + withUnsafeMutablePointer(to: &int) { + #expect($0.pointee == .int(1)) + + metadata.injectEnumTag(tag: 1, $0) + #expect(metadata.enumTag($0) == 1) + #expect($0.pointee == .double(Double(bitPattern: 1))) + + metadata.injectEnumTag(tag: 2, $0) + #expect($0.pointee == .none) + + metadata.injectEnumTag(tag: 3, $0) + #expect($0.pointee == .empty) + } + } + #if OPENGRAPH_SUPPORT_2024_API - @Test(.enabled(if: swiftToolchainSupported)) func descriptor() { let t1 = Metadata(T1.self).descriptor let t2 = Metadata(T2.self).descriptor @@ -67,8 +121,8 @@ struct MetadataTests { #expect(t1 == Metadata(T1.self).descriptor) } #endif - - @Test(.enabled(if: swiftToolchainSupported)) + + @Test func nominalDescriptor() { let t1 = Metadata(T1.self).nominalDescriptor let t2 = Metadata(T2.self).nominalDescriptor @@ -82,8 +136,8 @@ struct MetadataTests { #expect(p == nil) #expect(optionalP != nil) } - - @Test(.enabled(if: swiftToolchainSupported)) + + @Test func nominalDescriptorName() throws { let t1 = Metadata(T1.self).nominalDescriptorName let t2 = Metadata(T2.self).nominalDescriptorName @@ -98,3 +152,8 @@ struct MetadataTests { try #expect(String(cString: #require(optionalP)) == "Optional") } } + +extension Metadata { + @_silgen_name("OGTypeGetEnumTag") + static func getEnumTag(_ value: V) -> UInt32 +}