Skip to content

Commit e3460e8

Browse files
committed
Switch struct to actor for DumpCommand
1 parent bc29e66 commit e3460e8

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

Sources/Semantic/SemanticStringBuilder.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ public enum SemanticStringBuilder {
1414

1515
public static func buildPartialBlock(first: SemanticString) -> [Element] { first.components }
1616

17+
public static func buildPartialBlock(first: some CustomStringConvertible) -> [Element] { [Standard(first.description)] }
18+
1719
public static func buildPartialBlock(accumulated: [Element], next: Element) -> [Element] { accumulated + [next] }
1820

1921
public static func buildPartialBlock(accumulated: [Element], next: [Element]) -> [Element] { accumulated + next }
2022

2123
public static func buildPartialBlock(accumulated: [Element], next: SemanticString) -> [Element] { accumulated + next.components }
24+
25+
public static func buildPartialBlock(accumulated: [Element], next: some CustomStringConvertible) -> [Element] { accumulated + [Standard(next.description)] }
2226

2327
public static func buildOptional(_ components: [Element]?) -> [Element] { components ?? [] }
2428

Sources/swift-section/DumpCommand.swift

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import MachOSwiftSection
66
import SwiftDump
77
import Semantic
88

9-
struct DumpCommand: AsyncParsableCommand {
9+
final actor DumpCommand: AsyncParsableCommand {
1010
static let configuration: CommandConfiguration = .init(
1111
commandName: "dump",
1212
abstract: "Dump Swift information from a Mach-O file or dyld shared cache.",
@@ -35,8 +35,7 @@ struct DumpCommand: AsyncParsableCommand {
3535
@Option(name: .shortAndLong, help: "The color scheme for the output.")
3636
var colorScheme: SemanticColorScheme = .none
3737

38-
@MainActor
39-
mutating func run() async throws {
38+
func run() async throws {
4039
let machOFile = try MachOFile.load(options: machOOptions)
4140

4241
if searchMetadata {
@@ -65,40 +64,36 @@ struct DumpCommand: AsyncParsableCommand {
6564
}
6665

6766
@MainActor
68-
private mutating func dumpTypes(using options: DemangleOptions, in machO: MachOFile) async throws {
67+
private func dumpTypes(using options: DemangleOptions, in machO: MachOFile) async throws {
6968
let typeContextDescriptors = try machO.swift.typeContextDescriptors
7069

7170
for typeContextDescriptor in typeContextDescriptors {
7271
switch typeContextDescriptor {
7372
case .type(let typeContextDescriptorWrapper):
7473
switch typeContextDescriptorWrapper {
7574
case .enum(let enumDescriptor):
76-
do {
77-
let enumType = try Enum(descriptor: enumDescriptor, in: machO)
78-
try dumpOrPrint(enumType.dump(using: options, in: machO))
79-
} catch {
80-
dumpError(error)
75+
await performDump {
76+
try Enum(descriptor: enumDescriptor, in: machO).dump(using: options, in: machO)
8177
}
8278
case .struct(let structDescriptor):
83-
do {
84-
let structType = try Struct(descriptor: structDescriptor, in: machO)
85-
try dumpOrPrint(structType.dump(using: options, in: machO))
86-
if let metadata = try metadataFinder?.metadata(for: structDescriptor) as StructMetadata? {
87-
try dumpOrPrint(metadata.fieldOffsets(for: structDescriptor, in: machO))
88-
}
89-
} catch {
90-
dumpError(error)
79+
await performDump {
80+
try Struct(descriptor: structDescriptor, in: machO).dump(using: options, in: machO)
9181
}
9282

83+
if let metadata: StructMetadata = try await metadataFinder?.metadata(for: structDescriptor) {
84+
await performDump {
85+
try metadata.fieldOffsets(for: structDescriptor, in: machO)
86+
}
87+
}
9388
case .class(let classDescriptor):
94-
do {
95-
let classType = try Class(descriptor: classDescriptor, in: machO)
96-
try dumpOrPrint(classType.dump(using: options, in: machO))
97-
if let metadata = try metadataFinder?.metadata(for: classDescriptor) as ClassMetadataObjCInterop? {
98-
try dumpOrPrint(metadata.fieldOffsets(for: classDescriptor, in: machO))
89+
await performDump {
90+
try Class(descriptor: classDescriptor, in: machO).dump(using: options, in: machO)
91+
}
92+
93+
if let metadata = try await metadataFinder?.metadata(for: classDescriptor) as ClassMetadataObjCInterop? {
94+
await performDump {
95+
try metadata.fieldOffsets(for: classDescriptor, in: machO)
9996
}
100-
} catch {
101-
dumpError(error)
10297
}
10398
}
10499
default:
@@ -108,65 +103,63 @@ struct DumpCommand: AsyncParsableCommand {
108103
}
109104

110105
@MainActor
111-
private mutating func dumpAssociatedTypes(using options: DemangleOptions, in machO: MachOFile) async throws {
106+
private func dumpAssociatedTypes(using options: DemangleOptions, in machO: MachOFile) async throws {
112107
let associatedTypeDescriptors = try machO.swift.associatedTypeDescriptors
113108
for associatedTypeDescriptor in associatedTypeDescriptors {
114-
do {
115-
try dumpOrPrint(AssociatedType(descriptor: associatedTypeDescriptor, in: machO).dump(using: options, in: machO) as SemanticString)
116-
} catch {
117-
dumpError(error)
109+
await performDump {
110+
try AssociatedType(descriptor: associatedTypeDescriptor, in: machO).dump(using: options, in: machO)
118111
}
119112
}
120113
}
121114

122115
@MainActor
123-
private mutating func dumpProtocols(using options: DemangleOptions, in machO: MachOFile) async throws {
116+
private func dumpProtocols(using options: DemangleOptions, in machO: MachOFile) async throws {
124117
let protocolDescriptors = try machO.swift.protocolDescriptors
125118
for protocolDescriptor in protocolDescriptors {
126-
do {
127-
try dumpOrPrint(Protocol(descriptor: protocolDescriptor, in: machO).dump(using: options, in: machO))
128-
} catch {
129-
dumpError(error)
119+
await performDump {
120+
try Protocol(descriptor: protocolDescriptor, in: machO).dump(using: options, in: machO)
130121
}
131122
}
132123
}
133124

134125
@MainActor
135-
private mutating func dumpProtocolConformances(using options: DemangleOptions, in machO: MachOFile) async throws {
126+
private func dumpProtocolConformances(using options: DemangleOptions, in machO: MachOFile) async throws {
136127
let protocolConformanceDescriptors = try machO.swift.protocolConformanceDescriptors
137128

138129
for protocolConformanceDescriptor in protocolConformanceDescriptors {
139-
do {
140-
try dumpOrPrint(ProtocolConformance(descriptor: protocolConformanceDescriptor, in: machO).dump(using: options, in: machO))
141-
} catch {
142-
dumpError(error)
130+
await performDump {
131+
try ProtocolConformance(descriptor: protocolConformanceDescriptor, in: machO).dump(using: options, in: machO)
143132
}
144133
}
145134
}
146135

147-
private mutating func dumpOrPrint<Value: CustomStringConvertible>(_ value: Value) {
148-
dumpOrPrint(value.description)
136+
private func performDump(@SemanticStringBuilder _ action: () async throws -> SemanticString) async {
137+
do {
138+
try await dumpOrPrint(action())
139+
} catch {
140+
dumpError(error)
141+
}
142+
}
143+
144+
private func dumpError(_ error: Swift.Error) {
145+
dumpOrPrint(SemanticString(components: Error(error.localizedDescription)))
149146
}
150147

151-
private mutating func dumpOrPrint(_ string: String) {
148+
private func dumpOrPrint(_ semanticString: SemanticString) {
152149
if outputPath != nil {
153-
dumpedString.append(string)
150+
dumpedString.append(semanticString.string)
154151
dumpedString.append("\n")
155152
} else {
156-
print(string)
153+
print(semanticString.components.map { $0.string.withColor(for: $0.type, colorScheme: colorScheme) }.joined())
157154
}
158155
}
159156

160-
private mutating func dumpError(_ error: Swift.Error) {
161-
dumpOrPrint(SemanticString(components: Error(error.localizedDescription)))
162-
}
163-
164-
private mutating func dumpOrPrint(_ semanticString: SemanticString) {
157+
private func dumpOrPrint(_ string: String) {
165158
if outputPath != nil {
166-
dumpedString.append(semanticString.string)
159+
dumpedString.append(string)
167160
dumpedString.append("\n")
168161
} else {
169-
print(semanticString.components.map { $0.string.withColor(for: $0.type, colorScheme: colorScheme) }.joined())
162+
print(string)
170163
}
171164
}
172165
}

0 commit comments

Comments
 (0)