Skip to content

Commit 08e829c

Browse files
authored
Add upcall for FoundationEssentials to initialize NSNumbers (#4966)
* Add upcall for FoundationEssentials to initialize NSNumbers * Update swift-foundation commit hash
1 parent 1b669a5 commit 08e829c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let package = Package(
8282
),
8383
.package(
8484
url: "https://github.com/apple/swift-foundation",
85-
revision: "db63ab39bb4f07eeb3ccf19fa7a9f928a7ca3972"
85+
revision: "3297fb33b49ba2d1161ba12757891c7b91c73a3e"
8686
),
8787
],
8888
targets: [

Sources/Foundation/NSNumber.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
@_implementationOnly import _CoreFoundation
12+
@_spi(SwiftCorelibsFoundation) @_exported import FoundationEssentials
1213

1314
internal let kCFNumberSInt8Type = CFNumberType.sInt8Type
1415
internal let kCFNumberSInt16Type = CFNumberType.sInt16Type
@@ -1172,3 +1173,18 @@ protocol _NSNumberCastingWithoutBridging {
11721173
}
11731174

11741175
extension NSNumber: _NSNumberCastingWithoutBridging {}
1176+
1177+
// Called by FoundationEssentials
1178+
internal final class _FoundationNSNumberInitializer : _NSNumberInitializer {
1179+
public static func initialize(value: some BinaryInteger) -> Any {
1180+
if let int64 = Int64(exactly: value) {
1181+
return NSNumber(value: int64)
1182+
} else {
1183+
return NSNumber(value: UInt64(value))
1184+
}
1185+
}
1186+
1187+
public static func initialize(value: Bool) -> Any {
1188+
NSNumber(value: value)
1189+
}
1190+
}

Tests/Foundation/TestFileManager.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,17 @@ class TestFileManager : XCTestCase {
17851785
}
17861786
}
17871787

1788+
func testNSNumberUpcall() throws {
1789+
let url = writableTestDirectoryURL.appending(component: "foo", directoryHint: .notDirectory)
1790+
try FileManager.default.createDirectory(at: writableTestDirectoryURL, withIntermediateDirectories: true)
1791+
XCTAssertTrue(FileManager.default.createFile(atPath: url.path, contents: Data("foo".utf8)))
1792+
let attrs = try FileManager.default.attributesOfItem(atPath: url.path)
1793+
let size = attrs[.size]
1794+
XCTAssertNotNil(size as? NSNumber)
1795+
XCTAssertNotNil(size as? UInt64)
1796+
XCTAssertNotNil(size as? Double) // Ensure implicit conversion to unexpected types works
1797+
}
1798+
17881799
// -----
17891800

17901801
var writableTestDirectoryURL: URL!

0 commit comments

Comments
 (0)