Skip to content

Commit

Permalink
Add OGTypeNominalDescriptorName
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Dec 29, 2024
1 parent e2c7ebc commit 1a99438
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 56 deletions.
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ if warningsAsErrorsCondition {

let openGraphShimsTarget = Target.target(
name: "OpenGraphShims",
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -122,6 +123,7 @@ let openGraphShimsTestTarget = Target.testTarget(
"OpenGraphShims",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -131,6 +133,7 @@ let openGraphTestTarget = Target.testTarget(
"OpenGraph",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
let openGraphCompatibilityTestTarget = Target.testTarget(
Expand All @@ -139,6 +142,7 @@ let openGraphCompatibilityTestTarget = Target.testTarget(
.product(name: "RealModule", package: "swift-numerics"),
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

Expand All @@ -165,6 +169,7 @@ let package = Package(
.target(
name: "OpenGraph",
dependencies: ["OpenGraph_SPI"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
),
openGraphShimsTarget,
Expand Down
24 changes: 17 additions & 7 deletions Sources/OpenGraph_SPI/Runtime/OGTypeID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

OGTypeKind OGTypeGetKind(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
switch (metadata->getKind()) {
case swift::MetadataKind::Class: // 0x0
return OGTypeKindClass;
Expand Down Expand Up @@ -44,7 +44,7 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) {

void const* OGTypeGetSignature(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
// TODO
return nullptr;
#else
Expand All @@ -53,7 +53,7 @@ void const* OGTypeGetSignature(OGTypeID typeID) {
}
void const* OGTypeGetDescriptor(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
return metadata->descriptor();
#else
return nullptr;
Expand All @@ -65,21 +65,31 @@ void const* OGTypeGetDescriptor(OGTypeID typeID) {
CFStringRef OGTypeDescription(OGTypeID typeID) {
CFMutableStringRef ref = CFStringCreateMutable(CFAllocatorGetDefault(), 0);
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
metadata->append_description(ref);
#endif
return ref;
}

void const* OGTypeNominalDescriptor(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
OG::swift::metadata const *metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);

return metadata->nominal_descriptor();
#else
return nullptr;
#endif
}

CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) {
// TODO
char const* OGTypeNominalDescriptorName(OGTypeID typeID) {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
auto metadata = reinterpret_cast<OG::swift::metadata const*>(typeID);
auto nominal_descriptor = metadata->nominal_descriptor();
if (nominal_descriptor == nullptr) {
return nullptr;
}
return nominal_descriptor->Name.get();
#else
return nullptr;
#endif
}
8 changes: 4 additions & 4 deletions Sources/OpenGraph_SPI/Runtime/OGTypeID.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ OGTypeKind OGTypeGetKind(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.kind(sel

OG_EXPORT
OG_REFINED_FOR_SWIFT
void const* OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:));
void const* _Nullable OGTypeGetSignature(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.signature(self:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
void const* OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:));
void const* _Nullable OGTypeGetDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.descriptor(self:));

#endif /* OPENGRAPH_RELEASE */

Expand All @@ -51,11 +51,11 @@ CFStringRef OGTypeDescription(OGTypeID typeID);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void const* OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:));
void const* _Nullable OGTypeNominalDescriptor(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptor(self:));

OG_EXPORT
OG_REFINED_FOR_SWIFT
CFStringRef OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:));
char const* _Nullable OGTypeNominalDescriptorName(OGTypeID typeID) OG_SWIFT_NAME(getter:Metadata.nominalDescriptorName(self:));

OG_EXTERN_C_END

Expand Down
15 changes: 0 additions & 15 deletions Sources/OpenGraph_SPI/Runtime/metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@

using OG::swift::metadata;

void const* metadata::descriptor() const OG_NOEXCEPT {
// TODO
return nullptr;
}

void const* metadata::nominal_descriptor() const OG_NOEXCEPT {
void const* descriptor = this->descriptor();
if (descriptor == nullptr) {
return nullptr;
}
// TODO
return nullptr;
}


void metadata::append_description(CFMutableStringRef description) const OG_NOEXCEPT {
// TODO
}
Expand Down
58 changes: 54 additions & 4 deletions Sources/OpenGraph_SPI/Runtime/metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,65 @@

#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
#include <swift/Runtime/Metadata.h>
#endif
#include <swift/Runtime/HeapObject.h>
using namespace swift;
#endif /* OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED */

namespace OG {
namespace swift {
#ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED
class metadata: public ::swift::Metadata {
class metadata: public Metadata {
public:
void const* descriptor() const OG_NOEXCEPT;
void const* nominal_descriptor() const OG_NOEXCEPT;
OG_INLINE OG_CONSTEXPR
Metadata const* getType() const OG_NOEXCEPT {
return this;
}

OG_INLINE OG_CONSTEXPR
TypeNamePair const name(bool qualified) const OG_NOEXCEPT {
return swift_getTypeName(getType(), qualified);
}

OG_INLINE OG_CONSTEXPR
MetadataKind const getKind() const OG_NOEXCEPT {
return getType()->getKind();
}

OG_INLINE OG_CONSTEXPR
TypeContextDescriptor const* descriptor() const OG_NOEXCEPT {
switch (getKind()) {
case MetadataKind::Class: {
const auto cls = static_cast<const ClassMetadata *>(getType());
// We may build this with a newer OS SDK but run on old OS.
// So instead of using `isTypeMetadata` / `(Data & SWIFT_CLASS_IS_SWIFT_MASK)`,
// we manully use 3 here to check isTypeMetadata
if ((cls->Data & 3) == 0) return nullptr;
return cls->getDescription();
}
case MetadataKind::Struct:
case MetadataKind::Enum:
case MetadataKind::Optional: {
return static_cast<const TargetValueMetadata<InProcess> *>(getType())->Description;
}
default:
return nullptr;
}
}

OG_INLINE OG_CONSTEXPR
TypeContextDescriptor const* nominal_descriptor() const OG_NOEXCEPT {
auto descriptor = this->descriptor();
if (descriptor == nullptr) {
return nullptr;
}
switch(descriptor->getKind()) {
case ContextDescriptorKind::Struct:
case ContextDescriptorKind::Enum:
return descriptor;
default:
return nullptr;
}
}

void append_description(CFMutableStringRef description) const OG_NOEXCEPT;
}; /* OG::swift::metadata */
Expand Down
92 changes: 66 additions & 26 deletions Tests/OpenGraphCompatibilityTests/Runtime/MetadataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
import Testing

struct MetadataTests {
class T1 {
var a = 0
var b: Double = 0
}

struct T2 {
var a: Int
var b: Double
}

enum T3 {
case a, b
}

protocol P {
var a: Int { get }
var b: Double { get }
}

@Test(.enabled(if: swiftToolchainSupported))
func kindCases() throws {
class T1 {}
struct T2 {}
enum T3 {}
protocol P {}

func kind() throws {
#expect(Metadata(T1.self).kind == .class)
#expect(Metadata(T2.self).kind == .struct)
#expect(Metadata(T3.self).kind == .enum)
Expand All @@ -34,29 +48,25 @@ struct MetadataTests {
#expect(Metadata(type(of: Int.self)).kind == .metatype)
}

@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func descriptor() throws {
let n1 = try #require(Metadata(Int.self).nominalDescriptor)
let n2 = try #require(Metadata(String.self).nominalDescriptor)
let n3 = try #require(Metadata(Int.self).nominalDescriptor)

#expect(n1 != n2)
#expect(n1 == n3)
}

class T1 {
var a = 0
var b: Double = 0
}
#if OPENGRAPH_RELEASE_2024
@Test(.enabled(if: swiftToolchainSupported))
func descriptor() {
let t1 = Metadata(T1.self).descriptor
let t2 = Metadata(T2.self).descriptor
let t3 = Metadata(T3.self).descriptor
let p = Metadata(P.self).descriptor
let optionalP = Metadata(P?.self).descriptor

struct T2 {
var a: Int
var b: Double
}
#expect(t1 != nil)
#expect(t2 != nil)
#expect(t3 != nil)

enum T3 {
case a, b
#expect(p == nil)
#expect(optionalP != nil)

#expect(t1 == Metadata(T1.self).descriptor)
}
#endif

@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func description() {
Expand All @@ -65,6 +75,36 @@ struct MetadataTests {
#expect(Metadata(T3.self).description == "MetadataTests.T3")
}

@Test(.enabled(if: swiftToolchainSupported))
func nominalDescriptor() {
let t1 = Metadata(T1.self).nominalDescriptor
let t2 = Metadata(T2.self).nominalDescriptor
let t3 = Metadata(T3.self).nominalDescriptor
let p = Metadata(P.self).nominalDescriptor
let optionalP = Metadata(P?.self).nominalDescriptor

#expect(t1 == nil)
#expect(t2 != nil)
#expect(t3 != nil)
#expect(p == nil)
#expect(optionalP != nil)
}

@Test(.enabled(if: swiftToolchainSupported))
func nominalDescriptorName() throws {
let t1 = Metadata(T1.self).nominalDescriptorName
let t2 = Metadata(T2.self).nominalDescriptorName
let t3 = Metadata(T3.self).nominalDescriptorName
let p = Metadata(P.self).nominalDescriptorName
let optionalP = Metadata(P?.self).nominalDescriptorName

#expect(t1 == nil)
try #expect(String(cString: #require(t2)) == "T2")
try #expect(String(cString: #require(t3)) == "T3")
#expect(p == nil)
try #expect(String(cString: #require(optionalP)) == "Optional")
}

@Test(.disabled(if: !compatibilityTestEnabled, "Metadata is not implemented"))
func forEachField() throws {
for options in [OGTypeApplyOptions._1] {
Expand Down

0 comments on commit 1a99438

Please sign in to comment.