Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ public func stringArray(array: [String]) -> [String] {
public func objectArray(array: [MySwiftClass]) -> [MySwiftClass] {
array
}

public func nestedByteArray(array: [[UInt8]]) -> [[UInt8]] {
array
}

public func nestedLongArray(array: [[Int64]]) -> [[Int64]] {
array
}

public func nestedStringArray(array: [[String]]) -> [[String]] {
array
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,76 @@ void objectArray() {
assertEquals(3, MySwiftLibrary.objectArray(input, arena).length);
}
}
}

@Test
void nestedByteArray() {
byte[][] input = new byte[][] {
{ 1, 2, 3 },
{ 4, 5 },
{ 6 }
};
byte[][] result = MySwiftLibrary.nestedByteArray(input);
assertEquals(input.length, result.length);
assertArrayEquals(input[0], result[0]);
assertArrayEquals(input[1], result[1]);
assertArrayEquals(input[2], result[2]);
}

@Test
void nestedByteArray_empty() {
byte[][] input = new byte[][] {};
byte[][] result = MySwiftLibrary.nestedByteArray(input);
assertEquals(0, result.length);
}

@Test
void nestedByteArray_emptyInner() {
byte[][] input = new byte[][] { {}, { 1 }, {} };
byte[][] result = MySwiftLibrary.nestedByteArray(input);
assertEquals(3, result.length);
assertArrayEquals(new byte[] {}, result[0]);
assertArrayEquals(new byte[] { 1 }, result[1]);
assertArrayEquals(new byte[] {}, result[2]);
}

@Test
void nestedLongArray() {
long[][] input = new long[][] {
{ 100, 200, 300 },
{ 400, 500 }
};
long[][] result = MySwiftLibrary.nestedLongArray(input);
assertEquals(input.length, result.length);
assertArrayEquals(input[0], result[0]);
assertArrayEquals(input[1], result[1]);
}

@Test
void nestedStringArray() {
String[][] input = new String[][] {
{ "hello", "world" },
{ "foo", "bar", "baz" }
};
String[][] result = MySwiftLibrary.nestedStringArray(input);
assertEquals(input.length, result.length);
assertArrayEquals(input[0], result[0]);
assertArrayEquals(input[1], result[1]);
}

@Test
void nestedStringArray_empty() {
String[][] input = new String[][] {};
String[][] result = MySwiftLibrary.nestedStringArray(input);
assertEquals(0, result.length);
}

