Skip to content

Commit 8bc7096

Browse files
committed
add memory-management-mode config
1 parent 912fe01 commit 8bc7096

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Sources/JavaKitConfigurationShared/Configuration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public struct Configuration: Codable {
5151
minimumInputAccessLevelMode ?? .default
5252
}
5353

54+
public var memoryManagementMode: JExtractMemoryManagementMode?
55+
public var effectiveMemoryManagementMode: JExtractMemoryManagementMode {
56+
memoryManagementMode ?? .forceExplicit
57+
}
58+
5459
// ==== java 2 swift ---------------------------------------------------------
5560

5661
/// The Java class path that should be passed along to the swift-java tool.

Sources/JavaKitConfigurationShared/GenerationMode.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ extension JExtractMinimumAccessLevelMode {
7676
.public
7777
}
7878
}
79+
80+
81+
/// Configures how memory should be managed by the user
82+
public enum JExtractMemoryManagementMode: String, Codable {
83+
/// Force users to provide an explicit `SwiftArena` to all calls that require them.
84+
case forceExplicit
85+
86+
/// Provide both explicit `SwiftArena` support
87+
/// and a default global automatic `SwiftArena` that will deallocate memory when the GC decides to.
88+
case allowAutomatic
89+
90+
/// Force all memory management to a default global automatic `SwiftArena`
91+
/// that will deallocate memory when the GC decides to.
92+
case forceAutomatic
93+
}

Sources/SwiftJavaTool/Commands/JExtractCommand.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ extension SwiftJava {
6767
@Option(help: "The lowest access level of Swift declarations that should be extracted, defaults to 'public'.")
6868
var minimumInputAccessLevel: JExtractMinimumAccessLevelMode = .default
6969

70+
@Option(help: "The memory management mode to use for the generated code. By default, the user must explicitly provide `SwiftArena` to all calls that require it. By choosing `allow-automatic`, user can omit this parameter and a global GC-based arena will be used. `force-automatic` removes all explicit memory management.")
71+
var memoryManagementMode: JExtractMemoryManagementMode = .forceExplicit
72+
7073
@Option(
7174
help: """
7275
A swift-java configuration file for a given Swift module name on which this module depends,
@@ -89,6 +92,7 @@ extension SwiftJava.JExtractCommand {
8992
config.writeEmptyFiles = writeEmptyFiles
9093
config.unsignedNumbersMode = unsignedNumbers
9194
config.minimumInputAccessLevelMode = minimumInputAccessLevel
95+
config.memoryManagementMode = memoryManagementMode
9296

9397
try checkModeCompatibility()
9498

@@ -117,6 +121,10 @@ extension SwiftJava.JExtractCommand {
117121
case .wrapGuava:
118122
() // OK
119123
}
124+
} else if self.mode == .ffm {
125+
guard self.memoryManagementMode == .forceExplicit else {
126+
throw IllegalModeCombinationError("FFM mode does not support '\(self.memoryManagementMode)' memory management mode! \(Self.helpMessage)")
127+
}
120128
}
121129
}
122130
}
@@ -148,3 +156,4 @@ struct IllegalModeCombinationError: Error {
148156
extension JExtractGenerationMode: ExpressibleByArgument {}
149157
extension JExtractUnsignedIntegerMode: ExpressibleByArgument {}
150158
extension JExtractMinimumAccessLevelMode: ExpressibleByArgument {}
159+
extension JExtractMemoryManagementMode: ExpressibleByArgument {}

0 commit comments

Comments
 (0)