Skip to content

Commit fea7909

Browse files
committed
fix multiple optionals
1 parent 485ea4c commit fea7909

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

Samples/JExtractJNISampleApp/Sources/MySwiftLibrary/Vehicle.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ public enum Vehicle {
1717
case car(String, trailer: String?)
1818
case motorbike(String, horsePower: Int64, helmets: Int32?)
1919
indirect case transformer(front: Vehicle, back: Vehicle)
20+
case boat(passengers: Int32?, length: Int16?)
2021

2122
public init?(name: String) {
2223
switch name {
2324
case "bicycle": self = .bicycle
2425
case "car": self = .car("Unknown", trailer: nil)
2526
case "motorbike": self = .motorbike("Unknown", horsePower: 0, helmets: nil)
27+
case "boat": self = .boat(passengers: nil, length: nil)
2628
default: return nil
2729
}
2830
}
@@ -33,6 +35,7 @@ public enum Vehicle {
3335
case .car: "car"
3436
case .motorbike: "motorbike"
3537
case .transformer: "transformer"
38+
case .boat: "boat"
3639
}
3740
}
3841

@@ -45,6 +48,7 @@ public enum Vehicle {
4548
case (.motorbike, .motorbike), (.motorbike, .transformer): false
4649
case (.transformer, .bicycle), (.transformer, .car), (.transformer, .motorbike): true
4750
case (.transformer, .transformer): false
51+
default: false
4852
}
4953
}
5054

@@ -53,7 +57,7 @@ public enum Vehicle {
5357
case .bicycle: self = .car("Unknown", trailer: nil)
5458
case .car: self = .motorbike("Unknown", horsePower: 0, helmets: nil)
5559
case .motorbike: self = .transformer(front: .car("BMW", trailer: nil), back: self)
56-
case .transformer: break
60+
case .transformer, .boat: break
5761
}
5862
}
5963
}

Samples/JExtractJNISampleApp/src/test/java/com/example/swift/VehicleEnumTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void getAsMotorbike() {
117117
assertEquals("Yamaha", motorbike.arg0());
118118
assertEquals(750, motorbike.horsePower());
119119
assertEquals(OptionalInt.empty(), motorbike.helmets());
120-
120+
121121
vehicle = Vehicle.motorbike("Yamaha", 750, OptionalInt.of(2), arena);
122122
motorbike = vehicle.getAsMotorbike().orElseThrow();
123123
assertEquals(OptionalInt.of(2), motorbike.helmets());
@@ -134,6 +134,16 @@ void getAsTransformer() {
134134
}
135135
}
136136

