Skip to content

Commit

Permalink
Update for Swift 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bradhilton committed Apr 2, 2018
1 parent c5bc81a commit 1414e12
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Reflection.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = "Reflection"
s.version = "0.15.0"
s.version = "0.16.0"
s.summary = "Advanced Swift Reflection"
s.description = <<-DESC
Reflection enables advanced runtime features like dynamic construction of types and manipulating instance properties.
DESC
s.homepage = "https://github.com/Zewo/Reflection"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Brad Hilton" => "[email protected]" }
s.source = { :git => "https://github.com/Zewo/Reflection.git", :tag => "0.15.0" }
s.source = { :git => "https://github.com/Zewo/Reflection.git", :tag => "0.16.0" }

s.ios.deployment_target = "8.0"
s.tvos.deployment_target = "9.0"
Expand Down
2 changes: 1 addition & 1 deletion Sources/Reflection/Construct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public func construct(_ type: Any.Type, constructor: (Property.Description) thro
private func constructValueType<T>(_ constructor: (Property.Description) throws -> Any) throws -> T {
guard Metadata(type: T.self)?.kind == .struct else { throw ReflectionError.notStruct(type: T.self) }
let pointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
defer { pointer.deallocate(capacity: 1) }
defer { pointer.deallocate() }
var values: [Any] = []
try constructType(storage: UnsafeMutableRawPointer(pointer), values: &values, properties: properties(T.self), constructor: constructor)
return pointer.move()
Expand Down
6 changes: 5 additions & 1 deletion Sources/Reflection/Metadata+Class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ extension Metadata {

static let kind: Kind? = .class
var pointer: UnsafePointer<_Metadata._Class>

var nominalTypeDescriptor: NominalTypeDescriptor {
return pointer.withMemoryRebound(to: NominalTypeDescriptor.self, capacity: 15, { $0[nominalTypeDescriptorLocation] })
}

var nominalTypeDescriptorOffsetLocation: Int {
var nominalTypeDescriptorLocation: Int {
return is64BitPlatform ? 8 : 11
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Reflection/Metadata+Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ extension Metadata {
struct Struct : NominalType {
static let kind: Kind? = .struct
var pointer: UnsafePointer<_Metadata._Struct>
var nominalTypeDescriptorOffsetLocation: Int {
return 1
var nominalTypeDescriptor: NominalTypeDescriptor {
return pointer.pointee.nominalTypeDescriptor
}
}
}

extension _Metadata {
struct _Struct {
var kind: Int
var nominalTypeDescriptorOffset: Int
var nominalTypeDescriptor: NominalTypeDescriptor
var parent: Metadata?
}
}
7 changes: 1 addition & 6 deletions Sources/Reflection/NominalType.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
protocol NominalType : MetadataType {
var nominalTypeDescriptorOffsetLocation: Int { get }
var nominalTypeDescriptor: NominalTypeDescriptor { get }
}

extension NominalType {
var nominalTypeDescriptor: NominalTypeDescriptor {
let pointer = UnsafePointer<Int>(self.pointer)
let base = pointer.advanced(by: nominalTypeDescriptorOffsetLocation)
return NominalTypeDescriptor(pointer: relativePointer(base: base, offset: base.pointee))
}

var fieldTypes: [Any.Type]? {
guard let function = nominalTypeDescriptor.fieldTypesAccessor else { return nil }
Expand Down
5 changes: 4 additions & 1 deletion Sources/Reflection/NominalTypeDescriptor.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
struct NominalTypeDescriptor : PointerType {
typealias Pointee = _NominalTypeDescriptor

var pointer: UnsafePointer<_NominalTypeDescriptor>

var mangledName: String {
return String(cString: relativePointer(base: pointer, offset: pointer.pointee.mangledName) as UnsafePointer<CChar>)
// return String(cString: pointer.pointee.mangledName)
return String(cString: relativePointer(base: pointer, offset: pointer.pointee.mangledName) as UnsafePointer<UInt8>)
}

var numberOfFields: Int {
Expand Down
8 changes: 0 additions & 8 deletions Sources/Reflection/ValueWitnessTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ struct _ValueWitnessTable {
let initializeWithCopy: Int
let assignWithCopy: Int
let initializeBufferWithTake: Int
let initializeWithTake: Int
let assignWithTake: Int
let allocateBuffer: Int
let initializeBufferWithTakeOrBuffer: Int
let destroyArray: Int
let initializeArrayWithCopy: Int
let initializeArrayWithTakeFrontToBack: Int
let initializeArrayWithTakeBackToFront: Int
let size: Int
let align: Int
let stride: Int
Expand Down
17 changes: 17 additions & 0 deletions Tests/ReflectionTests/InternalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import XCTest

public class InternalTests : XCTestCase {

func testDumbedDownTest() {
struct NominalTypeDescriptor {
let something: Int32
let numberOfProperties: Int32
}

struct StructMetadata {
let kind: Int
let nominalTypeDescriptor: UnsafePointer<NominalTypeDescriptor>
}

let type: Any.Type = Person.self
let numberOfProperties = unsafeBitCast(type, to: UnsafePointer<StructMetadata>.self).pointee.nominalTypeDescriptor.pointee.numberOfProperties
print(numberOfProperties)
}


func testShallowMetadata() {
func testShallowMetadata<T>(type: T.Type, expectedKind: Metadata.Kind) {
let shallowMetadata = Metadata(type: type)
Expand Down

0 comments on commit 1414e12

Please sign in to comment.