Skip to content

Commit c710bcf

Browse files
committedMay 30, 2023
Foundation: implement Bundle.init(for:) on Windows
This was disabled due to the use of `dlsym` which has been removed in favour of CoreFoundation APIs. Enable this code path and tweak the test to allow passing on Windows. This function is required to support apple/swift-docc on Windows.
1 parent 818de48 commit c710bcf

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed
 

‎Sources/Foundation/Bundle.swift

-7
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ open class Bundle: NSObject {
107107
self.init(path: url.path)
108108
}
109109

110-
#if os(Windows)
111-
@available(Windows, deprecated, message: "Not yet implemented.")
112-
public init(for aClass: AnyClass) {
113-
NSUnimplemented()
114-
}
115-
#else
116110
public init(for aClass: AnyClass) {
117111
let pointerInImageOfClass = _getTypeContextDescriptor(of: aClass)
118112
guard let imagePath = _CFBundleCopyLoadedImagePathForAddress(pointerInImageOfClass)?._swiftObject else {
@@ -138,7 +132,6 @@ open class Bundle: NSObject {
138132
let bundle = _CFBundleCreateWithExecutableURLIfMightBeBundle(kCFAllocatorSystemDefault, url._cfObject)?.takeRetainedValue()
139133
_bundleStorage = bundle ?? CFBundleGetMainBundle()
140134
}
141-
#endif
142135

143136
public init?(identifier: String) {
144137
super.init()

‎Tests/Foundation/Tests/TestBundle.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,11 @@ class TestBundle : XCTestCase {
560560
let maybeURL = Bundle.main.executableURL
561561
XCTAssertNotNil(maybeURL)
562562
guard let url = maybeURL else { return }
563-
564-
XCTAssertEqual(url.path, ProcessInfo.processInfo._processPath)
563+
564+
let path = url.withUnsafeFileSystemRepresentation {
565+
String(cString: $0!)
566+
}
567+
XCTAssertEqual(path, ProcessInfo.processInfo._processPath)
565568
}
566569
#endif
567570

@@ -585,7 +588,7 @@ class TestBundle : XCTestCase {
585588
("test_bundleForClass", testExpectedToFailOnWindows(test_bundleForClass, "Functionality not yet implemented on Windows. SR-XXXX")),
586589
]
587590

588-
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
591+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
589592
tests.append(contentsOf: [
590593
("test_mainBundleExecutableURL", test_mainBundleExecutableURL),
591594
("test_bundleReverseBundleLookup", test_bundleReverseBundleLookup),

0 commit comments

Comments
 (0)