-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up metadata reflection code (#372)
Our OpenCombine fork no longer depends on Runtime, and we don't need much from it other than struct metadata. I removed the unused bits and bobs and kept only a minimal subset of it that we really need. This should make it easier for us to test and debug, as #367 has shown that some weird stuff may still lurk in that area. * Add a test for environment injection We had some issues in this code area previously and I'm thinking of refactoring it in attempt to fix #367. Would be great to increase the test coverage here before further refactoring. * Update copyright years in `MountedElement.swift` * Update copyright years in the rest of the files * Vend the Runtime library directly * Remove unused class, enum, tuple, func reflection * Remove unused models and protocol metadata * Remove unused MetadataType and NominalMetadataType * Remove unused protocols, rename RelativePointer * Remove more unused protocols * Use immutable pointers for reflection * Update copyright headers
- Loading branch information
1 parent
07ccef8
commit 0e89ea9
Showing
37 changed files
with
1,453 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Sources/TokamakCore/Reflection/Layouts/ExistentialContainter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2017-2021 Wesley Wickwire and Tokamak contributors | ||
// | ||
// 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 ExistentialContainer { | ||
let buffer: ExistentialContainerBuffer | ||
let type: Any.Type | ||
let witnessTable: Int | ||
} | ||
|
||
struct ExistentialContainerBuffer { | ||
let buffer1: Int | ||
let buffer2: Int | ||
let buffer3: Int | ||
} | ||
|
||
extension ExistentialContainerBuffer { | ||
static func size() -> Int { | ||
MemoryLayout<ExistentialContainerBuffer>.size | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
Sources/TokamakCore/Reflection/Layouts/FieldDescriptor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2017-2021 Wesley Wickwire and Tokamak contributors | ||
// | ||
// 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. | ||
|
||
#if canImport(CRuntime) | ||
import CRuntime | ||
#endif | ||
|
||
@_silgen_name("swift_getTypeByMangledNameInContext") | ||
func _getTypeByMangledNameInContext( | ||
_ name: UnsafePointer<UInt8>, | ||
_ nameLength: UInt, | ||
_ genericContext: UnsafeRawPointer?, | ||
_ genericArguments: UnsafeRawPointer? | ||
) | ||
-> Any.Type? | ||
|
||
/// https://github.com/apple/swift/blob/f2c42509628bed66bf5b8ee02fae778a2ba747a1/include/swift/Reflection/Records.h#L160 | ||
struct FieldDescriptor { | ||
let mangledTypeNameOffset: Int32 | ||
let superClassOffset: Int32 | ||
let _kind: UInt16 | ||
let fieldRecordSize: Int16 | ||
let numFields: Int32 | ||
let fields: FieldRecord | ||
|
||
var kind: FieldDescriptorKind { | ||
FieldDescriptorKind(rawValue: _kind)! | ||
} | ||
} | ||
|
||
extension UnsafePointer where Pointee == FieldRecord { | ||
func fieldName() -> String { | ||
String(cString: advance(offset: \._fieldName)) | ||
} | ||
|
||
func type( | ||
genericContext: UnsafeRawPointer?, | ||
genericArguments: UnsafeRawPointer? | ||
) -> Any.Type { | ||
let typeName = advance(offset: \._mangledTypeName) | ||
return _getTypeByMangledNameInContext( | ||
typeName, | ||
getSymbolicMangledNameLength(typeName), | ||
genericContext, | ||
genericArguments?.assumingMemoryBound(to: UnsafeRawPointer?.self) | ||
)! | ||
} | ||
} | ||
|
||
private func getSymbolicMangledNameLength(_ base: UnsafeRawPointer) -> UInt { | ||
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<Int>.size | ||
} | ||
} | ||
|
||
return UInt(end - base) | ||
} | ||
|
||
struct FieldRecord { | ||
let fieldRecordFlags: Int32 | ||
let _mangledTypeName: MetadataOffset<UInt8> | ||
let _fieldName: MetadataOffset<UInt8> | ||
|
||
var isVar: Bool { | ||
(fieldRecordFlags & 0x2) == 0x2 | ||
} | ||
} | ||
|
||
enum FieldDescriptorKind: UInt16 { | ||
case `struct` | ||
case `class` | ||
case `enum` | ||
case multiPayloadEnum | ||
case `protocol` | ||
case classProtocol | ||
case objcProtocol | ||
case objcClass | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/TokamakCore/Reflection/Layouts/ProtocolTypeContainer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2017-2021 Wesley Wickwire and Tokamak contributors | ||
// | ||
// 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 ProtocolTypeContainer { | ||
let type: Any.Type | ||
let witnessTable: Int | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/TokamakCore/Reflection/Layouts/StructMetadataLayout.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2017-2021 Wesley Wickwire and Tokamak contributors | ||
// | ||
// 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 StructMetadataLayout { | ||
let _kind: Int | ||
let typeDescriptor: UnsafePointer<StructTypeDescriptor> | ||
} |
34 changes: 34 additions & 0 deletions
34
Sources/TokamakCore/Reflection/Layouts/StructTypeDescriptor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2017-2021 Wesley Wickwire and Tokamak contributors | ||
// | ||
// 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. | ||
|
||
typealias FieldTypeAccessor = @convention(c) (UnsafePointer<Int>) -> UnsafePointer<Int> | ||
|
||
struct StructTypeDescriptor { | ||
let flags: Int32 | ||
let parent: Int32 | ||
let mangledName: MetadataOffset<CChar> | ||
let accessFunctionPtr: MetadataOffset<UnsafeRawPointer> | ||
let fieldDescriptor: MetadataOffset<FieldDescriptor> | ||
let numberOfFields: Int32 | ||
let offsetToTheFieldOffsetVector: RelativeVectorPointer<Int32, Int32> | ||
let genericContextHeader: TargetTypeGenericContextDescriptorHeader | ||
} |
Oops, something went wrong.