137+
@Test
138+
void getAsBoat() {
139+
try (var arena = new ConfinedSwiftMemorySession()) {
140+
Vehicle vehicle = Vehicle.boat(OptionalInt.of(10), Optional.of((short) 1), arena);
141+
Vehicle.Boat boat = vehicle.getAsBoat().orElseThrow();
142+
assertEquals(OptionalInt.of(10), boat.passengers());
143+
assertEquals(Optional.of((short) 1), boat.length());
144+
}
145+
}
146+
137147
@Test
138148
void associatedValuesAreCopied() {
139149
try (var arena = new ConfinedSwiftMemorySession()) {
@@ -184,6 +194,9 @@ void switchGetCase() {
184194
case Vehicle.Transformer transformer:
185195
fail("Was transformer");
186196
break;
197+
case Vehicle.Boat b:
198+
fail("Was boat");
199+
break;
187200
}
188201
}
189202
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaTranslation.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ extension JNISwift2JavaGenerator {
555555
conversion: .combinedValueToOptional(
556556
.placeholder,
557557
nextIntergralTypeWithSpaceForByte.javaType,
558+
resultName: resultName,
558559
valueType: javaType,
559560
valueSizeInBytes: nextIntergralTypeWithSpaceForByte.valueBytes,
560561
optionalType: optionalClass
@@ -754,7 +755,7 @@ extension JNISwift2JavaGenerator {
754755

755756
case isOptionalPresent
756757

757-
indirect case combinedValueToOptional(JavaNativeConversionStep, JavaType, valueType: JavaType, valueSizeInBytes: Int, optionalType: String)
758+
indirect case combinedValueToOptional(JavaNativeConversionStep, JavaType, resultName: String, valueType: JavaType, valueSizeInBytes: Int, optionalType: String)
758759

759760
indirect case ternary(JavaNativeConversionStep, thenExp: JavaNativeConversionStep, elseExp: JavaNativeConversionStep)
760761

@@ -834,22 +835,22 @@ extension JNISwift2JavaGenerator {
834835
let argsStr = args.joined(separator: ", ")
835836
return "\(inner).\(methodName)(\(argsStr))"
836837

837-
case .combinedValueToOptional(let combined, let combinedType, let valueType, let valueSizeInBytes, let optionalType):
838+
case .combinedValueToOptional(let combined, let combinedType, let resultName, let valueType, let valueSizeInBytes, let optionalType):
838839
let combined = combined.render(&printer, placeholder)
839840
printer.print(
840841
"""
841-
\(combinedType) combined$ = \(combined);
842-
byte discriminator$ = (byte) (combined$ & 0xFF);
842+
\(combinedType) \(resultName)_combined$ = \(combined);
843+
byte \(resultName)_discriminator$ = (byte) (\(resultName)_combined$ & 0xFF);
843844
"""
844845
)
845846

846847
if valueType == .boolean {
847-
printer.print("boolean value$ = ((byte) (combined$ >> 8)) != 0;")
848+
printer.print("boolean \(resultName)_value$ = ((byte) (\(resultName)_combined$ >> 8)) != 0;")
848849
} else {
849-
printer.print("\(valueType) value$ = (\(valueType)) (combined$ >> \(valueSizeInBytes * 8));")
850+
printer.print("\(valueType) \(resultName)_value$ = (\(valueType)) (\(resultName)_combined$ >> \(valueSizeInBytes * 8));")
850851
}
851852

852-
return "discriminator$ == 1 ? \(optionalType).of(value$) : \(optionalType).empty()"
853+
return "\(resultName)_discriminator$ == 1 ? \(optionalType).of(\(resultName)_value$) : \(optionalType).empty()"
853854

854855
case .ternary(let cond, let thenExp, let elseExp):
855856
let cond = cond.render(&printer, placeholder)
@@ -920,7 +921,7 @@ extension JNISwift2JavaGenerator {
920921
case .method(let inner, _, let args):
921922
return inner.requiresSwiftArena || args.contains(where: \.requiresSwiftArena)
922923

923-
case .combinedValueToOptional(let inner, _, _, _, _):
924+
case .combinedValueToOptional(let inner, _, _, _, _, _):
924925
return inner.requiresSwiftArena
925926

926927
case .ternary(let cond, let thenExp, let elseExp):

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ extension JNISwift2JavaGenerator {
269269
conversion: .getJNIValue(
270270
.optionalRaisingWidenIntegerType(
271271
.placeholder,
272+
resultName: resultName,
272273
valueType: javaType,
273274
combinedSwiftType: nextIntergralTypeWithSpaceForByte.swiftType,
274275
valueSizeInBytes: nextIntergralTypeWithSpaceForByte.valueBytes
@@ -396,7 +397,7 @@ extension JNISwift2JavaGenerator {
396397
guard let genericArgs = nominalType.genericArguments, genericArgs.count == 1 else {
397398
throw JavaTranslationError.unsupportedSwiftType(swiftResult.type)
398399
}
399-
return try translateOptionalResult(wrappedType: genericArgs[0])
400+
return try translateOptionalResult(wrappedType: genericArgs[0], resultName: resultName)
400401

401402
default:
402403
guard let javaType = JNIJavaTypeTranslator.translate(knownType: knownType, config: self.config), javaType.implementsJavaValue else {
@@ -429,7 +430,7 @@ extension JNISwift2JavaGenerator {
429430
)
430431

431432
case .optional(let wrapped):
432-
return try translateOptionalResult(wrappedType: wrapped)
433+
return try translateOptionalResult(wrappedType: wrapped, resultName: resultName)
433434

434435
case .metatype, .tuple, .function, .existential, .opaque, .genericParameter:
435436
throw JavaTranslationError.unsupportedSwiftType(swiftResult.type)
@@ -501,7 +502,7 @@ extension JNISwift2JavaGenerator {
501502

502503
indirect case optionalChain(NativeSwiftConversionStep)
503504

504-
indirect case optionalRaisingWidenIntegerType(NativeSwiftConversionStep, valueType: JavaType, combinedSwiftType: SwiftKnownTypeDeclKind, valueSizeInBytes: Int)
505+
indirect case optionalRaisingWidenIntegerType(NativeSwiftConversionStep, resultName: String, valueType: JavaType, combinedSwiftType: SwiftKnownTypeDeclKind, valueSizeInBytes: Int)
505506

506507
indirect case optionalRaisingIndirectReturn(NativeSwiftConversionStep, returnType: JavaType, discriminatorParameterName: String, placeholderValue: NativeSwiftConversionStep)
507508

@@ -630,18 +631,18 @@ extension JNISwift2JavaGenerator {
630631
let inner = inner.render(&printer, placeholder)
631632
return "\(inner)?"
632633

633-
case .optionalRaisingWidenIntegerType(let inner, let valueType, let combinedSwiftType, let valueSizeInBytes):
634+
case .optionalRaisingWidenIntegerType(let inner, let resultName, let valueType, let combinedSwiftType, let valueSizeInBytes):
634635
let inner = inner.render(&printer, placeholder)
635636
let value = valueType == .boolean ? "$0 ? 1 : 0" : "$0"
636637
let combinedSwiftTypeName = combinedSwiftType.moduleAndName.name
637638
printer.print(
638639
"""
639-
let value$ = \(inner).map {
640+
let \(resultName)_value$ = \(inner).map {
640641
\(combinedSwiftTypeName)(\(value)) << \(valueSizeInBytes * 8) | \(combinedSwiftTypeName)(1)
641642
} ?? 0
642643
"""
643644
)
644-
return "value$"
645+
return "\(resultName)_value$"
645646

646647
case .optionalRaisingIndirectReturn(let inner, let returnType, let discriminatorParameterName, let placeholderValue):
647648
printer.print("let result$: \(returnType.jniTypeName)")

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extension JNISwift2JavaGenerator {
9191
printer.print("static var \(JNICaching.cacheName(for: enumCase)): _JNICache!")
9292
}
9393
printer.println()
94-
printer.printBraceBlock("func cleanup()") { printer in
94+
printer.printBraceBlock("static func cleanup()") { printer in
9595
for enumCase in enumCases {
9696
printer.print("JNICaches.\(JNICaching.cacheName(for: enumCase)) = nil")
9797
}

0 commit comments

Comments
 (0)