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

Search all available dlyd images for known providers #38

Merged
merged 3 commits into from
Mar 15, 2024
Merged
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
50 changes: 22 additions & 28 deletions Sources/StorybookKit/Internals/machOLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,28 @@ extension Book {
guard !moduleName.isEmpty else {
return nil
}
guard
let imageIndex = (0..<_dyld_image_count()).first(
where: {
guard let pathC = _dyld_get_image_name($0) else {
return false
}
let path = String(cString: pathC)
let imageName = path
.components(separatedBy: "/")
.last?
.components(separatedBy: ".")
.first
if imageName == moduleName {
print(path)
}
return imageName == moduleName
}
var results: [any BookProvider.Type] = []
for imageIndex in 0 ..< _dyld_image_count() {
self.findAllBookProviders(
inImageIndex: .init(imageIndex),
filterByStorybookPageMacro: filterByStorybookPageMacro,
results: &results
)
else {
return nil
}
return results
}

private static func findAllBookProviders(
inImageIndex imageIndex: UInt32,
filterByStorybookPageMacro: Bool,
results: inout [any BookProvider.Type]
) {
// Follows same approach here: https://github.com/apple/swift-testing/blob/main/Sources/TestingInternals/Discovery.cpp#L318
guard
let headerRawPtr: UnsafeRawPointer = _dyld_get_image_header(imageIndex)
.map(UnsafeRawPointer.init(_:))
else {
return nil
return
}
let headerPtr = headerRawPtr.assumingMemoryBound(
to: mach_header_64.self
Expand All @@ -79,23 +73,23 @@ extension Book {
)
.map(UnsafeRawPointer.init(_:))
else {
return nil
return
}
let capacity: Int = .init(size) / MemoryLayout<SwiftTypeMetadataRecord>.size
let sectionPtr = sectionRawPtr.assumingMemoryBound(
to: SwiftTypeMetadataRecord.self
)
return (0 ..< capacity).compactMap { index in
for index in 0 ..< capacity {
let record = sectionPtr.advanced(by: index)
guard
let contextDescriptor = record.pointee.contextDescriptor(
from: record
)
else {
return nil
continue
}
guard !contextDescriptor.pointee.isGeneric() else {
return nil
continue
}
guard
contextDescriptor.pointee.kind().canConformToProtocol,
Expand All @@ -106,23 +100,23 @@ extension Book {
}
)
else {
return nil
continue
}
let metadataClosure = contextDescriptor.resolveValue(for: \.metadataAccessFunction)
let metadata = metadataClosure(0xFF)
guard
let metadataAccessFunction = metadata.value
else {
return nil
continue
}
let anyType = unsafeBitCast(
metadataAccessFunction,
to: Any.Type.self
)
guard let bookProviderType = anyType as? any BookProvider.Type else {
return nil
continue
}
return bookProviderType
results.append(bookProviderType)
}
}
}
Expand Down
Loading