Skip to content
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

[SR-953] Implement Bundle(for: class) #1573

Closed
wants to merge 3 commits into from
Closed

[SR-953] Implement Bundle(for: class) #1573

wants to merge 3 commits into from

Conversation

millenomi
Copy link
Contributor

@millenomi millenomi commented Jun 2, 2018

Implements Bundle(for:) by using dladdr() on the type descriptor of a class. For further discussion, see swiftlang/swift#16964, which is a dependency of this patch.

Resolves SR-953.

@millenomi
Copy link
Contributor Author

This requires new specific tests before merging.

@millenomi
Copy link
Contributor Author

Please test with the following:
swiftlang/swift#16964

@swift-ci please test


// dladdr() on Linux is documented to return argv[0]
// If this is argv[0], do not create a new bundle (which will use the main bundle below).
let pathNS = unsafeBitCast(path, to: NSString.self)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't need this intermediate anymore.


let url = unsafeBitCast(NSURL(fileURLWithPath: pathString), to: CFURL.self)
if let bundleURL = _CFBundleCopyBundleURLForExecutableURL(url) {
_bundle = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL.takeRetainedValue())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should probably .takeRetainedValue above.

// If this is argv[0], do not create a new bundle (which will use the main bundle below).
let pathNS = unsafeBitCast(path, to: NSString.self)
let pathString = pathNS as String
if ProcessInfo.processInfo.arguments.first != pathString {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe Bundle.main.executablePath would be clearer?

@ephemer
Copy link

ephemer commented Jun 8, 2018

Looks good, and from a quick eyeballing it seems like it should work on Android too 👍

@millenomi
Copy link
Contributor Author

Please test with the following:
swiftlang/swift#16964

@swift-ci please test

1 similar comment
@millenomi
Copy link
Contributor Author

Please test with the following:
swiftlang/swift#16964

@swift-ci please test

@@ -54,7 +54,26 @@ open class Bundle: NSObject {
}

public init(for aClass: AnyClass) {
NSUnimplemented()
let pointer = unsafeBitCast(aClass, to: UnsafeRawPointer.self)
Copy link

Choose a reason for hiding this comment

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

Just wondering, why are we using unsafeBitCast here and not an inout?

var theClass = aClass
if let path = _CFBundleBlah(&theClass) {...}

My understanding is they do the same thing but the latter is more resilient to changes in ABI

@spevans
Copy link
Contributor

spevans commented Jun 25, 2018

@swift-ci please test

1 similar comment
@millenomi
Copy link
Contributor Author

@swift-ci please test

@millenomi
Copy link
Contributor Author

And I didn’t see the merge conflict marker. Whoops.

@shahmishal shahmishal closed this Oct 6, 2020
@shahmishal
Copy link
Member

The Swift project moved the default branch to main and deleted master branch, so GitHub automatically closed the PR. Please re-create pull request with main branch.

More detail about the branch update - https://forums.swift.org/t/updating-branch-names/40412

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants