diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..80b34c3 --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,6 @@ +disabled_rules: + - identifier_name + - trailing_whitespace + - cyclomatic_complexity +included: + - Sources diff --git a/.travis.yml b/.travis.yml index 4119c24..8dff327 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ matrix: include: - os: osx env: JOB=SwiftPM_OSX - osx_image: xcode10 + osx_image: xcode10.2 - os: linux env: JOB=SwiftPM_linux dist: trusty diff --git a/Package.resolved b/Package.resolved index cab035f..9395228 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/wickwirew/CRuntime.git", "state": { "branch": null, - "revision": "8d0dd0ca3787d15682c1f44de3d298459870b121", - "version": "1.0.0" + "revision": "1d820f9211a9836ea30735c3fe0d77908fd230ff", + "version": "2.0.1" } } ] diff --git a/Package.swift b/Package.swift index b744fd6..aa70b4e 100644 --- a/Package.swift +++ b/Package.swift @@ -1,14 +1,14 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( name: "Runtime", products: [ .library( name: "Runtime", - targets: ["Runtime"]), + targets: ["Runtime"]) ], dependencies: [ - .package(url: "https://github.com/wickwirew/CRuntime.git", from: "1.0.0") + .package(url: "https://github.com/wickwirew/CRuntime.git", from: "2.0.1") ], targets: [ .target( @@ -18,5 +18,5 @@ let package = Package( name: "RuntimeTests", dependencies: ["Runtime"]) ], - swiftLanguageVersions: [.v4_2] + swiftLanguageVersions: [.v5] ) diff --git a/Runtime.podspec b/Runtime.podspec index cddd42c..13f1ce7 100644 --- a/Runtime.podspec +++ b/Runtime.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "Runtime" - s.version = "0.7.1" + s.version = "2.0.0" s.summary = "Runtime" s.description = <<-DESC Runtime abilities for native swift objects. @@ -10,6 +10,6 @@ Pod::Spec.new do |s| s.license = "MIT" s.author = { "Wesley Wickwire" => "wickwirew@gmail.com" } s.platform = :ios, "9.0" - s.source = { :git => "https://github.com/wickwirew/Runtime.git", :tag => "0.7.1" } + s.source = { :git => "https://github.com/wickwirew/Runtime.git", :tag => s.version } s.source_files = 'Runtime/**/*.swift' end diff --git a/Runtime.xcodeproj/project.pbxproj b/Runtime.xcodeproj/project.pbxproj index 03e6504..078fd21 100644 --- a/Runtime.xcodeproj/project.pbxproj +++ b/Runtime.xcodeproj/project.pbxproj @@ -5,7 +5,7 @@ objects = { "CRuntime::CRuntime::ProductTarget" = { isa = "PBXAggregateTarget"; - buildConfigurationList = "OBJ_73"; + buildConfigurationList = "OBJ_84"; buildPhases = ( ); dependencies = ( @@ -16,6 +16,7 @@ "OBJ_1" = { isa = "PBXProject"; attributes = { + LastSwiftMigration = "9999"; LastUpgradeCheck = "9999"; }; buildConfigurationList = "OBJ_2"; @@ -26,7 +27,7 @@ "en" ); mainGroup = "OBJ_5"; - productRefGroup = "OBJ_69"; + productRefGroup = "OBJ_73"; projectDirPath = "."; targets = ( "CRuntime::CRuntime::ProductTarget", @@ -37,99 +38,148 @@ ); }; "OBJ_10" = { - isa = "PBXFileReference"; - path = "DefaultValue.swift"; + isa = "PBXGroup"; + children = ( + "OBJ_11", + "OBJ_12" + ); + name = "Factory"; + path = "Factory"; sourceTree = ""; }; "OBJ_100" = { isa = "PBXBuildFile"; - fileRef = "OBJ_31"; + fileRef = "OBJ_20"; }; "OBJ_101" = { isa = "PBXBuildFile"; - fileRef = "OBJ_32"; + fileRef = "OBJ_21"; }; "OBJ_102" = { isa = "PBXBuildFile"; - fileRef = "OBJ_33"; + fileRef = "OBJ_22"; }; "OBJ_103" = { isa = "PBXBuildFile"; - fileRef = "OBJ_34"; + fileRef = "OBJ_23"; }; "OBJ_104" = { isa = "PBXBuildFile"; - fileRef = "OBJ_35"; + fileRef = "OBJ_24"; }; "OBJ_105" = { isa = "PBXBuildFile"; - fileRef = "OBJ_37"; + fileRef = "OBJ_25"; }; "OBJ_106" = { isa = "PBXBuildFile"; - fileRef = "OBJ_38"; + fileRef = "OBJ_26"; }; "OBJ_107" = { isa = "PBXBuildFile"; - fileRef = "OBJ_39"; + fileRef = "OBJ_27"; }; "OBJ_108" = { isa = "PBXBuildFile"; - fileRef = "OBJ_40"; + fileRef = "OBJ_28"; }; "OBJ_109" = { isa = "PBXBuildFile"; - fileRef = "OBJ_41"; + fileRef = "OBJ_29"; }; "OBJ_11" = { isa = "PBXFileReference"; - path = "Factory.swift"; + path = "DefaultValue.swift"; sourceTree = ""; }; "OBJ_110" = { isa = "PBXBuildFile"; - fileRef = "OBJ_42"; + fileRef = "OBJ_30"; }; "OBJ_111" = { isa = "PBXBuildFile"; - fileRef = "OBJ_44"; + fileRef = "OBJ_31"; }; "OBJ_112" = { isa = "PBXBuildFile"; - fileRef = "OBJ_45"; + fileRef = "OBJ_33"; }; "OBJ_113" = { isa = "PBXBuildFile"; - fileRef = "OBJ_46"; + fileRef = "OBJ_34"; }; "OBJ_114" = { isa = "PBXBuildFile"; - fileRef = "OBJ_47"; + fileRef = "OBJ_35"; }; "OBJ_115" = { isa = "PBXBuildFile"; - fileRef = "OBJ_49"; + fileRef = "OBJ_36"; }; "OBJ_116" = { isa = "PBXBuildFile"; - fileRef = "OBJ_50"; + fileRef = "OBJ_37"; }; "OBJ_117" = { isa = "PBXBuildFile"; - fileRef = "OBJ_51"; + fileRef = "OBJ_38"; }; "OBJ_118" = { isa = "PBXBuildFile"; - fileRef = "OBJ_52"; + fileRef = "OBJ_39"; }; "OBJ_119" = { isa = "PBXBuildFile"; - fileRef = "OBJ_53"; + fileRef = "OBJ_40"; }; "OBJ_12" = { + isa = "PBXFileReference"; + path = "Factory.swift"; + sourceTree = ""; + }; + "OBJ_120" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_41"; + }; + "OBJ_121" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_43"; + }; + "OBJ_122" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_44"; + }; + "OBJ_123" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_45"; + }; + "OBJ_124" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_46"; + }; + "OBJ_125" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_47"; + }; + "OBJ_126" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_48"; + }; + "OBJ_127" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_49"; + }; + "OBJ_128" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_51"; + }; + "OBJ_129" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_52"; + }; + "OBJ_13" = { isa = "PBXGroup"; children = ( - "OBJ_13", "OBJ_14", "OBJ_15", "OBJ_16", @@ -142,37 +192,75 @@ "OBJ_23", "OBJ_24", "OBJ_25", - "OBJ_26" + "OBJ_26", + "OBJ_27", + "OBJ_28", + "OBJ_29", + "OBJ_30", + "OBJ_31" ); name = "Layouts"; path = "Layouts"; sourceTree = ""; }; - "OBJ_120" = { + "OBJ_130" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_53"; + }; + "OBJ_131" = { isa = "PBXBuildFile"; fileRef = "OBJ_54"; }; - "OBJ_121" = { + "OBJ_132" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_55"; + }; + "OBJ_133" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_57"; + }; + "OBJ_134" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_58"; + }; + "OBJ_135" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_59"; + }; + "OBJ_136" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_60"; + }; + "OBJ_137" = { + isa = "PBXBuildFile"; + fileRef = "OBJ_61"; + }; + "OBJ_138" = { isa = "PBXFrameworksBuildPhase"; files = ( ); }; - "OBJ_123" = { + "OBJ_14" = { + isa = "PBXFileReference"; + path = "ClassHeader.swift"; + sourceTree = ""; + }; + "OBJ_140" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_124", - "OBJ_125" + "OBJ_141", + "OBJ_142" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_124" = { + "OBJ_141" = { isa = "XCBuildConfiguration"; buildSettings = { LD = "/usr/bin/true"; OTHER_SWIFT_FLAGS = ( "-swift-version", - "4.2", + "5", "-I", "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", "-target", @@ -180,17 +268,17 @@ "-sdk", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; }; name = "Debug"; }; - "OBJ_125" = { + "OBJ_142" = { isa = "XCBuildConfiguration"; buildSettings = { LD = "/usr/bin/true"; OTHER_SWIFT_FLAGS = ( "-swift-version", - "4.2", + "5", "-I", "$(TOOLCHAIN_DIR)/usr/lib/swift/pm/4_2", "-target", @@ -198,60 +286,60 @@ "-sdk", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; }; name = "Release"; }; - "OBJ_126" = { + "OBJ_143" = { isa = "PBXSourcesBuildPhase"; files = ( - "OBJ_127" + "OBJ_144" ); }; - "OBJ_127" = { + "OBJ_144" = { isa = "PBXBuildFile"; fileRef = "OBJ_6"; }; - "OBJ_129" = { + "OBJ_146" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_130", - "OBJ_131" + "OBJ_147", + "OBJ_148" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_13" = { - isa = "PBXFileReference"; - path = "ClassHeader.swift"; - sourceTree = ""; - }; - "OBJ_130" = { + "OBJ_147" = { isa = "XCBuildConfiguration"; buildSettings = { }; name = "Debug"; }; - "OBJ_131" = { + "OBJ_148" = { isa = "XCBuildConfiguration"; buildSettings = { }; name = "Release"; }; - "OBJ_132" = { + "OBJ_149" = { isa = "PBXTargetDependency"; target = "Runtime::RuntimeTests"; }; - "OBJ_134" = { + "OBJ_15" = { + isa = "PBXFileReference"; + path = "ClassMetadataLayout.swift"; + sourceTree = ""; + }; + "OBJ_151" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_135", - "OBJ_136" + "OBJ_152", + "OBJ_153" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_135" = { + "OBJ_152" = { isa = "XCBuildConfiguration"; buildSettings = { CLANG_ENABLE_MODULES = "YES"; @@ -262,14 +350,16 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/CRuntime.git--1672828878229581488/Sources/CRuntime" + "$(SRCROOT)/.build/checkouts/CRuntime/Sources/CRuntime" ); INFOPLIST_FILE = "Runtime.xcodeproj/RuntimeTests_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@loader_path/../Frameworks", "@loader_path/Frameworks" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)" ); @@ -282,12 +372,14 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "RuntimeTests"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Debug"; }; - "OBJ_136" = { + "OBJ_153" = { isa = "XCBuildConfiguration"; buildSettings = { CLANG_ENABLE_MODULES = "YES"; @@ -298,14 +390,16 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/CRuntime.git--1672828878229581488/Sources/CRuntime" + "$(SRCROOT)/.build/checkouts/CRuntime/Sources/CRuntime" ); INFOPLIST_FILE = "Runtime.xcodeproj/RuntimeTests_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@loader_path/../Frameworks", "@loader_path/Frameworks" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)" ); @@ -318,93 +412,85 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "RuntimeTests"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Release"; }; - "OBJ_137" = { + "OBJ_154" = { isa = "PBXSourcesBuildPhase"; files = ( - "OBJ_138", - "OBJ_139", - "OBJ_140", - "OBJ_141", - "OBJ_142", - "OBJ_143", - "OBJ_144" + "OBJ_155", + "OBJ_156", + "OBJ_157", + "OBJ_158", + "OBJ_159", + "OBJ_160", + "OBJ_161" ); }; - "OBJ_138" = { + "OBJ_155" = { isa = "PBXBuildFile"; - fileRef = "OBJ_57"; + fileRef = "OBJ_65"; }; - "OBJ_139" = { + "OBJ_156" = { isa = "PBXBuildFile"; - fileRef = "OBJ_58"; - }; - "OBJ_14" = { - isa = "PBXFileReference"; - path = "ClassMetadataLayout.swift"; - sourceTree = ""; + fileRef = "OBJ_66"; }; - "OBJ_140" = { + "OBJ_157" = { isa = "PBXBuildFile"; - fileRef = "OBJ_59"; + fileRef = "OBJ_67"; }; - "OBJ_141" = { + "OBJ_158" = { isa = "PBXBuildFile"; - fileRef = "OBJ_60"; + fileRef = "OBJ_68"; }; - "OBJ_142" = { + "OBJ_159" = { isa = "PBXBuildFile"; - fileRef = "OBJ_61"; + fileRef = "OBJ_69"; }; - "OBJ_143" = { + "OBJ_16" = { + isa = "PBXFileReference"; + path = "ClassTypeDescriptor.swift"; + sourceTree = ""; + }; + "OBJ_160" = { isa = "PBXBuildFile"; - fileRef = "OBJ_62"; + fileRef = "OBJ_70"; }; - "OBJ_144" = { + "OBJ_161" = { isa = "PBXBuildFile"; - fileRef = "OBJ_63"; + fileRef = "OBJ_71"; }; - "OBJ_145" = { + "OBJ_162" = { isa = "PBXFrameworksBuildPhase"; files = ( - "OBJ_146" + "OBJ_163" ); }; - "OBJ_146" = { + "OBJ_163" = { isa = "PBXBuildFile"; fileRef = "Runtime::Runtime::Product"; }; - "OBJ_147" = { + "OBJ_164" = { isa = "PBXTargetDependency"; target = "Runtime::Runtime"; }; - "OBJ_15" = { - isa = "PBXFileReference"; - path = "ClassTypeDescriptor.swift"; - sourceTree = ""; - }; - "OBJ_16" = { - isa = "PBXFileReference"; - path = "EnumMetadataLayout.swift"; - sourceTree = ""; - }; "OBJ_17" = { isa = "PBXFileReference"; - path = "ExistentialContainter.swift"; + path = "EnumMetadataLayout.swift"; sourceTree = ""; }; "OBJ_18" = { isa = "PBXFileReference"; - path = "FunctionMetadataLayout.swift"; + path = "EnumTypeDescriptor.swift"; sourceTree = ""; }; "OBJ_19" = { isa = "PBXFileReference"; - path = "MetadataLayoutType.swift"; + path = "ExistentialContainter.swift"; sourceTree = ""; }; "OBJ_2" = { @@ -418,63 +504,52 @@ }; "OBJ_20" = { isa = "PBXFileReference"; - path = "ProtocolDescriptor.swift"; + path = "FieldDescriptor.swift"; sourceTree = ""; }; "OBJ_21" = { isa = "PBXFileReference"; - path = "ProtocolMetadataLayout.swift"; + path = "FunctionMetadataLayout.swift"; sourceTree = ""; }; "OBJ_22" = { isa = "PBXFileReference"; - path = "ProtocolTypeContainer.swift"; + path = "MetadataLayoutType.swift"; sourceTree = ""; }; "OBJ_23" = { isa = "PBXFileReference"; - path = "StructMetadataLayout.swift"; + path = "ProtocolDescriptor.swift"; sourceTree = ""; }; "OBJ_24" = { isa = "PBXFileReference"; - path = "StructTypeDescriptor.swift"; + path = "ProtocolMetadataLayout.swift"; sourceTree = ""; }; "OBJ_25" = { isa = "PBXFileReference"; - path = "TupleMetadataLayout.swift"; + path = "ProtocolTypeContainer.swift"; sourceTree = ""; }; "OBJ_26" = { isa = "PBXFileReference"; - path = "ValueWitnessTable.swift"; + path = "StructMetadataLayout.swift"; sourceTree = ""; }; "OBJ_27" = { - isa = "PBXGroup"; - children = ( - "OBJ_28", - "OBJ_29", - "OBJ_30", - "OBJ_31", - "OBJ_32", - "OBJ_33", - "OBJ_34", - "OBJ_35" - ); - name = "Metadata"; - path = "Metadata"; + isa = "PBXFileReference"; + path = "StructTypeDescriptor.swift"; sourceTree = ""; }; "OBJ_28" = { isa = "PBXFileReference"; - path = "ClassMetadata.swift"; + path = "TargetTypeGenericContextDescriptorHeader.swift"; sourceTree = ""; }; "OBJ_29" = { isa = "PBXFileReference"; - path = "EnumMetadata.swift"; + path = "TupleMetadataLayout.swift"; sourceTree = ""; }; "OBJ_3" = { @@ -488,8 +563,9 @@ ENABLE_NS_ASSERTIONS = "YES"; GCC_OPTIMIZATION_LEVEL = "0"; GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - "$(inherited)" + "$(inherited)", + "SWIFT_PACKAGE=1", + "DEBUG=1" ); MACOSX_DEPLOYMENT_TARGET = "10.10"; ONLY_ACTIVE_ARCH = "YES"; @@ -508,6 +584,7 @@ "watchsimulator" ); SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", "SWIFT_PACKAGE", "DEBUG" ); @@ -518,61 +595,64 @@ }; "OBJ_30" = { isa = "PBXFileReference"; - path = "FuntionMetadata.swift"; + path = "TypeDescriptor.swift"; sourceTree = ""; }; "OBJ_31" = { isa = "PBXFileReference"; - path = "Metadata.swift"; + path = "ValueWitnessTable.swift"; sourceTree = ""; }; "OBJ_32" = { - isa = "PBXFileReference"; - path = "MetadataType.swift"; + isa = "PBXGroup"; + children = ( + "OBJ_33", + "OBJ_34", + "OBJ_35", + "OBJ_36", + "OBJ_37", + "OBJ_38", + "OBJ_39", + "OBJ_40", + "OBJ_41" + ); + name = "Metadata"; + path = "Metadata"; sourceTree = ""; }; "OBJ_33" = { isa = "PBXFileReference"; - path = "ProtocolMetadata.swift"; + path = "ClassMetadata.swift"; sourceTree = ""; }; "OBJ_34" = { isa = "PBXFileReference"; - path = "StructMetadata.swift"; + path = "EnumMetadata.swift"; sourceTree = ""; }; "OBJ_35" = { isa = "PBXFileReference"; - path = "TupleMetadata.swift"; + path = "FuntionMetadata.swift"; sourceTree = ""; }; "OBJ_36" = { - isa = "PBXGroup"; - children = ( - "OBJ_37", - "OBJ_38", - "OBJ_39", - "OBJ_40", - "OBJ_41", - "OBJ_42" - ); - name = "Models"; - path = "Models"; + isa = "PBXFileReference"; + path = "Metadata.swift"; sourceTree = ""; }; "OBJ_37" = { isa = "PBXFileReference"; - path = "Errors.swift"; + path = "MetadataType.swift"; sourceTree = ""; }; "OBJ_38" = { isa = "PBXFileReference"; - path = "FunctionInfo.swift"; + path = "NominalMetadataType.swift"; sourceTree = ""; }; "OBJ_39" = { isa = "PBXFileReference"; - path = "Kind.swift"; + path = "ProtocolMetadata.swift"; sourceTree = ""; }; "OBJ_4" = { @@ -584,6 +664,10 @@ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DYLIB_INSTALL_NAME_BASE = "@rpath"; GCC_OPTIMIZATION_LEVEL = "s"; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "SWIFT_PACKAGE=1" + ); MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_SWIFT_FLAGS = ( "-DXcode" @@ -600,6 +684,7 @@ "watchsimulator" ); SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( + "$(inherited)", "SWIFT_PACKAGE" ); SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; @@ -609,68 +694,62 @@ }; "OBJ_40" = { isa = "PBXFileReference"; - path = "PropertyInfo.swift"; + path = "StructMetadata.swift"; sourceTree = ""; }; "OBJ_41" = { isa = "PBXFileReference"; - path = "TypeInfo.swift"; + path = "TupleMetadata.swift"; sourceTree = ""; }; "OBJ_42" = { - isa = "PBXFileReference"; - path = "TypeInfoConvertible.swift"; - sourceTree = ""; - }; - "OBJ_43" = { isa = "PBXGroup"; children = ( + "OBJ_43", "OBJ_44", "OBJ_45", "OBJ_46", - "OBJ_47" + "OBJ_47", + "OBJ_48", + "OBJ_49" ); - name = "Pointers"; - path = "Pointers"; + name = "Models"; + path = "Models"; + sourceTree = ""; + }; + "OBJ_43" = { + isa = "PBXFileReference"; + path = "Case.swift"; sourceTree = ""; }; "OBJ_44" = { isa = "PBXFileReference"; - path = "Pointers.swift"; + path = "Errors.swift"; sourceTree = ""; }; "OBJ_45" = { isa = "PBXFileReference"; - path = "RelativePointer.swift"; + path = "FunctionInfo.swift"; sourceTree = ""; }; "OBJ_46" = { isa = "PBXFileReference"; - path = "RelativeVectorPointer.swift"; + path = "Kind.swift"; sourceTree = ""; }; "OBJ_47" = { isa = "PBXFileReference"; - path = "Vector.swift"; + path = "PropertyInfo.swift"; sourceTree = ""; }; "OBJ_48" = { - isa = "PBXGroup"; - children = ( - "OBJ_49", - "OBJ_50", - "OBJ_51", - "OBJ_52", - "OBJ_53", - "OBJ_54" - ); - name = "Utilities"; - path = "Utilities"; + isa = "PBXFileReference"; + path = "TypeInfo.swift"; sourceTree = ""; }; "OBJ_49" = { isa = "PBXFileReference"; - path = "GetFieldAt.swift"; + path = "TypeInfoConvertible.swift"; sourceTree = ""; }; "OBJ_5" = { @@ -678,50 +757,57 @@ children = ( "OBJ_6", "OBJ_7", - "OBJ_55", - "OBJ_64", - "OBJ_65", - "OBJ_66", - "OBJ_67", - "OBJ_68", - "OBJ_69" + "OBJ_62", + "OBJ_72", + "OBJ_73", + "OBJ_76", + "OBJ_77", + "OBJ_78", + "OBJ_79", + "OBJ_80", + "OBJ_81", + "OBJ_82" ); path = ""; sourceTree = ""; }; "OBJ_50" = { - isa = "PBXFileReference"; - path = "GettersSetters.swift"; + isa = "PBXGroup"; + children = ( + "OBJ_51", + "OBJ_52", + "OBJ_53", + "OBJ_54", + "OBJ_55" + ); + name = "Pointers"; + path = "Pointers"; sourceTree = ""; }; "OBJ_51" = { isa = "PBXFileReference"; - path = "IntegerConvertible.swift"; + path = "Pointers.swift"; sourceTree = ""; }; "OBJ_52" = { isa = "PBXFileReference"; - path = "Pointer+Extensions.swift"; + path = "RelativePointer.swift"; sourceTree = ""; }; "OBJ_53" = { isa = "PBXFileReference"; - path = "RetainCounts.swift"; + path = "RelativeVectorPointer.swift"; sourceTree = ""; }; "OBJ_54" = { isa = "PBXFileReference"; - path = "String+Extensions.swift"; + path = "Union.swift"; sourceTree = ""; }; "OBJ_55" = { - isa = "PBXGroup"; - children = ( - "OBJ_56" - ); - name = "Tests"; - path = ""; - sourceTree = "SOURCE_ROOT"; + isa = "PBXFileReference"; + path = "Vector.swift"; + sourceTree = ""; }; "OBJ_56" = { isa = "PBXGroup"; @@ -730,27 +816,25 @@ "OBJ_58", "OBJ_59", "OBJ_60", - "OBJ_61", - "OBJ_62", - "OBJ_63" + "OBJ_61" ); - name = "RuntimeTests"; - path = "Tests/RuntimeTests"; - sourceTree = "SOURCE_ROOT"; + name = "Utilities"; + path = "Utilities"; + sourceTree = ""; }; "OBJ_57" = { isa = "PBXFileReference"; - path = "FactoryTests.swift"; + path = "GettersSetters.swift"; sourceTree = ""; }; "OBJ_58" = { isa = "PBXFileReference"; - path = "GetSetClassTests.swift"; + path = "IntegerConvertible.swift"; sourceTree = ""; }; "OBJ_59" = { isa = "PBXFileReference"; - path = "GetSetStructTests.swift"; + path = "Pointer+Extensions.swift"; sourceTree = ""; }; "OBJ_6" = { @@ -761,45 +845,89 @@ }; "OBJ_60" = { isa = "PBXFileReference"; - path = "MetadataTests.swift"; + path = "RetainCounts.swift"; sourceTree = ""; }; "OBJ_61" = { isa = "PBXFileReference"; - path = "ValuePointerTests.swift"; + path = "String+Extensions.swift"; sourceTree = ""; }; "OBJ_62" = { - isa = "PBXFileReference"; - path = "ValueWitnessTableTests.swift"; - sourceTree = ""; + isa = "PBXGroup"; + children = ( + "OBJ_63" + ); + name = "Tests"; + path = ""; + sourceTree = "SOURCE_ROOT"; }; "OBJ_63" = { - isa = "PBXFileReference"; - path = "XCTestManifests.swift"; - sourceTree = ""; + isa = "PBXGroup"; + children = ( + "OBJ_64", + "OBJ_65", + "OBJ_66", + "OBJ_67", + "OBJ_68", + "OBJ_69", + "OBJ_70", + "OBJ_71" + ); + name = "RuntimeTests"; + path = "Tests/RuntimeTests"; + sourceTree = "SOURCE_ROOT"; }; "OBJ_64" = { isa = "PBXFileReference"; - path = "Resources"; - sourceTree = "SOURCE_ROOT"; + path = "Info.plist"; + sourceTree = ""; }; "OBJ_65" = { isa = "PBXFileReference"; - path = "Runtime"; - sourceTree = "SOURCE_ROOT"; + path = "FactoryTests.swift"; + sourceTree = ""; }; "OBJ_66" = { isa = "PBXFileReference"; - path = "Scripts"; - sourceTree = "SOURCE_ROOT"; + path = "GetSetClassTests.swift"; + sourceTree = ""; }; "OBJ_67" = { isa = "PBXFileReference"; - path = "RuntimeTests"; - sourceTree = "SOURCE_ROOT"; + path = "GetSetStructTests.swift"; + sourceTree = ""; }; "OBJ_68" = { + isa = "PBXFileReference"; + path = "MetadataTests.swift"; + sourceTree = ""; + }; + "OBJ_69" = { + isa = "PBXFileReference"; + path = "ValuePointerTests.swift"; + sourceTree = ""; + }; + "OBJ_7" = { + isa = "PBXGroup"; + children = ( + "OBJ_8" + ); + name = "Sources"; + path = ""; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_70" = { + isa = "PBXFileReference"; + path = "ValueWitnessTableTests.swift"; + sourceTree = ""; + }; + "OBJ_71" = { + isa = "PBXFileReference"; + path = "XCTestManifests.swift"; + sourceTree = ""; + }; + "OBJ_72" = { isa = "PBXGroup"; children = ( ); @@ -807,56 +935,97 @@ path = ""; sourceTree = ""; }; - "OBJ_69" = { + "OBJ_73" = { isa = "PBXGroup"; children = ( - "Runtime::Runtime::Product", - "Runtime::RuntimeTests::Product" + "Runtime::RuntimeTests::Product", + "Runtime::Runtime::Product" ); name = "Products"; path = ""; sourceTree = "BUILT_PRODUCTS_DIR"; }; - "OBJ_7" = { + "OBJ_76" = { + isa = "PBXFileReference"; + path = "Resources"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_77" = { + isa = "PBXFileReference"; + path = "Runtime"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_78" = { + isa = "PBXFileReference"; + path = "Scripts"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_79" = { + isa = "PBXFileReference"; + path = "RuntimeTests"; + sourceTree = "SOURCE_ROOT"; + }; + "OBJ_8" = { isa = "PBXGroup"; children = ( - "OBJ_8" + "OBJ_9", + "OBJ_10", + "OBJ_13", + "OBJ_32", + "OBJ_42", + "OBJ_50", + "OBJ_56" ); - name = "Sources"; - path = ""; + name = "Runtime"; + path = "Sources/Runtime"; sourceTree = "SOURCE_ROOT"; }; - "OBJ_73" = { + "OBJ_80" = { + isa = "PBXFileReference"; + path = "LICENSE"; + sourceTree = ""; + }; + "OBJ_81" = { + isa = "PBXFileReference"; + path = "README.md"; + sourceTree = ""; + }; + "OBJ_82" = { + isa = "PBXFileReference"; + path = "Runtime.podspec"; + sourceTree = ""; + }; + "OBJ_84" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_74", - "OBJ_75" + "OBJ_85", + "OBJ_86" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_74" = { + "OBJ_85" = { isa = "XCBuildConfiguration"; buildSettings = { }; name = "Debug"; }; - "OBJ_75" = { + "OBJ_86" = { isa = "XCBuildConfiguration"; buildSettings = { }; name = "Release"; }; - "OBJ_77" = { + "OBJ_88" = { isa = "XCConfigurationList"; buildConfigurations = ( - "OBJ_78", - "OBJ_79" + "OBJ_89", + "OBJ_90" ); defaultConfigurationIsVisible = "0"; defaultConfigurationName = "Release"; }; - "OBJ_78" = { + "OBJ_89" = { isa = "XCBuildConfiguration"; buildSettings = { ENABLE_TESTABILITY = "YES"; @@ -866,13 +1035,15 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/CRuntime.git--1672828878229581488/Sources/CRuntime" + "$(SRCROOT)/.build/checkouts/CRuntime/Sources/CRuntime" ); INFOPLIST_FILE = "Runtime.xcodeproj/Runtime_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)" ); @@ -889,12 +1060,19 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "Runtime"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Debug"; }; - "OBJ_79" = { + "OBJ_9" = { + isa = "PBXFileReference"; + path = "Runtime.h"; + sourceTree = ""; + }; + "OBJ_90" = { isa = "XCBuildConfiguration"; buildSettings = { ENABLE_TESTABILITY = "YES"; @@ -904,13 +1082,15 @@ ); HEADER_SEARCH_PATHS = ( "$(inherited)", - "$(SRCROOT)/.build/checkouts/CRuntime.git--1672828878229581488/Sources/CRuntime" + "$(SRCROOT)/.build/checkouts/CRuntime/Sources/CRuntime" ); INFOPLIST_FILE = "Runtime.xcodeproj/Runtime_Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = "8.0"; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "$(TOOLCHAIN_DIR)/usr/lib/swift/macosx" ); + MACOSX_DEPLOYMENT_TARGET = "10.10"; OTHER_CFLAGS = ( "$(inherited)" ); @@ -927,39 +1107,16 @@ SWIFT_ACTIVE_COMPILATION_CONDITIONS = ( "$(inherited)" ); - SWIFT_VERSION = "4.2"; + SWIFT_VERSION = "5.0"; TARGET_NAME = "Runtime"; + TVOS_DEPLOYMENT_TARGET = "9.0"; + WATCHOS_DEPLOYMENT_TARGET = "2.0"; }; name = "Release"; }; - "OBJ_8" = { - isa = "PBXGroup"; - children = ( - "OBJ_9", - "OBJ_12", - "OBJ_27", - "OBJ_36", - "OBJ_43", - "OBJ_48" - ); - name = "Runtime"; - path = "Sources/Runtime"; - sourceTree = "SOURCE_ROOT"; - }; - "OBJ_80" = { + "OBJ_91" = { isa = "PBXSourcesBuildPhase"; files = ( - "OBJ_81", - "OBJ_82", - "OBJ_83", - "OBJ_84", - "OBJ_85", - "OBJ_86", - "OBJ_87", - "OBJ_88", - "OBJ_89", - "OBJ_90", - "OBJ_91", "OBJ_92", "OBJ_93", "OBJ_94", @@ -988,101 +1145,64 @@ "OBJ_117", "OBJ_118", "OBJ_119", - "OBJ_120" - ); - }; - "OBJ_81" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_10"; - }; - "OBJ_82" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_11"; - }; - "OBJ_83" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_13"; - }; - "OBJ_84" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_14"; - }; - "OBJ_85" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_15"; - }; - "OBJ_86" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_16"; - }; - "OBJ_87" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_17"; - }; - "OBJ_88" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_18"; - }; - "OBJ_89" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_19"; - }; - "OBJ_9" = { - isa = "PBXGroup"; - children = ( - "OBJ_10", - "OBJ_11" + "OBJ_120", + "OBJ_121", + "OBJ_122", + "OBJ_123", + "OBJ_124", + "OBJ_125", + "OBJ_126", + "OBJ_127", + "OBJ_128", + "OBJ_129", + "OBJ_130", + "OBJ_131", + "OBJ_132", + "OBJ_133", + "OBJ_134", + "OBJ_135", + "OBJ_136", + "OBJ_137" ); - name = "Factory"; - path = "Factory"; - sourceTree = ""; - }; - "OBJ_90" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_20"; - }; - "OBJ_91" = { - isa = "PBXBuildFile"; - fileRef = "OBJ_21"; }; "OBJ_92" = { isa = "PBXBuildFile"; - fileRef = "OBJ_22"; + fileRef = "OBJ_11"; }; "OBJ_93" = { isa = "PBXBuildFile"; - fileRef = "OBJ_23"; + fileRef = "OBJ_12"; }; "OBJ_94" = { isa = "PBXBuildFile"; - fileRef = "OBJ_24"; + fileRef = "OBJ_14"; }; "OBJ_95" = { isa = "PBXBuildFile"; - fileRef = "OBJ_25"; + fileRef = "OBJ_15"; }; "OBJ_96" = { isa = "PBXBuildFile"; - fileRef = "OBJ_26"; + fileRef = "OBJ_16"; }; "OBJ_97" = { isa = "PBXBuildFile"; - fileRef = "OBJ_28"; + fileRef = "OBJ_17"; }; "OBJ_98" = { isa = "PBXBuildFile"; - fileRef = "OBJ_29"; + fileRef = "OBJ_18"; }; "OBJ_99" = { isa = "PBXBuildFile"; - fileRef = "OBJ_30"; + fileRef = "OBJ_19"; }; "Runtime::Runtime" = { isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_77"; + buildConfigurationList = "OBJ_88"; buildPhases = ( - "OBJ_80", - "OBJ_121" + "OBJ_91", + "OBJ_138" ); dependencies = ( ); @@ -1098,24 +1218,24 @@ }; "Runtime::RuntimePackageTests::ProductTarget" = { isa = "PBXAggregateTarget"; - buildConfigurationList = "OBJ_129"; + buildConfigurationList = "OBJ_146"; buildPhases = ( ); dependencies = ( - "OBJ_132" + "OBJ_149" ); name = "RuntimePackageTests"; productName = "RuntimePackageTests"; }; "Runtime::RuntimeTests" = { isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_134"; + buildConfigurationList = "OBJ_151"; buildPhases = ( - "OBJ_137", - "OBJ_145" + "OBJ_154", + "OBJ_162" ); dependencies = ( - "OBJ_147" + "OBJ_164" ); name = "RuntimeTests"; productName = "RuntimeTests"; @@ -1129,9 +1249,9 @@ }; "Runtime::SwiftPMPackageDescription" = { isa = "PBXNativeTarget"; - buildConfigurationList = "OBJ_123"; + buildConfigurationList = "OBJ_140"; buildPhases = ( - "OBJ_126" + "OBJ_143" ); dependencies = ( ); diff --git a/Sources/Runtime/Factory/DefaultValue.swift b/Sources/Runtime/Factory/DefaultValue.swift index 5c62d21..42ba5b9 100644 --- a/Sources/Runtime/Factory/DefaultValue.swift +++ b/Sources/Runtime/Factory/DefaultValue.swift @@ -22,7 +22,6 @@ import Foundation - public protocol DefaultConstructor { init() } @@ -50,10 +49,8 @@ extension Array: DefaultConstructor {} extension Dictionary: DefaultConstructor {} extension Set: DefaultConstructor {} - extension Character: DefaultConstructor { public init() { self = " " } } - diff --git a/Sources/Runtime/Factory/Factory.swift b/Sources/Runtime/Factory/Factory.swift index 7521392..e4a523b 100644 --- a/Sources/Runtime/Factory/Factory.swift +++ b/Sources/Runtime/Factory/Factory.swift @@ -22,7 +22,6 @@ import Foundation - public func createInstance(constructor: ((PropertyInfo) throws -> Any)? = nil) throws -> T { if let value = try createInstance(of: T.self, constructor: constructor) as? T { return value @@ -81,7 +80,9 @@ func buildStruct(type: Any.Type, constructor: ((PropertyInfo) throws -> Any)? = } #endif -func setProperties(typeInfo: TypeInfo, pointer: UnsafeMutableRawPointer, constructor: ((PropertyInfo) throws -> Any)? = nil) throws { +func setProperties(typeInfo: TypeInfo, + pointer: UnsafeMutableRawPointer, + constructor: ((PropertyInfo) throws -> Any)? = nil) throws { for property in typeInfo.properties { let value = try constructor.map { (resolver) -> Any in return try resolver(property) @@ -93,7 +94,6 @@ func setProperties(typeInfo: TypeInfo, pointer: UnsafeMutableRawPointer, constru } } - func defaultValue(of type: Any.Type) throws -> Any { if let constructable = type as? DefaultConstructor.Type { diff --git a/Sources/Runtime/Layouts/ClassHeader.swift b/Sources/Runtime/Layouts/ClassHeader.swift index 29f13b1..41a0177 100644 --- a/Sources/Runtime/Layouts/ClassHeader.swift +++ b/Sources/Runtime/Layouts/ClassHeader.swift @@ -22,15 +22,12 @@ import Foundation - - struct ClassHeader { var isaPointer: Int var strongRetainCounts: Int32 var weakRetainCounts: Int32 } - extension ClassHeader { static func size() -> Int { diff --git a/Sources/Runtime/Layouts/ClassMetadataLayout.swift b/Sources/Runtime/Layouts/ClassMetadataLayout.swift index ad70844..ce04d82 100644 --- a/Sources/Runtime/Layouts/ClassMetadataLayout.swift +++ b/Sources/Runtime/Layouts/ClassMetadataLayout.swift @@ -22,22 +22,19 @@ import Foundation - -struct ClassMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var isaPointer: Int +struct ClassMetadataLayout: NominalMetadataLayoutType { + var _kind: Int // isaPointer for classes var superClass: Any.Type var objCRuntimeReserve1: Int var objCRuntimeReserve2: Int var rodataPointer: Int var classFlags: Int32 - var instanceAddressPoint: Int32 - var instanceSize: Int32 - var instanceAlignmentMask: Int16 - var runtimeReserveField: Int16 - var classObjectSize: Int32 - var classObjectAddressPoint: Int32 + var instanceAddressPoint: UInt32 + var instanceSize: UInt32 + var instanceAlignmentMask: UInt16 + var reserved: UInt16 + var classSize: UInt32 + var classAddressPoint: UInt32 var typeDescriptor: UnsafeMutablePointer - var genericParameterVector: RelativeVectorPointer + var iVarDestroyer: UnsafeRawPointer } - diff --git a/Sources/Runtime/Layouts/ClassTypeDescriptor.swift b/Sources/Runtime/Layouts/ClassTypeDescriptor.swift index a37ea8d..9759ce1 100644 --- a/Sources/Runtime/Layouts/ClassTypeDescriptor.swift +++ b/Sources/Runtime/Layouts/ClassTypeDescriptor.swift @@ -20,15 +20,39 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -struct ClassTypeDescriptor { - var flags: Int32 +struct ClassTypeDescriptor: TypeDescriptor { + var flags: ContextDescriptorFlags var parent: Int32 - var className: RelativePointer + var mangledName: RelativePointer var fieldTypesAccessor: RelativePointer + var fieldDescriptor: RelativePointer var superClass: RelativePointer - var resilientMetadataBounds: Int32 + var negativeSizeAndBoundsUnion: NegativeSizeAndBoundsUnion var metadataPositiveSizeInWords: Int32 var numImmediateMembers: Int32 var numberOfFields: Int32 - var fieldOffsetVectorOffset: RelativeVectorPointer + var offsetToTheFieldOffsetVector: RelativeVectorPointer + var genericContextHeader: TargetTypeGenericContextDescriptorHeader + + struct NegativeSizeAndBoundsUnion: Union { + var raw: Int32 + + var metadataNegativeSizeInWords: Int32 { + return raw + } + + mutating func resilientMetadataBounds() -> UnsafeMutablePointer> { + return bind() + } + } +} + +struct TargetStoredClassMetadataBounds { + var immediateMembersOffset: Int + var bounds: TargetMetadataBounds +} + +struct TargetMetadataBounds { + var negativeSizeWords: UInt32 + var positiveSizeWords: UInt32 } diff --git a/Sources/Runtime/Layouts/EnumMetadataLayout.swift b/Sources/Runtime/Layouts/EnumMetadataLayout.swift index 073eded..a6fe59f 100644 --- a/Sources/Runtime/Layouts/EnumMetadataLayout.swift +++ b/Sources/Runtime/Layouts/EnumMetadataLayout.swift @@ -22,11 +22,7 @@ import Foundation - - -struct EnumMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var kind: Int - var typeDescriptor: UnsafeMutablePointer - var parent: Int +struct EnumMetadataLayout: NominalMetadataLayoutType { + var _kind: Int + var typeDescriptor: UnsafeMutablePointer } diff --git a/Sources/Runtime/Layouts/EnumTypeDescriptor.swift b/Sources/Runtime/Layouts/EnumTypeDescriptor.swift new file mode 100644 index 0000000..c1f1a9d --- /dev/null +++ b/Sources/Runtime/Layouts/EnumTypeDescriptor.swift @@ -0,0 +1,18 @@ +// +// EnumTypeDescriptor.swift +// Runtime +// +// Created by Wes Wickwire on 4/6/19. +// + +struct EnumTypeDescriptor: TypeDescriptor { + var flags: ContextDescriptorFlags + var parent: RelativePointer + var mangledName: RelativePointer + var accessFunctionPointer: RelativePointer + var fieldDescriptor: RelativePointer + var numPayloadCasesAndPayloadSizeOffset: UInt32 + var numberOfFields: Int32 // numEmptyCases + var offsetToTheFieldOffsetVector: RelativeVectorPointer + var genericContextHeader: TargetTypeGenericContextDescriptorHeader +} diff --git a/Sources/Runtime/Layouts/ExistentialContainter.swift b/Sources/Runtime/Layouts/ExistentialContainter.swift index a3e8a64..0bc8201 100644 --- a/Sources/Runtime/Layouts/ExistentialContainter.swift +++ b/Sources/Runtime/Layouts/ExistentialContainter.swift @@ -22,8 +22,6 @@ import Foundation - - struct ExistentialContainer { let buffer: ExistentialContainerBuffer let type: Any.Type @@ -36,7 +34,6 @@ struct ExistentialContainerBuffer { let buffer3: Int } - extension ExistentialContainerBuffer { static func size() -> Int { return MemoryLayout.size diff --git a/Sources/Runtime/Layouts/FieldDescriptor.swift b/Sources/Runtime/Layouts/FieldDescriptor.swift new file mode 100644 index 0000000..39cc161 --- /dev/null +++ b/Sources/Runtime/Layouts/FieldDescriptor.swift @@ -0,0 +1,95 @@ +// MIT License +// +// Copyright (c) 2017 Wesley Wickwire +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import CRuntime + +/// https://github.com/apple/swift/blob/f2c42509628bed66bf5b8ee02fae778a2ba747a1/include/swift/Reflection/Records.h#L160 +struct FieldDescriptor { + + var mangledTypeNameOffset: Int32 + var superClassOffset: Int32 + var _kind: UInt16 + var fieldRecordSize: Int16 + var numFields: Int32 + var fields: Vector + + var kind: FieldDescriptorKind { + return FieldDescriptorKind(rawValue: _kind)! + } +} + +struct FieldRecord { + + var fieldRecordFlags: Int32 + var _mangledTypeName: RelativePointer + var _fieldName: RelativePointer + + var isVar: Bool { + return (fieldRecordFlags & 0x2) == 0x2 + } + + mutating func fieldName() -> String { + return String(cString: _fieldName.advanced()) + } + + mutating func mangedTypeName() -> String { + return String(cString: _mangledTypeName.advanced()) + } + + mutating func type(genericContext: UnsafeRawPointer?, + genericArguments: UnsafeRawPointer?) -> Any.Type { + let typeName = _mangledTypeName.advanced() + let metadataPtr = swift_getTypeByMangledNameInContext( + typeName, + getSymbolicMangledNameLength(typeName), + genericContext, + genericArguments?.assumingMemoryBound(to: Optional.self) + )! + + return unsafeBitCast(metadataPtr, to: Any.Type.self) + } + + func getSymbolicMangledNameLength(_ base: UnsafeRawPointer) -> Int32 { + var end = base + while let current = Optional(end.load(as: UInt8.self)), current != 0 { + end += 1 + if current >= 0x1 && current <= 0x17 { + end += 4 + } else if current >= 0x18 && current <= 0x1F { + end += MemoryLayout.size + } + } + + return Int32(end - base) + } +} + +enum FieldDescriptorKind: UInt16 { + case `struct` + case `class` + case `enum` + case multiPayloadEnum + case `protocol` + case classProtocol + case objcProtocol + case objcClass +} diff --git a/Sources/Runtime/Layouts/FunctionMetadataLayout.swift b/Sources/Runtime/Layouts/FunctionMetadataLayout.swift index ca6ea50..0f79754 100644 --- a/Sources/Runtime/Layouts/FunctionMetadataLayout.swift +++ b/Sources/Runtime/Layouts/FunctionMetadataLayout.swift @@ -22,11 +22,8 @@ import Foundation - - struct FunctionMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var kind: Int + var _kind: Int var flags: Int var argumentVector: Vector } diff --git a/Sources/Runtime/Layouts/MetadataLayoutType.swift b/Sources/Runtime/Layouts/MetadataLayoutType.swift index cf3e2d7..a0ceffa 100644 --- a/Sources/Runtime/Layouts/MetadataLayoutType.swift +++ b/Sources/Runtime/Layouts/MetadataLayoutType.swift @@ -22,7 +22,11 @@ import Foundation - protocol MetadataLayoutType { - var valueWitnessTable: UnsafePointer { get set } + var _kind: Int { get set } +} + +protocol NominalMetadataLayoutType: MetadataLayoutType { + associatedtype Descriptor: TypeDescriptor + var typeDescriptor: UnsafeMutablePointer { get set } } diff --git a/Sources/Runtime/Layouts/ProtocolDescriptor.swift b/Sources/Runtime/Layouts/ProtocolDescriptor.swift index 3f20ad1..9c8a06d 100644 --- a/Sources/Runtime/Layouts/ProtocolDescriptor.swift +++ b/Sources/Runtime/Layouts/ProtocolDescriptor.swift @@ -22,8 +22,6 @@ import Foundation - - struct ProtocolDescriptor { var isaPointer: Int var mangledName: UnsafeMutablePointer diff --git a/Sources/Runtime/Layouts/ProtocolMetadataLayout.swift b/Sources/Runtime/Layouts/ProtocolMetadataLayout.swift index dec6bc3..b0ba8df 100644 --- a/Sources/Runtime/Layouts/ProtocolMetadataLayout.swift +++ b/Sources/Runtime/Layouts/ProtocolMetadataLayout.swift @@ -22,11 +22,8 @@ import Foundation - - struct ProtocolMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var kind: Int + var _kind: Int var layoutFlags: Int var numberOfProtocols: Int var protocolDescriptorVector: UnsafeMutablePointer diff --git a/Sources/Runtime/Layouts/StructMetadataLayout.swift b/Sources/Runtime/Layouts/StructMetadataLayout.swift index 05a88a3..639be51 100644 --- a/Sources/Runtime/Layouts/StructMetadataLayout.swift +++ b/Sources/Runtime/Layouts/StructMetadataLayout.swift @@ -22,9 +22,7 @@ import Foundation - -struct StructMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var kind: Int +struct StructMetadataLayout: NominalMetadataLayoutType { + var _kind: Int var typeDescriptor: UnsafeMutablePointer } diff --git a/Sources/Runtime/Layouts/StructTypeDescriptor.swift b/Sources/Runtime/Layouts/StructTypeDescriptor.swift index 80e2ae3..f164594 100644 --- a/Sources/Runtime/Layouts/StructTypeDescriptor.swift +++ b/Sources/Runtime/Layouts/StructTypeDescriptor.swift @@ -22,20 +22,15 @@ import Foundation - typealias FieldTypeAccessor = @convention(c) (UnsafePointer) -> UnsafePointer -struct StructTypeDescriptor { - var flags: Int32 +struct StructTypeDescriptor: TypeDescriptor { + var flags: ContextDescriptorFlags var parent: Int32 var mangledName: RelativePointer var unknown3: Int32 + var fieldDescriptor: RelativePointer var numberOfFields: Int32 var offsetToTheFieldOffsetVector: RelativeVectorPointer - var fieldTypeAccessor: RelativePointer - var metadataPattern: Int32 - var inclusiveGenericParametersCount: Int32 - var exclusiveGenericParametersCount: Int32 - var idk2: Int32 - var genericParameterVector: RelativeVectorPointer + var genericContextHeader: TargetTypeGenericContextDescriptorHeader } diff --git a/Sources/Runtime/Layouts/TargetTypeGenericContextDescriptorHeader.swift b/Sources/Runtime/Layouts/TargetTypeGenericContextDescriptorHeader.swift new file mode 100644 index 0000000..f6285c5 --- /dev/null +++ b/Sources/Runtime/Layouts/TargetTypeGenericContextDescriptorHeader.swift @@ -0,0 +1,34 @@ +// MIT License +// +// Copyright (c) 2017 Wesley Wickwire +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +struct TargetTypeGenericContextDescriptorHeader { + var instantiationCache: Int32 + var defaultInstantiationPattern: Int32 + var base: TargetGenericContextDescriptorHeader +} + +struct TargetGenericContextDescriptorHeader { + var numberOfParams: UInt16 + var numberOfRequirements: UInt16 + var numberOfKeyArguments: UInt16 + var numberOfExtraArguments: UInt16 +} diff --git a/Sources/Runtime/Layouts/TupleMetadataLayout.swift b/Sources/Runtime/Layouts/TupleMetadataLayout.swift index d0b7661..de47c30 100644 --- a/Sources/Runtime/Layouts/TupleMetadataLayout.swift +++ b/Sources/Runtime/Layouts/TupleMetadataLayout.swift @@ -20,19 +20,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import Foundation - - - struct TupleMetadataLayout: MetadataLayoutType { - var valueWitnessTable: UnsafePointer - var kind: Int + var _kind: Int var numberOfElements: Int var labelsString: UnsafeMutablePointer var elementVector: Vector } - struct TupleElementLayout { var type: Any.Type var offset: Int diff --git a/Sources/Runtime/Layouts/TypeDescriptor.swift b/Sources/Runtime/Layouts/TypeDescriptor.swift new file mode 100644 index 0000000..ad020c9 --- /dev/null +++ b/Sources/Runtime/Layouts/TypeDescriptor.swift @@ -0,0 +1,36 @@ +// MIT License +// +// Copyright (c) 2017 Wesley Wickwire +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +protocol TypeDescriptor { + + /// The offset type can differ between TypeDescriptors + /// e.g. Struct are an Int32 and classes are an Int + associatedtype FieldOffsetVectorOffsetType: IntegerConvertible + + var mangledName: RelativePointer { get set } + var fieldDescriptor: RelativePointer { get set } + var numberOfFields: Int32 { get set } + var offsetToTheFieldOffsetVector: RelativeVectorPointer { get set } + var genericContextHeader: TargetTypeGenericContextDescriptorHeader { get set } +} + +typealias ContextDescriptorFlags = Int32 diff --git a/Sources/Runtime/Layouts/ValueWitnessTable.swift b/Sources/Runtime/Layouts/ValueWitnessTable.swift index 306e1d0..d921782 100644 --- a/Sources/Runtime/Layouts/ValueWitnessTable.swift +++ b/Sources/Runtime/Layouts/ValueWitnessTable.swift @@ -20,27 +20,20 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import Foundation - - -let valueWitnessTableOffset = -1 - struct ValueWitnessTable { - var a: Int - var b: Int - var c: Int - var d: Int - var e: Int - var f: Int - var g: Int - var h: Int - var i: Int + var initializeBufferWithCopyOfBuffer: UnsafeRawPointer + var destroy: UnsafeRawPointer + var initializeWithCopy: UnsafeRawPointer + var assignWithCopy: UnsafeRawPointer + var initializeWithTake: UnsafeRawPointer + var assignWithTake: UnsafeRawPointer + var getEnumTagSinglePayload: UnsafeRawPointer + var storeEnumTagSinglePayload: UnsafeRawPointer var size: Int - var flags: Int var stride: Int + var flags: Int } - struct ValueWitnessFlags { static let alignmentMask = 0x0000FFFF static let isNonPOD = 0x00010000 diff --git a/Sources/Runtime/Metadata/ClassMetadata.swift b/Sources/Runtime/Metadata/ClassMetadata.swift index 06a4d29..520030b 100644 --- a/Sources/Runtime/Metadata/ClassMetadata.swift +++ b/Sources/Runtime/Metadata/ClassMetadata.swift @@ -22,39 +22,44 @@ import Foundation -struct ClassMetadata: MetadataType { +struct ClassMetadata: NominalMetadataType { - var type: Any.Type - var metadata: UnsafeMutablePointer - var typeDescriptor: UnsafeMutablePointer - var base: UnsafeMutablePointer + var pointer: UnsafeMutablePointer - init(type: Any.Type, metadata: UnsafeMutablePointer, base: UnsafeMutablePointer) { - self.type = type - self.metadata = metadata - self.typeDescriptor = metadata.pointee.typeDescriptor - self.base = base + var hasResilientSuperclass: Bool { + return (0x4000 & pointer.pointee.classFlags) != 0 } - mutating func className() -> String { - return String(cString: typeDescriptor.pointee.className.advanced()) + var areImmediateMembersNegative: Bool { + return (0x800 & pointer.pointee.classFlags) != 0 } - mutating func numberOfFields() -> Int { - return typeDescriptor.pointee - .numberOfFields - .getInt() - } - - mutating func fieldOffsets() -> [Int] { - return typeDescriptor.pointee - .fieldOffsetVectorOffset - .vector(metadata: base, n: numberOfFields()) - .map{ Int($0) } + var genericArgumentOffset: Int { + let typeDescriptor = pointer.pointee.typeDescriptor + + if !hasResilientSuperclass { + return areImmediateMembersNegative + ? -Int(typeDescriptor.pointee.negativeSizeAndBoundsUnion.metadataNegativeSizeInWords) + : Int(typeDescriptor.pointee.metadataPositiveSizeInWords - typeDescriptor.pointee.numImmediateMembers) + } + + /* + let storedBounds = typeDescriptor.pointee + .negativeSizeAndBoundsUnion + .resilientMetadataBounds() + .pointee + .advanced() + .pointee + */ + + // To do this something like `computeMetadataBoundsFromSuperclass` in Metadata.cpp + // will need to be implemented. To do that we also need to get the resilient superclass + // from the trailing objects. + fatalError("Cannot get the `genericArgumentOffset` for classes with a resilient superclass") } func superClassMetadata() -> ClassMetadata? { - let superClass = metadata.pointee.superClass + let superClass = pointer.pointee.superClass // type comparison directly to NSObject.self does not work. // just compare the type name instead. if superClass != swiftObject() && "\(superClass)" != "NSObject" { @@ -66,9 +71,9 @@ struct ClassMetadata: MetadataType { mutating func toTypeInfo() -> TypeInfo { var info = TypeInfo(metadata: self) - info.mangledName = className() - - info.properties = getProperties(of: type, offsets: fieldOffsets()) + info.mangledName = mangledName() + info.properties = properties() + info.genericTypes = genericArguments() var superClass = superClassMetadata() while var sc = superClass { @@ -77,6 +82,7 @@ struct ClassMetadata: MetadataType { info.properties.append(contentsOf: superInfo.properties) superClass = sc.superClassMetadata() } + return info } } diff --git a/Sources/Runtime/Metadata/EnumMetadata.swift b/Sources/Runtime/Metadata/EnumMetadata.swift index 921911f..0f3f2f9 100644 --- a/Sources/Runtime/Metadata/EnumMetadata.swift +++ b/Sources/Runtime/Metadata/EnumMetadata.swift @@ -22,38 +22,30 @@ import Foundation - - -struct EnumMetadata: MetadataType { - - var type: Any.Type - var metadata: UnsafeMutablePointer - var typeDescriptor: UnsafeMutablePointer - var base: UnsafeMutablePointer - - init(type: Any.Type, metadata: UnsafeMutablePointer, base: UnsafeMutablePointer) { - self.type = type - self.metadata = metadata - self.typeDescriptor = metadata.pointee.typeDescriptor - self.base = base - } +struct EnumMetadata: NominalMetadataType { - mutating func mangledName() -> String { - return String(cString: typeDescriptor.pointee.mangledName.advanced()) - } - - mutating func numberOfFields() -> Int { - return typeDescriptor.pointee.numberOfFields.getInt() - } - - mutating func fieldOffsets() -> [Int] { - return typeDescriptor.pointee.offsetToTheFieldOffsetVector.vector(metadata: base, n: numberOfFields()).map{ Int($0) } + var pointer: UnsafeMutablePointer + + mutating func cases() -> [Case] { + let fieldDescriptor = pointer.pointee.typeDescriptor.pointee + .fieldDescriptor + .advanced() + + return (0.. TypeInfo { var info = TypeInfo(metadata: self) - info.properties = getProperties(of: type, offsets: fieldOffsets()) info.mangledName = mangledName() + info.cases = cases() + info.genericTypes = genericArguments() return info } } diff --git a/Sources/Runtime/Metadata/FuntionMetadata.swift b/Sources/Runtime/Metadata/FuntionMetadata.swift index 33a77e9..4f6d03c 100644 --- a/Sources/Runtime/Metadata/FuntionMetadata.swift +++ b/Sources/Runtime/Metadata/FuntionMetadata.swift @@ -22,13 +22,9 @@ import Foundation - - struct FunctionMetadata: MetadataType { - var type: Any.Type - var metadata: UnsafeMutablePointer - var base: UnsafeMutablePointer + var pointer: UnsafeMutablePointer func info() -> FunctionInfo { let (numberOfArguments, argumentTypes, returnType) = argumentInfo() @@ -40,7 +36,7 @@ struct FunctionMetadata: MetadataType { private func argumentInfo() -> (Int, [Any.Type], Any.Type) { let n = numberArguments() - var argTypes = metadata.pointee.argumentVector.vector(n: n + 1) + var argTypes = pointer.pointee.argumentVector.vector(n: n + 1) let resultType = argTypes[0] argTypes.removeFirst() @@ -49,10 +45,10 @@ struct FunctionMetadata: MetadataType { } private func numberArguments() -> Int { - return metadata.pointee.flags & 0x00FFFFFF + return pointer.pointee.flags & 0x00FFFFFF } private func `throws`() -> Bool { - return metadata.pointee.flags & 0x01000000 != 0 + return pointer.pointee.flags & 0x01000000 != 0 } } diff --git a/Sources/Runtime/Metadata/Metadata.swift b/Sources/Runtime/Metadata/Metadata.swift index e9faa63..e234fae 100644 --- a/Sources/Runtime/Metadata/Metadata.swift +++ b/Sources/Runtime/Metadata/Metadata.swift @@ -22,8 +22,6 @@ import Foundation - - func metadataPointer(type: Any.Type) -> UnsafeMutablePointer { return unsafeBitCast(type, to: UnsafeMutablePointer.self) } @@ -51,6 +49,5 @@ func metadata(of type: Any.Type) throws -> MetadataInfo { func swiftObject() -> Any.Type { class Temp {} let md = ClassMetadata(type: Temp.self) - return md.metadata.pointee.superClass + return md.pointer.pointee.superClass } - diff --git a/Sources/Runtime/Metadata/MetadataType.swift b/Sources/Runtime/Metadata/MetadataType.swift index e9459a2..b903e48 100644 --- a/Sources/Runtime/Metadata/MetadataType.swift +++ b/Sources/Runtime/Metadata/MetadataType.swift @@ -33,35 +33,48 @@ protocol MetadataInfo { } protocol MetadataType: MetadataInfo, TypeInfoConvertible { + associatedtype Layout: MetadataLayoutType - var type: Any.Type { get set } - var metadata: UnsafeMutablePointer { get set } - var base: UnsafeMutablePointer { get set } - init(type: Any.Type, metadata: UnsafeMutablePointer, base: UnsafeMutablePointer) + + var pointer: UnsafeMutablePointer { get set } + + init(pointer: UnsafeMutablePointer) } extension MetadataType { + init(type: Any.Type) { + self = Self(pointer: unsafeBitCast(type, to: UnsafeMutablePointer.self)) + } + + var type: Any.Type { + return unsafeBitCast(pointer, to: Any.Type.self) + } + var kind: Kind { - return Kind(flag: base.pointee) + return Kind(flag: pointer.pointee._kind) } var size: Int { - return metadata.pointee.valueWitnessTable.pointee.size + return valueWitnessTable.pointee.size } var alignment: Int { - return (metadata.pointee.valueWitnessTable.pointee.flags & ValueWitnessFlags.alignmentMask) + 1 + return (valueWitnessTable.pointee.flags & ValueWitnessFlags.alignmentMask) + 1 } var stride: Int { - return metadata.pointee.valueWitnessTable.pointee.stride + return valueWitnessTable.pointee.stride } - init(type: Any.Type) { - let base = metadataPointer(type: type) - let metadata = base.advanced(by: valueWitnessTableOffset).raw.assumingMemoryBound(to: Layout.self) - self.init(type: type, metadata: metadata, base: base) + /// The ValueWitnessTable for the type. + /// A pointer to the table is located one pointer sized word behind the metadata pointer. + var valueWitnessTable: UnsafeMutablePointer { + return pointer + .raw + .advanced(by: -MemoryLayout.size) + .assumingMemoryBound(to: UnsafeMutablePointer.self) + .pointee } mutating func toTypeInfo() -> TypeInfo { diff --git a/Sources/Runtime/Metadata/NominalMetadataType.swift b/Sources/Runtime/Metadata/NominalMetadataType.swift new file mode 100644 index 0000000..12b7f16 --- /dev/null +++ b/Sources/Runtime/Metadata/NominalMetadataType.swift @@ -0,0 +1,95 @@ +// MIT License +// +// Copyright (c) 2017 Wesley Wickwire +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +protocol NominalMetadataType: MetadataType where Layout: NominalMetadataLayoutType { + + /// The offset of the generic type vector in pointer sized words from the + /// start of the metadata record. + var genericArgumentOffset: Int { get } +} + +extension NominalMetadataType { + + var genericArgumentOffset: Int { + // default to 2. This would put it right after the type descriptor which is valid + // for all types except for classes + return 2 + } + + mutating func mangledName() -> String { + return String(cString: pointer.pointee.typeDescriptor.pointee.mangledName.advanced()) + } + + mutating func numberOfFields() -> Int { + return pointer.pointee.typeDescriptor.pointee.numberOfFields.getInt() + } + + mutating func fieldOffsets() -> [Int] { + return pointer.pointee.typeDescriptor.pointee + .offsetToTheFieldOffsetVector + .vector(metadata: pointer.raw.assumingMemoryBound(to: Int.self), n: numberOfFields()) + .map { $0.getInt() } + } + + mutating func properties() -> [PropertyInfo] { + let offsets = fieldOffsets() + let fieldDescriptor = pointer.pointee.typeDescriptor.pointee + .fieldDescriptor + .advanced() + + let genericVector = genericArgumentVector() + + return (0.. [Any.Type] { + let n = pointer.pointee.typeDescriptor.pointee.genericContextHeader.base.numberOfParams + guard n > 0 else { return [] } + + let vector = genericArgumentVector() + return (0.. UnsafeMutablePointer> { + return pointer + .advanced(by: genericArgumentOffset, wordSize: MemoryLayout.size) + .assumingMemoryBound(to: Vector.self) + } +} diff --git a/Sources/Runtime/Metadata/ProtocolMetadata.swift b/Sources/Runtime/Metadata/ProtocolMetadata.swift index bf3a775..be026a4 100644 --- a/Sources/Runtime/Metadata/ProtocolMetadata.swift +++ b/Sources/Runtime/Metadata/ProtocolMetadata.swift @@ -22,23 +22,11 @@ import Foundation - - struct ProtocolMetadata: MetadataType { - var type: Any.Type - var metadata: UnsafeMutablePointer - var base: UnsafeMutablePointer - var protocolDescriptor: UnsafeMutablePointer - - init(type: Any.Type, metadata: UnsafeMutablePointer, base: UnsafeMutablePointer) { - self.type = type - self.metadata = metadata - self.base = base - self.protocolDescriptor = metadata.pointee.protocolDescriptorVector - } + var pointer: UnsafeMutablePointer mutating func mangledName() -> String { - return String(cString: protocolDescriptor.pointee.mangledName) + return String(cString: pointer.pointee.protocolDescriptorVector.pointee.mangledName) } } diff --git a/Sources/Runtime/Metadata/StructMetadata.swift b/Sources/Runtime/Metadata/StructMetadata.swift index f4f9cb9..7feb13f 100644 --- a/Sources/Runtime/Metadata/StructMetadata.swift +++ b/Sources/Runtime/Metadata/StructMetadata.swift @@ -22,40 +22,15 @@ import Foundation - -struct StructMetadata: MetadataType { - - var type: Any.Type - var metadata: UnsafeMutablePointer - var typeDescriptor: UnsafeMutablePointer - var base: UnsafeMutablePointer +struct StructMetadata: NominalMetadataType { - init(type: Any.Type, metadata: UnsafeMutablePointer, base: UnsafeMutablePointer) { - self.type = type - self.metadata = metadata - self.typeDescriptor = metadata.pointee.typeDescriptor - self.base = base - } - - mutating func mangledName() -> String { - return String(cString: typeDescriptor.pointee.mangledName.advanced()) - } - - mutating func numberOfFields() -> Int { - return typeDescriptor.pointee.numberOfFields.getInt() - } - - mutating func fieldOffsets() -> [Int] { - return typeDescriptor.pointee - .offsetToTheFieldOffsetVector - .vector(metadata: base, n: numberOfFields()) - .map{ Int($0) } - } + var pointer: UnsafeMutablePointer mutating func toTypeInfo() -> TypeInfo { var info = TypeInfo(metadata: self) - info.properties = getProperties(of: type, offsets: fieldOffsets()) + info.properties = properties() info.mangledName = mangledName() + info.genericTypes = genericArguments() return info } } diff --git a/Sources/Runtime/Metadata/TupleMetadata.swift b/Sources/Runtime/Metadata/TupleMetadata.swift index 5efe198..428fc59 100644 --- a/Sources/Runtime/Metadata/TupleMetadata.swift +++ b/Sources/Runtime/Metadata/TupleMetadata.swift @@ -21,21 +21,17 @@ // SOFTWARE. import Foundation - - struct TupleMetadata: MetadataType, TypeInfoConvertible { - var type: Any.Type - var metadata: UnsafeMutablePointer - var base: UnsafeMutablePointer - + var pointer: UnsafeMutablePointer + func numberOfElements() -> Int { - return metadata.pointee.numberOfElements + return pointer.pointee.numberOfElements } func labels() -> [String] { - guard Int(bitPattern: metadata.pointee.labelsString) != 0 else { return (0.. [TupleElementLayout] { let n = numberOfElements() guard n > 0 else { return [] } - return metadata.pointee.elementVector.vector(n: n) + return pointer.pointee.elementVector.vector(n: n) } func properies() -> [PropertyInfo] { @@ -52,7 +48,7 @@ struct TupleMetadata: MetadataType, TypeInfoConvertible { let num = numberOfElements() var properties = [PropertyInfo]() for i in 0.. FunctionInfo { return try functionInfo(of: type(of: function)) } diff --git a/Sources/Runtime/Models/Kind.swift b/Sources/Runtime/Models/Kind.swift index c64003c..f54d25e 100644 --- a/Sources/Runtime/Models/Kind.swift +++ b/Sources/Runtime/Models/Kind.swift @@ -41,19 +41,33 @@ public enum Kind { init(flag: Int) { switch flag { case 1: self = .struct + case (0 | Flags.kindIsNonHeap): self = .struct case 2: self = .enum + case (1 | Flags.kindIsNonHeap): self = .enum case 3: self = .optional + case (2 | Flags.kindIsNonHeap): self = .optional case 8: self = .opaque + case (3 | Flags.kindIsNonHeap): self = .foreignClass case 9: self = .tuple + case (0 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .opaque case 10: self = .function + case (1 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .tuple case 12: self = .existential + case (2 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .function case 13: self = .metatype + case (3 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .existential case 14: self = .objCClassWrapper + case (4 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .metatype case 15: self = .existentialMetatype + case (5 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .objCClassWrapper case 16: self = .foreignClass + case (6 | Flags.kindIsRuntimePrivate | Flags.kindIsNonHeap): self = .existentialMetatype case 64: self = .heapLocalVariable + case (0 | Flags.kindIsNonType): self = .heapLocalVariable case 65: self = .heapGenericLocalVariable + case (0 | Flags.kindIsNonType | Flags.kindIsRuntimePrivate): self = .heapGenericLocalVariable case 128: self = .errorObject + case (1 | Flags.kindIsNonType | Flags.kindIsRuntimePrivate): self = .errorObject default: self = .class } } @@ -62,5 +76,10 @@ public enum Kind { let pointer = metadataPointer(type: type) self.init(flag: pointer.pointee) } - + + struct Flags { + static let kindIsNonHeap = 0x200 + static let kindIsRuntimePrivate = 0x100 + static let kindIsNonType = 0x400 + } } diff --git a/Sources/Runtime/Models/PropertyInfo.swift b/Sources/Runtime/Models/PropertyInfo.swift index 435f4a5..3619c77 100644 --- a/Sources/Runtime/Models/PropertyInfo.swift +++ b/Sources/Runtime/Models/PropertyInfo.swift @@ -22,11 +22,11 @@ import Foundation - public struct PropertyInfo { public let name: String public let type: Any.Type + public let isVar: Bool public let offset: Int public let ownerType: Any.Type @@ -66,11 +66,11 @@ public struct PropertyInfo { } } - extension PropertyInfo: Equatable { - public static func ==(lhs: PropertyInfo, rhs: PropertyInfo) -> Bool { + public static func == (lhs: PropertyInfo, rhs: PropertyInfo) -> Bool { return lhs.name == rhs.name && lhs.type == rhs.type + && lhs.isVar == rhs.isVar && lhs.offset == rhs.offset && lhs.ownerType == rhs.ownerType } diff --git a/Sources/Runtime/Models/TypeInfo.swift b/Sources/Runtime/Models/TypeInfo.swift index a1f16b7..eb3ceef 100644 --- a/Sources/Runtime/Models/TypeInfo.swift +++ b/Sources/Runtime/Models/TypeInfo.swift @@ -22,7 +22,6 @@ import Foundation - public struct TypeInfo { public var kind: Kind = .class @@ -34,6 +33,8 @@ public struct TypeInfo { public var size: Int = 0 public var alignment: Int = 0 public var stride: Int = 0 + public var cases: [Case] = [] + public var genericTypes: [Any.Type] = [] init(metadata: Metadata) { kind = metadata.kind diff --git a/Sources/Runtime/Models/TypeInfoConvertible.swift b/Sources/Runtime/Models/TypeInfoConvertible.swift index b9a055f..069a8b6 100644 --- a/Sources/Runtime/Models/TypeInfoConvertible.swift +++ b/Sources/Runtime/Models/TypeInfoConvertible.swift @@ -22,8 +22,6 @@ import Foundation - - protocol TypeInfoConvertible { mutating func toTypeInfo() -> TypeInfo } diff --git a/Sources/Runtime/Pointers/Pointers.swift b/Sources/Runtime/Pointers/Pointers.swift index 382e284..b6b1d5a 100644 --- a/Sources/Runtime/Pointers/Pointers.swift +++ b/Sources/Runtime/Pointers/Pointers.swift @@ -22,8 +22,9 @@ import Foundation - -func withValuePointer(of value: inout Value, _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { +func withValuePointer( + of value: inout Value, + _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { let kind = Kind(type: Value.self) @@ -39,19 +40,23 @@ func withValuePointer(of value: inout Value, _ body: (UnsafeMutab } } -func withClassValuePointer(of value: inout Value, _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { +func withClassValuePointer( + of value: inout Value, + _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { return try withUnsafePointer(to: &value) { - let pointer = $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1){$0.pointee} + let pointer = $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {$0.pointee} return try body(pointer) } } -func withExistentialValuePointer(of value: inout Value, _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { +func withExistentialValuePointer( + of value: inout Value, + _ body: (UnsafeMutableRawPointer) throws -> Result) throws -> Result { return try withUnsafePointer(to: &value) { - let container = $0.withMemoryRebound(to: ExistentialContainer.self, capacity: 1){$0.pointee} + let container = $0.withMemoryRebound(to: ExistentialContainer.self, capacity: 1) {$0.pointee} let info = try metadata(of: container.type) if info.kind == .class || info.size > ExistentialContainerBuffer.size() { - let base = $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1){$0.pointee} + let base = $0.withMemoryRebound(to: UnsafeMutableRawPointer.self, capacity: 1) {$0.pointee} if info.kind == .struct { return try body(base.advanced(by: existentialHeaderSize)) } else { @@ -74,6 +79,3 @@ var existentialHeaderSize: Int { var is64Bit: Bool { return MemoryLayout.size == 8 } - - - diff --git a/Sources/Runtime/Pointers/RelativePointer.swift b/Sources/Runtime/Pointers/RelativePointer.swift index 75de45c..4f04002 100644 --- a/Sources/Runtime/Pointers/RelativePointer.swift +++ b/Sources/Runtime/Pointers/RelativePointer.swift @@ -22,7 +22,6 @@ import Foundation - struct RelativePointer { var offset: Offset diff --git a/Sources/Runtime/Pointers/RelativeVectorPointer.swift b/Sources/Runtime/Pointers/RelativeVectorPointer.swift index 0d5a548..bf85426 100644 --- a/Sources/Runtime/Pointers/RelativeVectorPointer.swift +++ b/Sources/Runtime/Pointers/RelativeVectorPointer.swift @@ -22,8 +22,6 @@ import Foundation - - struct RelativeVectorPointer { var offset: Offset mutating func vector(metadata: UnsafePointer, n: IntegerConvertible) -> [Pointee] { diff --git a/Sources/Runtime/Utilities/GetFieldAt.swift b/Sources/Runtime/Pointers/Union.swift similarity index 50% rename from Sources/Runtime/Utilities/GetFieldAt.swift rename to Sources/Runtime/Pointers/Union.swift index 0df0f43..974ae57 100644 --- a/Sources/Runtime/Utilities/GetFieldAt.swift +++ b/Sources/Runtime/Pointers/Union.swift @@ -20,31 +20,16 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -import Foundation -import CRuntime - -/// Type to use as the context in the `_getFieldAt` function. -struct PropertyInfoContext { - let name: String - let type: Any.Type +/// Helper for when binding to a c++ `union` +protocol Union { + associatedtype Raw + var raw: Raw { get set } } -/// Gets the list of properties of the inputted type. -func getProperties(of type: Any.Type, offsets: [Int]) -> [PropertyInfo] { - return (0..() -> UnsafeMutablePointer { + return withUnsafePointer(to: &self) { pointer in + return pointer.raw.assumingMemoryBound(to: T.self).mutable + } } } diff --git a/Sources/Runtime/Pointers/Vector.swift b/Sources/Runtime/Pointers/Vector.swift index 5260fda..9f051b0 100644 --- a/Sources/Runtime/Pointers/Vector.swift +++ b/Sources/Runtime/Pointers/Vector.swift @@ -22,9 +22,10 @@ import Foundation - struct Vector { + var element: Element + mutating func vector(n: IntegerConvertible) -> [Element] { return withUnsafePointer(to: &self) { $0.withMemoryRebound(to: Element.self, capacity: 1) { start in @@ -32,4 +33,10 @@ struct Vector { } } } + + mutating func element(at i: IntegerConvertible) -> UnsafeMutablePointer { + return withUnsafePointer(to: &self) { + return $0.raw.assumingMemoryBound(to: Element.self).advanced(by: i.getInt()).mutable + } + } } diff --git a/Sources/Runtime/Utilities/GettersSetters.swift b/Sources/Runtime/Utilities/GettersSetters.swift index 45f46cf..b17f9b4 100644 --- a/Sources/Runtime/Utilities/GettersSetters.swift +++ b/Sources/Runtime/Utilities/GettersSetters.swift @@ -22,7 +22,6 @@ import Foundation - protocol Getters {} extension Getters { static func get(from pointer: UnsafeRawPointer) -> Any { @@ -35,7 +34,6 @@ func getters(type: Any.Type) -> Getters.Type { return unsafeBitCast(container, to: Getters.Type.self) } - protocol Setters {} extension Setters { static func set(value: Any, pointer: UnsafeMutableRawPointer) { @@ -49,4 +47,3 @@ func setters(type: Any.Type) -> Setters.Type { let container = ProtocolTypeContainer(type: type, witnessTable: 0) return unsafeBitCast(container, to: Setters.Type.self) } - diff --git a/Sources/Runtime/Utilities/IntegerConvertible.swift b/Sources/Runtime/Utilities/IntegerConvertible.swift index 2617544..8cee04c 100644 --- a/Sources/Runtime/Utilities/IntegerConvertible.swift +++ b/Sources/Runtime/Utilities/IntegerConvertible.swift @@ -22,7 +22,6 @@ import Foundation - protocol IntegerConvertible { func getInt() -> Int } @@ -38,3 +37,9 @@ extension Int32: IntegerConvertible { return Int(self) } } + +extension UInt16: IntegerConvertible { + func getInt() -> Int { + return Int(self) + } +} diff --git a/Sources/Runtime/Utilities/Pointer+Extensions.swift b/Sources/Runtime/Utilities/Pointer+Extensions.swift index 5b5bc70..6aa75b5 100644 --- a/Sources/Runtime/Utilities/Pointer+Extensions.swift +++ b/Sources/Runtime/Utilities/Pointer+Extensions.swift @@ -22,7 +22,6 @@ import Foundation - extension UnsafePointer { var raw: UnsafeRawPointer { @@ -65,4 +64,8 @@ extension UnsafeMutablePointer { } return result } + + func advanced(by n: Int, wordSize: Int) -> UnsafeMutableRawPointer { + return self.raw.advanced(by: n * wordSize) + } } diff --git a/Sources/Runtime/Utilities/RetainCounts.swift b/Sources/Runtime/Utilities/RetainCounts.swift index 9705f39..29d763d 100644 --- a/Sources/Runtime/Utilities/RetainCounts.swift +++ b/Sources/Runtime/Utilities/RetainCounts.swift @@ -22,15 +22,12 @@ import Foundation - - public func retainCounts(of object: inout AnyObject) throws -> Int { return try withValuePointer(of: &object) { pointer in return pointer.assumingMemoryBound(to: ClassHeader.self).pointee.strongRetainCounts.getInt() } } - public func weakRetainCounts(of object: inout AnyObject) throws -> Int { return try withValuePointer(of: &object) { pointer in return pointer.assumingMemoryBound(to: ClassHeader.self).pointee.weakRetainCounts.getInt() diff --git a/Sources/Runtime/Utilities/String+Extensions.swift b/Sources/Runtime/Utilities/String+Extensions.swift index f52dfdc..cebc321 100644 --- a/Sources/Runtime/Utilities/String+Extensions.swift +++ b/Sources/Runtime/Utilities/String+Extensions.swift @@ -22,7 +22,6 @@ import Foundation - extension Array where Element == String { static func from(pointer: UnsafePointer, n: Int) -> [String] { diff --git a/Tests/RuntimeTests/FactoryTests.swift b/Tests/RuntimeTests/FactoryTests.swift index 2bef9e8..33fb157 100644 --- a/Tests/RuntimeTests/FactoryTests.swift +++ b/Tests/RuntimeTests/FactoryTests.swift @@ -23,16 +23,16 @@ import XCTest @testable import Runtime - class FactoryTests: XCTestCase { static var allTests: [(String, (FactoryTests) -> () throws -> Void)] { return [ ("testStruct", testStruct), - ("testStructUntyped", testStructUntyped), + ("testStructUntyped", testStructUntyped) ] } + // swiftlint:disable force_cast func testStruct() throws { let person: PersonStruct = try createInstance() XCTAssert(person.firstname == "") @@ -55,26 +55,32 @@ class FactoryTests: XCTestCase { } #if os(iOS) // does not work on macOS or Linux - func testClass() throws { - let person: PersonClass = try createInstance() - XCTAssert(person.firstname == "") - XCTAssert(person.lastname == "") - XCTAssert(person.age == 0) - XCTAssert(person.pet.name == "") - XCTAssert(person.pet.age == 0) - XCTAssert(person.favoriteNumbers == []) - } + func testClass() throws { + let person: PersonClass = try createInstance() + XCTAssert(person.firstname == "") + XCTAssert(person.lastname == "") + XCTAssert(person.age == 0) + XCTAssert(person.pet.name == "") + XCTAssert(person.pet.age == 0) + XCTAssert(person.favoriteNumbers == []) + } + + func testGenericClass() throws { + class B {} + class A { let a: Int = 0; let b = B() } + let a: A = try createInstance() + XCTAssert(a.a == 0) + } #endif + // swiftlint:enable force_cast } - - fileprivate struct PersonStruct { var firstname = "Jobie" var lastname = "Gillis" var age = 40 var pet = PetStruct() - var favoriteNumbers = [1,2,3,4,5] + var favoriteNumbers = [1, 2, 3, 4, 5] } fileprivate struct PetStruct { @@ -87,12 +93,12 @@ fileprivate struct PetStruct { } } -fileprivate class PersonClass { +fileprivate class PersonClass { var firstname = "Jobie" var lastname = "Gillis" var age = 40 var pet = PetClass() - var favoriteNumbers = [1,2,3,4,5] + var favoriteNumbers = [1, 2, 3, 4, 5] } fileprivate class PetClass { diff --git a/Tests/RuntimeTests/GetSetClassTests.swift b/Tests/RuntimeTests/GetSetClassTests.swift index 6d9df5e..8e002c5 100644 --- a/Tests/RuntimeTests/GetSetClassTests.swift +++ b/Tests/RuntimeTests/GetSetClassTests.swift @@ -23,7 +23,6 @@ import XCTest @testable import Runtime - class GetSetClassTests: XCTestCase { static var allTests: [(String, (GetSetClassTests) -> () throws -> Void)] { @@ -55,7 +54,8 @@ class GetSetClassTests: XCTestCase { ("testSetArrayUntyped", testSetArrayUntyped) ] } - + + // swiftlint:disable force_cast func testGet() throws { let info = try typeInfo(of: Person.self) let firstname = try info.property(named: "firstname") @@ -125,7 +125,7 @@ class GetSetClassTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person = Person() let value: [Int] = try favoriteNumbers.get(from: person) - XCTAssert(value == [1,2,3,4,5]) + XCTAssert(value == [1, 2, 3, 4, 5]) } func testGetArrayUntypedValue() throws { @@ -133,7 +133,7 @@ class GetSetClassTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person = Person() let value: Any = try favoriteNumbers.get(from: person) - XCTAssert(value as! [Int] == [1,2,3,4,5]) + XCTAssert(value as! [Int] == [1, 2, 3, 4, 5]) } func testGetArrayUntypedObject() throws { @@ -141,7 +141,7 @@ class GetSetClassTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person: Any = Person() let value: [Int] = try favoriteNumbers.get(from: person) - XCTAssert(value == [1,2,3,4,5]) + XCTAssert(value == [1, 2, 3, 4, 5]) } func testGetArrayUntyped() throws { @@ -149,10 +149,9 @@ class GetSetClassTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person: Any = Person() let value: Any = try favoriteNumbers.get(from: person) - XCTAssert(value as! [Int] == [1,2,3,4,5]) + XCTAssert(value as! [Int] == [1, 2, 3, 4, 5]) } - func testSet() throws { let info = try typeInfo(of: Person.self) let firstname = try info.property(named: "firstname") @@ -191,36 +190,36 @@ class GetSetClassTests: XCTestCase { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert(person.favoriteNumbers == [5,4,3,2,1]) + XCTAssert(person.favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntypedValue() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert(person.favoriteNumbers == [5,4,3,2,1]) + XCTAssert(person.favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntypedObject() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person: Any = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert((person as! Person).favoriteNumbers == [5,4,3,2,1]) + XCTAssert((person as! Person).favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntyped() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person: Any = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert((person as! Person).favoriteNumbers == [5,4,3,2,1]) + XCTAssert((person as! Person).favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetClass() throws { @@ -274,15 +273,15 @@ class GetSetClassTests: XCTestCase { XCTAssert(property.name == "username") XCTAssert(property.type == String.self) } + // swiftlint:enable force_cast } - fileprivate class Person { var firstname = "Wes" var lastname = "Wickwire" var age = 25 var pet = Pet() - var favoriteNumbers = [1,2,3,4,5] + var favoriteNumbers = [1, 2, 3, 4, 5] } fileprivate class Pet { @@ -294,5 +293,3 @@ fileprivate class Pet { self.age = age } } - - diff --git a/Tests/RuntimeTests/GetSetStructTests.swift b/Tests/RuntimeTests/GetSetStructTests.swift index d5f07c7..92b4a8e 100644 --- a/Tests/RuntimeTests/GetSetStructTests.swift +++ b/Tests/RuntimeTests/GetSetStructTests.swift @@ -23,7 +23,7 @@ import XCTest @testable import Runtime - +// swiftlint:disable type_body_length class GetSetStructTests: XCTestCase { static var allTests: [(String, (GetSetStructTests) -> () throws -> Void)] { @@ -56,6 +56,7 @@ class GetSetStructTests: XCTestCase { ] } + // swiftlint:disable force_cast func testGet() throws { let info = try typeInfo(of: Person.self) let firstname = try info.property(named: "firstname") @@ -64,6 +65,15 @@ class GetSetStructTests: XCTestCase { XCTAssert(name == "Wes") } + func testGetSimple() throws { + struct Test { let a: Int = 2 } + let info = try typeInfo(of: Test.self) + let a = try info.property(named: "a") + let value = Test() + let result: Int = try a.get(from: value) + XCTAssert(result == 2) + } + func testGetUntypedValue() throws { let info = try typeInfo(of: Person.self) let firstname = try info.property(named: "firstname") @@ -125,7 +135,7 @@ class GetSetStructTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person = Person() let value: [Int] = try favoriteNumbers.get(from: person) - XCTAssert(value == [1,2,3,4,5]) + XCTAssert(value == [1, 2, 3, 4, 5]) } func testGetArrayUntypedValue() throws { @@ -133,7 +143,7 @@ class GetSetStructTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person = Person() let value: Any = try favoriteNumbers.get(from: person) - XCTAssert(value as! [Int] == [1,2,3,4,5]) + XCTAssert(value as! [Int] == [1, 2, 3, 4, 5]) } func testGetArrayUntypedObject() throws { @@ -141,7 +151,7 @@ class GetSetStructTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person: Any = Person() let value: [Int] = try favoriteNumbers.get(from: person) - XCTAssert(value == [1,2,3,4,5]) + XCTAssert(value == [1, 2, 3, 4, 5]) } func testGetArrayUntyped() throws { @@ -149,10 +159,9 @@ class GetSetStructTests: XCTestCase { let favoriteNumbers = try info.property(named: "favoriteNumbers") let person: Any = Person() let value: Any = try favoriteNumbers.get(from: person) - XCTAssert(value as! [Int] == [1,2,3,4,5]) + XCTAssert(value as! [Int] == [1, 2, 3, 4, 5]) } - func testSet() throws { let info = try typeInfo(of: Person.self) let firstname = try info.property(named: "firstname") @@ -191,36 +200,36 @@ class GetSetStructTests: XCTestCase { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert(person.favoriteNumbers == [5,4,3,2,1]) + XCTAssert(person.favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntypedValue() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert(person.favoriteNumbers == [5,4,3,2,1]) + XCTAssert(person.favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntypedObject() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person: Any = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert((person as! Person).favoriteNumbers == [5,4,3,2,1]) + XCTAssert((person as! Person).favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetArrayUntyped() throws { let info = try typeInfo(of: Person.self) let favoriteNumbers = try info.property(named: "favoriteNumbers") var person: Any = Person() - let new = [5,4,3,2,1] + let new = [5, 4, 3, 2, 1] try favoriteNumbers.set(value: new, on: &person) - XCTAssert((person as! Person).favoriteNumbers == [5,4,3,2,1]) + XCTAssert((person as! Person).favoriteNumbers == [5, 4, 3, 2, 1]) } func testSetStruct() throws { @@ -276,15 +285,15 @@ class GetSetStructTests: XCTestCase { try age.set(value: newValue, on: &person) XCTAssert(person.age == 25) } + // swiftlint:enable force_cast } - fileprivate struct Person { var firstname = "Wes" var lastname = "Wickwire" var age = 25 var pet = Pet() - var favoriteNumbers = [1,2,3,4,5] + var favoriteNumbers = [1, 2, 3, 4, 5] } fileprivate struct Pet { @@ -296,4 +305,4 @@ fileprivate struct Pet { self.age = age } } - +// swiftlint:enable type_body_length diff --git a/Tests/RuntimeTests/LinuxMain.swift b/Tests/RuntimeTests/LinuxMain.swift index 40d3ae6..28d28e0 100644 --- a/Tests/RuntimeTests/LinuxMain.swift +++ b/Tests/RuntimeTests/LinuxMain.swift @@ -8,5 +8,5 @@ XCTMain([ testCase(GetSetStructTests.allTests), testCase(MetadataTests.allTests), testCase(ValuePointerTests.allTests), - testCase(ValueWitnessTableTests.allTests), -]) \ No newline at end of file + testCase(ValueWitnessTableTests.allTests) +]) diff --git a/Tests/RuntimeTests/MetadataTests.swift b/Tests/RuntimeTests/MetadataTests.swift index 126ee4b..11c13d7 100644 --- a/Tests/RuntimeTests/MetadataTests.swift +++ b/Tests/RuntimeTests/MetadataTests.swift @@ -21,9 +21,9 @@ // SOFTWARE. import XCTest +import UIKit @testable import Runtime - class MetadataTests: XCTestCase { static var allTests: [(String, (MetadataTests) -> () throws -> Void)] { @@ -36,14 +36,15 @@ class MetadataTests: XCTestCase { ("testFunction", testFunction), ("testFunctionThrows", testFunctionThrows), ("testVoidFunction", testVoidFunction), - ("testEnum", testEnum), + ("testEnum", testEnum) ] } func testClass() { var md = ClassMetadata(type: MyClass.self) let info = md.toTypeInfo() - XCTAssert(info.properties.first{$0.name == "baseProperty"} != nil) + XCTAssert(md.genericArgumentOffset == 15) + XCTAssert(info.properties.first {$0.name == "baseProperty"} != nil) XCTAssert(info.inheritance[0] == BaseClass.self) XCTAssert(info.superClass == BaseClass.self) XCTAssert(info.mangledName != "") @@ -53,17 +54,40 @@ class MetadataTests: XCTestCase { XCTAssert(info.size == MemoryLayout>.size) XCTAssert(info.alignment == MemoryLayout>.alignment) XCTAssert(info.stride == MemoryLayout>.stride) + XCTAssert(info.genericTypes.count == 1) + XCTAssert(info.genericTypes[0] == Int.self) + } + + func testGenericStruct() { + struct A { let b: B } + var md = StructMetadata(type: A.self) + var args = md.genericArguments() + let props = md.properties() + XCTAssert(args.count == 7) + XCTAssert(args[0] == Int.self) + XCTAssert(args[1] == String.self) + XCTAssert(args[2] == Bool.self) + XCTAssert(args[3] == Int.self) + XCTAssert(props.count == 1) + XCTAssert(props[0].type == Int.self) } func testStruct() { - var md = StructMetadata(type: MyStruct.self) + struct A { + let a, b, c, d: Int + var e: Int + } + + var md = StructMetadata(type: A.self) let info = md.toTypeInfo() XCTAssert(info.kind == .struct) - XCTAssert(info.type == MyStruct.self) - XCTAssert(info.properties.count == 4) - XCTAssert(info.size == MemoryLayout>.size) - XCTAssert(info.alignment == MemoryLayout>.alignment) - XCTAssert(info.stride == MemoryLayout>.stride) + XCTAssert(info.type == A.self) + XCTAssert(info.properties.count == 5) + XCTAssert(info.size == MemoryLayout.size) + XCTAssert(info.alignment == MemoryLayout.alignment) + XCTAssert(info.stride == MemoryLayout.stride) + XCTAssert(!info.properties[0].isVar) + XCTAssert(info.properties[4].isVar) } func testProtocol() { @@ -123,14 +147,18 @@ class MetadataTests: XCTestCase { } func testEnum() { - var md = EnumMetadata(type: MyEnum.self) + var md = EnumMetadata(type: MyEnum.self) let info = md.toTypeInfo() + XCTAssert(info.cases[0].name == "a") + XCTAssert(info.cases[1].name == "b") + XCTAssert(info.cases[2].name == "c") + XCTAssert(info.cases[3].name == "d") } } fileprivate enum MyEnum: Int { - case a,b,c + case a, b, c, d } func voidFunction() { @@ -159,7 +187,7 @@ fileprivate class MyClass: BaseClass { } fileprivate struct MyStruct { - var a,b: Int + var a, b: Int var c: String var d: T } diff --git a/Tests/RuntimeTests/ValuePointerTests.swift b/Tests/RuntimeTests/ValuePointerTests.swift index 515da75..904a4fe 100644 --- a/Tests/RuntimeTests/ValuePointerTests.swift +++ b/Tests/RuntimeTests/ValuePointerTests.swift @@ -23,7 +23,6 @@ import XCTest @testable import Runtime - class ValuePointerTests: XCTestCase { static var allTests: [(String, (ValuePointerTests) -> () throws -> Void)] { @@ -33,7 +32,7 @@ class ValuePointerTests: XCTestCase { ("testClassValuePointer", testClassValuePointer), ("testProtocolClassValuePointer", testProtocolClassValuePointer), ("testAnyClassValuePointer", testAnyClassValuePointer), - ("testAnyStructValuePointer", testAnyStructValuePointer), + ("testAnyStructValuePointer", testAnyStructValuePointer) ] } @@ -83,7 +82,6 @@ class ValuePointerTests: XCTestCase { } } - fileprivate protocol PersonProtocol { var fistname: String { get set } var lastname: String { get set } diff --git a/Tests/RuntimeTests/ValueWitnessTableTests.swift b/Tests/RuntimeTests/ValueWitnessTableTests.swift index 626f2d7..0681531 100644 --- a/Tests/RuntimeTests/ValueWitnessTableTests.swift +++ b/Tests/RuntimeTests/ValueWitnessTableTests.swift @@ -24,14 +24,13 @@ import Foundation import XCTest @testable import Runtime - class ValueWitnessTableTests: XCTestCase { static var allTests: [(String, (ValueWitnessTableTests) -> () throws -> Void)] { return [ ("testSize", testSize), ("testAlignment", testAlignment), - ("testStride", testStride), + ("testStride", testStride) ] } @@ -52,7 +51,6 @@ class ValueWitnessTableTests: XCTestCase { } - fileprivate struct Person { let firstname: String let lastname: String diff --git a/Tests/RuntimeTests/XCTestManifests.swift b/Tests/RuntimeTests/XCTestManifests.swift index 788758d..9ce4465 100644 --- a/Tests/RuntimeTests/XCTestManifests.swift +++ b/Tests/RuntimeTests/XCTestManifests.swift @@ -3,7 +3,7 @@ import XCTest extension FactoryTests { static let __allTests = [ ("testStruct", testStruct), - ("testStructUntyped", testStructUntyped), + ("testStructUntyped", testStructUntyped) ] } @@ -33,7 +33,7 @@ extension GetSetClassTests { ("testSetClassUntypedValue", testSetClassUntypedValue), ("testSetUntyped", testSetUntyped), ("testSetUntypedObject", testSetUntypedObject), - ("testSetUntypedValue", testSetUntypedValue), + ("testSetUntypedValue", testSetUntypedValue) ] } @@ -64,7 +64,7 @@ extension GetSetStructTests { ("testSetStructUntypedValue", testSetStructUntypedValue), ("testSetUntyped", testSetUntyped), ("testSetUntypedObject", testSetUntypedObject), - ("testSetUntypedValue", testSetUntypedValue), + ("testSetUntypedValue", testSetUntypedValue) ] } @@ -78,7 +78,7 @@ extension MetadataTests { ("testStruct", testStruct), ("testTuple", testTuple), ("testTupleNoLabels", testTupleNoLabels), - ("testVoidFunction", testVoidFunction), + ("testVoidFunction", testVoidFunction) ] } @@ -89,7 +89,7 @@ extension ValuePointerTests { ("testClassValuePointer", testClassValuePointer), ("testProtocolClassValuePointer", testProtocolClassValuePointer), ("testProtocolStructValuePointer", testProtocolStructValuePointer), - ("testStructValuePointer", testStructValuePointer), + ("testStructValuePointer", testStructValuePointer) ] } @@ -97,7 +97,7 @@ extension ValueWitnessTableTests { static let __allTests = [ ("testAlignment", testAlignment), ("testSize", testSize), - ("testStride", testStride), + ("testStride", testStride) ] } @@ -109,7 +109,7 @@ public func __allTests() -> [XCTestCaseEntry] { testCase(GetSetStructTests.__allTests), testCase(MetadataTests.__allTests), testCase(ValuePointerTests.__allTests), - testCase(ValueWitnessTableTests.__allTests), + testCase(ValueWitnessTableTests.__allTests) ] } #endif