@Test
void nestedStringArray_emptyInner() {
String[][] input = new String[][] { {}, { "a" }, {} };
String[][] result = MySwiftLibrary.nestedStringArray(input);
assertEquals(3, result.length);
assertArrayEquals(new String[] {}, result[0]);
assertArrayEquals(new String[] { "a" }, result[1]);
assertArrayEquals(new String[] {}, result[2]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void fishBoxHasExpectedMethods() throws Exception {

@Test
void fishBoxDoesNotHaveGenericTypeParameter() {
// FishBox is a concrete specialization no generic type parameters
// FishBox is a concrete specialization - no generic type parameters
assertEquals(0, FishBox.class.getTypeParameters().length,
"FishBox should have no generic type parameters");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void insertIntoStringToLongDictionary() {
assertEquals(2L, modified.get("world"));
assertEquals(42L, modified.get("swift"));

// The original dictionary is unchanged (Swift value semantics it's a copy)
// The original dictionary is unchanged (Swift value semantics - it's a copy)
assertEquals(2, original.size());
assertNull(original.get("swift"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void insertIntoStringSet() {
assertTrue(modified.contains("world"));
assertTrue(modified.contains("swift"));

// The original set is unchanged (Swift value semantics it's a copy)
// The original set is unchanged (Swift value semantics - it's a copy)
assertEquals(2, original.size());
assertFalse(original.contains("swift"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ import SwiftJavaJNICore

/// Determine if the given type needs any extra annotations that should be included
/// in Java sources when the corresponding Java type is rendered.
func getTypeAnnotations(swiftType: SwiftType, config: Configuration) -> [JavaAnnotation] {
func getJavaTypeAnnotations(swiftType: SwiftType, config: Configuration) -> [JavaAnnotation] {
if swiftType.isUnsignedInteger {
return [JavaAnnotation.unsigned]
}

switch swiftType.asNominalType?.asKnownType {
case .array(let element) where element.isUnsignedInteger:
return [JavaAnnotation.unsigned]
case .array(let element): // check recursively for [[UInt8]] etc
return getJavaTypeAnnotations(swiftType: element, config: config)

case .set(let element) where element.isUnsignedInteger:
return [JavaAnnotation.unsigned]
case .set(let element):
return getJavaTypeAnnotations(swiftType: element, config: config)

case .dictionary(let key, let value) where key.isUnsignedInteger || value.isUnsignedInteger:
return [JavaAnnotation.unsigned]
case .dictionary(let key, let value):
return getJavaTypeAnnotations(swiftType: key, config: config) + getJavaTypeAnnotations(swiftType: value, config: config)

default:
return []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ extension JavaType {
switch self {
case .boolean, .byte, .char, .short, .int, .long, .float, .double, .void, .javaLangString:
true
case .array(let element):
element.implementsJavaValue
default:
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ extension FFMSwift2JavaGenerator {
genericRequirements: [SwiftGenericRequirement]
) throws -> TranslatedParameter {
// If the result type should cause any annotations on the method, include them here.
let parameterAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let parameterAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

// If there is a 1:1 mapping between this Swift type and a C type, that can
// be expressed as a Java primitive type.
Expand Down Expand Up @@ -694,7 +694,7 @@ extension FFMSwift2JavaGenerator {
) throws -> TranslatedResult {
let swiftType = swiftResult.type
// If the result type should cause any annotations on the method, include them here.
let resultAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let resultAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

// If there is a 1:1 mapping between this Swift type and a C type, that can
// be expressed as a Java primitive type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ extension JNISwift2JavaGenerator {
) throws -> TranslatedParameter {

// If the result type should cause any annotations on the method, include them here.
let parameterAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let parameterAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

switch swiftType {
case .nominal(let nominalType):
Expand Down Expand Up @@ -804,7 +804,7 @@ extension JNISwift2JavaGenerator {
genericParameters: [SwiftGenericParameterDeclaration],
genericRequirements: [SwiftGenericRequirement],
) throws -> TranslatedParameter {
let parameterAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let parameterAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

switch swiftType {
case .nominal(let nominalType):
Expand Down Expand Up @@ -896,7 +896,7 @@ extension JNISwift2JavaGenerator {
let swiftType = swiftResult.type

// If the result type should cause any annotations on the method, include them here.
let resultAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let resultAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

switch swiftType {
case .nominal(let nominalType):
Expand Down Expand Up @@ -1251,7 +1251,7 @@ extension JNISwift2JavaGenerator {
) throws -> TranslatedResult {
let discriminatorName = "\(resultName)$_discriminator$"

let parameterAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: swiftType, config: config)
let parameterAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: swiftType, config: config)

switch swiftType {
case .nominal(let nominalType):
Expand Down Expand Up @@ -1365,9 +1365,30 @@ extension JNISwift2JavaGenerator {
genericParameters: [SwiftGenericParameterDeclaration],
genericRequirements: [SwiftGenericRequirement],
) throws -> TranslatedParameter {
let parameterAnnotations: [JavaAnnotation] = getTypeAnnotations(swiftType: elementType, config: config)
let parameterAnnotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: elementType, config: config)

switch elementType {
case .nominal(let nominalType) where nominalType.nominalTypeDecl.knownTypeKind == .array:
guard let fullKnownType = nominalType.asKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

guard case .array(let innerElement) = fullKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

let innerParam = try translateArrayParameter(
elementType: innerElement,
parameterName: parameterName,
genericParameters: genericParameters,
genericRequirements: genericRequirements
)
let innerJavaType = innerParam.parameter.type.javaType
return TranslatedParameter(
parameter: JavaParameter(name: parameterName, type: .array(innerJavaType), annotations: parameterAnnotations),
conversion: .requireNonNull(.placeholder, message: "\(parameterName) must not be null")
)

case .nominal(let nominalType):
if let knownType = nominalType.nominalTypeDecl.knownTypeKind {
guard let javaType = JNIJavaTypeTranslator.translate(knownType: knownType, config: self.config) else {
Expand Down Expand Up @@ -1417,9 +1438,30 @@ extension JNISwift2JavaGenerator {
genericParameters: [SwiftGenericParameterDeclaration],
genericRequirements: [SwiftGenericRequirement],
) throws -> TranslatedResult {
let annotations: [JavaAnnotation] = getTypeAnnotations(swiftType: elementType, config: config)
let annotations: [JavaAnnotation] = getJavaTypeAnnotations(swiftType: elementType, config: config)

switch elementType {
case .nominal(let nominalType) where nominalType.nominalTypeDecl.knownTypeKind == .array:
guard let fullKnownType = nominalType.asKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

guard case .array(let innerElement) = fullKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

let innerResult = try translateArrayResult(
elementType: innerElement,
genericParameters: genericParameters,
genericRequirements: genericRequirements
)
return TranslatedResult(
javaType: .array(innerResult.javaType),
annotations: annotations,
outParameters: [],
conversion: .placeholder
)

case .nominal(let nominalType):
if let knownType = nominalType.nominalTypeDecl.knownTypeKind {
guard let javaType = JNIJavaTypeTranslator.translate(knownType: knownType, config: self.config) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,22 @@ extension JNISwift2JavaGenerator {
resultName: String
) throws -> NativeResult {
switch elementType {
case .nominal(let nominalType) where nominalType.nominalTypeDecl.knownTypeKind == .array:
guard let fullKnownType = nominalType.asKnownType else {
throw JavaTranslationError.unsupportedSwiftType(known: .array(elementType))
}

guard case .array(let innerElement) = fullKnownType else {
throw JavaTranslationError.unsupportedSwiftType(known: .array(elementType))
}

let innerResult = try translateArrayResult(elementType: innerElement, resultName: resultName)
return NativeResult(
javaType: .array(innerResult.javaType),
conversion: .getJNIValue(.placeholder),
outParameters: []
)

case .nominal(let nominalType):
if let knownType = nominalType.nominalTypeDecl.knownTypeKind {
guard let javaType = JNIJavaTypeTranslator.translate(knownType: knownType, config: self.config),
Expand Down Expand Up @@ -897,6 +913,28 @@ extension JNISwift2JavaGenerator {
parameterName: String
) throws -> NativeParameter {
switch elementType {
case .nominal(let nominalType) where nominalType.nominalTypeDecl.knownTypeKind == .array:
guard let fullKnownType = nominalType.asKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

guard case .array(let innerElement) = fullKnownType else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}

let innerParam = try translateArrayParameter(elementType: innerElement, parameterName: parameterName)
guard case .concrete(let innerJavaType) = innerParam.parameters.first?.type else {
throw JavaTranslationError.unsupportedSwiftType(elementType)
}
return NativeParameter(
parameters: [
JavaParameter(name: parameterName, type: .array(innerJavaType))
],
conversion: .initFromJNI(.placeholder, swiftType: knownTypes.arraySugar(elementType)),
indirectConversion: nil,
conversionCheck: nil
)

case .nominal(let nominalType):
if let knownType = nominalType.nominalTypeDecl.knownTypeKind {
guard let javaType = JNIJavaTypeTranslator.translate(knownType: knownType, config: self.config),
Expand Down
Loading
Loading