Skip to content

Commit 2f63210

Browse files
authored
Merge pull request #95 from Supereg/fix/factory-linux-create-class
Fix Factory `createInstance` for class types on linux platforms
2 parents ed4bca1 + 5910ef1 commit 2f63210

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

Sources/Runtime/Factory/Factory.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,15 @@ func buildClass(type: Any.Type) throws -> Any {
6161
var md = ClassMetadata(type: type)
6262
let info = md.toTypeInfo()
6363
let metadata = unsafeBitCast(type, to: UnsafeRawPointer.self)
64-
let instanceSize = Int32(md.pointer.pointee.classSize)
65-
let alignment = Int32(md.alignment)
64+
let instanceSize = Int32(md.pointer.pointee.instanceSize)
6665

67-
guard let value = swift_allocObject(metadata, instanceSize, alignment) else {
66+
// https://github.com/wickwirew/Runtime/issues/49
67+
// Docs specify that the alignment should be "always one less than a power of 2 that's at least alignof(void*)"
68+
// https://github.com/apple/swift/blob/7123d2614b5f222d03b3762cb110d27a9dd98e24/include/swift/Runtime/HeapObject.h#L56-L57
69+
// We could use md.alignment and deduct 1, or just use the instanceAlignmentMask from the ClassMetadata.
70+
let alignmentMask = Int32(md.pointer.pointee.instanceAlignmentMask)
71+
72+
guard let value = swift_allocObject(metadata, instanceSize, alignmentMask) else {
6873
throw RuntimeError.unableToBuildType(type: type)
6974
}
7075

Tests/RuntimeTests/GetSetStructTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class GetSetStructTests: XCTestCase {
2929
static var allTests: [(String, (GetSetStructTests) -> () throws -> Void)] {
3030
return [
3131
("testGet", testGet),
32+
("testGetSimple", testGetSimple),
3233
("testGetUntypedValue", testGetUntypedValue),
3334
("testGetUntypedObject", testGetUntypedObject),
3435
("testGetUntyped", testGetUntyped),

Tests/RuntimeTests/MetadataTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,15 @@ class MetadataTests: XCTestCase {
227227
XCTAssert(hasPayload.payloadType == Int.self)
228228
XCTAssert(hasTuplePayload.payloadType == (Bool, Int).self)
229229
}
230-
231-
#if canImport(Foundation)
230+
232231
func testObjcEnum() {
232+
#if canImport(Foundation)
233233
var md = EnumMetadata(type: ComparisonResult.self)
234234
let info = md.toTypeInfo()
235235
XCTAssertEqual(info.numberOfEnumCases, 3)
236236
XCTAssertEqual(info.numberOfPayloadEnumCases, 0)
237+
#endif
237238
}
238-
#endif
239239

240240
func testOptional() throws {
241241
let info = try typeInfo(of: Double?.self)

Tests/RuntimeTests/XCTestManifests.swift

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import XCTest
33
extension FactoryTests {
44
static let __allTests = [
55
("testStruct", testStruct),
6-
("testStructUntyped", testStructUntyped)
6+
("testStructUntyped", testStructUntyped),
7+
("testClass", testClass),
8+
("testGenericClass", testGenericClass)
79
]
810
}
911

@@ -33,13 +35,14 @@ extension GetSetClassTests {
3335
("testSetClassUntypedValue", testSetClassUntypedValue),
3436
("testSetUntyped", testSetUntyped),
3537
("testSetUntypedObject", testSetUntypedObject),
36-
("testSetUntypedValue", testSetUntypedValue)
38+
("testSetUntypedValue", testSetUntypedValue)
3739
]
3840
}
3941

4042
extension GetSetStructTests {
4143
static let __allTests = [
4244
("testGet", testGet),
45+
("testGetSimple", testGetSimple),
4346
("testGetArray", testGetArray),
4447
("testGetArrayUntyped", testGetArrayUntyped),
4548
("testGetArrayUntypedObject", testGetArrayUntypedObject),
@@ -64,21 +67,27 @@ extension GetSetStructTests {
6467
("testSetStructUntypedValue", testSetStructUntypedValue),
6568
("testSetUntyped", testSetUntyped),
6669
("testSetUntypedObject", testSetUntypedObject),
67-
("testSetUntypedValue", testSetUntypedValue)
70+
("testSetUntypedValue", testSetUntypedValue)
6871
]
6972
}
7073

7174
extension MetadataTests {
7275
static let __allTests = [
7376
("testClass", testClass),
74-
("testEnum", testEnum),
75-
("testFunction", testFunction),
76-
("testFunctionThrows", testFunctionThrows),
77-
("testProtocol", testProtocol),
77+
("testResilientClass", testResilientClass),
7878
("testStruct", testStruct),
79+
("testGenericStruct", testGenericStruct),
80+
("testNestedStruct", testNestedStruct),
81+
("testProtocol", testProtocol),
7982
("testTuple", testTuple),
8083
("testTupleNoLabels", testTupleNoLabels),
81-
("testVoidFunction", testVoidFunction)
84+
("testFunction", testFunction),
85+
("testFunctionThrows", testFunctionThrows),
86+
("testVoidFunction", testVoidFunction),
87+
("testEnum", testEnum),
88+
("testEnumTestEnumWithPayload", testEnumTestEnumWithPayload),
89+
("testObjcEnum", testObjcEnum),
90+
("testOptional", testOptional)
8291
]
8392
}
8493

@@ -89,15 +98,15 @@ extension ValuePointerTests {
8998
("testClassValuePointer", testClassValuePointer),
9099
("testProtocolClassValuePointer", testProtocolClassValuePointer),
91100
("testProtocolStructValuePointer", testProtocolStructValuePointer),
92-
("testStructValuePointer", testStructValuePointer)
101+
("testStructValuePointer", testStructValuePointer)
93102
]
94103
}
95104

96105
extension ValueWitnessTableTests {
97106
static let __allTests = [
98107
("testAlignment", testAlignment),
99108
("testSize", testSize),
100-
("testStride", testStride)
109+
("testStride", testStride)
101110
]
102111
}
103112

@@ -109,7 +118,7 @@ public func __allTests() -> [XCTestCaseEntry] {
109118
testCase(GetSetStructTests.__allTests),
110119
testCase(MetadataTests.__allTests),
111120
testCase(ValuePointerTests.__allTests),
112-
testCase(ValueWitnessTableTests.__allTests)
121+
testCase(ValueWitnessTableTests.__allTests)
113122
]
114123
}
115124
#endif

0 commit comments

Comments
 (0)