Skip to content

Add upcall for FoundationEssentials to initialize NSNumbers #4966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let package = Package(
),
.package(
url: "https://github.com/apple/swift-foundation",
revision: "db63ab39bb4f07eeb3ccf19fa7a9f928a7ca3972"
revision: "3297fb33b49ba2d1161ba12757891c7b91c73a3e"
),
],
targets: [
Expand Down
16 changes: 16 additions & 0 deletions Sources/Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


@_implementationOnly import _CoreFoundation
@_spi(SwiftCorelibsFoundation) @_exported import FoundationEssentials
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it @_exported so that _typeByName("Foundation._FoundationNSNumberInitializer") is visible to FoundationEssentials?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @_exported attribute is the attribute that re-exports all of the symbols from FoundationEssentials. All of our imports of FoundationEssentials and FoundationInternationalization are @_exported so that Foundation.URL == FoundationEssentials.URL because Foundation is re-exporting FoundationEssentials and everything within it, so it's not specific to this change it's just something we have to add to every import of those modules


internal let kCFNumberSInt8Type = CFNumberType.sInt8Type
internal let kCFNumberSInt16Type = CFNumberType.sInt16Type
Expand Down Expand Up @@ -1172,3 +1173,18 @@ protocol _NSNumberCastingWithoutBridging {
}

extension NSNumber: _NSNumberCastingWithoutBridging {}

// Called by FoundationEssentials
internal final class _FoundationNSNumberInitializer : _NSNumberInitializer {
public static func initialize(value: some BinaryInteger) -> Any {
if let int64 = Int64(exactly: value) {
return NSNumber(value: int64)
} else {
return NSNumber(value: UInt64(value))
}
}

public static func initialize(value: Bool) -> Any {
NSNumber(value: value)
}
}
11 changes: 11 additions & 0 deletions Tests/Foundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,17 @@ class TestFileManager : XCTestCase {
}
}

func testNSNumberUpcall() throws {
let url = writableTestDirectoryURL.appending(component: "foo", directoryHint: .notDirectory)
try FileManager.default.createDirectory(at: writableTestDirectoryURL, withIntermediateDirectories: true)
XCTAssertTrue(FileManager.default.createFile(atPath: url.path, contents: Data("foo".utf8)))
let attrs = try FileManager.default.attributesOfItem(atPath: url.path)
let size = attrs[.size]
XCTAssertNotNil(size as? NSNumber)
XCTAssertNotNil(size as? UInt64)
XCTAssertNotNil(size as? Double) // Ensure implicit conversion to unexpected types works
}

// -----

var writableTestDirectoryURL: URL!
Expand Down