Skip to content

Commit 6060a2a

Browse files
committed
use effective modes
1 parent 12dd5db commit 6060a2a

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

Sources/JExtractSwiftLib/Swift2Java.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public struct SwiftToJava {
9595

9696
try translator.analyze()
9797

98-
switch config.mode {
99-
case .some(.ffm), .none:
98+
switch config.effectiveMode {
99+
case .ffm:
100100
let generator = FFMSwift2JavaGenerator(
101101
config: self.config,
102102
translator: translator,

Sources/SwiftJavaDocumentation/Documentation.docc/SupportedFeatures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ on the Java side.
139139
#### Unsigned numbers mode: wrapGuava
140140

141141
You can configure `jextract` (in FFM mode) to instead import unsigned values as their unsigned type-safe representations
142-
as offered by the Guava library: `UnsignedLong` or `UnsignedInt`. To enable this mode pass the `--unsigned-numbers wrapGuava`
142+
as offered by the Guava library: `UnsignedLong` or `UnsignedInt`. To enable this mode pass the `--unsigned-numbers-mode wrapGuava`
143143
command line option, or set the corresponding configuration value in `swift-java.config` (TODO).
144144

145145
This approach is type-safe, however it incurs a performance penalty for allocating a wrapper class for every

Sources/SwiftJavaTool/Commands/JExtractCommand.swift

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ extension SwiftJava {
6262
var writeEmptyFiles: Bool = false
6363

6464
@Option(help: "The mode of generation to use for the output files. Used with jextract mode. By default, unsigned Swift types are imported as their bit-width compatible signed Java counterparts, and annotated using the '@Unsigned' annotation. You may choose the 'wrapGuava' mode in order to import types as class wrapper types (`UnsignedInteger` et al) defined by the Google Guava library's `com.google.common.primitives' package. that ensure complete type-safety with regards to unsigned values, however they incur an allocation and performance overhead.")
65-
var unsignedNumbers: JExtractUnsignedIntegerMode?
65+
var unsignedNumbersMode: JExtractUnsignedIntegerMode?
6666

6767
@Option(help: "The lowest access level of Swift declarations that should be extracted, defaults to 'public'.")
68-
var minimumInputAccessLevel: JExtractMinimumAccessLevelMode?
68+
var minimumInputAccessLevelMode: JExtractMinimumAccessLevelMode?
6969

7070
@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 `allowGlobalAutomatic`, user can omit this parameter and a global GC-based arena will be used.")
7171
var memoryManagementMode: JExtractMemoryManagementMode?
@@ -86,16 +86,21 @@ extension SwiftJava.JExtractCommand {
8686
if let javaPackage {
8787
config.javaPackage = javaPackage
8888
}
89-
configure(&config.mode, overrideWith: mode)
89+
configure(&config.mode, overrideWith: self.mode)
9090
config.swiftModule = self.effectiveSwiftModule
9191
config.outputJavaDirectory = outputJava
9292
config.outputSwiftDirectory = outputSwift
93+
94+
// @Flag does not support optional, so we check ourself if it is passed
95+
let writeEmptyFiles = CommandLine.arguments.contains("--write-empty-files") ? true : nil
96+
configure(&config.writeEmptyFiles, overrideWith: writeEmptyFiles)
97+
9398
config.writeEmptyFiles = writeEmptyFiles
94-
configure(&config.unsignedNumbersMode, overrideWith: unsignedNumbers)
95-
configure(&config.minimumInputAccessLevelMode, overrideWith: minimumInputAccessLevelMode)
96-
configure(&config.memoryManagementMode, overrideWith: memoryManagementMode)
99+
configure(&config.unsignedNumbersMode, overrideWith: self.unsignedNumbersMode)
100+
configure(&config.minimumInputAccessLevelMode, overrideWith: self.minimumInputAccessLevelMode)
101+
configure(&config.memoryManagementMode, overrideWith: self.memoryManagementMode)
97102

98-
try checkModeCompatibility()
103+
try checkModeCompatibility(config: config)
99104

100105
if let inputSwift = commonOptions.inputSwift {
101106
config.inputSwiftDirectory = inputSwift
@@ -104,7 +109,7 @@ extension SwiftJava.JExtractCommand {
104109
config.inputSwiftDirectory = "\(FileManager.default.currentDirectoryPath)/Sources/\(swiftModule)"
105110
}
106111

107-
print("[debug][swift-java] Running 'swift-java jextract' in mode: " + "\(config.mode ?? .ffm)".bold)
112+
print("[debug][swift-java] Running 'swift-java jextract' in mode: " + "\(config.effectiveMode)".bold)
108113

109114
// Load all of the dependent configurations and associate them with Swift modules.
110115
let dependentConfigs = try loadDependentConfigs(dependsOn: self.dependsOn)
@@ -114,16 +119,16 @@ extension SwiftJava.JExtractCommand {
114119
}
115120

116121
/// Check if the configured modes are compatible, and fail if not
117-
func checkModeCompatibility() throws {
118-
if self.mode == .jni {
119-
switch self.unsignedNumbers {
122+
func checkModeCompatibility(config: Configuration) throws {
123+
if config.effectiveMode == .jni {
124+
switch config.effectiveUnsignedNumbersMode {
120125
case .annotate:
121126
() // OK
122127
case .wrapGuava:
123128
throw IllegalModeCombinationError("JNI mode does not support '\(JExtractUnsignedIntegerMode.wrapGuava)' Unsigned integer mode! \(Self.helpMessage)")
124129
}
125-
} else if self.mode == .ffm {
126-
guard self.memoryManagementMode == .explicit else {
130+
} else if config.effectiveMode == .ffm {
131+
guard config.effectiveMemoryManagementMode == .explicit else {
127132
throw IllegalModeCombinationError("FFM mode does not support '\(self.memoryManagementMode)' memory management mode! \(Self.helpMessage)")
128133
}
129134
}

Sources/SwiftJavaTool/SwiftJavaBaseAsyncParsableCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ extension SwiftJavaBaseAsyncParsableCommand {
167167
config.logLevel = command.logLevel
168168
return config
169169
}
170-
}
170+
}

0 commit comments

Comments
 (0)